[HIP] Batched kv_store_f16 (M=B): fill all B KV positions before batched flash

k_kv_store_f16_batched: grid (n_kv, M); k_in/v_in [M,n_kv,hd]; each token m writes
FP16 cache position pos[m] (K as-is post-clip, V RMS-normalized then __float2half_rn).
Same semantics as the M=1 kv_store_f16. Batched prefill stores all B K/V before the
causal flash M=B reads the cache.

Parity vs B sequential kv_store_f16: B=20 n_kv=2 hd=128, K/V caches BYTE-IDENTICAL.

Additive — decode default unchanged.

Verified: hip GPU 26 green, workspace CPU -D warnings green, 6 Lean 0 sorryAx.
This commit is contained in:
adm committed 2026-12-01 23:43:34 +01:00
1 parent a7a1ab11b0
commit 10db2f11e4
2 files changed
+89

No files matched your search

+48
View File
@@ -121,6 +121,7 @@ mod ffi {
pub fn vetta_relpos_bias(r: *const c_float, rel_proj: *const c_float, rel_bias: *mut c_float, n_head: c_int, d_rel: c_int, rel_extent: c_int, lim: c_int, stream: *mut c_void) -> c_int;
pub fn vetta_kv_store(k_in: *const c_float, v_in: *const c_float, k_cache: *mut c_float, v_cache: *mut c_float, pos: c_int, n_kv: c_int, hd: c_int, seq: c_int, rms_eps: c_float, stream: *mut c_void) -> c_int;
pub fn vetta_kv_store_f16(k_in: *const c_float, v_in: *const c_float, k_cache: *mut u16, v_cache: *mut u16, pos: c_int, n_kv: c_int, hd: c_int, seq: c_int, rms_eps: c_float, stream: *mut c_void) -> c_int;
pub fn vetta_kv_store_f16_batched(k_in: *const c_float, v_in: *const c_float, k_cache: *mut u16, v_cache: *mut u16, pos: *const c_int, n_kv: c_int, hd: c_int, seq: c_int, rms_eps: c_float, m: c_int, stream: *mut c_void) -> c_int;
pub fn vetta_softcap(x: *const c_float, y: *mut c_float, n: usize, cap: c_float, stream: *mut c_void) -> c_int;
pub fn vetta_ctrl_pack(cur: *const c_float, msg: *const c_float, packed: *mut u16, d: c_int, eps: c_float, stream: *mut c_void) -> c_int;
pub fn vetta_ctrl_apply(cur: *const c_float, msg: *const c_float, gain: *const c_float, forget: *const c_float, out: *mut c_float, n: usize, stream: *mut c_void) -> c_int;
@@ -683,6 +684,16 @@ pub fn kv_store_f16(k_in: &DeviceBuf, v_in: &DeviceBuf, k_cache: &DeviceBuf, v_c
Ok(())
}
/// Batched KV store (M=B): k_in/v_in [M,n_kv,hd], each token m writes FP16 cache
/// position pos[m]. Fills all B KV positions before batched flash reads them.
#[cfg(feature = "hip")]
#[allow(clippy::too_many_arguments)]
pub fn kv_store_f16_batched(k_in: &DeviceBuf, v_in: &DeviceBuf, k_cache: &DeviceBuf, v_cache: &DeviceBuf, pos: &DeviceBuf, n_kv: i32, hd: i32, seq: i32, rms_eps: f32, m: i32) -> Result<(), String> {
let rc = unsafe { ffi::vetta_kv_store_f16_batched(k_in.ptr as *const f32, v_in.ptr as *const f32, k_cache.ptr as *mut u16, v_cache.ptr as *mut u16, pos.ptr as *const i32, n_kv, hd, seq, rms_eps, m, std::ptr::null_mut()) };
if rc != 0 { return Err("kv_store_f16_batched launch failed".into()); }
Ok(())
}
/// Launch k_softcap: y = cap * tanh(x / cap).
#[cfg(feature = "hip")]
pub fn softcap(x: &DeviceBuf, y: &DeviceBuf, n: usize, cap: f32) -> Result<(), String> {
@@ -1421,6 +1432,43 @@ mod gpu_tests {
assert!(max_rel_err < 1e-5, "flash prefill parity failed: {max_rel_err}");
}
/// Batched kv_store_f16 (M=B) must produce byte-identical FP16 caches to B
/// sequential kv_store_f16 calls at pos[m]=base+m.
#[test]
#[ignore]
fn kv_store_f16_batched_parity() {
let (n_kv, hd, seq, b, base) = (2usize, 128usize, 64usize, 20usize, 10i32);
let eps = 1e-6f32;
let ki: Vec<f32> = (0..b * n_kv * hd).map(|i| ((i % 41) as f32 - 20.0) * 0.02).collect();
let vi: Vec<f32> = (0..b * n_kv * hd).map(|i| ((i % 37) as f32 - 18.0) * 0.02).collect();
let dki = DeviceBuf::alloc(ki.len() * 4).unwrap(); dki.h2d(&ki).unwrap();
let dvi = DeviceBuf::alloc(vi.len() * 4).unwrap(); dvi.h2d(&vi).unwrap();
let pos: Vec<i32> = (0..b).map(|m| base + m as i32).collect();
let pos_bytes: Vec<u8> = pos.iter().flat_map(|x| x.to_le_bytes()).collect();
let dpos = DeviceBuf::alloc(b * 4).unwrap(); dpos.h2d_bytes(&pos_bytes).unwrap();
let cache_bytes = n_kv * seq * hd * 2;
let zeros = vec![0u8; cache_bytes];
let dk_b = DeviceBuf::alloc(cache_bytes).unwrap(); dk_b.h2d_bytes(&zeros).unwrap();
let dv_b = DeviceBuf::alloc(cache_bytes).unwrap(); dv_b.h2d_bytes(&zeros).unwrap();
kv_store_f16_batched(&dki, &dvi, &dk_b, &dv_b, &dpos, n_kv as i32, hd as i32, seq as i32, eps, b as i32).unwrap();
let dk_s = DeviceBuf::alloc(cache_bytes).unwrap(); dk_s.h2d_bytes(&zeros).unwrap();
let dv_s = DeviceBuf::alloc(cache_bytes).unwrap(); dv_s.h2d_bytes(&zeros).unwrap();
let dk1 = DeviceBuf::alloc(n_kv * hd * 4).unwrap();
let dv1 = DeviceBuf::alloc(n_kv * hd * 4).unwrap();
for m in 0..b {
dk1.h2d(&ki[m * n_kv * hd..(m + 1) * n_kv * hd]).unwrap();
dv1.h2d(&vi[m * n_kv * hd..(m + 1) * n_kv * hd]).unwrap();
kv_store_f16(&dk1, &dv1, &dk_s, &dv_s, pos[m], n_kv as i32, hd as i32, seq as i32, eps).unwrap();
}
let mut kb = vec![0u8; cache_bytes]; dk_b.d2h_bytes(&mut kb).unwrap();
let mut vb = vec![0u8; cache_bytes]; dv_b.d2h_bytes(&mut vb).unwrap();
let mut ks = vec![0u8; cache_bytes]; dk_s.d2h_bytes(&mut ks).unwrap();
let mut vs = vec![0u8; cache_bytes]; dv_s.d2h_bytes(&mut vs).unwrap();
assert_eq!(kb, ks, "batched kv_store K cache must be byte-identical");
assert_eq!(vb, vs, "batched kv_store V cache must be byte-identical");
eprintln!("kv_store_f16_batched B={b} n_kv={n_kv} hd={hd}: K/V caches byte-identical");
}
#[test]
#[ignore]
fn shortconv4_multistep_parity() {