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-runtime / tests / decoding_observed_test.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 | //! sc#294 (Qwen3 v4 review): greedy decoding must be OBSERVED, not only |
| 17 | //! documented. The real submission path the daemon uses (`submit_work` -> |
| 18 | //! scheduler -> `NativeTransformerBackend::execute_step`) reports, in |
| 19 | //! `CompletionEvent::Finished`, how the backend chose the tokens, counted in |
| 20 | //! the sampling branch it took. CPU reference tensors, micro model. |
| 21 | use aien_inference_abi::{ |
| 22 | DecodeObservation, MockInferenceBackend, ModelConfig, NativeTransformerBackend, |
| 23 | ReferenceCpuBackend, SamplingParams, TransformerWeights, |
| 24 | }; |
| 25 | use aien_kv_cache::{create_shared_kv_manager, SharedKvManager}; |
| 26 | use aien_runtime::spine::AienRuntimeSpine; |
| 27 | use aien_scheduler::{ChannelCompletionSink, CompletionEvent, PromptHandle, SchedulerConfig}; |
| 28 | use std::sync::Arc; |
| 29 | |
| 30 | fn micro_config() -> ModelConfig { |
| 31 | ModelConfig { |
| 32 | model_id: "aien-micro-v1".to_string(), |
| 33 | max_sequence_length: 512, |
| 34 | block_size: 16, |
| 35 | num_layers: 2, |
| 36 | num_heads: 4, |
| 37 | head_dim: 16, |
| 38 | num_kv_heads: 2, |
| 39 | hidden_dim: 64, |
| 40 | intermediate_dim: 128, |
| 41 | vocab_size: 256, |
| 42 | rms_norm_eps: 1e-5, |
| 43 | rope_theta: 10000.0, |
| 44 | rope_scaling: None, |
| 45 | tie_word_embeddings: false, |
| 46 | eos_token_ids: Vec::new(), |
| 47 | qk_norm: false, |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | fn spine() -> (AienRuntimeSpine, SharedKvManager) { |
| 52 | let kv = create_shared_kv_manager(64, 16); |
| 53 | let cfg = SchedulerConfig { |
| 54 | max_batch_size: 16, |
| 55 | max_batch_tokens: 512, |
| 56 | max_prefill_tokens: 256, |
| 57 | prefill_chunk_size: 64, |
| 58 | chunk_prefill: true, |
| 59 | watermark_blocks: 4, |
| 60 | }; |
| 61 | (AienRuntimeSpine::new(64, cfg, kv.clone()), kv) |
| 62 | } |
| 63 | |
| 64 | /// Runs one request to completion; returns the streamed tokens, the |
| 65 | /// `Finished` decoding report and the sequence id. |
| 66 | async fn run<B: aien_inference_abi::AienInferenceBackend>( |
| 67 | spine: &mut AienRuntimeSpine, |
| 68 | backend: &mut B, |
| 69 | sampling: SamplingParams, |
| 70 | ) -> (Vec<u32>, Option<DecodeObservation>, u64) { |
| 71 | let (sink, mut rx) = ChannelCompletionSink::channel(); |
| 72 | let sink_id = spine.register_completion_sink(Arc::new(sink)); |
| 73 | let prompt: PromptHandle = Arc::from([1u32, 10, 25, 42].as_slice()); |
| 74 | let seq = spine |
| 75 | .submit_work(prompt, sampling, 1, Some(sink_id)) |
| 76 | .unwrap(); |
| 77 | spine.run_until_complete(backend, 40).await.unwrap(); |
| 78 | let (mut tokens, mut decoding, mut finished) = (Vec::new(), None, false); |
| 79 | while let Ok(event) = rx.try_recv() { |
| 80 | match event { |
| 81 | CompletionEvent::Token { token, .. } => tokens.push(token), |
| 82 | CompletionEvent::Finished { decoding: d, .. } => { |
| 83 | finished = true; |
| 84 | decoding = d; |
| 85 | } |
| 86 | CompletionEvent::Error { message, .. } => panic!("{message}"), |
| 87 | } |
| 88 | } |
| 89 | assert!(finished, "the request must finish"); |
| 90 | (tokens, decoding, seq) |
| 91 | } |
| 92 | |
| 93 | fn native(kv: SharedKvManager) -> NativeTransformerBackend { |
| 94 | NativeTransformerBackend::with_shared_kv_and_backend( |
| 95 | TransformerWeights::reference_test_weights(µ_config()), |
| 96 | Arc::new(ReferenceCpuBackend::new()), |
| 97 | kv, |
| 98 | ) |
| 99 | } |
| 100 | |
| 101 | /// Every token the backend chose was streamed, plus at most the final one |
| 102 | /// (a stop token is chosen but not streamed). |
| 103 | fn assert_counts_cover(tokens: &[u32], chosen: u64) { |
| 104 | let n = tokens.len() as u64; |
| 105 | assert!(n >= 1, "the request produced no token"); |
| 106 | assert!( |
| 107 | chosen == n || chosen == n + 1, |
| 108 | "backend chose {chosen} tokens, {n} were streamed" |
| 109 | ); |
| 110 | } |
| 111 | |
| 112 | #[tokio::test] |
| 113 | async fn greedy_request_reports_greedy_from_the_backend() { |
| 114 | let (mut spine, kv) = spine(); |
| 115 | let mut backend = native(kv); |
| 116 | let sampling = SamplingParams { |
| 117 | temperature: 0.0, |
| 118 | top_p: 1.0, |
| 119 | max_tokens: 5, |
| 120 | stop_token_ids: vec![], |
| 121 | }; |
| 122 | let (tokens, d, _) = run(&mut spine, &mut backend, sampling).await; |
| 123 | let d = d.expect("the native backend reports how it decoded"); |
| 124 | assert_eq!(d.mode(), "greedy", "{d:?}"); |
| 125 | assert_eq!(d.sampled_tokens, 0); |
| 126 | assert_counts_cover(&tokens, d.greedy_tokens); |
| 127 | assert_eq!( |
| 128 | (d.temperature, d.top_p, d.seed_request_id), |
| 129 | (None, None, None) |
| 130 | ); |
| 131 | } |
| 132 | |
| 133 | #[tokio::test] |
| 134 | async fn sampled_request_reports_sampling_from_the_backend() { |
| 135 | let (mut spine, kv) = spine(); |
| 136 | let mut backend = native(kv); |
| 137 | let sampling = SamplingParams { |
| 138 | temperature: 0.7, |
| 139 | top_p: 0.9, |
| 140 | max_tokens: 5, |
| 141 | stop_token_ids: vec![], |
| 142 | }; |
| 143 | let (tokens, d, seq) = run(&mut spine, &mut backend, sampling).await; |
| 144 | let d = d.expect("the native backend reports how it decoded"); |
| 145 | assert_eq!(d.mode(), "sampled", "{d:?}"); |
| 146 | assert_eq!(d.greedy_tokens, 0); |
| 147 | assert_counts_cover(&tokens, d.sampled_tokens); |
| 148 | assert_eq!(d.temperature, Some(0.7)); |
| 149 | assert_eq!(d.top_p, Some(0.9)); |
| 150 | assert_eq!(d.seed_request_id, Some(seq)); |
| 151 | } |
| 152 | |
| 153 | /// A backend that does not observe its decoding makes no claim: None, never |
| 154 | /// a default "greedy". |
| 155 | #[tokio::test] |
| 156 | async fn a_backend_without_the_observation_makes_no_claim() { |
| 157 | let (mut spine, _kv) = spine(); |
| 158 | let mut backend = MockInferenceBackend::new(1); |
| 159 | let sampling = SamplingParams { |
| 160 | temperature: 0.0, |
| 161 | top_p: 1.0, |
| 162 | max_tokens: 3, |
| 163 | stop_token_ids: vec![], |
| 164 | }; |
| 165 | let (_, d, _) = run(&mut spine, &mut backend, sampling).await; |
| 166 | assert_eq!(d, None); |
| 167 | } |
| 168 | |
| 169 | /// The record field: present with the backend's report, absent without one. |
| 170 | #[test] |
| 171 | fn record_field_matches_the_report_and_is_absent_without_one() { |
| 172 | let id = aien_runtime::generation::ModelIdentity { |
| 173 | model_sha256: "m".repeat(64), |
| 174 | model_digest_kind: "file".into(), |
| 175 | model_path: "/m".into(), |
| 176 | tokenizer_sha256: "t".repeat(64), |
| 177 | tokenizer_path: "/t".into(), |
| 178 | }; |
| 179 | let report = DecodeObservation { |
| 180 | greedy_tokens: 4, |
| 181 | ..Default::default() |
| 182 | }; |
| 183 | let ev = |d| aien_runtime::generation::TurnEvidence { |
| 184 | prompt_ids: &[1, 2], |
| 185 | output_ids: &[3, 4, 5], |
| 186 | text: "abc", |
| 187 | total_tokens: 5, |
| 188 | finish_reason: "eos", |
| 189 | request_id: 1, |
| 190 | operation_id: 1, |
| 191 | decoding: d, |
| 192 | ops: None, |
| 193 | }; |
| 194 | let start = aien_runtime::generation::DaemonStart(1); |
| 195 | let with = aien_runtime::generation::build_record(&id, &ev(Some(&report)), start); |
| 196 | assert_eq!( |
| 197 | with["decoding"], |
| 198 | serde_json::json!({"mode": "greedy", "greedy_tokens": 4, "sampled_tokens": 0}) |
| 199 | ); |
| 200 | let without = aien_runtime::generation::build_record(&id, &ev(None), start); |
| 201 | assert!(without.get("decoding").is_none(), "{without}"); |
| 202 | } |