pub struct CachedTokenizer { /* private fields */ }Expand description
Implementations§
Source§impl CachedTokenizer
impl CachedTokenizer
Sourcepub fn new(
inner: Arc<dyn Tokenizer>,
special_tokens: Vec<String>,
max_memory_bytes: usize,
) -> Result<Self>
pub fn new( inner: Arc<dyn Tokenizer>, special_tokens: Vec<String>, max_memory_bytes: usize, ) -> Result<Self>
Construct a cached tokenizer.
special_tokens is the list of atomic special-token strings the inner
tokenizer recognizes (typically extracted via the HuggingFace tokenizer’s
get_added_tokens_decoder() filtering by special == true). An empty list
disables L1 — encode/encode_batch short-circuit to the inner tokenizer
without touching the cache or its counters. An overlapping token set also disables
L1, with a warning, because its boundaries are ambiguous.
max_memory_bytes is the L1 cache byte budget.
§Errors
Returns the inner tokenizer’s compatibility error when it cannot be safely wrapped in the prefix cache.
Sourcepub fn with_extend(self, enabled: bool) -> Self
pub fn with_extend(self, enabled: bool) -> Self
Enable partial-hit extension. When on, a partial cache hit also caches the freshly-tokenized suffix at its deepest special-token boundary, so each turn of a growing multi-turn conversation hits deeper than the last and per-turn tokenization cost stops growing with conversation length. Default off.
Sourcepub fn with_observer(self, on_hit: CacheEventFn, on_miss: CacheEventFn) -> Self
pub fn with_observer(self, on_hit: CacheEventFn, on_miss: CacheEventFn) -> Self
Install hit/miss callbacks so each L1 lookup pushes an event into the
supplied closures (e.g. Prometheus::Counter::inc). Replaces any
previously-set observer.
Sourcepub fn with_token_observer(self, observer: CacheTokenUsageFn) -> Self
pub fn with_token_observer(self, observer: CacheTokenUsageFn) -> Self
Install a callback that receives exact cached and uncached token counts after each successful encode while L1 is active. A partial hit reports both categories, which lets consumers maintain token-level cache totals and derive a reuse ratio. Replaces any previously-set token observer.
This observer is not called when the special-token set is empty (and L1 is therefore disabled) or when encoding returns an error.
Sourcepub fn cache_stats(&self) -> L1CacheStats
pub fn cache_stats(&self) -> L1CacheStats
Snapshot of L1 cache statistics (cumulative hits/misses/entries/memory).
Sourcepub fn clear_cache(&self)
pub fn clear_cache(&self)
Clear all cached entries and reset counters.
Trait Implementations§
Source§impl Decoder for CachedTokenizer
impl Decoder for CachedTokenizer
fn decode( &self, token_ids: &[TokenIdType], skip_special_tokens: bool, ) -> Result<DecodeResult>
Source§impl Encoder for CachedTokenizer
impl Encoder for CachedTokenizer
fn encode(&self, input: &str) -> Result<Encoding>
fn encode_batch(&self, inputs: &[&str]) -> Result<Vec<Encoding>>
Source§fn encode_segments(&self, segments: &[EncodeSegment<'_>]) -> Result<Encoding>
fn encode_segments(&self, segments: &[EncodeSegment<'_>]) -> Result<Encoding>
Source§impl Tokenizer for CachedTokenizer
impl Tokenizer for CachedTokenizer
Source§fn vocab_size(&self) -> Option<usize>
fn vocab_size(&self) -> Option<usize>
None for backends without a bounded id space or
vocabulary introspection.Source§fn token_to_id(&self, token: &str) -> Result<Option<TokenIdType>>
fn token_to_id(&self, token: &str) -> Result<Option<TokenIdType>>
Ok(None) when the token is not in the
vocabulary. Err when this backend cannot do id lookup at all —
kept distinct from a genuine vocabulary miss so callers can tell
“unsupported” from “looked up, not found”. Read moreSource§fn special_token_ids(&self) -> Result<Vec<TokenIdType>>
fn special_token_ids(&self) -> Result<Vec<TokenIdType>>
Ok(vec![])
for backends that genuinely have none, or that do not distinguish
special from ordinary vocabulary ids. Err when the backend
cannot enumerate added tokens at all — an empty Vec alone
cannot carry that distinction. Read moreSource§fn num_special_tokens_added(&self) -> Result<usize>
fn num_special_tokens_added(&self) -> Result<usize>
encode’s add_special_tokens: true
path would add to a bare encoding (e.g. BOS/EOS), available
without performing an encode. Ok(0) for backends that
genuinely add none. Err when the backend cannot determine this
at all — a plain 0 alone cannot carry that distinction, and
this value feeds token-budget accounting where a silent 0
would under-count rather than fail loudly. Read moreSource§fn validate_prefix_cache(&self) -> Result<()>
fn validate_prefix_cache(&self) -> Result<()>
Source§fn with_options(self, options: TokenizerOptions) -> Selfwhere
Self: Sized,
fn with_options(self, options: TokenizerOptions) -> Selfwhere
Self: Sized,
TokenizerOptions. Read moreAuto Trait Implementations§
impl !Freeze for CachedTokenizer
impl !RefUnwindSafe for CachedTokenizer
impl !UnwindSafe for CachedTokenizer
impl Send for CachedTokenizer
impl Sync for CachedTokenizer
impl Unpin for CachedTokenizer
impl UnsafeUnpin for CachedTokenizer
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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