diff --git a/crates/tokenizer/examples/long_prompts.rs b/crates/tokenizer/examples/long_prompts.rs index 2cff84fa..cf0b34b5 100644 --- a/crates/tokenizer/examples/long_prompts.rs +++ b/crates/tokenizer/examples/long_prompts.rs @@ -12,8 +12,11 @@ use std::{env, fs, path::Path, process::ExitCode}; use leleka_tokenizer::Tokenizer; -const PREAMBLE: &str = "<|im_start|>user\nBelow is part of a code base with its documentation. Read it, then answer the question at the end.\n\n"; -const QUESTION: &str = "\n\nQuestion: In four or five sentences, which component of the text above looks the most performance critical, and why? Mention one concrete risk.<|im_end|>\n<|im_start|>assistant\n\n\n\n\n"; +const PREAMBLE: &str = "<|im_start|>user\nBelow is part of a code base with its documentation. Read it, then answer \ + the question at the end.\n\n"; +const QUESTION: &str = "\n\nQuestion: In four or five sentences, which component of the text above looks the most \ + performance critical, and why? Mention one concrete \ + risk.<|im_end|>\n<|im_start|>assistant\n\n\n\n\n"; fn main() -> ExitCode { let args: Vec = env::args().collect(); diff --git a/crates/tokenizer/src/cache.rs b/crates/tokenizer/src/cache.rs index cecac043..6b74d67f 100644 --- a/crates/tokenizer/src/cache.rs +++ b/crates/tokenizer/src/cache.rs @@ -1,14 +1,15 @@ //! A bounded cache of the token ids of text segments. //! -//! A chat prompt is split at the special tokens (`<|im_start|>`, ``, ``, ...) before byte-level BPE, so -//! the ids of the text *between* two special tokens depend on that text alone. A coding agent sends its whole history -//! again on every turn; all of the history except the last few segments is text this process tokenized on an earlier turn. -//! Keyed by the exact segment text, the cache returns those ids without the pre-tokenizer or the merges, and a hit is -//! verified byte for byte against the stored text, so a hash collision can only cost a miss, never a wrong id. +//! A chat prompt is split at the special tokens (`<|im_start|>`, ``, ``, ...) before byte-level BPE, +//! so the ids of the text *between* two special tokens depend on that text alone. A coding agent sends its whole +//! history again on every turn; all of the history except the last few segments is text this process tokenized on an +//! earlier turn. Keyed by the exact segment text, the cache returns those ids without the pre-tokenizer or the merges, +//! and a hit is verified byte for byte against the stored text, so a hash collision can only cost a miss, never a wrong +//! id. //! -//! The bound is a two-generation scheme: inserts go to the young generation; when it has taken half the budget it becomes -//! the old generation and the previous old one is dropped. A hit in the old generation is promoted. That evicts what has -//! not been used for a whole generation, in O(1), without an LRU list. +//! The bound is a two-generation scheme: inserts go to the young generation; when it has taken half the budget it +//! becomes the old generation and the previous old one is dropped. A hit in the old generation is promoted. That evicts +//! what has not been used for a whole generation, in O(1), without an LRU list. use std::{ collections::HashMap, @@ -72,7 +73,8 @@ impl SegmentCache { } } - /// `(hash, ids)` for a segment seen before; the hash is returned either way so the caller can insert without hashing again. + /// `(hash, ids)` for a segment seen before; the hash is returned either way so the caller can insert without + /// hashing again. pub fn lookup(&self, text: &str) -> (u64, Option>) { let key = digest(text); let Ok(mut cache) = self.inner.lock() else { diff --git a/crates/tokenizer/src/decoding.rs b/crates/tokenizer/src/decoding.rs index e6ac5a5a..826ccc67 100644 --- a/crates/tokenizer/src/decoding.rs +++ b/crates/tokenizer/src/decoding.rs @@ -73,7 +73,8 @@ impl Tokenizer { Ok(bytes) } - /// Whether `ids` decode to exactly `text`, without building the decoded bytes. An id that cannot be decoded is `false`. + /// Whether `ids` decode to exactly `text`, without building the decoded bytes. An id that cannot be decoded is + /// `false`. pub fn decodes_to(&self, ids: &[u32], text: &[u8]) -> bool { let mut at = 0; for &id in ids { diff --git a/crates/tokenizer/src/fast.rs b/crates/tokenizer/src/fast.rs index 48ee1ff2..50f0415a 100644 --- a/crates/tokenizer/src/fast.rs +++ b/crates/tokenizer/src/fast.rs @@ -1,14 +1,15 @@ -//! The tokenizer's hot path: a hand-written scanner for the pinned `qwen35` pre-tokenizer pattern and a table-driven byte-level -//! BPE over token ids. +//! The tokenizer's hot path: a hand-written scanner for the pinned `qwen35` pre-tokenizer pattern and a table-driven +//! byte-level BPE over token ids. //! //! Neither changes what the tokenizer returns. The scanner is the pattern's seven alternatives written out with the -//! backtracking they imply (see [`Classes::piece_end`]); its Unicode classes (`\p{L}`, `\p{M}`, `\p{N}`, `\s`) are read out -//! of `regex-syntax`, the crate the regex engine resolves them with, so both agree by construction on every scalar value -//! (`tests/fast_path.rs` checks all of them in many contexts, plus fuzzed text, against the regex). The BPE keeps the -//! reference algorithm (repeatedly merge the lowest-ranked adjacent pair, leftmost on ties) but keeps symbols as vocabulary -//! ids and ranks in an open-addressing table instead of re-building strings and hashing them for every comparison. -//! A vocabulary for which the id-based merge could differ from the string-based one (a merge whose result is not a vocabulary -//! token, a byte with no token) disables the fast path: [`Fast::build`] returns `None` and the string BPE stays in charge. +//! backtracking they imply (see [`Classes::piece_end`]); its Unicode classes (`\p{L}`, `\p{M}`, `\p{N}`, `\s`) are read +//! out of `regex-syntax`, the crate the regex engine resolves them with, so both agree by construction on every scalar +//! value (`tests/fast_path.rs` checks all of them in many contexts, plus fuzzed text, against the regex). The BPE keeps +//! the reference algorithm (repeatedly merge the lowest-ranked adjacent pair, leftmost on ties) but keeps symbols as +//! vocabulary ids and ranks in an open-addressing table instead of re-building strings and hashing them for every +//! comparison. A vocabulary for which the id-based merge could differ from the string-based one (a merge whose result +//! is not a vocabulary token, a byte with no token) disables the fast path: [`Fast::build`] returns `None` and the +//! string BPE stays in charge. use std::collections::HashMap; diff --git a/crates/tokenizer/src/gguf_bpe/impl_fmt_display_for.rs b/crates/tokenizer/src/gguf_bpe/impl_fmt_display_for.rs index a338f6fc..c8c3fe81 100644 --- a/crates/tokenizer/src/gguf_bpe/impl_fmt_display_for.rs +++ b/crates/tokenizer/src/gguf_bpe/impl_fmt_display_for.rs @@ -1,4 +1,5 @@ -//! Moved out of the parent module by STRA-338: impl fmt_Display_for, impl std_error_Error, fn byte_alphabet, fn byte_chars. +//! Moved out of the parent module by STRA-338: impl fmt_Display_for, impl std_error_Error, fn byte_alphabet, fn +//! byte_chars. #[allow(unused_imports)] use super::*; diff --git a/crates/tokenizer/src/lib.rs b/crates/tokenizer/src/lib.rs index bdba47b9..719269ae 100644 --- a/crates/tokenizer/src/lib.rs +++ b/crates/tokenizer/src/lib.rs @@ -49,7 +49,8 @@ pub fn pre_tokenize(text: &str) -> Result> { Ok(pieces) } -/// [`pre_tokenize`] by the hand-written scanner the tokenizer's fast path uses (same pieces; exposed for the differential tests). +/// [`pre_tokenize`] by the hand-written scanner the tokenizer's fast path uses (same pieces; exposed for the +/// differential tests). #[doc(hidden)] pub fn pre_tokenize_fast(text: &str) -> Vec<&str> { static CLASSES: std::sync::OnceLock = std::sync::OnceLock::new();