libr4d: fp8_v4 and k6_v4 caches -- stores, folded prologues, h256/gqa6 attention

Two narrower layouts of an attention cache, a head's dims in groups of 32 with an f16 scale:
fp8_v4 (E4M3 K, int4 V; 25 * head_dim / 16 bytes a token and head) and k6_v4 (int6 K, int4 V;
22 / 16). kv_store and qk_norm_rope_gate_kv_store write them (the folds byte-identical to the
pairs); attn_prefill / attn_decode at h256 gqa6 widen the codes to f16 in a tile's staging, V a
key pair's 8 dims a thread fetched a tile ahead and widened with packed f16 (one rounding, a
dword store holding both keys), K6's high bits laid out so one mask and shift join them to the
low nibbles. r4d_selftest: the stores against an exactly representable set, the folds, and the
attention rows against the inline oracle extended to the 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 a14fab2317
commit c00a650bce
10 files changed
+781 -45

No files matched your search

+62 -8
View File
@@ -50,14 +50,14 @@ extern "C" void r4d_attn_dims(int* head_dim, int* gqa, int* block_size, int* max
#define P_OPT 3954187
// KVP: 0 = fp8 e4m3 cache, 1 = bf16 cache. Reached only through the two named wrappers below.
template <int KVP>
template <int KVP, int XOPT = 0>
static int prefill_launch(const R4DArgs* a, hipStream_t s) {
if (a->head_dim != A_HEAD_DIM || a->block_size != A_BLOCK_SIZE) return -1;
if (a->q_heads / a->kv_heads != A_GQA) return -2;
constexpr int BQ = (P_NWARPS * 16) / A_GQA;
dim3 grid((a->q_len + BQ - 1) / BQ, a->kv_heads, a->num_seqs);
hipLaunchKernelGGL((r4d_attn_prefill_kernel<P_NWARPS, P_TILE, A_HEAD_DIM, A_GQA, A_BLOCK_SIZE,
KVP, P_OPT>),
KVP, P_OPT | XOPT>),
grid, dim3(P_NWARPS * 32), 0, s, *a);
CHK();
return 0;
@@ -69,6 +69,12 @@ extern "C" int r4d_attn_prefill_h256_gqa6_fp8kv(const R4DArgs* a, hipStream_t s)
extern "C" int r4d_attn_prefill_h256_gqa6_bf16kv(const R4DArgs* a, hipStream_t s) {
return prefill_launch<1>(a, s);
}
extern "C" int r4d_attn_prefill_h256_gqa6_fp8v4kv(const R4DArgs* a, hipStream_t s) {
return prefill_launch<0, O_V4>(a, s);
}
extern "C" int r4d_attn_prefill_h256_gqa6_k6v4kv(const R4DArgs* a, hipStream_t s) {
return prefill_launch<0, O_V4 | O_K6>(a, s);
}
// ---- decode ----------------------------------------------------------------------------------
#define D_TILE 16
@@ -90,7 +96,7 @@ extern "C" long r4d_attn_decode_h256_gqa6_scratch_bytes(const R4DArgs* a) {
return r4d_decode_scratch(a, r4d_decode_splits(a, D_TILE), D_OPT);
}
template <int KVP>
template <int KVP, int XOPT = 0>
static int decode_launch(const R4DArgs* a, hipStream_t s) {
if (a->head_dim != A_HEAD_DIM || a->block_size != A_BLOCK_SIZE) return -1;
if (a->q_heads / a->kv_heads != A_GQA) return -2;
@@ -101,7 +107,7 @@ static int decode_launch(const R4DArgs* a, hipStream_t s) {
#define D_LAUNCH(NW) \
hipLaunchKernelGGL((r4d_attn_decode_kernel<NW, D_TILE, A_HEAD_DIM, A_GQA, A_BLOCK_SIZE, \
KVP, D_OPT>), \
KVP, D_OPT | XOPT>), \
dim3(splits, a->kv_heads, a->num_seqs), dim3(NW * 32), 0, s, *a, splits)
if (nw == 1) D_LAUNCH(1);
else if (nw == 2) D_LAUNCH(2);
@@ -138,6 +144,12 @@ extern "C" int r4d_attn_decode_h256_gqa6_fp8kv(const R4DArgs* a, hipStream_t s)
extern "C" int r4d_attn_decode_h256_gqa6_bf16kv(const R4DArgs* a, hipStream_t s) {
return decode_launch<1>(a, s);
}
extern "C" int r4d_attn_decode_h256_gqa6_fp8v4kv(const R4DArgs* a, hipStream_t s) {
return decode_launch<0, O_V4>(a, s);
}
extern "C" int r4d_attn_decode_h256_gqa6_k6v4kv(const R4DArgs* a, hipStream_t s) {
return decode_launch<0, O_V4 | O_K6>(a, s);
}
// ============================================================== the plugin ABI entry points
//
@@ -190,7 +202,7 @@ static int64_t decode_scratch_bytes_of(const R4DArgs* r, int which) {
return (int64_t)r4d_attn_decode_h256_gqa6_scratch_bytes(r);
}
static int64_t decode_scratch(const RadArgs* a, int head_dim, int which) {
static int64_t decode_scratch(const RadArgs* a, int head_dim, int which, long long row = 0) {
const int sp = split_of(a);
if (sp < 0) return R4D_NO(RAD_E_INVAL);
@@ -220,7 +232,7 @@ static int64_t decode_scratch(const RadArgs* a, int head_dim, int which) {
return worst;
}
int rc = fill_paged(a, &r, head_dim);
int rc = fill_paged_row(a, &r, head_dim, row > 0 ? row : 2LL * head_dim);
if (rc != RAD_OK) return rc;
r.splits = sp;
return decode_scratch_bytes_of(&r, which);
@@ -234,10 +246,10 @@ extern "C" int64_t r4d_rad_attn_decode_h128_scratch(const RadArgs* a) {
return decode_scratch(a, 128, 1);
}
static int decode_common(const RadArgs* a, R4DArgs* r, int head_dim, int which) {
static int decode_common(const RadArgs* a, R4DArgs* r, int head_dim, int which, long long row = 0) {
const int sp = split_of(a);
if (sp < 0) return R4D_NO(RAD_E_INVAL);
int rc = fill_paged(a, r, head_dim);
int rc = fill_paged_row(a, r, head_dim, row > 0 ? row : 2LL * head_dim);
if (rc != RAD_OK) return rc;
r->splits = sp;
const int64_t need = decode_scratch_bytes_of(r, which);
@@ -254,6 +266,48 @@ extern "C" int r4d_rad_attn_decode_h256_fp8(const RadArgs* a, RadStream s) {
return r4d_attn_decode_h256_gqa6_fp8kv(&r, (hipStream_t)s);
}
/* The fp8_v4 cache: a token's row is 25 * 256 / 16 = 400 bytes (E4M3 K, int4 V, f16 scales). */
extern "C" int r4d_rad_attn_prefill_h256_fp8v4(const RadArgs* a, RadStream s) {
if (check_dims_once() != RAD_OK) return R4D_NO(RAD_E_SHAPE);
R4DArgs r;
int rc = fill_paged_row(a, &r, 256, 400);
if (rc != RAD_OK) return rc;
return r4d_attn_prefill_h256_gqa6_fp8v4kv(&r, (hipStream_t)s);
}
extern "C" int64_t r4d_rad_attn_decode_h256_v4_scratch(const RadArgs* a) {
return decode_scratch(a, 256, 0, 400);
}
extern "C" int r4d_rad_attn_decode_h256_fp8v4(const RadArgs* a, RadStream s) {
if (check_dims_once() != RAD_OK) return R4D_NO(RAD_E_SHAPE);
R4DArgs r;
int rc = decode_common(a, &r, 256, 0, 400);
if (rc != RAD_OK) return rc;
return r4d_attn_decode_h256_gqa6_fp8v4kv(&r, (hipStream_t)s);
}
/* The k6_v4 cache: a token's row is 22 * 256 / 16 = 352 bytes (int6 K, int4 V, f16 scales). */
extern "C" int r4d_rad_attn_prefill_h256_k6v4(const RadArgs* a, RadStream s) {
if (check_dims_once() != RAD_OK) return R4D_NO(RAD_E_SHAPE);
R4DArgs r;
int rc = fill_paged_row(a, &r, 256, 352);
if (rc != RAD_OK) return rc;
return r4d_attn_prefill_h256_gqa6_k6v4kv(&r, (hipStream_t)s);
}
extern "C" int64_t r4d_rad_attn_decode_h256_k6_scratch(const RadArgs* a) {
return decode_scratch(a, 256, 0, 352);
}
extern "C" int r4d_rad_attn_decode_h256_k6v4(const RadArgs* a, RadStream s) {
if (check_dims_once() != RAD_OK) return R4D_NO(RAD_E_SHAPE);
R4DArgs r;
int rc = decode_common(a, &r, 256, 0, 352);
if (rc != RAD_OK) return rc;
return r4d_attn_decode_h256_gqa6_k6v4kv(&r, (hipStream_t)s);
}
extern "C" int r4d_rad_attn_decode_h256_bf16(const RadArgs* a, RadStream s) {
if (check_dims_once() != RAD_OK) return R4D_NO(RAD_E_SHAPE);
R4DArgs r;