class Llama::State

Overview

Wrapper for state management functions in llama.cpp Provides methods for saving and loading model state

Defined in:

llama/state.cr
llama/state/error.cr

Constructors

Instance Method Summary

Constructor Detail

def self.new(ctx : Context) #

Creates a new State instance

Parameters:

  • ctx: The Context to manage state for

To avoid circular references, we store the context pointer rather than the context object

Raises:

  • Llama::State::Error if the context pointer is null

[View source]

Instance Method Detail

def finalize #

Frees the resources associated with this state


[View source]
def get_data : Bytes #

Gets the current state data

Returns:

  • A Bytes object containing the state data

Raises:

  • Llama::State::Error if the operation fails

[View source]
def load_file(path : String, max_tokens : Int32 = 1024) : Array(Int32) #

Loads state from a file

Parameters:

  • path: Path to the session file
  • max_tokens: Maximum number of tokens to load (default: 1024)

Returns:

  • An array of tokens loaded from the file

Raises:

  • ArgumentError if path is empty or max_tokens is not positive
  • Llama::State::Error if the file cannot be loaded

[View source]
def save_file(path : String, tokens : Array(Int32)) : Bool #

Saves state to a file

Parameters:

  • path: Path to save the session file
  • tokens: Array of tokens to save with the state

Returns:

  • true if successful, false otherwise

Raises:

  • ArgumentError if path is empty
  • Llama::State::Error if the operation fails

[View source]
def seq_get_data(seq_id : Int32) : Bytes #

Gets the state data for a specific sequence

Parameters:

  • seq_id: The sequence ID

Returns:

  • A Bytes object containing the sequence state data

Raises:

  • Llama::State::Error if the operation fails

[View source]
def seq_load_file(path : String, dest_seq_id : Int32, max_tokens : Int32 = 1024) : Array(Int32) #

Loads a sequence's state from a file

Parameters:

  • path: Path to the sequence file
  • dest_seq_id: The destination sequence ID
  • max_tokens: Maximum number of tokens to load (default: 1024)

Returns:

  • An array of tokens loaded from the file

Raises:

  • ArgumentError if path is empty or max_tokens is not positive
  • Llama::State::Error if the file cannot be loaded

[View source]
def seq_save_file(path : String, seq_id : Int32, tokens : Array(Int32)) : LibC::SizeT #

Saves a sequence's state to a file

Parameters:

  • path: Path to save the sequence file
  • seq_id: The sequence ID to save
  • tokens: Array of tokens to save with the state

Returns:

  • The number of bytes written, or 0 if failed

Raises:

  • ArgumentError if path is empty
  • Llama::State::Error if the operation fails

[View source]
def seq_set_data(data : Bytes, dest_seq_id : Int32) : LibC::SizeT #

Sets the state for a specific sequence from data

Parameters:

  • data: The state data to set
  • dest_seq_id: The destination sequence ID

Returns:

  • The number of bytes read, or 0 if failed

Raises:

  • ArgumentError if data is empty
  • Llama::State::Error if the operation fails

[View source]
def seq_size(seq_id : Int32) : LibC::SizeT #

Gets the size needed to store a specific sequence's state

Parameters:

  • seq_id: The sequence ID

Returns:

  • The size in bytes

Raises:

  • Llama::State::Error if the operation fails

[View source]
def set_data(data : Bytes) : LibC::SizeT #

Sets the state from data

Parameters:

  • data: The state data to set

Returns:

  • The number of bytes read

Raises:

  • ArgumentError if data is empty
  • Llama::State::Error if the operation fails

[View source]
def size : LibC::SizeT #

Returns the size in bytes needed to store the current state

Returns:

  • The size in bytes

Raises:

  • Llama::State::Error if the operation fails

[View source]