Skip to main content

LlamaContext

Struct LlamaContext 

Source
pub struct LlamaContext<'model> {
    pub context: NonNull<llama_context>,
    pub model: &'model LlamaModel,
    /* private fields */
}

Fields§

§context: NonNull<llama_context>§model: &'model LlamaModel

Implementations§

Source§

impl LlamaContext<'_>

Source

pub fn copy_cache(&mut self, src: i32, dest: i32, size: i32)

Source

pub fn copy_kv_cache_seq( &mut self, src: i32, dest: i32, p0: Option<u32>, p1: Option<u32>, ) -> Result<(), KvCacheConversionError>

§Errors

If either position exceeds i32::MAX.

Source

pub fn clear_kv_cache_seq( &mut self, src: Option<u32>, p0: Option<u32>, p1: Option<u32>, ) -> Result<bool, KvCacheConversionError>

§Errors

If the sequence id or either position exceeds i32::MAX.

Source

pub fn clear_kv_cache(&mut self)

Source

pub fn kv_cache_seq_keep(&mut self, seq_id: i32)

Source

pub fn kv_cache_seq_add( &mut self, seq_id: i32, p0: Option<u32>, p1: Option<u32>, delta: i32, ) -> Result<(), KvCacheSeqAddError>

§Errors

If either position exceeds i32::MAX, or the underlying memory operation reports a failure.

Source

pub fn kv_cache_seq_div( &mut self, seq_id: i32, p0: Option<u32>, p1: Option<u32>, d: NonZeroU8, ) -> Result<(), KvCacheSeqDivError>

§Errors

If either position exceeds i32::MAX, or the underlying memory operation reports a failure.

Source

pub fn kv_cache_seq_pos_max(&self, seq_id: i32) -> i32

Source§

impl LlamaContext<'_>

Source

pub fn state_save_file( &self, path_session: impl AsRef<Path>, tokens: &[LlamaToken], ) -> Result<(), SaveSessionError>

§Errors

Fails if the path is not a valid utf8 or llama.cpp fails to save the state file.

Source

pub fn state_load_file( &mut self, path_session: impl AsRef<Path>, max_tokens: usize, ) -> Result<Vec<LlamaToken>, LoadSessionError>

§Errors

Fails if the path is not a valid utf8 or llama.cpp fails to load the state file.

Source

pub fn state_seq_save_file( &self, filepath: impl AsRef<Path>, seq_id: i32, tokens: &[LlamaToken], ) -> Result<usize, SaveSeqStateError>

§Errors

Fails if the path is not a valid utf8 or llama.cpp fails to save the sequence state file.

Source

pub fn state_seq_load_file( &mut self, filepath: impl AsRef<Path>, dest_seq_id: i32, max_tokens: usize, ) -> Result<(Vec<LlamaToken>, usize), LoadSeqStateError>

§Errors

Fails if the path is not a valid utf8 or llama.cpp fails to load the sequence state file.

Source

pub fn get_state_size(&self) -> usize

Source

pub unsafe fn copy_state_data(&self, dest: &mut [u8]) -> usize

§Safety

The dest buffer must be large enough to hold the complete state data.

Source

pub unsafe fn set_state_data(&mut self, src: &[u8]) -> usize

§Safety

The src buffer must contain data previously obtained from copy_state_data on a compatible context (same model and parameters). Passing arbitrary or corrupted bytes will lead to undefined behavior.

Source

pub fn state_seq_get_size_ext( &self, seq_id: i32, flags: &LlamaStateSeqFlags, ) -> usize

Source

pub unsafe fn state_seq_get_data_ext( &self, dest: &mut [u8], seq_id: i32, flags: &LlamaStateSeqFlags, ) -> usize

§Safety

The dest buffer must be large enough to hold the complete state data.

Source

pub unsafe fn state_seq_set_data_ext( &mut self, src: &[u8], dest_seq_id: i32, flags: &LlamaStateSeqFlags, ) -> usize

§Safety

