[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

+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) {