class
Llama::State
- Llama::State
- Reference
- Object
Overview
Wrapper for state management functions in llama.cpp Provides methods for saving and loading model state
Defined in:
llama/state.crllama/state/error.cr
Constructors
-
.new(ctx : Context)
Creates a new State instance
Instance Method Summary
-
#finalize
Frees the resources associated with this state
-
#get_data : Bytes
Gets the current state data
-
#load_file(path : String, max_tokens : Int32 = 1024) : Array(Int32)
Loads state from a file
-
#save_file(path : String, tokens : Array(Int32)) : Bool
Saves state to a file
-
#seq_get_data(seq_id : Int32) : Bytes
Gets the state data for a specific sequence
-
#seq_load_file(path : String, dest_seq_id : Int32, max_tokens : Int32 = 1024) : Array(Int32)
Loads a sequence's state from a file
-
#seq_save_file(path : String, seq_id : Int32, tokens : Array(Int32)) : LibC::SizeT
Saves a sequence's state to a file
-
#seq_set_data(data : Bytes, dest_seq_id : Int32) : LibC::SizeT
Sets the state for a specific sequence from data
-
#seq_size(seq_id : Int32) : LibC::SizeT
Gets the size needed to store a specific sequence's state
-
#set_data(data : Bytes) : LibC::SizeT
Sets the state from data
-
#size : LibC::SizeT
Returns the size in bytes needed to store the current state
Constructor Detail
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
Instance Method Detail
Gets the current state data
Returns:
- A Bytes object containing the state data
Raises:
- Llama::State::Error if the operation fails
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
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
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
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
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
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
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
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
Returns the size in bytes needed to store the current state
Returns:
- The size in bytes
Raises:
- Llama::State::Error if the operation fails