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
aien-sovereign-core-mv / crates / aien-inference-abi / tests / bitnet_tokenizer_real.rs
| 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 | //! Our BitNet tokenizer on the real `tokenizer.json` (ignored: needs the checkpoint). |
| 17 | //! |
| 18 | //! BITNET_DIR=~/models/BitNet-b1.58-2B-4T-bf16 cargo test -p aien-inference-abi \ |
| 19 | //! --test bitnet_tokenizer_real -- --ignored |
| 20 | //! |
| 21 | //! The yardsticks are our own: the five acceptance id lists recorded in the ternary-oracle spec |
| 22 | //! (section 6), spot ranks from the reference `tokenizer.model` (spec section 6: "The" = 791, |
| 23 | //! " is" = 374, " France" = 9822), and exact round trips. No other tokenizer runs. |
| 24 | |
| 25 | use std::path::PathBuf; |
| 26 | |
| 27 | use aien_inference_abi::bitnet_omega::{sha256_file, BITNET_2B4T_TOKENIZER_SHA256}; |
| 28 | use aien_inference_abi::bitnet_tokenizer::BitNetTokenizer; |
| 29 | |
| 30 | fn dir() -> PathBuf { |
| 31 | std::env::var_os("BITNET_DIR") |
| 32 | .map(PathBuf::from) |
| 33 | .unwrap_or_else(|| { |
| 34 | PathBuf::from(std::env::var("HOME").expect("HOME")) |
| 35 | .join("models/BitNet-b1.58-2B-4T-bf16") |
| 36 | }) |
| 37 | } |
| 38 | |
| 39 | #[test] |
| 40 | #[ignore = "needs the BitNet 2B-4T checkpoint"] |
| 41 | fn real_tokenizer_matches_the_spec_ids_and_round_trips() { |
| 42 | let path = dir().join("tokenizer.json"); |
| 43 | assert_eq!( |
| 44 | sha256_file(&path).unwrap(), |
| 45 | BITNET_2B4T_TOKENIZER_SHA256, |
| 46 | "tokenizer.json is not the pinned file" |
| 47 | ); |
| 48 | let t = BitNetTokenizer::from_file(&path).unwrap(); |
| 49 | assert_eq!(t.vocab_len(), 128_256); |
| 50 | assert_eq!(t.bos, 128_000); |
| 51 | |
| 52 | let spec: [(&str, &[u32]); 5] = [ |
| 53 | ( |
| 54 | "The capital of France is", |
| 55 | &[128000, 791, 6864, 315, 9822, 374], |
| 56 | ), |
| 57 | ( |
| 58 | "The capital of Germany is", |
| 59 | &[128000, 791, 6864, 315, 10057, 374], |
| 60 | ), |
| 61 | ( |
| 62 | "Count from 6 to 10:", |
| 63 | &[128000, 2568, 505, 220, 21, 311, 220, 605, 25], |
| 64 | ), |
| 65 | ( |
| 66 | "The boiling point of water in Celsius is", |
| 67 | &[128000, 791, 50937, 1486, 315, 3090, 304, 62447, 374], |
| 68 | ), |
| 69 | ( |
| 70 | "The largest planet in the solar system is", |
| 71 | &[128000, 791, 7928, 11841, 304, 279, 13238, 1887, 374], |
| 72 | ), |
| 73 | ]; |
| 74 | for (text, ids) in spec { |
| 75 | assert_eq!(t.encode(text, true), ids, "{text:?}"); |
| 76 | assert_eq!(t.decode(&ids[1..]).unwrap(), text); |
| 77 | } |
| 78 | let chat = t.encode("<|begin_of_text|>User: hi<|eot_id|>", false); |
| 79 | assert_eq!((chat[0], *chat.last().unwrap()), (128000, 128009)); |
| 80 | assert_eq!( |
| 81 | t.decode(&chat).unwrap(), |
| 82 | "<|begin_of_text|>User: hi<|eot_id|>" |
| 83 | ); |
| 84 | assert_eq!(*t.encode("<|eot_id|>", false), [128009]); |
| 85 | |
| 86 | let stress = "Tab\there, CRLF\r\n\r\n spaced out \u{3000}x; I'M we'Ve it'ſ \ |
| 87 | über-naïve 東京タワー 12345 ½ ٣٤٥ emoji 🙂🙂 \u{200d} trailing "; |
| 88 | let ids = t.encode(stress, false); |
| 89 | assert_eq!(t.decode(&ids).unwrap(), stress); |
| 90 | assert!(ids.iter().all(|&i| i < 128_000)); |
| 91 | } |