Rust comments that named scripts/offline/*, gen_goldens.py, oracle/gen_*.{sh,cpp,py}, build_oracle.sh and similar retired generators now say where the data
came from (the Original's code, Strata 41cf05a0) or drop the sentence. The path/generator text of seven generated fixtures (sampler_oracle.json, sampling_keys.json,
memory_plan_golden.json, rope_golden.json, cvec_oracle.json, stra161 fixtures.json, commentary-tool-prompt.json) is reworded; no test reads those fields and no pin
covers their bytes. cmd_time/doc.rs: the two path texts in the format string; its lines are re-broken so they stay within 120 columns (the string is unchanged
otherwise). Comment-only except those data/string spots; no behaviour change.
66 lines
2.7 KiB
Rust
66 lines
2.7 KiB
Rust
//! Tokenizer versus `Strata 41cf05a0, tools/strata_tokenizer.py` (the unmodified oracle, regex 2026.9.10).
|
|
//!
|
|
//! `tests/fixtures/tokenizer_oracle.json` was recorded from the Original's Python code: 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.
|
|
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:?}");
|
|
}
|
|
}
|