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 / src / bin / own30b_ttft.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 | //! OWN-30B time-to-first-token, whole-prompt prefill OFF vs ON, in one process (W-L1G-W). |
| 17 | //! |
| 18 | //! own30b_ttft <checkpoint> <receipt-dir> [--lens 512,2048] [--new-tokens 32] [--reps 2] |
| 19 | //! [--max-context 8192] [--force-memory] |
| 20 | //! [--devhost] (W-L1G-E: also ON with router logits + combine on the chip, and expert agreement) |
| 21 | //! |
| 22 | //! Loads the FP8 model once (fused attention), then for every prompt length runs, `--reps` |
| 23 | //! times, OFF (the engine's chunked prefill) then ON (`set_whole_prompt(true)`), each from an |
| 24 | //! empty KV (`reset`), greedy, no stop ids, so exactly `--new-tokens` tokens. TTFT = wall time |
| 25 | //! from the `generate_with` call to the first emitted token (prefill + the first lm_head). |
| 26 | //! Compares OFF vs ON tokens (first differing position) and the first-step logits (cosine, max |
| 27 | //! abs diff, top-2 margins). Writes `run.json` and `run.txt` into the receipt dir. |
| 28 | use std::path::PathBuf; |
| 29 | use std::time::Instant; |
| 30 | |
| 31 | use aien_inference_abi::qwen3_moe_omega::{ |
| 32 | AttnPath, DevicePaths, GenerateParams, Qwen3MoeOmegaEngine, StepTiming, WeightFormat, |
| 33 | }; |
| 34 | use aien_inference_abi::qwen3_moe_omega_prefill::WpTrace; |
| 35 | use aien_inference_abi::tokenizer::ChatTokenizer as Tokenizer; |
| 36 | |
| 37 | fn mem_available_gb() -> f64 { |
| 38 | std::fs::read_to_string("/proc/meminfo") |
| 39 | .ok() |
| 40 | .and_then(|t| { |
| 41 | t.lines() |
| 42 | .find(|l| l.starts_with("MemAvailable:")) |
| 43 | .and_then(|l| l.split_whitespace().nth(1)) |
| 44 | .and_then(|v| v.parse::<f64>().ok()) |
| 45 | }) |
| 46 | .map(|kb| kb / 1024.0 / 1024.0) |
| 47 | .unwrap_or(0.0) |
| 48 | } |
| 49 | |
| 50 | /// A chat prompt of exactly `n` tokens: a fixed head, numbered log lines (each differs), cut so |
| 51 | /// that head + body + tail = n. Encoded piecewise (the tokenizer refuses > 2048 tokens per call). |
| 52 | fn prompt_ids(tok: &Tokenizer, n: usize) -> Vec<u32> { |
| 53 | let enc = |t: &str| tok.encode_with_special(t, false).expect("encode"); |
| 54 | let mut ids = enc("<|im_start|>user\nYou are a careful engineering assistant. Read the log below and say, in plain words, which block number has the largest decode time.\n\n"); |
| 55 | let tail = enc("\nAnswer with the block number only.<|im_end|>\n<|im_start|>assistant\n"); |
| 56 | assert!( |
| 57 | n > ids.len() + tail.len() + 8, |
| 58 | "prompt length {n} too short" |
| 59 | ); |
| 60 | let mut b = 0usize; |
| 61 | while ids.len() + tail.len() < n { |
| 62 | ids.extend(enc(&format!( |
| 63 | "[block {b}] loaded 48 layers in {} s, resident {}.{} GB\n[block {b}] prompt {} tokens, prefill {}.{} s\n[block {b}] decode 16 tokens, {} ms/token\n", |
| 64 | 200 + (b * 7) % 23, |
| 65 | 58 + b % 5, |
| 66 | (b * 3) % 10, |
| 67 | 20 + (b * 37) % 700, |
| 68 | 1 + (b * 5) % 40, |
| 69 | (b * 7) % 10, |
| 70 | 50 + (b * 11) % 37, |
| 71 | ))); |
| 72 | b += 1; |
| 73 | } |
| 74 | ids.truncate(n - tail.len()); |
| 75 | ids.extend(tail); |
| 76 | assert_eq!(ids.len(), n); |
| 77 | ids |
| 78 | } |
| 79 | |
| 80 | struct Run { |
| 81 | tokens: Vec<u32>, |
| 82 | logits0: Vec<f32>, |
| 83 | ttft_ms: f64, |
| 84 | prefill_ms: u64, |
| 85 | decode_ms: u64, |
| 86 | pt: StepTiming, |
| 87 | } |
| 88 | |
| 89 | fn run(engine: &mut Qwen3MoeOmegaEngine, ids: &[u32], new_tokens: usize) -> Run { |
| 90 | engine.reset(); |
| 91 | let mut tokens = Vec::new(); |
| 92 | let mut logits0 = Vec::new(); |
| 93 | let mut first: Option<f64> = None; |
| 94 | let t0 = Instant::now(); |
| 95 | let o = engine |
| 96 | .generate_with( |
| 97 | ids, |
| 98 | &GenerateParams::greedy(new_tokens, Vec::new()), |
| 99 | &mut |logits, step| { |
| 100 | if step == 0 { |
| 101 | logits0 = logits.to_vec(); |
| 102 | } |
| 103 | aien_inference_abi::tensor::sample_argmax(logits).0 |
| 104 | }, |
| 105 | &mut |t| { |
| 106 | if first.is_none() { |
| 107 | first = Some(t0.elapsed().as_secs_f64() * 1e3); |
| 108 | } |
| 109 | tokens.push(t); |
| 110 | true |
| 111 | }, |
| 112 | ) |
| 113 | .expect("generate"); |
| 114 | Run { |
| 115 | tokens, |
| 116 | logits0, |
| 117 | ttft_ms: first.expect("no token"), |
| 118 | prefill_ms: o.prefill_ms, |
| 119 | decode_ms: o.decode_ms, |
| 120 | pt: o.prefill_timing, |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | fn top2(l: &[f32]) -> (usize, f32) { |
| 125 | let mut idx: Vec<usize> = (0..l.len()).collect(); |
| 126 | idx.sort_by(|&a, &b| l[b].total_cmp(&l[a])); |
| 127 | (idx[0], l[idx[0]] - l[idx[1]]) |
| 128 | } |
| 129 | |
| 130 | fn cosine(a: &[f32], b: &[f32]) -> f64 { |
| 131 | let (mut ab, mut aa, mut bb) = (0.0f64, 0.0f64, 0.0f64); |
| 132 | for (x, y) in a.iter().zip(b) { |
| 133 | ab += *x as f64 * *y as f64; |
| 134 | aa += *x as f64 * *x as f64; |
| 135 | bb += *y as f64 * *y as f64; |
| 136 | } |
| 137 | ab / (aa.sqrt() * bb.sqrt()) |
| 138 | } |
| 139 | |
| 140 | fn main() { |
| 141 | let args: Vec<String> = std::env::args().collect(); |
| 142 | let checkpoint = PathBuf::from(args.get(1).expect("checkpoint dir")); |
| 143 | let receipt = PathBuf::from(args.get(2).expect("receipt dir")); |
| 144 | let (mut lens, mut new_tokens, mut reps, mut max_context, mut force, mut cached) = ( |
| 145 | vec![512usize, 2048], |
| 146 | 32usize, |
| 147 | 2usize, |
| 148 | 8192usize, |
| 149 | false, |
| 150 | false, |
| 151 | ); |
| 152 | let (mut devhost, mut devhost_exact) = (false, true); |
| 153 | let mut it = args[3..].iter(); |
| 154 | while let Some(a) = it.next() { |
| 155 | match a.as_str() { |
| 156 | "--lens" => { |
| 157 | lens = it |
| 158 | .next() |
| 159 | .expect("--lens") |
| 160 | .split(',') |
| 161 | .map(|s| s.parse().expect("len")) |
| 162 | .collect() |
| 163 | } |
| 164 | "--new-tokens" => new_tokens = it.next().expect("--new-tokens").parse().expect("n"), |
| 165 | "--reps" => reps = it.next().expect("--reps").parse().expect("n"), |
| 166 | "--max-context" => max_context = it.next().expect("--max-context").parse().expect("n"), |
| 167 | "--force-memory" => force = true, |
| 168 | "--cached" => cached = true, |
| 169 | "--devhost" => devhost = true, |
| 170 | o => panic!("unknown argument {o}"), |
| 171 | } |
| 172 | } |
| 173 | std::fs::create_dir_all(&receipt).expect("receipt dir"); |
| 174 | let mut log: Vec<String> = Vec::new(); |
| 175 | let mut say = |s: String| { |
| 176 | println!("{s}"); |
| 177 | log.push(s); |
| 178 | }; |
| 179 | let avail = mem_available_gb(); |
| 180 | say(format!("mem available before load: {avail:.1} GB")); |
| 181 | if avail < 70.0 && !force { |
| 182 | say("REFUSED: less than 70 GB available; --force-memory to override".into()); |
| 183 | std::process::exit(3); |
| 184 | } |
| 185 | if let Some(us) = |
| 186 | aien_omega_gpu::parse_spin_us(std::env::var(aien_omega_gpu::SPIN_US_ENV).ok().as_deref()) |
| 187 | .expect("spin env") |
| 188 | { |
| 189 | aien_omega_gpu::set_spin_us(us).expect("set spin window"); |
| 190 | } |
| 191 | say(format!( |
| 192 | "build native={} pf={} pattn={} pattn_dev={} spin_us={:?}", |
| 193 | aien_omega_gpu::is_native(), |
| 194 | aien_omega_pf::is_native(), |
| 195 | aien_omega_pf::has_attention(), |
| 196 | aien_omega_pf::has_attention_dev(), |
| 197 | aien_omega_gpu::spin_us() |
| 198 | )); |
| 199 | let tok = Tokenizer::from_file(checkpoint.join("tokenizer.json")).expect("tokenizer"); |
| 200 | let t0 = Instant::now(); |
| 201 | let (mut engine, report) = Qwen3MoeOmegaEngine::load_omega_paths( |
| 202 | &checkpoint, |
| 203 | max_context, |
| 204 | WeightFormat::Fp8All, |
| 205 | AttnPath::Fused, |
| 206 | DevicePaths::default(), |
| 207 | &mut |_| {}, |
| 208 | ) |
| 209 | .expect("load on Omega"); |
| 210 | say(format!( |
| 211 | "LOAD {:.1}s resident {:.2} GB attention {} prefill_gemm {} chunk {} mem_available_after {:.1} GB", |
| 212 | t0.elapsed().as_secs_f64(), |
| 213 | report.resident_bytes as f64 / 1e9, |
| 214 | engine.attention_path(), |
| 215 | engine.prefill_gemm, |
| 216 | engine.prefill_chunk, |
| 217 | mem_available_gb() |
| 218 | )); |
| 219 | let mut rows = Vec::new(); |
| 220 | let mut all_identical = true; |
| 221 | let mut offs: Vec<(usize, Vec<u32>, Vec<f32>, f64)> = Vec::new(); |
| 222 | for &n in &lens { |
| 223 | let ids = prompt_ids(&tok, n); |
| 224 | for rep in 0..reps { |
| 225 | engine.set_whole_prompt(false).expect("off"); |
| 226 | let off = run(&mut engine, &ids, new_tokens); |
| 227 | engine |
| 228 | .set_whole_prompt(true) |
| 229 | .expect("whole-prompt prefill refused"); |
| 230 | let on = run(&mut engine, &ids, new_tokens); |
| 231 | let st = engine.whole_prompt_stats().cloned().unwrap_or_default(); |
| 232 | let diff = off |
| 233 | .tokens |
| 234 | .iter() |
| 235 | .zip(&on.tokens) |
| 236 | .position(|(a, b)| a != b) |
| 237 | .or((off.tokens.len() != on.tokens.len()) |
| 238 | .then_some(off.tokens.len().min(on.tokens.len()))); |
| 239 | let cos = cosine(&off.logits0, &on.logits0); |
| 240 | let maxd = off |
| 241 | .logits0 |
| 242 | .iter() |
| 243 | .zip(&on.logits0) |
| 244 | .map(|(a, b)| (a - b).abs()) |
| 245 | .fold(0.0f32, f32::max); |
| 246 | let (ta, ma) = top2(&off.logits0); |
| 247 | let (tb, mb) = top2(&on.logits0); |
| 248 | all_identical &= diff.is_none(); |
| 249 | if rep + 1 == reps { |
| 250 | offs.push((n, off.tokens.clone(), off.logits0.clone(), off.ttft_ms)); |
| 251 | } |
| 252 | say(format!( |
| 253 | "len {n} rep {rep}: TTFT off {:.1} ms on {:.1} ms (x{:.2}); prefill_ms off {} on {}; decode {} tokens off {} ms on {} ms; tokens {} ; logits0 cosine {cos:.7} maxdiff {maxd:.3e} top1 {ta}/{tb} margin {ma:.3}/{mb:.3}", |
| 254 | off.ttft_ms, |
| 255 | on.ttft_ms, |
| 256 | off.ttft_ms / on.ttft_ms, |
| 257 | off.prefill_ms, |
| 258 | on.prefill_ms, |
| 259 | new_tokens, |
| 260 | off.decode_ms, |
| 261 | on.decode_ms, |
| 262 | match diff { |
| 263 | None => "IDENTICAL".to_string(), |
| 264 | Some(p) => format!("DIFFER at {p}"), |
| 265 | } |
| 266 | )); |
| 267 | say(format!( |
| 268 | " on: launches {} chip(GEMM+quant) {:.1} ms attention chip {:.1} ms host {:.1} ms attn_dev {} | off: chip {:.1} ms launches {}", |
| 269 | st.launches(), |
| 270 | st.chip_ns as f64 / 1e6, |
| 271 | st.attn_ns as f64 / 1e6, |
| 272 | st.host_ns as f64 / 1e6, |
| 273 | st.attn_dev, |
| 274 | off.pt.chip_ns as f64 / 1e6, |
| 275 | off.pt.launches |
| 276 | )); |
| 277 | let p = &st.prof; |
| 278 | let ms = |v: u64| v as f64 / 1e6; |
| 279 | say(format!( |
| 280 | " on profile ms: wall {:.1} dev(calls) {:.1} | host: embed {:.1} norm_in {:.1} resid {:.1} norm_post {:.1} router {:.1} plan {:.1} tiles {:.1} combine {:.1} final {:.1}", |
| 281 | ms(p.wall_ns), ms(p.dev_ns), ms(p.embed_ns), ms(p.norm_in_ns), ms(p.resid_ns), ms(p.norm_post_ns), ms(p.router_ns), ms(p.plan_ns), ms(p.tiles_ns), ms(p.combine_ns), ms(p.final_ns) |
| 282 | )); |
| 283 | rows.push(serde_json::json!({ |
| 284 | "len": n, "rep": rep, |
| 285 | "ttft_ms_off": off.ttft_ms, "ttft_ms_on": on.ttft_ms, |
| 286 | "prefill_ms_off": off.prefill_ms, "prefill_ms_on": on.prefill_ms, |
| 287 | "decode_ms_off": off.decode_ms, "decode_ms_on": on.decode_ms, |
| 288 | "tokens_off": off.tokens, "tokens_on": on.tokens, "first_diff": diff, |
| 289 | "logits0_cosine": cos, "logits0_maxdiff": maxd, "top1_off": ta, "top1_on": tb, "margin_off": ma, "margin_on": mb, |
| 290 | "on_stats": {"launches": st.launches(), "chip_ns": st.chip_ns, "attn_ns": st.attn_ns, "host_ns": st.host_ns, "attn_dev": st.attn_dev}, |
| 291 | "off_timing": {"chip_ns": off.pt.chip_ns, "launches": off.pt.launches, "total_ns": off.pt.total_ns}, |
| 292 | })); |
| 293 | if devhost { |
| 294 | // Phase D (W-L1G-E): the same ON run with the router logits + MoE combine on the chip. |
| 295 | engine |
| 296 | .set_whole_prompt_device_host(true) |
| 297 | .expect("device router / combine refused"); |
| 298 | let dv = run(&mut engine, &ids, new_tokens); |
| 299 | let sd = engine.whole_prompt_stats().cloned().unwrap_or_default(); |
| 300 | engine |
| 301 | .set_whole_prompt_device_host(false) |
| 302 | .expect("device host work off"); |
| 303 | let same_tokens = dv.tokens == on.tokens; |
| 304 | let bits_differ = dv |
| 305 | .logits0 |
| 306 | .iter() |
| 307 | .zip(&on.logits0) |
| 308 | .filter(|(a, b)| a.to_bits() != b.to_bits()) |
| 309 | .count() |
| 310 | + dv.logits0.len().abs_diff(on.logits0.len()); |
| 311 | let diff_off = off.tokens.iter().zip(&dv.tokens).position(|(a, b)| a != b); |
| 312 | all_identical &= diff_off.is_none(); |
| 313 | devhost_exact &= same_tokens && bits_differ == 0; |
| 314 | say(format!( |
| 315 | "len {n} rep {rep} DEVHOST: TTFT off {:.1} ms on(host) {:.1} ms on(dev) {:.1} ms (x{:.2} vs off, x{:.2} vs on-host); prefill_ms on(dev) {}; vs on-host tokens {} logits0 bits differ {bits_differ}; vs off tokens {}", |
| 316 | off.ttft_ms, |
| 317 | on.ttft_ms, |
| 318 | dv.ttft_ms, |
| 319 | off.ttft_ms / dv.ttft_ms, |
| 320 | on.ttft_ms / dv.ttft_ms, |
| 321 | dv.prefill_ms, |
| 322 | if same_tokens { "IDENTICAL" } else { "DIFFER" }, |
| 323 | diff_off.map_or("IDENTICAL".to_string(), |p| format!("DIFFER at {p}")) |
| 324 | )); |
| 325 | let p = &sd.prof; |
| 326 | say(format!( |
| 327 | " on(dev): launches {} + router {} + combine {} chip {:.1} ms attention {:.1} ms host {:.1} ms | profile ms: wall {:.1} dev(calls) {:.1} | host: embed {:.1} norm_in {:.1} resid {:.1} norm_post {:.1} router {:.1} plan {:.1} tiles {:.1} combine {:.1} final {:.1}", |
| 328 | sd.launches(), sd.router, sd.combine, |
| 329 | sd.chip_ns as f64 / 1e6, sd.attn_ns as f64 / 1e6, sd.host_ns as f64 / 1e6, |
| 330 | ms(p.wall_ns), ms(p.dev_ns), ms(p.embed_ns), ms(p.norm_in_ns), ms(p.resid_ns), ms(p.norm_post_ns), ms(p.router_ns), ms(p.plan_ns), ms(p.tiles_ns), ms(p.combine_ns), ms(p.final_ns) |
| 331 | )); |
| 332 | rows.push(serde_json::json!({"len": n, "rep": rep, "devhost": true, "ttft_ms_off": off.ttft_ms, "ttft_ms_on_host": on.ttft_ms, "ttft_ms_on_dev": dv.ttft_ms, |
| 333 | "prefill_ms_on_dev": dv.prefill_ms, "tokens_on_dev": dv.tokens, "tokens_equal_on_host": same_tokens, "logits0_bits_differ_vs_on_host": bits_differ, "first_diff_vs_off": diff_off, |
| 334 | "on_dev_stats": {"launches": sd.launches(), "router": sd.router, "combine": sd.combine, "chip_ns": sd.chip_ns, "attn_ns": sd.attn_ns, "host_ns": sd.host_ns, "wall_ns": p.wall_ns, |
| 335 | "router_ns": p.router_ns, "plan_ns": p.plan_ns, "combine_ns": p.combine_ns}})); |
| 336 | } |
| 337 | } |
| 338 | if devhost { |
| 339 | // Expert agreement (W-L1G-E), untimed: one traced ON run per routing path, every layer's expert ids compared. |
| 340 | engine.set_whole_prompt(true).expect("on"); |
| 341 | engine.set_whole_prompt_device_host(false).expect("host"); |
| 342 | engine |
| 343 | .whole_prompt_set_trace(Some(WpTrace::new(None))) |
| 344 | .expect("trace"); |
| 345 | let _ = run(&mut engine, &ids, 1); |
| 346 | let th = engine.whole_prompt_take_trace().expect("host trace"); |
| 347 | engine.set_whole_prompt_device_host(true).expect("dev"); |
| 348 | engine |
| 349 | .whole_prompt_set_trace(Some(WpTrace::new(None))) |
| 350 | .expect("trace"); |
| 351 | let _ = run(&mut engine, &ids, 1); |
| 352 | let td = engine.whole_prompt_take_trace().expect("device trace"); |
| 353 | engine.set_whole_prompt_device_host(false).expect("host"); |
| 354 | let slots: usize = th.experts.iter().map(Vec::len).sum(); |
| 355 | let flips: usize = th |
| 356 | .experts |
| 357 | .iter() |
| 358 | .zip(&td.experts) |
| 359 | .map(|(a, b)| { |
| 360 | a.iter().zip(b).filter(|(x, y)| x != y).count() + a.len().abs_diff(b.len()) |
| 361 | }) |
| 362 | .sum(); |
| 363 | let first_layer = th.experts.iter().zip(&td.experts).position(|(a, b)| a != b); |
| 364 | let resid_bits: usize = th |
| 365 | .x_out |
| 366 | .iter() |
| 367 | .zip(&td.x_out) |
| 368 | .map(|(a, b)| { |
| 369 | a.iter() |
| 370 | .zip(b) |
| 371 | .filter(|(x, y)| x.to_bits() != y.to_bits()) |
| 372 | .count() |
| 373 | }) |
| 374 | .sum(); |
| 375 | devhost_exact &= flips == 0 |
| 376 | && resid_bits == 0 |
| 377 | && th.experts.len() == td.experts.len() |
| 378 | && !th.experts.is_empty(); |
| 379 | say(format!( |
| 380 | "len {n} EXPERTS host vs device routing: {} layers, {slots} expert slots, flips {flips}, first differing layer {:?}; residual stream bits differ {resid_bits}", |
| 381 | th.experts.len(), |
| 382 | first_layer |
| 383 | )); |
| 384 | rows.push(serde_json::json!({"len": n, "experts_layers": th.experts.len(), "expert_slots": slots, "expert_flips": flips, "first_flip_layer": first_layer, "residual_bits_differ": resid_bits})); |
| 385 | } |
| 386 | } |
| 387 | if cached { |
| 388 | // Phase B: the whole-prompt GEMMs read a GPU-cached copy of the weights (the OFF runs above are the baseline). |
| 389 | engine.set_whole_prompt(true).expect("on"); |
| 390 | let t = Instant::now(); |
| 391 | let bytes = engine |
| 392 | .whole_prompt_cache_weights() |
| 393 | .expect("cached weight copy"); |
| 394 | say(format!( |
| 395 | "CACHED copy {:.2} GB in {:.2} s, mem_available {:.1} GB", |
| 396 | bytes as f64 / 1e9, |
| 397 | t.elapsed().as_secs_f64(), |
| 398 | mem_available_gb() |
| 399 | )); |
| 400 | for (n, off_tokens, off_logits, off_ttft) in &offs { |
| 401 | let ids = prompt_ids(&tok, *n); |
| 402 | for rep in 0..reps { |
| 403 | let on = run(&mut engine, &ids, new_tokens); |
| 404 | let st = engine.whole_prompt_stats().cloned().unwrap_or_default(); |
| 405 | let diff = off_tokens.iter().zip(&on.tokens).position(|(a, b)| a != b); |
| 406 | let cos = cosine(off_logits, &on.logits0); |
| 407 | let maxd = off_logits |
| 408 | .iter() |
| 409 | .zip(&on.logits0) |
| 410 | .map(|(a, b)| (a - b).abs()) |
| 411 | .fold(0.0f32, f32::max); |
| 412 | all_identical &= diff.is_none(); |
| 413 | say(format!( |
| 414 | "len {n} rep {rep} CACHED: TTFT off {off_ttft:.1} ms on {:.1} ms (x{:.2}); prefill_ms on {}; tokens {} ; logits0 cosine {cos:.7} maxdiff {maxd:.3e}; launches {} chip(GEMM+quant) {:.1} ms attention {:.1} ms host {:.1} ms", |
| 415 | on.ttft_ms, |
| 416 | off_ttft / on.ttft_ms, |
| 417 | on.prefill_ms, |
| 418 | diff.map_or("IDENTICAL".to_string(), |p| format!("DIFFER at {p}")), |
| 419 | st.launches(), |
| 420 | st.chip_ns as f64 / 1e6, |
| 421 | st.attn_ns as f64 / 1e6, |
| 422 | st.host_ns as f64 / 1e6 |
| 423 | )); |
| 424 | rows.push(serde_json::json!({"len": n, "rep": rep, "cached": true, "ttft_ms_on": on.ttft_ms, "prefill_ms_on": on.prefill_ms, "tokens_on": on.tokens, "first_diff": diff, "logits0_cosine": cos, "logits0_maxdiff": maxd, |
| 425 | "on_stats": {"launches": st.launches(), "chip_ns": st.chip_ns, "attn_ns": st.attn_ns, "host_ns": st.host_ns}})); |
| 426 | } |
| 427 | } |
| 428 | } |
| 429 | say(format!( |
| 430 | "SUMMARY tokens {}", |
| 431 | if all_identical { "IDENTICAL" } else { "DIFFER" } |
| 432 | )); |
| 433 | if devhost { |
| 434 | say(format!( |
| 435 | "SUMMARY devhost vs host routing+combine {}", |
| 436 | if devhost_exact { |
| 437 | "BIT-IDENTICAL" |
| 438 | } else { |
| 439 | "DIFFER" |
| 440 | } |
| 441 | )); |
| 442 | } |
| 443 | let doc = serde_json::json!({"checkpoint": checkpoint, "lens": lens, "new_tokens": new_tokens, "reps": reps, "rows": rows}); |
| 444 | std::fs::write( |
| 445 | receipt.join("run.json"), |
| 446 | serde_json::to_string_pretty(&doc).unwrap(), |
| 447 | ) |
| 448 | .expect("run.json"); |
| 449 | std::fs::write(receipt.join("run.txt"), log.join("\n") + "\n").expect("run.txt"); |
| 450 | } |