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 / plain_model_load.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 | //! A base (non-chat) model directory has no chat template: it loads with an explicit |
| 17 | //! "no chat template" state, plain encode/decode work, and chat rendering is refused. |
| 18 | //! An existing but unrecognized template is still refused at load. |
| 19 | |
| 20 | use aien_inference_abi::tokenizer::{ChatTemplate, ChatTokenizer}; |
| 21 | use std::path::{Path, PathBuf}; |
| 22 | |
| 23 | const DIR: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/tests/fixtures/openwaldo-byte"); |
| 24 | |
| 25 | fn scratch(name: &str) -> PathBuf { |
| 26 | let dir = std::env::temp_dir().join(format!("aien-plain-{name}-{}", std::process::id())); |
| 27 | let _ = std::fs::remove_dir_all(&dir); |
| 28 | std::fs::create_dir_all(&dir).unwrap(); |
| 29 | for f in ["tokenizer.json", "config.json", "generation_config.json"] { |
| 30 | std::fs::copy(Path::new(DIR).join(f), dir.join(f)).unwrap(); |
| 31 | } |
| 32 | dir |
| 33 | } |
| 34 | |
| 35 | #[test] |
| 36 | fn model_dir_without_template_loads_as_plain() { |
| 37 | let tok = ChatTokenizer::from_model_dir(Path::new(DIR), None).expect("plain model loads"); |
| 38 | assert_eq!(tok.template(), ChatTemplate::None); |
| 39 | assert!(tok.stop_token_ids().contains(&2)); |
| 40 | let ids = tok.encode("hi").expect("plain encode"); |
| 41 | assert!(!ids.is_empty()); |
| 42 | assert!(tok |
| 43 | .decode_opts(&ids, true) |
| 44 | .expect("plain decode") |
| 45 | .contains("hi")); |
| 46 | } |
| 47 | |
| 48 | #[test] |
| 49 | fn chat_render_on_plain_model_is_refused() { |
| 50 | let tok = ChatTokenizer::from_model_dir(Path::new(DIR), None).unwrap(); |
| 51 | let err = tok |
| 52 | .try_format_chat(&[("user", "hello")]) |
| 53 | .unwrap_err() |
| 54 | .to_string(); |
| 55 | assert!(err.contains("no chat template"), "{err}"); |
| 56 | let err = ChatTemplate::None |
| 57 | .try_render(&[("user", "hello")]) |
| 58 | .unwrap_err() |
| 59 | .to_string(); |
| 60 | assert!(err.contains("no chat template"), "{err}"); |
| 61 | } |
| 62 | |
| 63 | #[test] |
| 64 | fn unrecognized_template_still_refused_at_load() { |
| 65 | let dir = scratch("unrec"); |
| 66 | std::fs::write( |
| 67 | dir.join("tokenizer_config.json"), |
| 68 | r#"{"chat_template": "{{ messages }}"}"#, |
| 69 | ) |
| 70 | .unwrap(); |
| 71 | let err = ChatTokenizer::from_model_dir(&dir, None) |
| 72 | .err() |
| 73 | .expect("refused") |
| 74 | .to_string(); |
| 75 | assert!(err.contains("unsupported chat template"), "{err}"); |
| 76 | std::fs::write(dir.join("tokenizer_config.json"), "{}").unwrap(); |
| 77 | std::fs::write(dir.join("chat_template.jinja"), "{{ messages }}").unwrap(); |
| 78 | assert!(ChatTokenizer::from_model_dir(&dir, None).is_err()); |
| 79 | let _ = std::fs::remove_dir_all(&dir); |
| 80 | } |
| 81 | |
| 82 | /// Plain prompt format: the text is passed to the tokenizer as-is, nothing is wrapped |
| 83 | /// around it. Whatever the model's own tokenizer.json adds (BOS via its post-processor) |
| 84 | /// is added, exactly as `from_file` + `encode` does; the OpenWALDO byte tokenizer has |
| 85 | /// none, so "hi" is its two bytes (h=104, i=105, ids = byte + 3). |
| 86 | #[test] |
| 87 | fn plain_prompt_is_text_encoded_as_is() { |
| 88 | let tok = ChatTokenizer::from_model_dir(Path::new(DIR), None).unwrap(); |
| 89 | let direct = ChatTokenizer::from_file(format!("{DIR}/tokenizer.json")).unwrap(); |
| 90 | assert_eq!(tok.encode("hi").unwrap(), direct.encode("hi").unwrap()); |
| 91 | assert_eq!(tok.encode("hi").unwrap(), vec![107, 108]); |
| 92 | } |
| 93 | |
| 94 | /// A `chat_template` key that is present but not a string (Hugging Face's list form |
| 95 | /// `[{name, template}, ...]`, `null`, or an object) is an unrecognized template, not |
| 96 | /// "no template": it is refused at load and never falls back to the plain state. |
| 97 | #[test] |
| 98 | fn non_string_template_key_is_refused_not_plain() { |
| 99 | let dir = scratch("nonstring"); |
| 100 | for form in [ |
| 101 | r#"{"chat_template": [{"name": "default", "template": "{{ messages }}"}]}"#, |
| 102 | r#"{"chat_template": null}"#, |
| 103 | r#"{"chat_template": {"template": "{{ messages }}"}}"#, |
| 104 | ] { |
| 105 | std::fs::write(dir.join("tokenizer_config.json"), form).unwrap(); |
| 106 | let err = ChatTokenizer::from_model_dir(&dir, None) |
| 107 | .err() |
| 108 | .unwrap_or_else(|| panic!("loaded as plain: {form}")) |
| 109 | .to_string(); |
| 110 | assert!( |
| 111 | err.contains("unrecognized chat template form"), |
| 112 | "{form}: {err}" |
| 113 | ); |
| 114 | } |
| 115 | let _ = std::fs::remove_dir_all(&dir); |
| 116 | } |
| 117 | |
| 118 | #[test] |
| 119 | fn render_refusal_is_its_own_variant_not_a_load_failure() { |
| 120 | let tok = ChatTokenizer::from_model_dir(Path::new(DIR), None).unwrap(); |
| 121 | let err = tok.try_format_chat(&[("user", "hello")]).unwrap_err(); |
| 122 | assert!( |
| 123 | matches!(err, aien_inference_abi::TokenizerError::NoChatTemplate(_)), |
| 124 | "{err:?}" |
| 125 | ); |
| 126 | let text = err.to_string(); |
| 127 | assert!(text.contains("no chat template"), "{text}"); |
| 128 | assert!(!text.contains("Failed to load tokenizer"), "{text}"); |
| 129 | } |
| 130 | |
| 131 | /// Without `generation_config.json` the stop set is the config's `eos_token_id`, also read from |
| 132 | /// `text_config` (the Qwen3.5-9B layout), preceded by the tokenizer's declared `eos_token`. |
| 133 | /// With a generation config, its list is used as is and the tokenizer's token is not added. |
| 134 | #[test] |
| 135 | fn stop_set_without_generation_config_reads_text_config_and_tokenizer_eos() { |
| 136 | let dir = scratch("eos-fallback"); |
| 137 | std::fs::remove_file(dir.join("generation_config.json")).unwrap(); |
| 138 | std::fs::write( |
| 139 | dir.join("config.json"), |
| 140 | r#"{"model_type":"qwen3_5","text_config":{"eos_token_id":2,"max_position_embeddings":16}}"#, |
| 141 | ) |
| 142 | .unwrap(); |
| 143 | std::fs::write( |
| 144 | dir.join("tokenizer_config.json"), |
| 145 | r#"{"eos_token":"<pad>"}"#, |
| 146 | ) |
| 147 | .unwrap(); |
| 148 | let tok = ChatTokenizer::from_model_dir(&dir, None).unwrap(); |
| 149 | assert_eq!(tok.stop_token_ids(), &[0, 2]); |
| 150 | // The declared token already in the list is not repeated. |
| 151 | std::fs::write( |
| 152 | dir.join("tokenizer_config.json"), |
| 153 | r#"{"eos_token":"<eos>"}"#, |
| 154 | ) |
| 155 | .unwrap(); |
| 156 | let tok = ChatTokenizer::from_model_dir(&dir, None).unwrap(); |
| 157 | assert_eq!(tok.stop_token_ids(), &[2]); |
| 158 | // A generation config decides alone. |
| 159 | std::fs::write( |
| 160 | dir.join("tokenizer_config.json"), |
| 161 | r#"{"eos_token":"<pad>"}"#, |
| 162 | ) |
| 163 | .unwrap(); |
| 164 | std::fs::write(dir.join("generation_config.json"), r#"{"eos_token_id":2}"#).unwrap(); |
| 165 | let tok = ChatTokenizer::from_model_dir(&dir, None).unwrap(); |
| 166 | assert_eq!(tok.stop_token_ids(), &[2]); |
| 167 | // No eos anywhere is still refused. |
| 168 | std::fs::remove_file(dir.join("generation_config.json")).unwrap(); |
| 169 | std::fs::write(dir.join("config.json"), r#"{"model_type":"qwen3_5"}"#).unwrap(); |
| 170 | assert!(ChatTokenizer::from_model_dir(&dir, None).is_err()); |
| 171 | let _ = std::fs::remove_dir_all(&dir); |
| 172 | } |