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 / qwen36_chat_template.rs
| 1 | //! Qwen3.6-35B-A3B chat template (L312: the motor is the 35B server). Pinned text: Hugging Face |
| 2 | //! `Qwen/Qwen3.6-35B-A3B-FP8` revision 95a723d0 (fixtures/chat-templates/qwen3.6-35b-a3b). |
| 3 | //! |
| 4 | //! Checks of the rendered bytes: |
| 5 | //! 1. JINJA ORACLE: `render_oracle.json`, the template rendered by jinja2 3.1.2 (scratch script |
| 6 | //! outside the repository) for 8 conversations x 6 option sets (thinking undefined, on, off; |
| 7 | //! preserve_thinking on, off; a reasoning_effort the template ignores), plus the template's own |
| 8 | //! refusals. |
| 9 | //! 2. GOLDEN PROMPT: with the checkpoint present (`AIEN_QWEN36_DIR`, ignored test), the |
| 10 | //! thinking-off rendering of the golden question tokenizes to the host reference's |
| 11 | //! `prompt_ids` (`evidence/q36-host-20261020/gen-chat-max-24.json`), the ids the HTTP gate replays. |
| 12 | use aien_inference_abi::tokenizer::{ChatOptions, ChatTemplate, ChatTokenizer}; |
| 13 | |
| 14 | const DIR: &str = concat!( |
| 15 | env!("CARGO_MANIFEST_DIR"), |
| 16 | "/fixtures/chat-templates/qwen3.6-35b-a3b" |
| 17 | ); |
| 18 | const GOLDEN: &str = concat!( |
| 19 | env!("CARGO_MANIFEST_DIR"), |
| 20 | "/../../evidence/q36-host-20261020/gen-chat-max-24.json" |
| 21 | ); |
| 22 | const QUESTION: &str = "Write a C function that returns the larger of two ints."; |
| 23 | |
| 24 | fn t() -> ChatTemplate { |
| 25 | let text = std::fs::read_to_string(format!("{DIR}/chat_template.jinja")).unwrap(); |
| 26 | ChatTemplate::detect(&text).expect("Qwen3.6 template accepted") |
| 27 | } |
| 28 | |
| 29 | fn oracle() -> serde_json::Value { |
| 30 | serde_json::from_str(&std::fs::read_to_string(format!("{DIR}/render_oracle.json")).unwrap()) |
| 31 | .unwrap() |
| 32 | } |
| 33 | |
| 34 | fn opts(v: &serde_json::Value) -> ChatOptions { |
| 35 | ChatOptions { |
| 36 | enable_thinking: v.get("enable_thinking").and_then(|x| x.as_bool()), |
| 37 | reasoning_effort: v |
| 38 | .get("reasoning_effort") |
| 39 | .and_then(|x| x.as_str()) |
| 40 | .map(String::from), |
| 41 | preserve_thinking: v.get("preserve_thinking").and_then(|x| x.as_bool()), |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | fn turns_of(case: &serde_json::Value) -> Vec<(String, String)> { |
| 46 | case["turns"] |
| 47 | .as_array() |
| 48 | .unwrap() |
| 49 | .iter() |
| 50 | .map(|p| (p[0].as_str().unwrap().into(), p[1].as_str().unwrap().into())) |
| 51 | .collect() |
| 52 | } |
| 53 | |
| 54 | #[test] |
| 55 | fn pinned_template_is_selected() { |
| 56 | assert_eq!(t(), ChatTemplate::Qwen36); |
| 57 | // The tokenizer_config.json copy of the same template lacks the final newline; it still selects. |
| 58 | let text = std::fs::read_to_string(format!("{DIR}/chat_template.jinja")).unwrap(); |
| 59 | assert_eq!( |
| 60 | ChatTemplate::detect(text.trim_end()).unwrap(), |
| 61 | ChatTemplate::Qwen36 |
| 62 | ); |
| 63 | } |
| 64 | |
| 65 | #[test] |
| 66 | fn rendering_matches_the_jinja_reference_rendering() { |
| 67 | let oracle = oracle(); |
| 68 | let tpl = t(); |
| 69 | let cases = oracle["cases"].as_array().unwrap(); |
| 70 | assert_eq!(cases.len(), 48); |
| 71 | for (i, case) in cases.iter().enumerate() { |
| 72 | let turns = turns_of(case); |
| 73 | let refs: Vec<(&str, &str)> = turns |
| 74 | .iter() |
| 75 | .map(|(r, c)| (r.as_str(), c.as_str())) |
| 76 | .collect(); |
| 77 | let o = opts(&case["options"]); |
| 78 | assert_eq!( |
| 79 | tpl.try_render_with(&refs, &o).unwrap(), |
| 80 | case["text"].as_str().unwrap(), |
| 81 | "case {i} options {o:?}" |
| 82 | ); |
| 83 | // The default is the template's default (thinking on, earlier assistant turns plain). |
| 84 | if o == ChatOptions::default() { |
| 85 | assert_eq!( |
| 86 | tpl.try_render(&refs).unwrap(), |
| 87 | case["text"].as_str().unwrap() |
| 88 | ); |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | #[test] |
| 94 | fn refusals_match_the_template_exceptions() { |
| 95 | let oracle = oracle(); |
| 96 | let tpl = t(); |
| 97 | let refusals = oracle["refusals"].as_array().unwrap(); |
| 98 | assert_eq!(refusals.len(), 5); |
| 99 | for case in refusals { |
| 100 | let turns = turns_of(case); |
| 101 | let refs: Vec<(&str, &str)> = turns |
| 102 | .iter() |
| 103 | .map(|(r, c)| (r.as_str(), c.as_str())) |
| 104 | .collect(); |
| 105 | let want = case["error"].as_str().expect("the template raised"); |
| 106 | let err = tpl |
| 107 | .try_render_with(&refs, &opts(&case["options"])) |
| 108 | .expect_err(&format!("{}: must be refused", case["name"])); |
| 109 | assert!( |
| 110 | err.to_string().contains(want), |
| 111 | "{}: {err} should carry {want:?}", |
| 112 | case["name"] |
| 113 | ); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | #[test] |
| 118 | fn options_change_the_rendering_and_the_ignored_one_does_not() { |
| 119 | let tpl = t(); |
| 120 | let turns = [("user", "Hi")]; |
| 121 | let on = tpl |
| 122 | .try_render_with(&turns, &ChatOptions::default()) |
| 123 | .unwrap(); |
| 124 | let off = tpl |
| 125 | .try_render_with( |
| 126 | &turns, |
| 127 | &ChatOptions { |
| 128 | enable_thinking: Some(false), |
| 129 | ..ChatOptions::default() |
| 130 | }, |
| 131 | ) |
| 132 | .unwrap(); |
| 133 | assert_ne!(on, off); |
| 134 | assert!(on.ends_with("<|im_start|>assistant\n<think>\n")); |
| 135 | assert!(off.ends_with("<|im_start|>assistant\n<think>\n\n</think>\n\n")); |
| 136 | // The 35B template has no reasoning-effort switch: any value renders the default text. |
| 137 | let effort = ChatOptions { |
| 138 | reasoning_effort: Some("max".into()), |
| 139 | ..ChatOptions::default() |
| 140 | }; |
| 141 | assert_eq!(tpl.try_render_with(&turns, &effort).unwrap(), on); |
| 142 | // preserve_thinking only touches assistant turns before the last user query. |
| 143 | let multi = [("user", "Q1"), ("assistant", "A1"), ("user", "Q2")]; |
| 144 | let plain = tpl |
| 145 | .try_render_with(&multi, &ChatOptions::default()) |
| 146 | .unwrap(); |
| 147 | let kept = tpl |
| 148 | .try_render_with( |
| 149 | &multi, |
| 150 | &ChatOptions { |
| 151 | preserve_thinking: Some(true), |
| 152 | ..ChatOptions::default() |
| 153 | }, |
| 154 | ) |
| 155 | .unwrap(); |
| 156 | assert!(plain.contains("<|im_start|>assistant\nA1<|im_end|>\n")); |
| 157 | assert!(kept.contains("<|im_start|>assistant\n<think>\n\n</think>\n\nA1<|im_end|>\n")); |
| 158 | assert_eq!( |
| 159 | tpl.try_render_with( |
| 160 | &multi, |
| 161 | &ChatOptions { |
| 162 | preserve_thinking: Some(false), |
| 163 | ..ChatOptions::default() |
| 164 | } |
| 165 | ) |
| 166 | .unwrap(), |
| 167 | plain |
| 168 | ); |
| 169 | assert!(tpl |
| 170 | .try_render_with(&[("user", "a"), ("tool", "out")], &ChatOptions::default()) |
| 171 | .is_err()); |
| 172 | } |
| 173 | |
| 174 | #[test] |
| 175 | #[ignore = "needs AIEN_QWEN36_DIR (the checkpoint) and evidence/q36-host-20261020/gen-chat-max-24.json"] |
| 176 | fn golden_prompt_tokenizes_to_the_reference_ids() { |
| 177 | let dir = std::env::var("AIEN_QWEN36_DIR").expect("AIEN_QWEN36_DIR"); |
| 178 | let g: serde_json::Value = |
| 179 | serde_json::from_str(&std::fs::read_to_string(GOLDEN).unwrap()).unwrap(); |
| 180 | let want: Vec<u32> = g["prompt_ids"] |
| 181 | .as_array() |
| 182 | .unwrap() |
| 183 | .iter() |
| 184 | .map(|v| v.as_u64().unwrap() as u32) |
| 185 | .collect(); |
| 186 | assert_eq!(want.len(), 24); |
| 187 | let tk = ChatTokenizer::from_model_dir(std::path::Path::new(&dir), None).unwrap(); |
| 188 | assert_eq!(tk.template(), ChatTemplate::Qwen36); |
| 189 | let off = ChatOptions { |
| 190 | enable_thinking: Some(false), |
| 191 | ..ChatOptions::default() |
| 192 | }; |
| 193 | let text = tk |
| 194 | .try_format_chat_with(&[("user", QUESTION)], &off) |
| 195 | .unwrap(); |
| 196 | assert_eq!(tk.encode_with_special(&text, true).unwrap(), want); |
| 197 | // Negative control: the thinking-on rendering of the same question must not match. |
| 198 | let on = tk |
| 199 | .try_format_chat_with(&[("user", QUESTION)], &ChatOptions::default()) |
| 200 | .unwrap(); |
| 201 | assert_ne!(tk.encode_with_special(&on, true).unwrap(), want); |
| 202 | assert_eq!(tk.stop_token_ids(), &[248046, 248044]); |
| 203 | // The limit comes from config.json (text_config), not the 2048 default: the server's 128k |
| 204 | // context needs a long prompt to encode. |
| 205 | let long = "word ".repeat(30_000); |
| 206 | assert!(tk.encode_with_special(&long, true).unwrap().len() > 2048); |
| 207 | } |
| 208 | |
| 209 | /// Qwen3.5-9B (Hugging Face `Qwen/Qwen3.5-9B` revision c2022362, |
| 210 | /// fixtures/chat-templates/qwen3.5-9b): the 35B template minus `preserve_thinking`. Its |
| 211 | /// `render_oracle.json` is the same 48 conversations x options and 5 refusals rendered by jinja2 |
| 212 | /// 3.1.2 from the 9B text; it differs from the 35B oracle only in the 3 cases that set |
| 213 | /// `preserve_thinking: true` on a conversation with an assistant turn before the last user query. |
| 214 | const DIR35: &str = concat!( |
| 215 | env!("CARGO_MANIFEST_DIR"), |
| 216 | "/fixtures/chat-templates/qwen3.5-9b" |
| 217 | ); |
| 218 | |
| 219 | fn oracle35() -> serde_json::Value { |
| 220 | serde_json::from_str(&std::fs::read_to_string(format!("{DIR35}/render_oracle.json")).unwrap()) |
| 221 | .unwrap() |
| 222 | } |
| 223 | |
| 224 | #[test] |
| 225 | fn qwen35_pinned_template_is_selected_and_distinct() { |
| 226 | let text = std::fs::read_to_string(format!("{DIR35}/chat_template.jinja")).unwrap(); |
| 227 | assert_eq!(ChatTemplate::detect(&text).unwrap(), ChatTemplate::Qwen35); |
| 228 | assert_eq!( |
| 229 | ChatTemplate::detect(text.trim_end()).unwrap(), |
| 230 | ChatTemplate::Qwen35 |
| 231 | ); |
| 232 | // One byte off the pinned text is refused, not approximated. |
| 233 | let edited = text.replacen("last_query_index", "last_query_indeks", 1); |
| 234 | assert!(ChatTemplate::detect(&edited).is_err()); |
| 235 | } |
| 236 | |
| 237 | #[test] |
| 238 | fn qwen35_rendering_and_refusals_match_the_jinja_reference() { |
| 239 | let q36 = oracle(); |
| 240 | let oracle = oracle35(); |
| 241 | let text = std::fs::read_to_string(format!("{DIR35}/chat_template.jinja")).unwrap(); |
| 242 | let tpl = ChatTemplate::detect(&text).unwrap(); |
| 243 | let cases = oracle["cases"].as_array().unwrap(); |
| 244 | assert_eq!(cases.len(), 48); |
| 245 | let mut differ_from_35b = 0; |
| 246 | for (i, case) in cases.iter().enumerate() { |
| 247 | let turns = turns_of(case); |
| 248 | let refs: Vec<(&str, &str)> = turns |
| 249 | .iter() |
| 250 | .map(|(r, c)| (r.as_str(), c.as_str())) |
| 251 | .collect(); |
| 252 | let o = opts(&case["options"]); |
| 253 | let got = tpl.try_render_with(&refs, &o).unwrap(); |
| 254 | assert_eq!( |
| 255 | got, |
| 256 | case["text"].as_str().unwrap(), |
| 257 | "case {i} options {o:?}" |
| 258 | ); |
| 259 | if got != q36["cases"][i]["text"].as_str().unwrap() { |
| 260 | assert_eq!(o.preserve_thinking, Some(true), "case {i}"); |
| 261 | differ_from_35b += 1; |
| 262 | } |
| 263 | } |
| 264 | // The oracle exercises the one switch the 9B template lacks. |
| 265 | assert_eq!(differ_from_35b, 3); |
| 266 | let refusals = oracle["refusals"].as_array().unwrap(); |
| 267 | assert_eq!(refusals.len(), 5); |
| 268 | for case in refusals { |
| 269 | let turns = turns_of(case); |
| 270 | let refs: Vec<(&str, &str)> = turns |
| 271 | .iter() |
| 272 | .map(|(r, c)| (r.as_str(), c.as_str())) |
| 273 | .collect(); |
| 274 | let want = case["error"].as_str().expect("the template raised"); |
| 275 | let err = tpl |
| 276 | .try_render_with(&refs, &opts(&case["options"])) |
| 277 | .expect_err(&format!("{}: must be refused", case["name"])); |
| 278 | assert!( |
| 279 | err.to_string().contains(want), |
| 280 | "{}: {err} should carry {want:?}", |
| 281 | case["name"] |
| 282 | ); |
| 283 | } |
| 284 | } |