[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() {
+41
View File
@@ -1597,6 +1597,47 @@ extern "C" int vetta_kv_store_f16(const float* k_in, const float* v_in, uint16_t
return 0;
}
// ---- k_kv_store_f16_batched: store B tokens' K/V (FP16) at pos[m] -----------
// Same K-clip-as-is / V-RMSnorm-round semantics as k_kv_store_f16, extended to M
// tokens: grid (n_kv, M); k_in/v_in [M,n_kv,hd]; each token m writes cache position
// pos[m]. Used by batched prefill to fill all B KV positions before flash reads.
__global__ void k_kv_store_f16_batched(const float* __restrict__ k_in, const float* __restrict__ v_in,
uint16_t* __restrict__ k_cache, uint16_t* __restrict__ v_cache,
const int32_t* __restrict__ pos, int n_kv, int hd, int seq,
float rms_eps, int M) {
const int h = blockIdx.x;
const int m = blockIdx.y;
if (h >= n_kv || m >= M) return;
const int tid = threadIdx.x;
const int nt = blockDim.x;
const float* kv = k_in + ((size_t)m * n_kv + h) * hd;
const float* vv = v_in + ((size_t)m * n_kv + h) * hd;
__shared__ float red[256];
__shared__ float fac_s;
const float v = (tid < hd) ? vv[tid] : 0.f;
red[tid] = v * v;
__syncthreads();
for (int t = nt / 2; t > 0; t >>= 1) { if (tid < t) red[tid] += red[tid + t]; __syncthreads(); }
if (tid == 0) fac_s = rsqrtf(red[0] / (float)hd + rms_eps);
__syncthreads();
if (tid < hd) {
const size_t off = (size_t)h * seq * hd + (size_t)pos[m] * hd + tid;
k_cache[off] = __half_as_ushort(__float2half_rn(kv[tid]));
v_cache[off] = __half_as_ushort(__float2half_rn(v * fac_s));
}
}
extern "C" int vetta_kv_store_f16_batched(const float* k_in, const float* v_in, uint16_t* k_cache,
uint16_t* v_cache, const int32_t* pos, int n_kv, int hd, int seq,
float rms_eps, int M, vetta_stream_t stream) {
if (n_kv <= 0 || hd <= 0 || hd > 256 || seq <= 0 || M <= 0) return 1;
dim3 grid(n_kv, M);
hipStream_t s = stream ? (hipStream_t)stream : (hipStream_t)0;
k_kv_store_f16_batched<<<grid, 256, 0, s>>>(k_in, v_in, k_cache, v_cache, pos, n_kv, hd, seq, rms_eps, M);
HIP_CHECK(hipGetLastError());
return 0;
}
// ---- k_softcap: logits = cap * tanh(x / cap) (final logit softcapping) -----
__global__ void k_softcap(const float* __restrict__ x, float* __restrict__ y, size_t n, float cap) {