Skip to main content

NgramCache

Struct NgramCache 

Source
pub struct NgramCache { /* private fields */ }
Expand description

A statistical n-gram cache: which tokens tend to follow which n-grams.

Where ngram_simple_draft takes the single most recent repeat, this accumulates a distribution over many observations and drafts the most likely continuation. It can be persisted, so a cache built once from a corpus — or grown across a user’s sessions — keeps paying off.

llama.cpp consults up to three caches at once, in priority order:

  • context — built from the current conversation, most specific;
  • dynamic — built from this user’s past generations;
  • static — built offline from a large corpus, used to validate.

Wraps common_ngram_cache.

Implementations§

Source§

impl NgramCache

Source

pub fn new() -> Self

An empty cache.

§Panics

Panics if the allocation fails.

Source

pub fn load(path: &str) -> Result<Self, NgramError>

Load a cache written by Self::save.

§Errors

Returns NgramError::Failed if the file is missing or malformed, or NgramError::Nul for an interior NUL in path.

Source

pub fn save(&mut self, path: &str) -> Result<(), NgramError>

Write this cache to disk.

§Errors

Returns NgramError::Failed if the file cannot be written, or NgramError::Nul for an interior NUL in path.

Source

pub fn merge(&mut self, other: &mut NgramCache) -> Result<(), NgramError>

Fold another cache’s counts into this one.

§Errors

Returns NgramError::Failed if llama.cpp throws.

Source

pub fn len(&self) -> usize

Number of distinct n-grams recorded.

Source

pub fn is_empty(&self) -> bool

Whether the cache has learned anything yet.

Source

pub fn update( &mut self, ngram_min: i32, ngram_max: i32, tokens: &[LlamaToken], nnew: i32, print_progress: bool, ) -> Result<(), NgramError>

Learn from a token sequence.

nnew is how many tokens were appended since the last call, so a live conversation only pays for its new tokens. Upstream requires tokens to be append-only: editing the middle invalidates the statistics and needs a rebuild from scratch.

§Errors

Returns NgramError::Failed if llama.cpp throws.

Trait Implementations§

Source§

impl Debug for NgramCache

Source§

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

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

impl Default for NgramCache

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Drop for NgramCache

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

impl Send for NgramCache

Auto Trait Implementations§

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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more