pub struct NgramMap { /* private fields */ }Expand description
An adaptive in-context n-gram drafter.
Like NgramCache it indexes repeated n-grams, but it also records how
many tokens each of its own drafts got accepted and uses that to decide
whether to draft again — so a context where lookup is not paying off stops
costing anything. Feed results back with Self::accept.
State lives only in memory and only for this generation; call
Self::begin when starting a new one.
Wraps common_ngram_map.
Implementations§
Source§impl NgramMap
impl NgramMap
Sourcepub fn new(
size_key: u16,
size_value: u16,
key_only: bool,
min_hits: u16,
) -> Result<Self, NgramError>
pub fn new( size_key: u16, size_value: u16, key_only: bool, min_hits: u16, ) -> Result<Self, NgramError>
Build a map.
size_key— length of the n-grams used as lookup keys.size_value— length of the continuations drafted.key_only— index keys without tracking continuations, which is cheaper but drafts nothing on its own.min_hits— how many times a key must recur before it is trusted.
§Errors
Returns NgramError::Failed if llama.cpp cannot allocate the map —
it reserves a 2^18-entry hash table up front.
Sourcepub fn begin(&mut self, tokens: &[LlamaToken]) -> Result<(), NgramError>
pub fn begin(&mut self, tokens: &[LlamaToken]) -> Result<(), NgramError>
Start a generation over tokens (the prompt).
§Errors
Returns NgramError::Failed if llama.cpp throws.
Sourcepub fn draft(
&mut self,
tokens: &[LlamaToken],
sampled: LlamaToken,
) -> Result<Vec<LlamaToken>, NgramError>
pub fn draft( &mut self, tokens: &[LlamaToken], sampled: LlamaToken, ) -> Result<Vec<LlamaToken>, NgramError>
Draft a continuation, given everything generated so far and the token just sampled.
Returns an empty draft when the map decides lookup is not worth it here.
§Errors
Returns NgramError::Failed if llama.cpp throws.