From b9cac6d0dff2a72627e1867db3267d4160b29cea Mon Sep 17 00:00:00 2001 From: iz0eyj Date: Tue, 1 Dec 2026 18:04:01 +0100 Subject: [PATCH] [LD][GW] Fix systematic token-2+ parity gap: per-pass KV cache + unpadded logit vocab Two root causes found by bisecting the pos-27/28 forward against strata-inkling (per-visit cur-norm dumps, STRATA_INK_NORM/POS, reverted after): 1. KV cache was shared across the 3 core loop passes. The reference assigns a DISTINCT KV pool per visit (visit_kv_slot: 40 SWA + 8 global), so each core pass reads its own KV history. Sharing one cache corrupted multi-token attention from the first core visit (visit6) onward. LayerState.k_cache/ v_cache are now [DeviceBuf; 3] indexed by pass (prelude/coda use pass 0). 2. Served/logit vocab used the PADDED size (201024). Padding tokens >= the unpadded vocab (200058) carry arbitrary logits and outranked real tokens (e.g. 200193 beat end_message 200010). Engine now uses unpadded_vocab, matching strata k_unembed_softcap V = vocab_unpadded. Verified: chat parity EXACT vs reference for 16 and 40 greedy tokens, both F32 (default) and FP16 KV. pos-28 per-visit norms now agree to ~3e-3 (was diverging by 50-80). Added serve_gen_multi_token_parity GPU regression. Suite: workspace -D warnings green; 18 ld + 18 hip + 4 gw GPU; 5 Lean 0 sorryAx. --- crates/vetta-infer-gw/src/serve.rs | 31 ++++++++++++++++++++++++- crates/vetta-infer-ld/src/layer.rs | 37 +++++++++++++++++------------- 2 files changed, 51 insertions(+), 17 deletions(-) diff --git a/crates/vetta-infer-gw/src/serve.rs b/crates/vetta-infer-gw/src/serve.rs index 005e805..7e3622d 100644 --- a/crates/vetta-infer-gw/src/serve.rs +++ b/crates/vetta-infer-gw/src/serve.rs @@ -87,7 +87,11 @@ impl InklingEngine { Ok(Self { ar, d, - vocab: t.vocab, + // The served/logit vocab is the UNPADDED size (exact strata + // k_unembed_softcap V = vocab_unpadded): rows >= unpadded are padding + // tokens whose arbitrary logits must never win the argmax. Using the + // padded vocab let padding tokens (e.g. 200193) outrank real ones. + vocab: t.unpadded_vocab, n_layers: t.n_layers, eos: cfg.eos_token_id as i32, cap: t.final_logit_softcapping as f32, @@ -394,4 +398,29 @@ mod tests { assert!(!lines.iter().any(|l| l.starts_with("T ")), "pre-warm emits no tokens"); assert!(lines.iter().any(|l| l.starts_with("DONE 0 2")), "DONE 0 2 ..."); } + + /// GPU regression: multi-token decode must match the reference EXACTLY. This + /// locks in the two correctness fixes that eliminated the systematic token-2+ + /// divergence: (1) per-pass KV cache (each core pass owns a distinct KV pool, + /// exact strata visit_kv_slot) and (2) unpadded logit vocab (padding tokens + /// >= unpadded_vocab_size must never win the argmax). Sequence measured against + /// strata-inkling --native model.gguf for "The capital of France is" (greedy). + #[test] + #[ignore] + fn serve_gen_multi_token_parity() { + let eng = InklingEngine::new("/mnt/models/Inkling/safetensors").unwrap(); + let g = match parse_serve_line("GEN 16 temperature=0 371,151451,149879,151194,149884", eng.vocab()).unwrap() { + ServeCommand::Gen(g) => g, + _ => panic!("expected Gen"), + }; + let stop = AtomicBool::new(false); + let mut lines: Vec = Vec::new(); + serve_gen(&eng, &g, &stop, &mut |l| lines.push(l)); + let tokens: Vec = lines.iter() + .filter(|l| l.starts_with("T ")) + .map(|l| l[2..].trim().parse().unwrap()) + .collect(); + let want = [152717, 13, 68014, 32, 13, 220, 44781, 164452, 68014, 33, 13, 220, 57345, 745, 164452, 68014]; + assert_eq!(tokens, want, "multi-token decode must match reference exactly"); + } } \ No newline at end of file diff --git a/crates/vetta-infer-ld/src/layer.rs b/crates/vetta-infer-ld/src/layer.rs index 1e8e25d..41ca0df 100644 --- a/crates/vetta-infer-ld/src/layer.rs +++ b/crates/vetta-infer-ld/src/layer.rs @@ -37,7 +37,11 @@ pub struct LayerWeights { /// Persistent per-layer decode state. pub struct LayerState { - pub k_cache: DeviceBuf, pub v_cache: DeviceBuf, // [n_kv, max_seq, hd] + // KV cache is PER-VISIT (exact strata visit_kv_slot: every core pass owns a + // distinct SWA/global KV pool, 40 SWA + 8 global). Index by pass: prelude/coda + // use pass 0; the core's 3 passes use pass 0/1/2. Sharing one cache across + // passes corrupts multi-token attention (the systematic token-2+ parity gap). + pub k_cache: [DeviceBuf; 3], pub v_cache: [DeviceBuf; 3], // [n_kv, max_seq, hd] each // ShortConv history is PER-VISIT (exact strata hist_pool_all + visit*stride): // each core pass has its own history ring, NOT shared across passes. Index // by pass (prelude/coda use pass 0; core uses pass 0/1/2). @@ -181,15 +185,16 @@ pub fn new_state(d: &Dims, max_seq: usize) -> Result { // VETTA_F16_KV=1 matches the strata default (KvDtype::FP16) and its // length-dependent rounding (the F32 cache diverges on near-ties as it grows). let kv_n = d.n_kv * max_seq * d.hd; - let (k_cache, v_cache) = if f16_kv() { - let z16 = |n: usize| -> Result { let b = DeviceBuf::alloc(n * 2)?; b.h2d_bytes(&vec![0u8; n * 2])?; Ok(b) }; - (z16(kv_n)?, z16(kv_n)?) - } else { - (zero(kv_n)?, zero(kv_n)?) + let kv_buf = || -> Result { + if f16_kv() { + let b = DeviceBuf::alloc(kv_n * 2)?; b.h2d_bytes(&vec![0u8; kv_n * 2])?; Ok(b) + } else { + let b = DeviceBuf::alloc(kv_n * 4)?; b.h2d(&vec![0f32; kv_n])?; Ok(b) + } }; Ok(LayerState { - k_cache, - v_cache, + k_cache: [kv_buf()?, kv_buf()?, kv_buf()?], + v_cache: [kv_buf()?, kv_buf()?, kv_buf()?], hist_k: [zero(3 * d.kv_dim())?, zero(3 * d.kv_dim())?, zero(3 * d.kv_dim())?], hist_v: [zero(3 * d.kv_dim())?, zero(3 * d.kv_dim())?, zero(3 * d.kv_dim())?], hist_a: [zero(3 * d.hidden)?, zero(3 * d.hidden)?, zero(3 * d.hidden)?], @@ -279,17 +284,17 @@ pub fn layer_forward_swa( let lim = (pos + 1).min(d.rel_extent as i32); vetta_infer_hip::relpos_bias(&r_raw, &w.rel_proj, &rel_bias, d.n_head as i32, d.d_rel as i32, d.rel_extent as i32, lim)?; if f16_kv() { - vetta_infer_hip::kv_store_f16(&k_cv, &v_cv, &st.k_cache, &st.v_cache, pos, d.n_kv as i32, d.hd as i32, st.max_seq as i32, d.eps)?; + vetta_infer_hip::kv_store_f16(&k_cv, &v_cv, &st.k_cache[pass], &st.v_cache[pass], pos, d.n_kv as i32, d.hd as i32, st.max_seq as i32, d.eps)?; } else { - vetta_infer_hip::kv_store(&k_cv, &v_cv, &st.k_cache, &st.v_cache, pos, d.n_kv as i32, d.hd as i32, st.max_seq as i32, d.eps)?; + vetta_infer_hip::kv_store(&k_cv, &v_cv, &st.k_cache[pass], &st.v_cache[pass], pos, d.n_kv as i32, d.hd as i32, st.max_seq as i32, d.eps)?; } if w.ip == 0 || (w.ip == 6 && pass == 1) { dbg_norm("q", &q, q_dim); dbg_norm("k_cv", &k_cv, kv_dim); dbg_norm("v_cv", &v_cv, kv_dim); dbg_norm("r_raw", &r_raw, d.n_head * d.d_rel); } // k3: flash SWA GQA. if f16_kv() { - vetta_infer_hip::flash_swa_gqa_decode_f16(&q, &st.k_cache, &st.v_cache, &aout, d.n_head as i32, d.n_kv as i32, st.max_seq as i32, d.hd as i32, d.scale(), d.swa_win, pos, Some(&rel_bias), d.rel_extent as i32, Some(&sinks), Some(&gatea))?; + vetta_infer_hip::flash_swa_gqa_decode_f16(&q, &st.k_cache[pass], &st.v_cache[pass], &aout, d.n_head as i32, d.n_kv as i32, st.max_seq as i32, d.hd as i32, d.scale(), d.swa_win, pos, Some(&rel_bias), d.rel_extent as i32, Some(&sinks), Some(&gatea))?; } else { - vetta_infer_hip::flash_swa_gqa_decode(&q, &st.k_cache, &st.v_cache, &aout, d.n_head as i32, d.n_kv as i32, st.max_seq as i32, d.hd as i32, d.scale(), d.swa_win, pos, Some(&rel_bias), d.rel_extent as i32, Some(&sinks), Some(&gatea))?; + vetta_infer_hip::flash_swa_gqa_decode(&q, &st.k_cache[pass], &st.v_cache[pass], &aout, d.n_head as i32, d.n_kv as i32, st.max_seq as i32, d.hd as i32, d.scale(), d.swa_win, pos, Some(&rel_bias), d.rel_extent as i32, Some(&sinks), Some(&gatea))?; } if w.ip == 0 || (w.ip == 6 && pass == 1) { dbg_norm("aout", &aout, q_dim); dbg_norm("sinks", &sinks, d.n_head); dbg_norm("gatea", &gatea, q_dim); } @@ -727,16 +732,16 @@ pub fn layer_forward_full( let lim = (pos + 1).min(d.rel_extent as i32); vetta_infer_hip::relpos_bias(&r_raw, &w.rel_proj, &rel_bias, d.n_head as i32, d.d_rel as i32, d.rel_extent as i32, lim)?; if f16_kv() { - vetta_infer_hip::kv_store_f16(&k_cv, &v_cv, &st.k_cache, &st.v_cache, pos, d.n_kv as i32, d.hd as i32, st.max_seq as i32, d.eps)?; + vetta_infer_hip::kv_store_f16(&k_cv, &v_cv, &st.k_cache[pass], &st.v_cache[pass], pos, d.n_kv as i32, d.hd as i32, st.max_seq as i32, d.eps)?; } else { - vetta_infer_hip::kv_store(&k_cv, &v_cv, &st.k_cache, &st.v_cache, pos, d.n_kv as i32, d.hd as i32, st.max_seq as i32, d.eps)?; + vetta_infer_hip::kv_store(&k_cv, &v_cv, &st.k_cache[pass], &st.v_cache[pass], pos, d.n_kv as i32, d.hd as i32, st.max_seq as i32, d.eps)?; } let aout = DeviceBuf::alloc(q_dim * 4)?; // global attention: window = 0. if f16_kv() { - vetta_infer_hip::flash_swa_gqa_decode_f16(&q, &st.k_cache, &st.v_cache, &aout, d.n_head as i32, d.n_kv as i32, st.max_seq as i32, d.hd as i32, d.scale(), 0, pos, Some(&rel_bias), d.rel_extent as i32, Some(&sinks), Some(&gatea))?; + vetta_infer_hip::flash_swa_gqa_decode_f16(&q, &st.k_cache[pass], &st.v_cache[pass], &aout, d.n_head as i32, d.n_kv as i32, st.max_seq as i32, d.hd as i32, d.scale(), 0, pos, Some(&rel_bias), d.rel_extent as i32, Some(&sinks), Some(&gatea))?; } else { - vetta_infer_hip::flash_swa_gqa_decode(&q, &st.k_cache, &st.v_cache, &aout, d.n_head as i32, d.n_kv as i32, st.max_seq as i32, d.hd as i32, d.scale(), 0, pos, Some(&rel_bias), d.rel_extent as i32, Some(&sinks), Some(&gatea))?; + vetta_infer_hip::flash_swa_gqa_decode(&q, &st.k_cache[pass], &st.v_cache[pass], &aout, d.n_head as i32, d.n_kv as i32, st.max_seq as i32, d.hd as i32, d.scale(), 0, pos, Some(&rel_bias), d.rel_extent as i32, Some(&sinks), Some(&gatea))?; } let aout_bf = DeviceBuf::alloc(q_dim * 2)?; vetta_infer_hip::cast_f32_bf16(&aout, &aout_bf, q_dim)?; let oproj = DeviceBuf::alloc(h * 4)?;