STRA-338 size-fn2: leleka-tokenizer lines within 120 columns (wide 16 -> 0; comments re-filled, example strings continued at a space with the same bytes)
This commit is contained in:
@@ -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<think>\n\n</think>\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<think>\n\n</think>\n\n";
|
||||
|
||||
fn main() -> ExitCode {
|
||||
let args: Vec<String> = env::args().collect();
|
||||
|
||||
@@ -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|>`, `<think>`, `<tool_call>`, ...) 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|>`, `<think>`, `<tool_call>`, ...) 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<Arc<[u32]>>) {
|
||||
let key = digest(text);
|
||||
let Ok(mut cache) = self.inner.lock() else {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -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::*;
|
||||
|
||||
@@ -49,7 +49,8 @@ pub fn pre_tokenize(text: &str) -> Result<Vec<&str>> {
|
||||
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<fast::Classes> = std::sync::OnceLock::new();
|
||||
|
||||
Reference in New Issue
Block a user