[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:
adm committed 2026-12-01 17:42:57 +01:00
1 parent dfa6013bfd
commit 74b3384270
4 files changed
+295 -18

No files matched your search

+139 -4
View File
@@ -118,6 +118,11 @@ __device__ __forceinline__ float bf16_round(float f) {
return __uint_as_float(((u + r) >> 16) << 16);
}
// FP16 -> f32 (used by the FP16 KV cache + FP16 expert paths).
__device__ __forceinline__ float f16_to_f32(uint16_t h) {
return __half2float(__ushort_as_half(h));
}
// ---- k_embed_bf16 (kernel #1): gather token rows from a BF16 table ---------
// out[b, :] = bf16_to_f32(table[token[b], :]). Exact (no arithmetic).
@@ -353,6 +358,103 @@ extern "C" int vetta_flash_swa_gqa_decode(const float* q, const float* k, const
return 0;
}
// ---- k_flash_swa_gqa_decode_f16: FP16 KV cache variant (exact strata default) --
// strata stores K/V in FP16 (KvDtype::FP16, "default lossless FP16"); attention
// math stays f32. Reading FP16 KV (rounded at store) matches the reference's
// length-dependent accumulation; F32 KV diverges on near-ties as the cache grows.
__global__ void k_flash_swa_gqa_decode_f16(const float* __restrict__ q,
const uint16_t* __restrict__ k,
const uint16_t* __restrict__ v,
float* __restrict__ out,
int n_head, int n_kv, int seq, int hd,
float scale, int swa_window, int cur_pos,
const float* __restrict__ rel_bias, int max_rel,
const float* __restrict__ sink_logit,
const float* __restrict__ gate) {
const int h = blockIdx.x;
const int kvh = h / (n_head / n_kv);
const int tid = threadIdx.x;
const int nt = blockDim.x;
const int k_start = (swa_window > 0 && cur_pos >= swa_window) ? (cur_pos - swa_window + 1) : 0;
const int n_win = cur_pos - k_start + 1;
extern __shared__ float smem[];
float* scores = smem;
float* red = smem + n_win;
const float* qh = q + (size_t)h * hd;
const uint16_t* kh = k + (size_t)kvh * seq * hd;
const uint16_t* vh = v + (size_t)kvh * seq * hd;
const float* rb = rel_bias ? (rel_bias + (size_t)h * max_rel) : nullptr;
const float sink = sink_logit ? sink_logit[h] : -INFINITY;
float lmax = sink;
for (int p = tid; p < n_win; p += nt) {
const int s = k_start + p;
const uint16_t* ks = kh + (size_t)s * hd;
float dot = 0.f;
for (int d = 0; d < hd; ++d) dot += qh[d] * f16_to_f32(ks[d]);
dot *= scale;
const int dist = cur_pos - s;
if (rb && dist < max_rel) dot += rb[dist];
scores[p] = dot;
lmax = fmaxf(lmax, dot);
}
red[tid] = lmax;
__syncthreads();
for (int s = nt / 2; s > 0; s >>= 1) {
if (tid < s) red[tid] = fmaxf(red[tid], red[tid + s]);
__syncthreads();
}
const float m = red[0];
__syncthreads();
float lsum = 0.f;
for (int p = tid; p < n_win; p += nt) {
float e = __expf(scores[p] - m);
scores[p] = e;
lsum += e;
}
red[tid] = lsum;
__syncthreads();
for (int s = nt / 2; s > 0; s >>= 1) {
if (tid < s) red[tid] += red[tid + s];
__syncthreads();
}
const float total = red[0] + (sink_logit ? __expf(sink - m) : 0.f);
const float inv = 1.f / fmaxf(total, 1e-30f);
__syncthreads();
float* oh = out + (size_t)h * hd;
for (int d = tid; d < hd; d += nt) {
float acc = 0.f;
for (int p = 0; p < n_win; ++p) {
const int s = k_start + p;
acc += scores[p] * inv * f16_to_f32(vh[(size_t)s * hd + d]);
}
if (gate) acc *= 1.f / (1.f + __expf(-gate[(size_t)h * hd + d]));
oh[d] = acc;
}
}
extern "C" int vetta_flash_swa_gqa_decode_f16(const float* q, const uint16_t* k, const uint16_t* v,
float* out, int n_head, int n_kv, int seq, int hd,
float scale, int swa_window, int cur_pos,
const float* rel_bias, int max_rel,
const float* sink_logit, const float* gate,
vetta_stream_t stream) {
if (n_head <= 0 || n_kv <= 0 || seq <= 0 || hd <= 0 || (n_head % n_kv) != 0) return 1;
if (cur_pos < 0 || cur_pos >= seq) return 1;
const int k_start = (swa_window > 0 && cur_pos >= swa_window) ? (cur_pos - swa_window + 1) : 0;
const int n_win = cur_pos - k_start + 1;
const int nt = 256;
const size_t shmem = ((size_t)n_win + nt) * sizeof(float);
hipStream_t s = stream ? (hipStream_t)stream : (hipStream_t)0;
k_flash_swa_gqa_decode_f16<<<n_head, nt, shmem, s>>>(q, k, v, out, n_head, n_kv, seq, hd,
scale, swa_window, cur_pos, rel_bias, max_rel,
sink_logit, gate);
HIP_CHECK(hipGetLastError());
return 0;
}
// ---- k_moe_router_topk (kernel #11): Inkling exact sigmoid router ----------
// Semantics verified against strata k4_moe_router_topk:
// p[e] = sigmoid(logits[m,e]) (NaN->0); score[e] = p[e] + bias[e] (NaN->-1e29);
@@ -512,10 +614,6 @@ extern "C" int vetta_cast_f32_f16(const float* x, uint16_t* y, size_t n,
return 0;
}
__device__ __forceinline__ float f16_to_f32(uint16_t h) {
return __half2float(__ushort_as_half(h));
}
// ---- NVFP4 (SF4) codecs — exact semantics from strata ink_hip_common -------
// E2M1 nibble LUT; F8_E4M3FN group scale (16 weights/group); f32 outer_scale.
// Layout: packed_e2m1 [N, K/2] (low nib = even col, high nib = odd col),
@@ -1070,6 +1168,43 @@ extern "C" int vetta_kv_store(const float* k_in, const float* v_in, float* k_cac
return 0;
}
// ---- k_kv_store_f16: store K/V rounded to FP16 (exact strata FP16 KV cache) --
// K is the post-L2-clip key; V is RMS-normalized then rounded. __float2half_rn
// matches strata's f32->fp16 store rounding.
__global__ void k_kv_store_f16(const float* __restrict__ k_in, const float* __restrict__ v_in,
uint16_t* __restrict__ k_cache, uint16_t* __restrict__ v_cache,
int pos, int n_kv, int hd, int seq, float rms_eps) {
const int h = blockIdx.x;
if (h >= n_kv) return;
const int tid = threadIdx.x;
const int nt = blockDim.x;
const float* kv = k_in + (size_t)h * hd;
const float* vv = v_in + (size_t)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 * 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(const float* k_in, const float* v_in, uint16_t* k_cache,
uint16_t* v_cache, int pos, int n_kv, int hd, int seq,
float rms_eps, vetta_stream_t stream) {
if (n_kv <= 0 || hd <= 0 || hd > 256 || seq <= 0 || pos < 0 || pos >= seq) return 1;
hipStream_t s = stream ? (hipStream_t)stream : (hipStream_t)0;
k_kv_store_f16<<<n_kv, 256, 0, s>>>(k_in, v_in, k_cache, v_cache, pos, n_kv, hd, seq, rms_eps);
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) {