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

+9 -3
View File
@@ -40,7 +40,9 @@ enum { AP_Q = 0, AP_KV, AP_BT, AP_SEQ, AP_KDS, AP_VDS, AP_CU, AP_OUT };
* (q_len = total_q / num_seqs) only when it is not. In the ragged case q_len is the batch maximum,
* taken from the parameter the arch block issued the op with (batch->max_q_len).
*/
static inline int fill_paged(const RadArgs* a, R4DArgs* r, int expect_head_dim) {
/* `row` is a token's bytes (elements for a 16-bit cache) in a (block, head) page: 2 * head_dim,
* or 25 * head_dim / 16 for the fp8_v4 cache (E4M3 K, int4 V, f16 group scales). */
static inline int fill_paged_row(const RadArgs* a, R4DArgs* r, int expect_head_dim, long long row) {
const RadTensor* q = r4d_opt(a, AP_Q);
const RadTensor* kv = r4d_opt(a, AP_KV);
const RadTensor* bt = r4d_opt(a, AP_BT);
@@ -83,8 +85,8 @@ static inline int fill_paged(const RadArgs* a, R4DArgs* r, int expect_head_dim)
* Each (block, head) slot must be dense, because the kernel walks it with vector loads. */
const long long block_size = kv->shape[2];
if (kv->stride[3] != 1) return R4D_NO(RAD_E_STRIDE);
if (kv->stride[2] != 2 * head_dim) return R4D_NO(RAD_E_STRIDE);
if (kv->shape[3] != 2 * head_dim) return R4D_NO(RAD_E_SHAPE);
if (kv->stride[2] != row) return R4D_NO(RAD_E_STRIDE);
if (kv->shape[3] != row) return R4D_NO(RAD_E_SHAPE);
/* 16-byte alignment for 128-bit loads. */
if (!r4d_aligned(q->data, 16) || !r4d_aligned(o->data, 16) || !r4d_aligned(kv->data, 16))
@@ -150,4 +152,8 @@ static inline int split_of(const RadArgs* a) {
}
static inline int fill_paged(const RadArgs* a, R4DArgs* r, int expect_head_dim) {
return fill_paged_row(a, r, expect_head_dim, 2LL * expect_head_dim);
}
#endif /* R4D_ATTN_ARGS_H */