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

+25
View File
@@ -67,6 +67,31 @@ template<> struct DT16<1> { // f16: 3 more mantissa bits,
};
// Widen 8 fp8 (2 dwords) to 8 16-bit values (4 dwords) in element order.
// Eight K dims of the k6_v4 cache as four f16 dwords, dims (2i, 2i+1) in dword i. `lo` holds
// their low nibbles (dim 2i in bits 8i..8i+3, 2i+1 in 8i+4..8i+7) and `hb` their 16-dim chunk's
// high-bit word (byte j: bits 0-1 dim 2j, 2-3 dim 2j+1, 4-5 dim 8+2j, 6-7 dim 9+2j); `half` says
// which eight of the sixteen these are. One mask and shift put the high bits beside the low
// nibbles, a byte a dim. Codes are 6-bit two's complement: ^ 32 makes them offset binary u,
// 0x6400 | u is the f16 1024 + u, and (x - 1056) * s is the value times the group scale `s2` (the
// f16 in both halves), rounded once.
__device__ __forceinline__ void k6x8_to_f16x4w(uint32_t lo, uint32_t hb, int half, uint32_t s2,
uint32_t* w4) {
typedef _Float16 h2 __attribute__((ext_vector_type(2)));
const uint32_t ev = ((lo & 0x0F0F0F0Fu) | ((half ? hb : hb << 4) & 0x30303030u)) ^ 0x20202020u;
const uint32_t od = (((lo >> 4) & 0x0F0F0F0Fu) | ((half ? hb >> 2 : hb << 2) & 0x30303030u))
^ 0x20202020u;
const h2 s = __builtin_bit_cast(h2, s2);
const h2 bias = { (_Float16)1056.0f, (_Float16)1056.0f };
#pragma unroll
for (int b = 0; b < 4; ++b) {
// Byte b of the even dims low, of the odd dims high; perm takes bytes 0-3 from its second
// operand, 4-7 from its first, and 0x0c is a zero byte.
const uint32_t sel = (uint32_t)b | 0x0c00u | ((uint32_t)(4 + b) << 16) | 0x0c000000u;
const uint32_t x = __builtin_amdgcn_perm(od, ev, sel) | 0x64006400u;
w4[b] = __builtin_bit_cast(uint32_t, (__builtin_bit_cast(h2, x) - bias) * s);
}
}
template<class D>
__device__ __forceinline__ void fp8x8_to_16x4w(uint2 raw, uint32_t* w4) {
#pragma unroll