Files
leleka/crates/tokenizer/tests/oracle_differential.rs
T
Pavlo RatushnyiandClaude Sonnet 5.5 debfbd0941 test(tokenizer): committed oracle differential, per-codepoint pre-tokenizer check, batch CLI
- crates/tokenizer pre_tokenize + model pre-tokenize-batch/tokenize-batch/detokenize-batch (debug/differential)
- scripts/offline/diff_tokenizer.py: every Unicode scalar x 13 contexts vs Python regex (0 real mismatches;
  14k strings differ only where regex 2026.9.10 has Unicode 17 and regex-syntax/unicodedata 16), a synthetic
  GGUF with BPE-trained merges past the heap path, 5-file extraction compare, encode/decode fuzz
- golden crates/tokenizer/tests/fixtures/tokenizer_oracle.json + integration test
- stricter-by-design: duplicate merges, missing chat template, negative special ids

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-14 15:32:23 +02:00

100 lines
3.1 KiB
Rust

//! Tokenizer versus `engine/strata/tools/strata_tokenizer.py` (the unmodified oracle, regex 2026.9.10).
//!
//! `tests/fixtures/tokenizer_oracle.json` is produced by `scripts/offline/gen_goldens.py tokenizer_oracle`: a
//! synthetic byte-level BPE vocabulary (256-byte alphabet, 300 trained merges that reach past the 64-symbol heap
//! path, type-3 and type-4 specials including ones with regex metacharacters) and the oracle's answers for ~190
//! boundary and fuzzed texts (ids with and without special parsing, decoded text, pre-tokenizer pieces) plus the
//! pre-tokenizer split of sampled Unicode scalar values in five contexts.
//! `scripts/offline/diff_tokenizer.py` runs the exhaustive live version (every scalar value x 13 contexts).
use leleka_tokenizer::{Tokenizer, pre_tokenize};
use serde_json::Value;
fn golden() -> Value {
serde_json::from_str(include_str!("fixtures/tokenizer_oracle.json")).unwrap()
}
fn strings(value: &Value) -> Vec<String> {
value
.as_array()
.unwrap()
.iter()
.map(|v| v.as_str().unwrap().to_owned())
.collect()
}
fn ids(value: &Value) -> Vec<u32> {
value
.as_array()
.unwrap()
.iter()
.map(|v| v.as_u64().unwrap() as u32)
.collect()
}
fn tokenizer(golden: &Value) -> Tokenizer {
let types = golden["token_types"]
.as_array()
.unwrap()
.iter()
.map(|v| v.as_u64().unwrap() as u32)
.collect();
Tokenizer::from_vocabulary(
"gpt2",
"qwen35",
strings(&golden["tokens"]),
strings(&golden["merges"]),
Some(types),
)
.unwrap()
}
#[test]
fn encode_matches_the_oracle_with_and_without_special_parsing() {
let golden = golden();
let tokenizer = tokenizer(&golden);
let cases = golden["cases"].as_array().unwrap();
assert!(cases.len() > 150);
for case in cases {
let text = case["text"].as_str().unwrap();
assert_eq!(
tokenizer.encode(text, false).unwrap(),
ids(&case["plain"]),
"plain {text:?}"
);
assert_eq!(
tokenizer.encode(text, true).unwrap(),
ids(&case["special"]),
"special {text:?}"
);
}
}
#[test]
fn decode_matches_the_oracle_including_split_multibyte_characters() {
let golden = golden();
let tokenizer = tokenizer(&golden);
for case in golden["decodes"].as_array().unwrap() {
let ids = ids(&case["ids"]);
match case["text"].as_str() {
Some(text) => assert_eq!(tokenizer.decode(&ids).unwrap(), text, "{ids:?}"),
// a token outside the byte alphabet (the `<|im_` literal): neither side decodes it
None => assert!(tokenizer.decode(&ids).is_err(), "{ids:?}"),
}
}
}
#[test]
fn pre_tokenizer_matches_python_regex() {
let golden = golden();
let splits = golden["pre_tokenizer"].as_array().unwrap();
assert!(splits.len() > 500);
for case in splits {
let text = case["text"].as_str().unwrap();
assert_eq!(
pre_tokenize(text).unwrap(),
strings(&case["pieces"]),
"{text:?}"
);
}
}