pub struct LlamaContext<'a> {
pub model: &'a LlamaModel,
/* private fields */
}Expand description
A safe wrapper around the llama_context C++ context.
This struct provides a safe interface to interact with the llama_context used by the LlamaModel.
It encapsulates the raw C++ context pointer and provides additional fields for managing the model and
context-specific settings like embeddings and logits.
The LlamaContext struct ensures that the C++ context is always valid by using the NonNull type for
the context pointer, preventing it from being null. The struct also holds a reference to the model
(LlamaModel) that the context is tied to, along with some internal state like whether embeddings are enabled
and the initialized logits for the context.
§Fields
context: A non-null pointer to the raw C++llama_context. This is the main context used for interacting with the model.model: A reference to theLlamaModelassociated with this context. This model provides the data and parameters that the context interacts with.initialized_logits: A vector used to store the initialized logits. These are used in the model’s processing and are kept separate from the context data.embeddings_enabled: A boolean flag indicating whether embeddings are enabled in the context. This is useful for controlling whether embedding data is generated during the interaction with the model.
Fields§
§model: &'a LlamaModela reference to the contexts model.
Implementations§
Source§impl LlamaContext<'_>
impl LlamaContext<'_>
Sourcepub fn copy_cache(&mut self, src: i32, dest: i32, size: i32)
pub fn copy_cache(&mut self, src: i32, dest: i32, size: i32)
Copy the cache from one sequence to another.
§Parameters
src- The sequence id to copy the cache from.dest- The sequence id to copy the cache to.size- The size of the cache to copy.
Sourcepub fn copy_kv_cache_seq(
&mut self,
src: i32,
dest: i32,
p0: Option<u32>,
p1: Option<u32>,
) -> Result<(), KvCacheConversionError>
pub fn copy_kv_cache_seq( &mut self, src: i32, dest: i32, p0: Option<u32>, p1: Option<u32>, ) -> Result<(), KvCacheConversionError>
Copy the cache from one sequence to another.
§Parameters
src- The sequence id to copy the cache from.dest- The sequence id to copy the cache to.p0- The start position of the cache to clear. IfNone, the entire cache is copied up top1.p1- The end position of the cache to clear. IfNone, the entire cache is copied starting fromp0.
§Errors
Returns KvCacheConversionError if either position exceeds the maximum i32 value.
Sourcepub fn clear_kv_cache_seq(
&mut self,
src: Option<u32>,
p0: Option<u32>,
p1: Option<u32>,
) -> Result<bool, KvCacheConversionError>
pub fn clear_kv_cache_seq( &mut self, src: Option<u32>, p0: Option<u32>, p1: Option<u32>, ) -> Result<bool, KvCacheConversionError>
Clear the kv cache for the given sequence within the specified range [p0, p1).
Returns false only when partial sequence removals fail. Full sequence removals always succeed.
§Parameters
src- The sequence id to clear the cache for. IfNone, matches all sequencesp0- The start position of the cache to clear. IfNone, the entire cache is cleared up top1.p1- The end position of the cache to clear. IfNone, the entire cache is cleared fromp0.
§Errors
Returns KvCacheConversionError if the sequence id or either position exceeds the maximum i32 value.
Sourcepub fn clear_kv_cache(&mut self)
pub fn clear_kv_cache(&mut self)
Clear the KV cache
Sourcepub fn llama_kv_cache_seq_keep(&mut self, seq_id: i32)
pub fn llama_kv_cache_seq_keep(&mut self, seq_id: i32)
Removes all tokens that do not belong to the specified sequence
§Parameters
seq_id- The sequence id to keep
Sourcepub fn kv_cache_seq_add(
&mut self,
seq_id: i32,
p0: Option<u32>,
p1: Option<u32>,
delta: i32,
) -> Result<(), KvCacheConversionError>
pub fn kv_cache_seq_add( &mut self, seq_id: i32, p0: Option<u32>, p1: Option<u32>, delta: i32, ) -> Result<(), KvCacheConversionError>
Adds relative position “delta” to all tokens that belong to the specified sequence and have positions in [p0, p1).
§Parameters
seq_id- The sequence id to updatep0- The start position of the cache to update. IfNone, the entire cache is updated up top1.p1- The end position of the cache to update. IfNone, the entire cache is updated starting fromp0.delta- The relative position to add to the tokens
§Errors
Returns KvCacheConversionError if either position exceeds the maximum i32 value.
Sourcepub fn kv_cache_seq_div(
&mut self,
seq_id: i32,
p0: Option<u32>,
p1: Option<u32>,
d: NonZeroU8,
) -> Result<(), KvCacheConversionError>
pub fn kv_cache_seq_div( &mut self, seq_id: i32, p0: Option<u32>, p1: Option<u32>, d: NonZeroU8, ) -> Result<(), KvCacheConversionError>
Integer division of the positions by factor of d > 1.
§Parameters
seq_id- The sequence id to updatep0- The start position of the cache to update. IfNone, the entire cache is updated up top1.p1- The end position of the cache to update. IfNone, the entire cache is updated starting fromp0.d- The factor to divide the positions by
§Errors
Returns KvCacheConversionError if either position exceeds the maximum i32 value.
Sourcepub fn kv_cache_seq_pos_max(&self, seq_id: i32) -> i32
pub fn kv_cache_seq_pos_max(&self, seq_id: i32) -> i32
Returns the largest position present in the KV cache for the specified sequence
§Parameters
seq_id- The sequence id to get the max position for
Source§impl LlamaContext<'_>
impl LlamaContext<'_>
Sourcepub fn save_session_file(
&self,
path_session: impl AsRef<Path>,
tokens: &[LlamaToken],
) -> Result<(), SaveSessionError>
pub fn save_session_file( &self, path_session: impl AsRef<Path>, tokens: &[LlamaToken], ) -> Result<(), SaveSessionError>
Save the current session to a file.
§Parameters
path_session- The file to save to.tokens- The tokens to associate the session with. This should be a prefix of a sequence of tokens that the context has processed, so that the relevant KV caches are already filled.
§Errors
Fails if the path is not a valid utf8, is not a valid c string, or llama.cpp fails to save the session file.
Sourcepub fn load_session_file(
&mut self,
path_session: impl AsRef<Path>,
max_tokens: usize,
) -> Result<Vec<LlamaToken>, LoadSessionError>
pub fn load_session_file( &mut self, path_session: impl AsRef<Path>, max_tokens: usize, ) -> Result<Vec<LlamaToken>, LoadSessionError>
Load a session file into the current context.
You still need to pass the returned tokens to the context for inference to work. What this function buys you is that the KV caches are already filled with the relevant data.
§Parameters
path_session- The file to load from. It must be a session file from a compatible context, otherwise the function will error.max_tokens- The maximum token length of the loaded session. If the session was saved with a longer length, the function will error.
§Errors
Fails if the path is not a valid utf8, is not a valid c string, or llama.cpp fails to load the session file. (e.g. the file does not exist, is not a session file, etc.)
Sourcepub fn get_state_size(&self) -> usize
pub fn get_state_size(&self) -> usize
Returns the maximum size in bytes of the state (rng, logits, embedding
and kv_cache) - will often be smaller after compacting tokens
Sourcepub unsafe fn copy_state_data(&self, dest: *mut u8) -> usize
pub unsafe fn copy_state_data(&self, dest: *mut u8) -> usize
Copies the state to the specified destination address.
Returns the number of bytes copied
§Safety
Destination needs to have allocated enough memory.
Sourcepub unsafe fn set_state_data(&mut self, src: &[u8]) -> usize
pub unsafe fn set_state_data(&mut self, src: &[u8]) -> usize
Set the state reading from the specified address Returns the number of bytes read
§Safety
help wanted: not entirely sure what the safety requirements are here.
Source§impl<'model> LlamaContext<'model>
impl<'model> LlamaContext<'model>
Sourcepub fn n_batch(&self) -> u32
pub fn n_batch(&self) -> u32
Gets the max number of logical tokens that can be submitted to decode. Must be greater than or equal to n_ubatch.
Sourcepub fn n_ubatch(&self) -> u32
pub fn n_ubatch(&self) -> u32
Gets the max number of physical tokens (hardware level) to decode in batch. Must be less than or equal to n_batch.
Sourcepub fn decode(&mut self, batch: &mut LlamaBatch) -> Result<(), DecodeError>
pub fn decode(&mut self, batch: &mut LlamaBatch) -> Result<(), DecodeError>
Decodes the batch.
§Errors
DecodeErrorif the decoding failed.
§Panics
- the returned
std::ffi::c_intfrom llama-cpp does not fit into a i32 (this should never happen on most systems)
Sourcepub fn encode(&mut self, batch: &mut LlamaBatch) -> Result<(), EncodeError>
pub fn encode(&mut self, batch: &mut LlamaBatch) -> Result<(), EncodeError>
Encodes the batch.
§Errors
EncodeErrorif the decoding failed.
§Panics
- the returned
std::ffi::c_intfrom llama-cpp does not fit into a i32 (this should never happen on most systems)
Sourcepub fn pooling_type(&self) -> LlamaPoolingType
pub fn pooling_type(&self) -> LlamaPoolingType
Return Pooling type for Llama’s Context
Sourcepub fn embeddings_seq_ith(&self, i: i32) -> Result<&[f32], EmbeddingsError>
pub fn embeddings_seq_ith(&self, i: i32) -> Result<&[f32], EmbeddingsError>
Get the embeddings for the ith sequence in the current context.
§Returns
A slice containing the embeddings for the last decoded batch.
The size corresponds to the n_embd parameter of the context’s model.
§Errors
- When the current context was constructed without enabling embeddings.
- If the current model had a pooling type of
llama_cpp_sys_4::LLAMA_POOLING_TYPE_NONE - If the given sequence index exceeds the max sequence id.
§Panics
n_embddoes not fit into a usize
Sourcepub fn embeddings_ith(&self, i: i32) -> Result<&[f32], EmbeddingsError>
pub fn embeddings_ith(&self, i: i32) -> Result<&[f32], EmbeddingsError>
Get the embeddings for the ith token in the current context.
§Returns
A slice containing the embeddings for the last decoded batch of the given token.
The size corresponds to the n_embd parameter of the context’s model.
§Errors
- When the current context was constructed without enabling embeddings.
- When the given token didn’t have logits enabled when it was passed.
- If the given token index exceeds the max token id.
§Panics
n_embddoes not fit into a usize
Sourcepub fn candidates(&self) -> impl Iterator<Item = LlamaTokenData> + '_
pub fn candidates(&self) -> impl Iterator<Item = LlamaTokenData> + '_
Sourcepub fn token_data_array(&self) -> LlamaTokenDataArray
pub fn token_data_array(&self) -> LlamaTokenDataArray
Sourcepub fn get_logits(&self) -> &[f32]
pub fn get_logits(&self) -> &[f32]
Token logits obtained from the last call to decode().
The logits for which batch.logits[i] != 0 are stored contiguously
in the order they have appeared in the batch.
Rows: number of tokens for which batch.logits[i] != 0
Cols: n_vocab
§Returns
A slice containing the logits for the last decoded token.
The size corresponds to the n_vocab parameter of the context’s model.
§Panics
n_vocabdoes not fit into a usize- token data returned is null
Sourcepub fn candidates_ith(
&self,
i: i32,
) -> impl Iterator<Item = LlamaTokenData> + '_
pub fn candidates_ith( &self, i: i32, ) -> impl Iterator<Item = LlamaTokenData> + '_
Sourcepub fn get_logits_ith(&self, i: i32) -> &[f32]
pub fn get_logits_ith(&self, i: i32) -> &[f32]
Get the logits for the ith token in the context.
§Panics
iis greater thann_ctxn_vocabdoes not fit into a usize- logit
iis not initialized.
Sourcepub fn n_threads_batch(&self) -> i32
pub fn n_threads_batch(&self) -> i32
Get the number of threads used for batch processing.
Sourcepub fn set_n_threads(&mut self, n_threads: i32, n_threads_batch: i32)
pub fn set_n_threads(&mut self, n_threads: i32, n_threads_batch: i32)
Set the number of threads used for generation and batch processing.
Sourcepub fn set_causal_attn(&mut self, causal_attn: bool)
pub fn set_causal_attn(&mut self, causal_attn: bool)
Set whether to use causal attention.
If set to false, the model will use non-causal attention, which is
needed for embedding models.
Sourcepub fn set_embeddings(&mut self, embeddings: bool)
pub fn set_embeddings(&mut self, embeddings: bool)
Set whether to compute embeddings.
This allows toggling embedding mode at runtime (as opposed to only at context creation time).
Sourcepub fn set_warmup(&mut self, warmup: bool)
pub fn set_warmup(&mut self, warmup: bool)
Mark the next computation as a warmup run.
Warmup runs are useful for GPU backends to compile kernels before actual inference begins.
Sourcepub fn synchronize(&mut self)
pub fn synchronize(&mut self)
Wait for all pending async computations to finish.
Sourcepub fn get_embeddings(&self) -> Result<&[f32], EmbeddingsError>
pub fn get_embeddings(&self) -> Result<&[f32], EmbeddingsError>
Get all embeddings for the current context.
Returns a slice of all embeddings from the last decoded batch.
For pooled embeddings use embeddings_seq_ith instead.
§Errors
- When the current context was constructed without enabling embeddings.
- If the embeddings pointer is null.
§Panics
n_embddoes not fit into a usize
Sourcepub fn reset_timings(&mut self)
pub fn reset_timings(&mut self)
Reset the timings for the context.
Sourcepub fn timings(&mut self) -> PerfContextData
pub fn timings(&mut self) -> PerfContextData
Returns the timings for the context.
Sourcepub fn perf_context_reset(&mut self)
pub fn perf_context_reset(&mut self)
Reset the performance counters for the context.
Sourcepub fn memory_can_shift(&self) -> bool
pub fn memory_can_shift(&self) -> bool
Check if the KV cache memory supports shifting.
Sourcepub fn memory_seq_pos_min(&self, seq_id: i32) -> i32
pub fn memory_seq_pos_min(&self, seq_id: i32) -> i32
Get the minimum position in a sequence’s KV cache.
Sourcepub fn memory_breakdown_print(&self)
pub fn memory_breakdown_print(&self)
Print a breakdown of the memory usage.
Sourcepub fn state_get_size(&mut self) -> usize
pub fn state_get_size(&mut self) -> usize
Get the size of the full context state in bytes.
This is the size needed for state_get_data and
state_set_data.
Sourcepub fn state_get_data(&mut self, dst: &mut [u8]) -> usize
pub fn state_get_data(&mut self, dst: &mut [u8]) -> usize
Copy the full context state into a byte buffer.
The buffer must be at least state_get_size bytes.
Returns the number of bytes written.
Sourcepub fn state_set_data(&mut self, src: &[u8]) -> usize
pub fn state_set_data(&mut self, src: &[u8]) -> usize
Restore the full context state from a byte buffer.
Returns the number of bytes read.
Sourcepub fn state_save_file(
&mut self,
path: impl AsRef<Path>,
tokens: &[LlamaToken],
) -> bool
pub fn state_save_file( &mut self, path: impl AsRef<Path>, tokens: &[LlamaToken], ) -> bool
Save the context state to a file along with the given tokens.
Returns true on success.
§Panics
Panics if the path contains null bytes.
Sourcepub fn state_load_file(
&mut self,
path: impl AsRef<Path>,
tokens_out: &mut Vec<LlamaToken>,
n_token_capacity: usize,
) -> bool
pub fn state_load_file( &mut self, path: impl AsRef<Path>, tokens_out: &mut Vec<LlamaToken>, n_token_capacity: usize, ) -> bool
Load a context state from a file.
Returns true on success and fills tokens_out with the saved tokens.
§Panics
Panics if the path contains null bytes.
Sourcepub fn state_seq_get_size(&mut self, seq_id: i32) -> usize
pub fn state_seq_get_size(&mut self, seq_id: i32) -> usize
Get the size of a single sequence’s state in bytes.
Sourcepub fn state_seq_get_data(&mut self, dst: &mut [u8], seq_id: i32) -> usize
pub fn state_seq_get_data(&mut self, dst: &mut [u8], seq_id: i32) -> usize
Copy a single sequence’s state into a byte buffer.
Returns the number of bytes written.
Sourcepub fn state_seq_set_data(&mut self, src: &[u8], dest_seq_id: i32) -> usize
pub fn state_seq_set_data(&mut self, src: &[u8], dest_seq_id: i32) -> usize
Restore a single sequence’s state from a byte buffer.
Returns the number of bytes read.
Sourcepub fn state_seq_save_file(
&mut self,
path: impl AsRef<Path>,
seq_id: i32,
tokens: &[LlamaToken],
) -> usize
pub fn state_seq_save_file( &mut self, path: impl AsRef<Path>, seq_id: i32, tokens: &[LlamaToken], ) -> usize
Save a single sequence’s state to a file.
Returns the number of bytes written (0 on failure).
§Panics
Panics if the path contains null bytes.
Sourcepub fn state_seq_load_file(
&mut self,
path: impl AsRef<Path>,
dest_seq_id: i32,
tokens_out: &mut Vec<LlamaToken>,
n_token_capacity: usize,
) -> usize
pub fn state_seq_load_file( &mut self, path: impl AsRef<Path>, dest_seq_id: i32, tokens_out: &mut Vec<LlamaToken>, n_token_capacity: usize, ) -> usize
Load a single sequence’s state from a file.
Returns the number of bytes read (0 on failure).
§Panics
Panics if the path contains null bytes.
Sourcepub fn set_adapter_cvec(
&mut self,
data: &[f32],
n_embd: i32,
il_start: i32,
il_end: i32,
) -> Result<(), i32>
pub fn set_adapter_cvec( &mut self, data: &[f32], n_embd: i32, il_start: i32, il_end: i32, ) -> Result<(), i32>
Set a control vector on the context.
§Parameters
data: The control vector data (embedding values). Pass an empty slice to clear.n_embd: The embedding dimension.il_start: The starting layer index (inclusive).il_end: The ending layer index (exclusive).
§Errors
Returns Err with the error code if the operation fails.
Sourcepub fn get_sampled_token_ith(&self, i: i32) -> LlamaToken
pub fn get_sampled_token_ith(&self, i: i32) -> LlamaToken
Get sampled token debug info for the ith position.
Returns the sampled token at position i from the last decode call.
Sourcepub fn get_sampled_candidates_ith(&self, i: i32) -> &[LlamaToken]
pub fn get_sampled_candidates_ith(&self, i: i32) -> &[LlamaToken]
Get sampled candidate tokens for the ith position.
Returns a slice of candidate tokens from the last decode call.
Sourcepub fn get_sampled_logits_count_ith(&self, i: i32) -> u32
pub fn get_sampled_logits_count_ith(&self, i: i32) -> u32
Get the number of sampled logits for the ith position.
Sourcepub fn get_sampled_logits_ith(&self, i: i32) -> &[f32]
pub fn get_sampled_logits_ith(&self, i: i32) -> &[f32]
Get sampled logits for the ith position.
Returns a slice of logit values from the last decode call.
Sourcepub fn get_sampled_probs_count_ith(&self, i: i32) -> u32
pub fn get_sampled_probs_count_ith(&self, i: i32) -> u32
Get the number of sampled probabilities for the ith position.
Sourcepub fn get_sampled_probs_ith(&self, i: i32) -> &[f32]
pub fn get_sampled_probs_ith(&self, i: i32) -> &[f32]
Get sampled probabilities for the ith position.
Returns a slice of probability values from the last decode call.
Sourcepub fn state_seq_get_size_ext(&mut self, seq_id: i32, flags: u32) -> usize
pub fn state_seq_get_size_ext(&mut self, seq_id: i32, flags: u32) -> usize
Get the size of a single sequence’s state with flags.
Sourcepub fn state_seq_get_data_ext(
&mut self,
dst: &mut [u8],
seq_id: i32,
flags: u32,
) -> usize
pub fn state_seq_get_data_ext( &mut self, dst: &mut [u8], seq_id: i32, flags: u32, ) -> usize
Copy a single sequence’s state into a byte buffer with flags.
Returns the number of bytes written.
Sourcepub fn state_seq_set_data_ext(
&mut self,
src: &[u8],
dest_seq_id: i32,
flags: u32,
) -> usize
pub fn state_seq_set_data_ext( &mut self, src: &[u8], dest_seq_id: i32, flags: u32, ) -> usize
Restore a single sequence’s state from a byte buffer with flags.
Returns the number of bytes read.
Sourcepub unsafe fn set_abort_callback(
&mut self,
callback: ggml_abort_callback,
data: *mut c_void,
)
pub unsafe fn set_abort_callback( &mut self, callback: ggml_abort_callback, data: *mut c_void, )
Set an abort callback for the context.
The callback is called periodically during computation. If it returns true,
the computation is aborted.
§Safety
The callback data must remain valid for the lifetime of the context or until the callback is replaced.
Sourcepub unsafe fn attach_threadpool(
&mut self,
threadpool: ggml_threadpool_t,
threadpool_batch: ggml_threadpool_t,
)
pub unsafe fn attach_threadpool( &mut self, threadpool: ggml_threadpool_t, threadpool_batch: ggml_threadpool_t, )
Attach a thread pool to the context.
§Safety
The thread pools must remain valid for the lifetime of the context or until they are detached.
Sourcepub fn detach_threadpool(&mut self)
pub fn detach_threadpool(&mut self)
Detach the thread pool from the context.
Sourcepub fn set_sampler(&mut self, seq_id: i32, sampler: &mut LlamaSampler) -> bool
pub fn set_sampler(&mut self, seq_id: i32, sampler: &mut LlamaSampler) -> bool
Set a sampler for a specific sequence.
Returns true on success.
Sourcepub fn get_model_ptr(&self) -> *const llama_model
pub fn get_model_ptr(&self) -> *const llama_model
Get the raw model pointer from this context.
This is mainly useful for FFI interop. In normal usage, access
the model via the model field instead.
Sourcepub fn lora_adapter_set(
&self,
adapter: &mut LlamaLoraAdapter,
scale: f32,
) -> Result<(), LlamaLoraAdapterSetError>
pub fn lora_adapter_set( &self, adapter: &mut LlamaLoraAdapter, scale: f32, ) -> Result<(), LlamaLoraAdapterSetError>
Sourcepub fn lora_adapter_remove(
&self,
_adapter: &mut LlamaLoraAdapter,
) -> Result<(), LlamaLoraAdapterRemoveError>
pub fn lora_adapter_remove( &self, _adapter: &mut LlamaLoraAdapter, ) -> Result<(), LlamaLoraAdapterRemoveError>
Remove all lora adapters from the context.
Note: as of llama.cpp b8249 the per-adapter remove API was replaced by
llama_set_adapters_lora which operates on the full adapter list at once.
Calling this function clears all adapters currently set on the context.
§Errors
See LlamaLoraAdapterRemoveError for more information.
Source§impl LlamaContext<'_>
impl LlamaContext<'_>
Sourcepub fn as_ptr(&self) -> *mut llama_context
pub fn as_ptr(&self) -> *mut llama_context
Expose the raw llama_context pointer for use with mtmd helpers.
§Safety
The pointer is valid for the lifetime of this LlamaContext and must
not be freed by the caller.