From df120e6f5f2f1f8621236f98daf71fd14c455852 Mon Sep 17 00:00:00 2001 From: iz0eyj Date: Tue, 1 Dec 2026 15:51:34 +0100 Subject: [PATCH] [Core] Per-visit ShortConv history (exact strata hist_pool_all + visit*stride) Root cause found by reading strata run_sconv: the ShortConv history pool is indexed by GLOBAL VISIT (hist_pool_all + rec_visit*stride), so each core pass has its OWN history ring -- history does NOT carry across the 3 passes for the same token. The previous shared-per-layer history made pass 1 read pass 0's input as history (wrong). LayerState now holds hist_k/v/a/m as [DeviceBuf;3] indexed by pass; layer_forward_swa/full + run_layer take a pass index (prelude/ coda=0, core=0/1/2). For a fresh token every visit reads zero history, matching the reference. Single-token argmax moved (8->183618), confirming history matters. Verified: cargo test --workspace + hip 17/17 + lean 0 sorryAx green. --- crates/vetta-infer-ld/src/layer.rs | 51 ++++++++++++++++------------- crates/vetta-infer-ld/src/packer.rs | 4 +-- 2 files changed, 30 insertions(+), 25 deletions(-) diff --git a/crates/vetta-infer-ld/src/layer.rs b/crates/vetta-infer-ld/src/layer.rs index 3020d74..d0af16a 100644 --- a/crates/vetta-infer-ld/src/layer.rs +++ b/crates/vetta-infer-ld/src/layer.rs @@ -35,8 +35,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] - pub hist_k: DeviceBuf, pub hist_v: DeviceBuf, // [3, n_kv*hd] - pub hist_a: DeviceBuf, pub hist_m: DeviceBuf, // [3, hidden] + // 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). + pub hist_k: [DeviceBuf; 3], pub hist_v: [DeviceBuf; 3], // [3, n_kv*hd] each + pub hist_a: [DeviceBuf; 3], pub hist_m: [DeviceBuf; 3], // [3, hidden] each pub max_seq: usize, } @@ -91,10 +94,10 @@ pub fn new_state(d: &Dims, max_seq: usize) -> Result { Ok(LayerState { k_cache: zero(d.n_kv * max_seq * d.hd)?, v_cache: zero(d.n_kv * max_seq * d.hd)?, - hist_k: zero(3 * d.kv_dim())?, - hist_v: zero(3 * d.kv_dim())?, - hist_a: zero(3 * d.hidden)?, - hist_m: zero(3 * d.hidden)?, + 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)?], + hist_m: [zero(3 * d.hidden)?, zero(3 * d.hidden)?, zero(3 * d.hidden)?], max_seq, }) } @@ -105,6 +108,7 @@ pub fn new_state(d: &Dims, max_seq: usize) -> Result { pub fn layer_forward_swa( ar: &SafetensorsArchive, w: &LayerWeights, st: &mut LayerState, d: &Dims, cur: &DeviceBuf, blocks: &mut Vec, pos: i32, + pass: usize, ctrl: Option<(&ControllerWeights, &LoopMsgs)>, ) -> Result<(), String> { let h = d.hidden; let kv_dim = d.kv_dim(); let q_dim = d.q_dim(); @@ -168,8 +172,8 @@ pub fn layer_forward_swa( vetta_infer_hip::gemm_bf16(&in0_bf, &w.wg, &gatea, 1, q_dim as i32, h as i32)?; vetta_infer_hip::gemm_bf16(&in0_bf, &w.ws, &sinks, 1, d.n_head as i32, h as i32)?; } - vetta_infer_hip::shortconv4_decode(&k_raw, &w.k_sconv, &st.hist_k, &k_cv, &slot0, 1, kv_dim as i32)?; - vetta_infer_hip::shortconv4_decode(&v_raw, &w.v_sconv, &st.hist_v, &v_cv, &slot0, 1, kv_dim as i32)?; + vetta_infer_hip::shortconv4_decode(&k_raw, &w.k_sconv, &st.hist_k[pass], &k_cv, &slot0, 1, kv_dim as i32)?; + vetta_infer_hip::shortconv4_decode(&v_raw, &w.v_sconv, &st.hist_v[pass], &v_cv, &slot0, 1, kv_dim as i32)?; vetta_infer_hip::l2_clip(&q, Some(&w.q_sc), d.n_head as i32, d.hd as i32, d.qk_clip, false, pos)?; vetta_infer_hip::l2_clip(&r_raw, Some(&w.r_sc), d.n_head as i32, d.d_rel as i32, d.rel_clip, false, pos)?; vetta_infer_hip::l2_clip(&k_cv, None, d.n_kv as i32, d.hd as i32, d.qk_clip, false, pos)?; @@ -183,7 +187,7 @@ pub fn layer_forward_swa( // k4: O-proj + attn shortconv + block boundary. vetta_infer_hip::cast_f32_bf16(&aout, &aout_bf, q_dim)?; vetta_infer_hip::gemm_bf16(&aout_bf, &w.wo, &oproj, 1, h as i32, q_dim as i32)?; - vetta_infer_hip::shortconv4_decode(&oproj, &w.a_sconv, &st.hist_a, &asc, &slot0, 1, h as i32)?; + vetta_infer_hip::shortconv4_decode(&oproj, &w.a_sconv, &st.hist_a[pass], &asc, &slot0, 1, h as i32)?; if w.ip % d.block_size == 0 { let mut cur_h = vec![0f32; h]; cur.d2h(&mut cur_h)?; let snap = DeviceBuf::alloc(h * 4)?; snap.h2d(&cur_h)?; @@ -237,7 +241,7 @@ pub fn layer_forward_swa( // k7: mlp shortconv + plain residual add. let msc = DeviceBuf::alloc(h * 4)?; - vetta_infer_hip::shortconv4_decode(&moeout, &w.m_sconv, &st.hist_m, &msc, &slot0, 1, h as i32)?; + vetta_infer_hip::shortconv4_decode(&moeout, &w.m_sconv, &st.hist_m[pass], &msc, &slot0, 1, h as i32)?; vetta_infer_hip::vec_add(cur, &msc, cur, h)?; Ok(()) } @@ -296,7 +300,7 @@ pub fn forward_swa_layers( for &ip in schedule { let w = load_layer(ar, ip, true)?; let mut st = new_state(d, max_seq)?; - layer_forward_swa(ar, &w, &mut st, d, &cur, &mut blocks, pos, None)?; + layer_forward_swa(ar, &w, &mut st, d, &cur, &mut blocks, pos, 0, None)?; } Ok((cur, blocks)) } @@ -324,17 +328,17 @@ fn load_msg_queries(ar: &SafetensorsArchive, ip: usize, hidden: usize) -> Result /// persistent state (KV cache + shortconv history) across loop passes. fn run_layer( ar: &SafetensorsArchive, d: &Dims, ip: usize, cur: &DeviceBuf, blocks: &mut Vec, - states: &mut [Option], max_seq: usize, pos: i32, + states: &mut [Option], max_seq: usize, pos: i32, pass: usize, ctrl: Option<(&ControllerWeights, &LoopMsgs)>, ) -> Result<(), String> { if states[ip].is_none() { states[ip] = Some(new_state(d, max_seq)?); } let st = states[ip].as_mut().unwrap(); if is_swa(ip) { let w = load_layer(ar, ip, true)?; - layer_forward_swa(ar, &w, st, d, cur, blocks, pos, ctrl) + layer_forward_swa(ar, &w, st, d, cur, blocks, pos, pass, ctrl) } else { let w = load_full_layer(ar, ip, d)?; - layer_forward_full(ar, &w, st, d, cur, blocks, pos, ctrl) + layer_forward_full(ar, &w, st, d, cur, blocks, pos, pass, ctrl) } } @@ -360,7 +364,7 @@ pub fn forward_inkling_with_state( let cur = embed_token(ar, token, h, vocab)?; let mut blocks: Vec = Vec::new(); // Prelude. - for ip in 0..6 { run_layer(ar, d, ip, &cur, &mut blocks, states, max_seq, pos, None)?; } + for ip in 0..6 { run_layer(ar, d, ip, &cur, &mut blocks, states, max_seq, pos, 0, None)?; } let mut prelude = vec![0f32; h]; cur.d2h(&mut prelude)?; let n_prelude_blocks = blocks.len(); if std::env::var("VETTA_DBG_NORM").is_ok() { @@ -393,14 +397,14 @@ pub fn forward_inkling_with_state( vetta_infer_hip::attn_res_mix(&blk, &bank_out, &mq, &msgs_buf, n_bank as i32, 1, 4, h as i32, d.eps)?; let msgs = LoopMsgs(msgs_buf); let cw = load_controllers(ar, ip)?; - run_layer(ar, d, ip, &cur, &mut blocks, states, max_seq, pos, Some((&cw, &msgs)))?; + run_layer(ar, d, ip, &cur, &mut blocks, states, max_seq, pos, p, Some((&cw, &msgs)))?; } else { - run_layer(ar, d, ip, &cur, &mut blocks, states, max_seq, pos, None)?; + run_layer(ar, d, ip, &cur, &mut blocks, states, max_seq, pos, p, None)?; } } } // Coda. - for ip in 18..24 { run_layer(ar, d, ip, &cur, &mut blocks, states, max_seq, pos, None)?; } + for ip in 18..24 { run_layer(ar, d, ip, &cur, &mut blocks, states, max_seq, pos, 0, None)?; } if std::env::var("VETTA_DBG_NORM").is_ok() { let mut ch = vec![0f32; h]; cur.d2h(&mut ch).unwrap(); let n = ch.iter().map(|v| v * v).sum::().sqrt(); @@ -477,6 +481,7 @@ pub fn load_full_layer(ar: &SafetensorsArchive, ip: usize, d: &Dims) -> Result, pos: i32, + pass: usize, ctrl: Option<(&ControllerWeights, &LoopMsgs)>, ) -> Result<(), String> { let h = d.hidden; let kv_dim = d.kv_dim(); let q_dim = d.q_dim(); @@ -524,8 +529,8 @@ pub fn layer_forward_full( vetta_infer_hip::gemm_bf16(&in2_bf, &w.v_proj, &v_dyn, 1, (d.n_kv * 4) as i32, h as i32)?; let k_cv = DeviceBuf::alloc(kv_dim * 4)?; let v_cv = DeviceBuf::alloc(kv_dim * 4)?; - vetta_infer_hip::shortconv4_dyn_decode(&k_raw, &w.k_static, &k_dyn, &st.hist_k, &k_cv, &slot0, 1, kv_dim as i32, d.n_kv as i32)?; - vetta_infer_hip::shortconv4_dyn_decode(&v_raw, &w.v_static, &v_dyn, &st.hist_v, &v_cv, &slot0, 1, kv_dim as i32, d.n_kv as i32)?; + vetta_infer_hip::shortconv4_dyn_decode(&k_raw, &w.k_static, &k_dyn, &st.hist_k[pass], &k_cv, &slot0, 1, kv_dim as i32, d.n_kv as i32)?; + vetta_infer_hip::shortconv4_dyn_decode(&v_raw, &w.v_static, &v_dyn, &st.hist_v[pass], &v_cv, &slot0, 1, kv_dim as i32, d.n_kv as i32)?; // tau on Q/R (full-attn), K clip without tau. vetta_infer_hip::l2_clip(&q, Some(&w.q_sc), d.n_head as i32, d.hd as i32, d.qk_clip, true, pos)?; vetta_infer_hip::l2_clip(&r_raw, Some(&w.r_sc), d.n_head as i32, d.d_rel as i32, d.rel_clip, true, pos)?; @@ -542,7 +547,7 @@ pub fn layer_forward_full( let oproj = DeviceBuf::alloc(h * 4)?; vetta_infer_hip::gemm_bf16(&aout_bf, &w.wo, &oproj, 1, h as i32, q_dim as i32)?; let asc = DeviceBuf::alloc(h * 4)?; - vetta_infer_hip::shortconv4_decode(&oproj, &w.a_sconv, &st.hist_a, &asc, &slot0, 1, h as i32)?; + vetta_infer_hip::shortconv4_decode(&oproj, &w.a_sconv, &st.hist_a[pass], &asc, &slot0, 1, h as i32)?; if w.ip % d.block_size == 0 { let mut cur_h = vec![0f32; h]; cur.d2h(&mut cur_h)?; let snap = DeviceBuf::alloc(h * 4)?; snap.h2d(&cur_h)?; blocks.push(snap); @@ -588,7 +593,7 @@ pub fn layer_forward_full( let moeout = DeviceBuf::alloc(h * 4)?; vetta_infer_hip::moe_reduce(&y_part, &weights, &moeout, d.top_k as i32, h as i32)?; let msc = DeviceBuf::alloc(h * 4)?; - vetta_infer_hip::shortconv4_decode(&moeout, &w.m_sconv, &st.hist_m, &msc, &slot0, 1, h as i32)?; + vetta_infer_hip::shortconv4_decode(&moeout, &w.m_sconv, &st.hist_m[pass], &msc, &slot0, 1, h as i32)?; vetta_infer_hip::vec_add(cur, &msc, cur, h)?; Ok(()) } @@ -685,7 +690,7 @@ mod tests { let b0 = DeviceBuf::alloc(d.hidden * 4).unwrap(); b0.h2d(&seed).unwrap(); blocks.push(b0); let w = load_full_layer(&ar, 5, &d).unwrap(); let mut st = new_state(&d, 1).unwrap(); - layer_forward_full(&ar, &w, &mut st, &d, &cur, &mut blocks, 0, None).unwrap(); + layer_forward_full(&ar, &w, &mut st, &d, &cur, &mut blocks, 0, 0, None).unwrap(); assert_eq!(blocks.len(), 1, "ip=5 does not snapshot (5%6!=0)"); let mut out = vec![0f32; d.hidden]; cur.d2h(&mut out).unwrap(); assert!(out.iter().all(|v| v.is_finite()), "full-attn residual must be finite"); diff --git a/crates/vetta-infer-ld/src/packer.rs b/crates/vetta-infer-ld/src/packer.rs index f4d754c..8a49e21 100644 --- a/crates/vetta-infer-ld/src/packer.rs +++ b/crates/vetta-infer-ld/src/packer.rs @@ -635,7 +635,7 @@ mod tests { let mut st = crate::layer::new_state(&dims, steps).unwrap(); let cur = DeviceBuf::alloc(hidden * 4).unwrap(); cur.h2d(&x).unwrap(); let mut blocks: Vec = Vec::new(); - crate::layer::layer_forward_swa(&ar, &lw, &mut st, &dims, &cur, &mut blocks, pos, None).unwrap(); + crate::layer::layer_forward_swa(&ar, &lw, &mut st, &dims, &cur, &mut blocks, pos, 0, None).unwrap(); assert_eq!(blocks.len(), 1, "ip=0 must snapshot one depth block"); let mut got = vec![0f32; hidden]; cur.d2h(&mut got).unwrap(); @@ -754,7 +754,7 @@ mod tests { let mut st = crate::layer::new_state(&dims, 1).unwrap(); let cur = DeviceBuf::alloc(hidden * 4).unwrap(); cur.h2d(&x).unwrap(); let mut blocks: Vec = Vec::new(); - crate::layer::layer_forward_full(&ar, &lw, &mut st, &dims, &cur, &mut blocks, pos, None).unwrap(); + crate::layer::layer_forward_full(&ar, &lw, &mut st, &dims, &cur, &mut blocks, pos, 0, None).unwrap(); let mut got = vec![0f32; hidden]; cur.d2h(&mut got).unwrap(); // CPU oracle let qn_f = read_bf16_f32(&ar, &format!("{l}.q_input_norm.weight"));