[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.
This commit is contained in:
1 parent
cd853cda8b
commit
b9cac6d0df
2 files changed
+51
-17
No files matched your search
@@ -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<LayerState, String> {
|
||||
// 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<DeviceBuf, String> { 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<DeviceBuf, String> {
|
||||
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)?;
|
||||
|
||||
Reference in new issue
Block a user