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 / qwen38_chat_template.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 | //! Qwen3.8-27B chat template (sc#642 serving). Pinned text: Hugging Face `Qwen/Qwen3.8-27B-FP8` |
| 17 | //! revision 017b9c7a (fixtures/chat-templates/qwen3.8-27b). |
| 18 | //! |
| 19 | //! Checks of the rendered bytes: |
| 20 | //! 1. JINJA ORACLE: `render_oracle.json`, the template rendered by jinja2 3.1.2 (scratch script |
| 21 | //! outside the repository) for 8 conversations x 6 option sets, thinking on, off and default. |
| 22 | //! 2. GOLDEN PROMPT: with the checkpoint present (`AIEN_QWEN38_DIR`, ignored test), the |
| 23 | //! thinking-off rendering of the golden question tokenizes to the host reference's |
| 24 | //! `prompt_ids` (`fixtures/qwen38/gen-chat-max-24.json`), the ids the chip gate replays. |
| 25 | use aien_inference_abi::tokenizer::{ChatOptions, ChatTemplate, ChatTokenizer}; |
| 26 | |
| 27 | const DIR: &str = concat!( |
| 28 | env!("CARGO_MANIFEST_DIR"), |
| 29 | "/fixtures/chat-templates/qwen3.8-27b" |
| 30 | ); |
| 31 | |
| 32 | /// Host reference golden (greedy, 24 tokens, thinking off) shipped with the repository; the |
| 33 | /// chip gate `tools/qwen38_serve_gate.sh` replays the same file. |
| 34 | const GOLDEN: &str = include_str!(concat!( |
| 35 | env!("CARGO_MANIFEST_DIR"), |
| 36 | "/fixtures/qwen38/gen-chat-max-24.json" |
| 37 | )); |
| 38 | |
| 39 | #[test] |
| 40 | fn shipped_golden_is_well_formed() { |
| 41 | let g: serde_json::Value = serde_json::from_str(GOLDEN).unwrap(); |
| 42 | let ids = |k: &str| g[k].as_array().map(|a| a.len()).unwrap_or(0); |
| 43 | assert_eq!(ids("generated_ids"), 24); |
| 44 | assert!(ids("prompt_ids") > 0); |
| 45 | // The reference answer opens a C code block (see PROVENANCE.md next to the file). |
| 46 | assert!(g["generated_text"] |
| 47 | .as_str() |
| 48 | .unwrap() |
| 49 | .starts_with("```c\nint max(")); |
| 50 | } |
| 51 | |
| 52 | /// The speed tool's references: 256 reference ids for its decode request, and a corpus long |
| 53 | /// enough for its 100,000-byte prompt (see fixtures/qwen38/PROVENANCE.md). |
| 54 | #[test] |
| 55 | fn shipped_speed_references_are_well_formed() { |
| 56 | let dir = concat!(env!("CARGO_MANIFEST_DIR"), "/fixtures/qwen38"); |
| 57 | let r: serde_json::Value = serde_json::from_str( |
| 58 | &std::fs::read_to_string(format!("{dir}/decode-lighthouses-256.json")).unwrap(), |
| 59 | ) |
| 60 | .unwrap(); |
| 61 | assert_eq!(r["ids"].as_array().unwrap().len(), 256); |
| 62 | assert_eq!(r["request"]["max_tokens"], 256); |
| 63 | assert_eq!(r["request"]["temperature"], 0); |
| 64 | assert_eq!(r["request"]["aien_return_token_ids"], true); |
| 65 | let corpus = std::fs::read(format!("{dir}/speed-corpus.txt")).unwrap(); |
| 66 | assert!(corpus.len() >= 100_000, "corpus is {} bytes", corpus.len()); |
| 67 | assert!(std::str::from_utf8(&corpus).is_ok()); |
| 68 | } |
| 69 | |
| 70 | fn t() -> ChatTemplate { |
| 71 | let text = std::fs::read_to_string(format!("{DIR}/chat_template.jinja")).unwrap(); |
| 72 | ChatTemplate::detect(&text).expect("Qwen3.8 template accepted") |
| 73 | } |
| 74 | |
| 75 | fn opts(v: &serde_json::Value) -> ChatOptions { |
| 76 | ChatOptions { |
| 77 | enable_thinking: v.get("enable_thinking").and_then(|x| x.as_bool()), |
| 78 | reasoning_effort: v |
| 79 | .get("reasoning_effort") |
| 80 | .and_then(|x| x.as_str()) |
| 81 | .map(String::from), |
| 82 | preserve_thinking: v.get("preserve_thinking").and_then(|x| x.as_bool()), |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | #[test] |
| 87 | fn pinned_template_is_selected() { |
| 88 | assert_eq!(t(), ChatTemplate::Qwen38); |
| 89 | } |
| 90 | |
| 91 | #[test] |
| 92 | fn rendering_matches_the_jinja_reference_rendering() { |
| 93 | let oracle: serde_json::Value = serde_json::from_str( |
| 94 | &std::fs::read_to_string(format!("{DIR}/render_oracle.json")).unwrap(), |
| 95 | ) |
| 96 | .unwrap(); |
| 97 | let tpl = t(); |
| 98 | let cases = oracle["cases"].as_array().unwrap(); |
| 99 | assert_eq!(cases.len(), 48); |
| 100 | for (i, case) in cases.iter().enumerate() { |
| 101 | let turns: Vec<(String, String)> = case["turns"] |
| 102 | .as_array() |
| 103 | .unwrap() |
| 104 | .iter() |
| 105 | .map(|p| (p[0].as_str().unwrap().into(), p[1].as_str().unwrap().into())) |
| 106 | .collect(); |
| 107 | let refs: Vec<(&str, &str)> = turns |
| 108 | .iter() |
| 109 | .map(|(r, c)| (r.as_str(), c.as_str())) |
| 110 | .collect(); |
| 111 | let o = opts(&case["options"]); |
| 112 | assert_eq!( |
| 113 | tpl.try_render_with(&refs, &o).unwrap(), |
| 114 | case["text"].as_str().unwrap(), |
| 115 | "case {i} options {o:?}" |
| 116 | ); |
| 117 | // The default is the template's default (thinking on, effort xhigh). |
| 118 | if o == ChatOptions::default() { |
| 119 | assert_eq!( |
| 120 | tpl.try_render(&refs).unwrap(), |
| 121 | case["text"].as_str().unwrap() |
| 122 | ); |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | #[test] |
| 128 | fn options_change_the_rendering_and_bad_ones_are_refused() { |
| 129 | let tpl = t(); |
| 130 | let turns = [("user", "Hi")]; |
| 131 | let on = tpl |
| 132 | .try_render_with(&turns, &ChatOptions::default()) |
| 133 | .unwrap(); |
| 134 | let off = tpl |
| 135 | .try_render_with( |
| 136 | &turns, |
| 137 | &ChatOptions { |
| 138 | enable_thinking: Some(false), |
| 139 | reasoning_effort: None, |
| 140 | preserve_thinking: None, |
| 141 | }, |
| 142 | ) |
| 143 | .unwrap(); |
| 144 | assert_ne!(on, off); |
| 145 | assert!(on.ends_with("<|im_start|>assistant\n<think>\n")); |
| 146 | assert!(off.ends_with("<|im_start|>assistant\n<think>\n\n</think>\n\n")); |
| 147 | let bad = ChatOptions { |
| 148 | enable_thinking: None, |
| 149 | reasoning_effort: Some("max".into()), |
| 150 | preserve_thinking: None, |
| 151 | }; |
| 152 | assert!(tpl.try_render_with(&turns, &bad).is_err()); |
| 153 | assert!(tpl |
| 154 | .try_render_with( |
| 155 | &[("user", "a"), ("system", "late")], |
| 156 | &ChatOptions::default() |
| 157 | ) |
| 158 | .is_err()); |
| 159 | assert!(tpl |
| 160 | .try_render_with(&[("assistant", "no user")], &ChatOptions::default()) |
| 161 | .is_err()); |
| 162 | } |
| 163 | |
| 164 | #[test] |
| 165 | #[ignore = "needs AIEN_QWEN38_DIR (the checkpoint)"] |
| 166 | fn golden_prompt_tokenizes_to_the_reference_ids() { |
| 167 | let dir = std::env::var("AIEN_QWEN38_DIR").expect("AIEN_QWEN38_DIR"); |
| 168 | let g: serde_json::Value = serde_json::from_str(GOLDEN).unwrap(); |
| 169 | let want: Vec<u32> = g["prompt_ids"] |
| 170 | .as_array() |
| 171 | .unwrap() |
| 172 | .iter() |
| 173 | .map(|v| v.as_u64().unwrap() as u32) |
| 174 | .collect(); |
| 175 | let tk = ChatTokenizer::from_model_dir(std::path::Path::new(&dir), None).unwrap(); |
| 176 | assert_eq!(tk.template(), ChatTemplate::Qwen38); |
| 177 | let off = ChatOptions { |
| 178 | enable_thinking: Some(false), |
| 179 | reasoning_effort: None, |
| 180 | preserve_thinking: None, |
| 181 | }; |
| 182 | let text = tk |
| 183 | .try_format_chat_with( |
| 184 | &[( |
| 185 | "user", |
| 186 | "Write a C function that returns the larger of two ints.", |
| 187 | )], |
| 188 | &off, |
| 189 | ) |
| 190 | .unwrap(); |
| 191 | assert_eq!(tk.encode_with_special(&text, true).unwrap(), want); |
| 192 | // Negative control: the thinking-on rendering of the same question must not match. |
| 193 | let on = tk |
| 194 | .try_format_chat_with( |
| 195 | &[( |
| 196 | "user", |
| 197 | "Write a C function that returns the larger of two ints.", |
| 198 | )], |
| 199 | &ChatOptions::default(), |
| 200 | ) |
| 201 | .unwrap(); |
| 202 | assert_ne!(tk.encode_with_special(&on, true).unwrap(), want); |
| 203 | assert_eq!(tk.stop_token_ids(), &[248046, 248044]); |
| 204 | // The limit comes from config.json text_config (262144), not the 2048 default: a 30k prompt |
| 205 | // encodes (the server's 128k context needs it). |
| 206 | let long = "word ".repeat(30_000); |
| 207 | assert!(tk.encode_with_special(&long, true).unwrap().len() > 2048); |
| 208 | } |