[HIP] FP16 KV cache path (strata default), opt-in VETTA_F16_KV
strata stores K/V in FP16 by default (KvDtype::FP16, 'default lossless FP16'); vetta used F32. Add k_kv_store_f16 (RNE round to u16 cache) + k_flash_swa_gqa_decode_f16 (f32 attention math over FP16 KV), FFI/wrappers, and a new_state/forward branch gated by VETTA_F16_KV=1 (F32 stays the CPU-oracle baseline). New GPU parity test fp16_kv_store_and_flash_parity: stored K == cast_f32_f16 bits, attention over the decoded FP16 KV matches a CPU oracle fed the same values (18 hip GPU green). MEASURED honestly: FP16 KV does NOT fix the chat token-2+ divergence (still ~3.6 logit gap vs reference, robust also to VETTA_F16_EXPERT and -ffp-contract=off). The gap is a systematic multi-token accumulation difference, not KV precision; kept as faithful opt-in infrastructure, not claimed as a parity fix.
This commit is contained in:
1 parent
dfa6013bfd
commit
74b3384270
4 files changed
+295
-18
No files matched your search
@@ -177,9 +177,19 @@ pub fn load_layer(ar: &SafetensorsArchive, ip: usize, is_swa: bool) -> Result<La
|
||||
|
||||
pub fn new_state(d: &Dims, max_seq: usize) -> Result<LayerState, String> {
|
||||
let zero = |n: usize| -> Result<DeviceBuf, String> { let b = DeviceBuf::alloc(n * 4)?; b.h2d(&vec![0f32; n])?; Ok(b) };
|
||||
// KV cache dtype: F32 by default (the verified CPU-oracle baseline); FP16 via
|
||||
// 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)?)
|
||||
};
|
||||
Ok(LayerState {
|
||||
k_cache: zero(d.n_kv * max_seq * d.hd)?,
|
||||
v_cache: zero(d.n_kv * max_seq * d.hd)?,
|
||||
k_cache,
|
||||
v_cache,
|
||||
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)?],
|
||||
@@ -188,6 +198,9 @@ pub fn new_state(d: &Dims, max_seq: usize) -> Result<LayerState, String> {
|
||||
})
|
||||
}
|
||||
|
||||
/// FP16 KV cache opt-in (strata default KV dtype is FP16).
|
||||
fn f16_kv() -> bool { std::env::var("VETTA_F16_KV").is_ok() }
|
||||
|
||||
/// Run one SWA layer step on `cur` (in place), appending a depth-block snapshot
|
||||
/// to `blocks` when `ip % block_size == 0`. `pos` is the decode position.
|
||||
/// Experts are loaded on demand from `ar` (zero requantization).
|
||||
@@ -265,11 +278,19 @@ pub fn layer_forward_swa(
|
||||
vetta_infer_hip::l2_clip(&k_cv, None, d.n_kv as i32, d.hd as i32, d.qk_clip, false, pos)?;
|
||||
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)?;
|
||||
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)?;
|
||||
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)?;
|
||||
} 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)?;
|
||||
}
|
||||
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.
|
||||
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))?;
|
||||
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))?;
|
||||
} 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))?;
|
||||
}
|
||||
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); }
|
||||
|
||||
// k4: O-proj + attn shortconv + block boundary.
|
||||
@@ -705,10 +726,18 @@ pub fn layer_forward_full(
|
||||
rel_bias.h2d(&vec![0f32; d.n_head * d.rel_extent])?;
|
||||
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)?;
|
||||
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)?;
|
||||
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)?;
|
||||
} 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)?;
|
||||
}
|
||||
let aout = DeviceBuf::alloc(q_dim * 4)?;
|
||||
// global attention: window = 0.
|
||||
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))?;
|
||||
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))?;
|
||||
} 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))?;
|
||||
}
|
||||
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)?;
|
||||
vetta_infer_hip::gemm_bf16(&aout_bf, &w.wo, &oproj, 1, h as i32, q_dim as i32)?;
|
||||
|
||||
Reference in new issue
Block a user