interplane.Get Omega
Read in your language

aien-sovereign-core-mv

AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination.

Brought by drakestapleton

branch main Branches Tags History

aien-sovereign-core-mv / crates / aien-inference-abi / tests / openwaldo_byte_tokenizer.rs

100 lines · 3737 bytes Raw Blame History
1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2026 Drake Stapleton (AIEN / aien-dev)
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16//! OpenWALDO byte tokenizer as a plain `tokenizer.json`.
17//!
18//! OpenWALDO exports ship a custom Python tokenizer (`OpenWALDOByteTokenizer`):
19//! `<pad>`=0, `<bos>`=1, `<eos>`=2, then byte `b` -> id `b + 3`. The fixture
20//! `tokenizer.json` states the same mapping as a BPE model with no merges and
21//! byte fallback, so AIEN loads it with no tokenizer code change. The
22//! reference ids and decoded strings in `reference.json` were produced by the
23//! exported Python class itself (transformers 5.17.0, CPU).
24
25use aien_inference_abi::tokenizer::ChatTokenizer;
26use serde_json::Value;
27
28const DIR: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/tests/fixtures/openwaldo-byte");
29
30fn load() -> (ChatTokenizer, Vec<Value>) {
31 let tok =
32 ChatTokenizer::from_file(format!("{DIR}/tokenizer.json")).expect("load tokenizer.json");
33 let text = std::fs::read_to_string(format!("{DIR}/reference.json")).expect("read reference");
34 let cases: Vec<Value> = serde_json::from_str(&text).expect("parse reference");
35 (tok, cases)
36}
37
38fn ids(v: &Value) -> Vec<u32> {
39 v["ids"]
40 .as_array()
41 .unwrap()
42 .iter()
43 .map(|i| i.as_u64().unwrap() as u32)
44 .collect()
45}
46
47#[test]
48fn encode_matches_openwaldo_reference() {
49 let (tok, cases) = load();
50 let mut checked = 0;
51 for case in cases.iter().filter(|c| c["text"].is_string()) {
52 let text = case["text"].as_str().unwrap();
53 let got = tok.encode_with_special(text, false).expect("encode");
54 assert_eq!(got, ids(case), "encode mismatch for {text:?}");
55 checked += 1;
56 }
57 assert_eq!(checked, 10);
58}
59
60#[test]
61fn decode_matches_openwaldo_reference_on_valid_utf8() {
62 let (tok, cases) = load();
63 for case in cases.iter().filter(|c| c["text"].is_string()) {
64 let got = tok.decode_opts(&ids(case), false).expect("decode");
65 assert_eq!(
66 got,
67 case["decoded"].as_str().unwrap(),
68 "decode mismatch for {:?}",
69 ids(case)
70 );
71 }
72}
73
74#[test]
75fn every_byte_maps_to_byte_plus_three() {
76 let (tok, _) = load();
77 for b in 0u8..=255 {
78 let id = u32::from(b) + 3;
79 let s = format!("<0x{b:02X}>");
80 assert_eq!(tok.token_to_id(&s), Some(id), "{s}");
81 }
82}
83
84/// Known limit: on an invalid UTF-8 byte run the two decoders disagree.
85/// Python (`errors="replace"`) emits one U+FFFD per maximal invalid subpart and
86/// keeps the following valid byte; the `tokenizers` ByteFallback decoder emits
87/// one U+FFFD per byte token of the whole run. Token ids are identical, so
88/// receipts that bind token ids are unaffected; decoded text is not.
89/// This test pins today's behaviour so a decoder change is noticed.
90#[test]
91fn decode_diverges_on_invalid_utf8_run() {
92 let (tok, cases) = load();
93 let case = cases
94 .iter()
95 .find(|c| c["text"].is_null())
96 .expect("invalid-run case");
97 assert_eq!(case["decoded"].as_str().unwrap(), "\u{FFFD}A");
98 let got = tok.decode_opts(&ids(case), false).expect("decode");
99 assert_eq!(got, "\u{FFFD}\u{FFFD}\u{FFFD}");
100}