The src buffer must contain data previously obtained from state_seq_get_data_ext on a compatible context.

Source§

impl<'model> LlamaContext<'model>

Source

pub const fn new( llama_model: &'model LlamaModel, llama_context: NonNull<llama_context>, embeddings_enabled: bool, ) -> Self

Source

pub fn from_model( model: &'model LlamaModel, _backend: &LlamaBackend, params: LlamaContextParams, ) -> Result<Self, LlamaContextLoadError>

§Errors

Returns LlamaContextLoadError when llama.cpp fails to allocate the context.

Source

pub fn n_batch(&self) -> u32

Source

pub fn n_ubatch(&self) -> u32

Source

pub fn n_ctx(&self) -> u32

Source

pub fn set_abort_flag(&mut self, flag: Arc<AtomicBool>)

Source

pub fn clear_abort_callback(&mut self)

Source

pub fn synchronize(&self)

Source

pub fn detach_threadpool(&self)

Source

pub fn mark_logits_initialized(&mut self, token_index: i32)

Source

pub fn decode(&mut self, batch: &mut LlamaBatch<'_>) -> Result<(), DecodeError>

§Errors
  • DecodeError if the decoding failed.
Source

pub fn encode(&mut self, batch: &mut LlamaBatch<'_>) -> Result<(), EncodeError>

§Errors
  • EncodeError if the encoding failed.
Source

pub fn embeddings_seq_ith( &self, sequence_index: i32, ) -> Result<&[f32], EmbeddingsError>

§Errors
Source

pub fn embeddings_ith( &self, token_index: i32, ) -> Result<&[f32], EmbeddingsError>

§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.
Source

pub fn candidates( &self, ) -> Result<impl Iterator<Item = LlamaTokenData> + '_, LogitsError>

§Errors

Returns LogitsError if logits are null or n_vocab overflows.

Source

pub fn token_data_array(&self) -> Result<LlamaTokenDataArray, LogitsError>

§Errors

Returns LogitsError if logits are null or n_vocab overflows.

Source

pub fn get_logits(&self) -> Result<&[f32], LogitsError>

§Errors

Returns LogitsError if the logits pointer is null or n_vocab overflows.

Source

pub fn candidates_ith( &self, token_index: i32, ) -> Result<impl Iterator<Item = LlamaTokenData> + '_, LogitsError>

§Errors

Returns LogitsError if the token is not initialized or out of range.

Source

pub fn token_data_array_ith( &self, token_index: i32, ) -> Result<LlamaTokenDataArray, LogitsError>

§Errors

Returns LogitsError if the token is not initialized or out of range.

Source

pub fn get_logits_ith(&self, token_index: i32) -> Result<&[f32], LogitsError>

§Errors

Returns LogitsError if the token is not initialized, out of range, or n_vocab overflows.

Source

pub fn reset_timings(&mut self)

Source

pub fn timings(&mut self) -> LlamaTimings

Source

pub fn lora_adapter_set( &self, adapter: &mut LlamaLoraAdapter, scale: f32, ) -> Result<(), LlamaLoraAdapterSetError>

§Errors

See LlamaLoraAdapterSetError for more information.

Source

pub fn lora_adapter_remove( &self, _adapter: &mut LlamaLoraAdapter, ) -> Result<(), LlamaLoraAdapterRemoveError>

§Errors

See LlamaLoraAdapterRemoveError for more information.

Trait Implementations§

Source§

impl Debug for LlamaContext<'_>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Drop for LlamaContext<'_>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

Auto Trait Implementations§

§

impl<'model> Freeze for LlamaContext<'model>

§

impl<'model> RefUnwindSafe for LlamaContext<'model>

§

impl<'model> !Send for LlamaContext<'model>

§

impl<'model> !Sync for LlamaContext<'model>

§

impl<'model> Unpin for LlamaContext<'model>

§

impl<'model> UnsafeUnpin for LlamaContext<'model>

§

impl<'model> UnwindSafe for LlamaContext<'model>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.