libr4d: review fixes -- the quantised stores apply v_scale; scales clamped so every code decodes finite; two more selftest shapes

kv_store_fp8v4 / k6v4 and the folds now read v_scale as the fp8 store does (attention already
applied v_descale). Scales are clamped to 8188 (V) and 2112 (K) so 7 or 31 times them stays an
f16: a group gone inf or NaN decodes finite, as E4M3's clamp to 448 does. r4d_selftest adds
decode at ctx 1000 (a partial last block) and q_len 7 (three waves) for both layouts.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
mici81andClaude Opus 5.5 authored and StillDeadcode committed 2026-10-19 15:28:21 +02:00
1 parent 1823ccd5e9
commit 3e656f0b02
4 files changed
+27 -14

No files matched your search

+2 -1
View File
@@ -814,7 +814,8 @@ base belongs to the block manager, not the batch.
**`kv_dtype` `fp8_v4` and `k6_v4`** (device-only) are the quantised layouts of
`--kv-cache-dtype` (`RadBuildCtx::kv_quant`). A head's dims are grouped by 32, each group with an
f16 scale (its absmax over the largest code), codes two's complement. A token's row in a head, at
f16 scale (its absmax over the largest code, clamped so every code decodes finite in f16),
codes two's complement. A token's row in a head, at
head_dim HD:
| bytes | `fp8_v4` (25 HD / 16) | `k6_v4` (22 HD / 16) |
+11 -7
View File
@@ -580,7 +580,7 @@ __global__ __launch_bounds__(kThreads) void kv_store_fp8v4_kernel(
const uint16_t* __restrict__ k, const uint16_t* __restrict__ v,
const int32_t* __restrict__ slot, uint8_t* __restrict__ kv,
uint32_t head_dim, uint32_t kv_heads, uint32_t block_size, uint32_t n_blocks,
uint32_t k_ld, uint32_t v_ld, float k_inv, uint32_t row) {
uint32_t k_ld, uint32_t v_ld, float k_inv, float v_inv, uint32_t row) {
const uint32_t t = blockIdx.x;
const int32_t s = slot[t];
if (s < 0 || (uint32_t)s / block_size >= n_blocks) return;
@@ -595,7 +595,7 @@ __global__ __launch_bounds__(kThreads) void kv_store_fp8v4_kernel(
#pragma unroll
for (int e = 0; e < 4; ++e) {
kf[e] = bf16_to_f32(k[(size_t)t * k_ld + j + (uint32_t)e]) * k_inv;
vf[e] = bf16_to_f32(v[(size_t)t * v_ld + j + (uint32_t)e]);
vf[e] = bf16_to_f32(v[(size_t)t * v_ld + j + (uint32_t)e]) * v_inv;
}
const uint32_t voff = K6 ? head_dim * 3 / 4 : head_dim;
const uint32_t vsoff = K6 ? head_dim * 21 / 16 : head_dim * 3 / 2;
@@ -603,7 +603,9 @@ __global__ __launch_bounds__(kThreads) void kv_store_fp8v4_kernel(
// A group of 32 is 8 lanes; its scale absmax / 31 in f16, codes 6-bit two's complement.
float kmax = fmaxf(fmaxf(fabsf(kf[0]), fabsf(kf[1])), fmaxf(fabsf(kf[2]), fabsf(kf[3])));
for (int m = 4; m >= 1; m >>= 1) kmax = fmaxf(kmax, __shfl_xor(kmax, m, 32));
const __fp16 k16 = (__fp16)fminf(kmax / 31.0f, 65504.0f);
// Clamped so 31 times it stays an f16 (65504): a group gone inf
// or NaN decodes finite, as the E4M3 form's clamp to 448 does.
const __fp16 k16 = (__fp16)fminf(kmax / 31.0f, 2112.0f);
const float ksc = (float)k16;
uint32_t q[4];
#pragma unroll
@@ -633,7 +635,8 @@ __global__ __launch_bounds__(kThreads) void kv_store_fp8v4_kernel(
float amax = fmaxf(fmaxf(fabsf(vf[0]), fabsf(vf[1])), fmaxf(fabsf(vf[2]), fabsf(vf[3])));
for (int m = 4; m >= 1; m >>= 1) amax = fmaxf(amax, __shfl_xor(amax, m, 32));
const __fp16 s16 = (__fp16)fminf(amax / 7.0f, 65504.0f);
// Clamped so 7 times it stays an f16: see the K scale above.
const __fp16 s16 = (__fp16)fminf(amax / 7.0f, 8188.0f);
const float sc = (float)s16;
uint32_t q[4];
#pragma unroll
@@ -1861,7 +1864,8 @@ static int kv_store_q4(const RadArgs* a, RadStream stream, int k6) {
if (g.kv->dtype != RAD_F8E4M3) return R4D_NO(RAD_E_DTYPE);
if (g.head_dim % 32) return R4D_NO(RAD_E_SHAPE);
const double ks = r4d_getf(a, "k_scale", 1.0);
if (!(ks > 0.0)) return R4D_NO(RAD_E_INVAL);
const double vs = r4d_getf(a, "v_scale", 1.0);
if (!(ks > 0.0) || !(vs > 0.0)) return R4D_NO(RAD_E_INVAL);
long long k_ld = g.k_ld ? g.k_ld : g.kv_heads * g.head_dim;
long long v_ld = g.v_ld ? g.v_ld : g.kv_heads * g.head_dim;
if (k6)
@@ -1870,13 +1874,13 @@ static int kv_store_q4(const RadArgs* a, RadStream stream, int k6) {
(const int32_t*)r4d_addr(g.sm), (uint8_t*)r4d_addr(g.kv),
(uint32_t)g.head_dim, (uint32_t)g.kv_heads, (uint32_t)g.block_size,
(uint32_t)g.n_blocks, (uint32_t)k_ld, (uint32_t)v_ld, (float)(1.0 / ks),
(uint32_t)(g.head_dim * r16 / 16)) == RAD_OK ? RAD_OK : RAD_E_DEVICE;
(float)(1.0 / vs), (uint32_t)(g.head_dim * r16 / 16)) == RAD_OK ? RAD_OK : RAD_E_DEVICE;
return R4D_EMIT(stream, kv_store_fp8v4_kernel<0>, dim3((uint32_t)g.T), dim3(kThreads), 0,
(const uint16_t*)r4d_addr(g.k), (const uint16_t*)r4d_addr(g.v),
(const int32_t*)r4d_addr(g.sm), (uint8_t*)r4d_addr(g.kv),
(uint32_t)g.head_dim, (uint32_t)g.kv_heads, (uint32_t)g.block_size,
(uint32_t)g.n_blocks, (uint32_t)k_ld, (uint32_t)v_ld, (float)(1.0 / ks),
(uint32_t)(g.head_dim * r16 / 16)) == RAD_OK ? RAD_OK : RAD_E_DEVICE;
(float)(1.0 / vs), (uint32_t)(g.head_dim * r16 / 16)) == RAD_OK ? RAD_OK : RAD_E_DEVICE;
}
extern "C" int r4d_rad_kv_store_fp8v4(const RadArgs* a, RadStream stream) { return kv_store_q4(a, stream, 0); }
extern "C" int r4d_rad_kv_store_k6v4(const RadArgs* a, RadStream stream) { return kv_store_q4(a, stream, 1); }
+6 -5
View File
@@ -174,7 +174,7 @@ __global__ __launch_bounds__(256) void r4d_qk_norm_rope_gate_kernel(
if constexpr (KV == QK_KV_K6V4) {
float kmax = fabsf(kf);
for (int m = 16; m >= 1; m >>= 1) kmax = fmaxf(kmax, __shfl_xor(kmax, m, 32));
const __fp16 k16 = (__fp16)fminf(kmax / 31.0f, 65504.0f);
const __fp16 k16 = (__fp16)fminf(kmax / 31.0f, 2112.0f); // finite codes, as kv_store_k6v4
const float ksc = (float)k16;
const float r = ksc > 0.0f ? rintf(kf / ksc) : 0.0f;
const int q = ((int)fminf(fmaxf(r, -31.0f), 31.0f)) & 0x3F;
@@ -191,10 +191,10 @@ __global__ __launch_bounds__(256) void r4d_qk_norm_rope_gate_kernel(
0, false);
c[t] = (unsigned char)(pk & 0xFF);
}
const float vf = __bfloat162float(vb);
const float vf = __bfloat162float(vb) * kvs.v_inv;
float amax = fabsf(vf);
for (int m = 16; m >= 1; m >>= 1) amax = fmaxf(amax, __shfl_xor(amax, m, 32));
const __fp16 s16 = (__fp16)fminf(amax / 7.0f, 65504.0f);
const __fp16 s16 = (__fp16)fminf(amax / 7.0f, 8188.0f); // finite codes, as kv_store_fp8v4
const float sc = (float)s16;
const float r = sc > 0.0f ? rintf(vf / sc) : 0.0f;
const int q = ((int)fminf(fmaxf(r, -7.0f), 7.0f)) & 0xF;
@@ -411,7 +411,8 @@ static int qk_kv_q4(const RadArgs* args, RadStream stream, int k6) {
if (v_ld < 0) return R4D_NO(RAD_E_STRIDE);
if (Mv < M || r4d_numel(sm) != M) return R4D_NO(RAD_E_SHAPE);
const double ks = r4d_getf(args, "k_scale", 1.0);
if (!(ks > 0.0)) return R4D_NO(RAD_E_INVAL);
const double vs = r4d_getf(args, "v_scale", 1.0);
if (!(ks > 0.0) || !(vs > 0.0)) return R4D_NO(RAD_E_INVAL);
QkKvStore kvs{};
kvs.v = (const __hip_bfloat16*)r4d_p(v);
kvs.slot = (const int32_t*)r4d_p(sm);
@@ -420,7 +421,7 @@ static int qk_kv_q4(const RadArgs* args, RadStream stream, int k6) {
kvs.block_size = (int)bs;
kvs.n_blocks = (int)kv->shape[0];
kvs.k_inv = 1.0f / (float)ks;
kvs.v_inv = 1.0f;
kvs.v_inv = 1.0f / (float)vs;
kvs.row = (int)(hd * r16 / 16);
return k6 ? qk_body<QK_KV_K6V4>(args, stream, kvs) : qk_body<QK_KV_F8V4>(args, stream, kvs);
}
+8 -1
View File
@@ -941,7 +941,7 @@ static void attn_numeric_case(const Plugin& pl, const char* name, int64_t head_d
if (!r) { ok(name, false, "row absent"); return; }
const int64_t seqs = 1, kv_heads = 2, bs = 16;
const int64_t q_heads = kv_heads * gqa;
const int64_t max_blocks = ctx / bs;
const int64_t max_blocks = (ctx + bs - 1) / bs; /* a ctx past a whole block: a partial last one */
const int64_t total_q = seqs * q_len;
const int kv_is_fp8 = std::strcmp(kv_dtype, "bf16") != 0;
const int kvq = !std::strcmp(kv_dtype, "fp8_v4") ? 1 : !std::strcmp(kv_dtype, "k6_v4") ? 2 : 0;
@@ -18563,6 +18563,13 @@ static void device_phase(const Plugin& pl) {
attn_numeric_case(pl, "attn_prefill_h256_gqa6_k6v4", 256, 6, 100, "k6_v4", 0, 1024);
attn_numeric_case(pl, "attn_decode_h256_gqa6_k6v4", 256, 6, 1, "k6_v4", 0, 1024);
attn_numeric_case(pl, "attn_decode_h256_gqa6_k6v4", 256, 6, 4, "k6_v4", 0, 2048);
/* A ctx past a whole block (a partial last tile), and q_len 7: three waves, where the V
* staging's unit count is not a multiple of the threads. */
for (const char* kv : { "fp8_v4", "k6_v4" }) {
const std::string dec = std::string("attn_decode_h256_gqa6_") + (kv[0] == 'f' ? "fp8v4" : "k6v4");
attn_numeric_case(pl, dec.c_str(), 256, 6, 1, kv, 0, 1000);
attn_numeric_case(pl, dec.c_str(), 256, 6, 7, kv, 0, 1000);
}
attn_numeric_case(pl, "attn_prefill_h256_gqa2_bf16kv", 256, 2, 64, "bf16", 1024, 2048);
attn_numeric_case(pl, "attn_decode_h256_gqa2_fp8kv", 256, 2, 1, "fp8_e4m3", 1024);
attn_numeric_case(pl, "attn_decode_h256_gqa2_bf16kv", 256, 2, 1, "bf16", 1024);