Archived
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:
1 parent
a14fab2317
commit
c00a650bce
10 files changed
+781
-45
No files matched your search
@@ -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 */
|
||||
@@ -52,6 +52,10 @@
|
||||
// fragment for fp8, 16 for bf16), costing 2 or 4 x HEAD_DIM/16 VGPRs; PF = HEAD_DIM/16 would hold
|
||||
// converted fragments and cost occupancy. Results are unchanged.
|
||||
#define O_KRAW 16777216
|
||||
// O_V4: the fp8_v4 cache (E4M3 K, int4 V in groups of 32 with an f16 scale), and with O_K6 the
|
||||
// k6_v4 cache (int6 K, int4 V); a token's row layout is RadBuildCtx::kv_quant's (abi/rad_builder.h).
|
||||
#define O_V4 33554432
|
||||
#define O_K6 67108864
|
||||
__host__ __device__ __forceinline__ int r4d_attn_tiles(int ctx, int tile) { return (ctx + tile - 1) / tile; }
|
||||
__host__ __device__ __forceinline__ int r4d_attn_tps(int ctx, int tile, int splits) {
|
||||
int t = r4d_attn_tiles(ctx, tile); return (t + splits - 1) / splits;
|
||||
@@ -85,10 +89,20 @@ void r4d_attn_decode_kernel(const R4DArgs a, int splits)
|
||||
constexpr int DOT2 = ((OPT & O_DOT2) && (OPT & O_F16)) ? 1 : 0;
|
||||
constexpr int BTS = (OPT & O_BTS) ? 1 : 0;
|
||||
constexpr int LDSB = (OPT & O_LDSB) ? 1 : 0;
|
||||
constexpr int V8 = ((OPT & O_V8) && !KVP) ? 1 : 0;
|
||||
constexpr int V4 = ((OPT & O_V4) && !KVP) ? 1 : 0;
|
||||
constexpr int K6 = ((OPT & O_K6) && V4) ? 1 : 0;
|
||||
// A token's row in a page, and where in it V's codes, V's scales and K's scales start.
|
||||
constexpr int ROWB = K6 ? HEAD_DIM * 22 / 16 : V4 ? HEAD_DIM * 25 / 16 : 2 * HEAD_DIM;
|
||||
constexpr int VOFF = K6 ? HEAD_DIM * 3 / 4 : HEAD_DIM;
|
||||
constexpr int VSOFF = K6 ? HEAD_DIM * 21 / 16 : HEAD_DIM * 3 / 2;
|
||||
constexpr int KSOFF = HEAD_DIM * 5 / 4;
|
||||
constexpr int V8 = ((OPT & O_V8) && !KVP && !V4) ? 1 : 0;
|
||||
static_assert(!V8 || (OPT & O_CONTIG), "the code tile is read in the contiguous layout");
|
||||
constexpr int KRAW = ((OPT & O_KRAW) && !KLDS) ? 1 : 0;
|
||||
static_assert(!KRAW || (OPT & O_CONTIG), "the raw K round reads the contiguous layout");
|
||||
static_assert(!K6 || (!KLDS && (OPT & O_CONTIG) && !KRAW),
|
||||
"k6_v4 K is widened from the paged cache, a lane's eight dims contiguous");
|
||||
static_assert(!V4 || (OPT & O_F16), "fp8_v4 widens to f16");
|
||||
typedef DT16<F16> D;
|
||||
typedef typename D::frag frag16;
|
||||
|
||||
@@ -104,7 +118,7 @@ void r4d_attn_decode_kernel(const R4DArgs a, int splits)
|
||||
// Up to three uint4 it fits in 255 VGPRs without spilling (two: +6-8% at depth; three, the
|
||||
// 48-row shape q_len 8 x gqa 6 at NWARPS 3: a further 2-15%). At four or more (NWARPS 1-2, or a
|
||||
// bf16 cache) it spills and loses ~10%, so the geometry gates it, not just the flag.
|
||||
constexpr int GPREV = ((OPT & O_GPREV) && VREGS <= 3) ? 1 : 0;
|
||||
constexpr int GPREV = ((OPT & O_GPREV) && VREGS <= 3 && !V4) ? 1 : 0;
|
||||
constexpr float PSHIFT = D::SHIFT;
|
||||
constexpr float PGROW = D::GROW;
|
||||
|
||||
@@ -185,6 +199,62 @@ void r4d_attn_decode_kernel(const R4DArgs a, int splits)
|
||||
const int vg = lane >> 3, vj = lane & 7;
|
||||
uint4 vreg[VREGS];
|
||||
|
||||
// ---- fp8_v4 / k6_v4: V's int4 codes into sV16 ([dim][key]) as the prefill kernel stages them
|
||||
// (r4d_attn_prefill_h256_gqa6.hip: a key pair's 8 dims a thread, two code words and the pair's
|
||||
// scale, widened with packed f16 into one dword a dim), fetched in the tile: no prefetch.
|
||||
constexpr int NP4 = (TILE / 2) * (HEAD_DIM / 8);
|
||||
constexpr int U4 = V4 ? (NP4 + NTHR - 1) / NTHR : 1;
|
||||
uint32_t v4c[U4][2], v4s[U4];
|
||||
auto fetchV4 = [&](int k0, const int* blks) {
|
||||
#pragma unroll
|
||||
for (int r = 0; r < U4; ++r) {
|
||||
const int u = tid + r * NTHR;
|
||||
if (NP4 % NTHR == 0 || u < NP4) {
|
||||
const int kp = u % (TILE / 2), o = u / (TILE / 2);
|
||||
uint32_t sc[2];
|
||||
#pragma unroll
|
||||
for (int j = 0; j < 2; ++j) {
|
||||
int kk = min(k0 + 2 * kp + j, ctx - 1);
|
||||
if (WIN && kk < wlo0) kk = wlo0;
|
||||
const int bk = BTS ? blks[(kk - k0) / BS] : bt[kk / BS];
|
||||
const uint8_t* base = (const uint8_t*)a.kv + (size_t)bk * a.kv_block_stride
|
||||
+ (size_t)kvh * a.kv_head_stride + (size_t)(kk % BS) * ROWB;
|
||||
v4c[r][j] = *(const uint32_t*)(base + VOFF + o * 4);
|
||||
sc[j] = *(const uint16_t*)(base + VSOFF + (o >> 2) * 2);
|
||||
}
|
||||
v4s[r] = sc[0] | (sc[1] << 16);
|
||||
}
|
||||
}
|
||||
};
|
||||
auto storeV4 = [&]() {
|
||||
typedef _Float16 h2 __attribute__((ext_vector_type(2)));
|
||||
#pragma unroll
|
||||
for (int r = 0; r < U4; ++r) {
|
||||
const int u = tid + r * NTHR;
|
||||
if (NP4 % NTHR == 0 || u < NP4) {
|
||||
const int kp = u % (TILE / 2), o = u / (TILE / 2);
|
||||
// Two's complement nibbles; ^ 8 makes them offset binary (value + 8).
|
||||
const uint32_t n0 = (v4c[r][0] & 0x0F0F0F0Fu) ^ 0x08080808u;
|
||||
const uint32_t m0 = ((v4c[r][0] >> 4) & 0x0F0F0F0Fu) ^ 0x08080808u;
|
||||
const uint32_t n1 = (v4c[r][1] & 0x0F0F0F0Fu) ^ 0x08080808u;
|
||||
const uint32_t m1 = ((v4c[r][1] >> 4) & 0x0F0F0F0Fu) ^ 0x08080808u;
|
||||
const h2 s2 = __builtin_bit_cast(h2, v4s[r]);
|
||||
const h2 bias = { (_Float16)1032.0f, (_Float16)1032.0f };
|
||||
uint16_t* dst = &sV16[(o * 8) * VSTR + 2 * kp];
|
||||
#pragma unroll
|
||||
for (int b = 0; b < 4; ++b) {
|
||||
const uint32_t sel = (uint32_t)b | 0x0c00u | ((uint32_t)(4 + b) << 16) | 0x0c000000u;
|
||||
const uint32_t lo = __builtin_amdgcn_perm(n1, n0, sel) | 0x64006400u;
|
||||
const uint32_t hi = __builtin_amdgcn_perm(m1, m0, sel) | 0x64006400u;
|
||||
const h2 vl = (__builtin_bit_cast(h2, lo) - bias) * s2;
|
||||
const h2 vh = (__builtin_bit_cast(h2, hi) - bias) * s2;
|
||||
*(uint32_t*)(dst + (2 * b) * VSTR) = __builtin_bit_cast(uint32_t, vl);
|
||||
*(uint32_t*)(dst + (2 * b + 1) * VSTR) = __builtin_bit_cast(uint32_t, vh);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
auto fetchV = [&](int k0, const int* blks) {
|
||||
#pragma unroll
|
||||
for (int r = 0; r < VREGS; ++r) {
|
||||
@@ -198,7 +268,7 @@ void r4d_attn_decode_kernel(const R4DArgs a, int splits)
|
||||
const int blk = BTS ? blks[(kk - k0) / BS] : bt[kk / BS];
|
||||
const size_t o = (size_t)blk * a.kv_block_stride
|
||||
+ (size_t)kvh * a.kv_head_stride
|
||||
+ (size_t)(kk % BS) * (2 * HEAD_DIM) + HEAD_DIM + d0;
|
||||
+ (size_t)(kk % BS) * ROWB + HEAD_DIM + d0;
|
||||
const void* p = KVP ? (const void*)((const uint16_t*)a.kv + o)
|
||||
: (const void*)((const uint8_t*)a.kv + o);
|
||||
vreg[r] = __builtin_bit_cast(uint4, load_tr_b128(p));
|
||||
@@ -275,7 +345,7 @@ void r4d_attn_decode_kernel(const R4DArgs a, int splits)
|
||||
if (WIN && kk < wlo0) kk = wlo0;
|
||||
const int kb = BTS ? blk[(kk - k0) / BS] : bt[kk / BS];
|
||||
const size_t o = (size_t)kb * a.kv_block_stride
|
||||
+ (size_t)kvh * a.kv_head_stride + (size_t)(kk % BS) * (2 * HEAD_DIM);
|
||||
+ (size_t)kvh * a.kv_head_stride + (size_t)(kk % BS) * ROWB;
|
||||
uint16_t* kd = &sK[key * KSTR];
|
||||
uint32_t w4[4];
|
||||
if (KVP) {
|
||||
@@ -289,8 +359,8 @@ void r4d_attn_decode_kernel(const R4DArgs a, int splits)
|
||||
*(uint2*)(kd + ch * 8 + 4) = make_uint2(w4[2], w4[3]);
|
||||
}
|
||||
}
|
||||
if (!GPREV) fetchV(k0, blk);
|
||||
storeV();
|
||||
if constexpr (V4) { fetchV4(k0, blk); storeV4(); }
|
||||
else { if (!GPREV) fetchV(k0, blk); storeV(); }
|
||||
if (LDSB) lds_barrier(); else __syncthreads();
|
||||
if (GPREV && ti + 1 < t_hi) fetchV(k0 + TILE, blkn);
|
||||
|
||||
@@ -302,7 +372,7 @@ void r4d_attn_decode_kernel(const R4DArgs a, int splits)
|
||||
if (WIN && kk < wlo0) kk = wlo0;
|
||||
const int kb = BTS ? blk[(kk - k0) / BS] : bt[kk / BS];
|
||||
grow = (const uint8_t*)a.kv + ((size_t)kb * a.kv_block_stride
|
||||
+ (size_t)kvh * a.kv_head_stride + (size_t)(kk % BS) * (2 * HEAD_DIM))
|
||||
+ (size_t)kvh * a.kv_head_stride + (size_t)(kk % BS) * ROWB)
|
||||
* (KVP ? 2 : 1);
|
||||
}
|
||||
// A fragment's raw K: 8 elements of 1 byte (fp8) or 2 (bf16).
|
||||
@@ -341,6 +411,15 @@ void r4d_attn_decode_kernel(const R4DArgs a, int splits)
|
||||
}
|
||||
kb[j] = D::mk(make_uint2(D::from_bf16w(l0.x), D::from_bf16w(l0.y)),
|
||||
make_uint2(D::from_bf16w(h0.x), D::from_bf16w(h0.y)));
|
||||
} else if constexpr (K6) {
|
||||
// k6_v4: this lane's eight dims (16t + 8h ..) from their low nibbles, the
|
||||
// chunk's high-bit word and the group's scale.
|
||||
uint32_t w4[4];
|
||||
const uint32_t sb = *(const uint16_t*)(grow + KSOFF + (t >> 1) * 2);
|
||||
k6x8_to_f16x4w(*(const uint32_t*)(grow + 8 * t + 4 * h),
|
||||
*(const uint32_t*)(grow + HEAD_DIM / 2 + 4 * t), h,
|
||||
sb | (sb << 16), w4);
|
||||
kb[j] = D::mk(make_uint2(w4[0], w4[1]), make_uint2(w4[2], w4[3]));
|
||||
} else {
|
||||
const uint8_t* kp = grow + 16 * t;
|
||||
uint32_t w4[4];
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -53,6 +53,9 @@
|
||||
// LDSB: an LDS-scoped barrier instead of __syncthreads() (lds_barrier() in r4d_common.h). The
|
||||
// default fence invalidates the vector cache twice per tile, though only LDS needs ordering.
|
||||
#define O_LDSB 2097152
|
||||
// O_V4: the fp8_v4 cache, O_V4 | O_K6 the k6_v4 cache (see r4d_attn_decode_h256_gqa6.hip).
|
||||
#define O_V4 33554432
|
||||
#define O_K6 67108864
|
||||
|
||||
// bf16 dword -> 16-bit dword in the target format, optionally scaled. Used once per kernel on Q.
|
||||
template<class D, int FOLDQ, int F16>
|
||||
@@ -77,7 +80,14 @@ void r4d_attn_prefill_kernel(const R4DArgs a)
|
||||
constexpr int SGB = (OPT & O_SGB) ? 1 : 0;
|
||||
constexpr int DOT2 = ((OPT & O_DOT2) && (OPT & O_F16)) ? 1 : 0;
|
||||
constexpr int BTS = (OPT & O_BTS) ? 1 : 0;
|
||||
constexpr int GPREV = (OPT & O_GPREV) ? 1 : 0;
|
||||
constexpr int V4 = ((OPT & O_V4) && !KVP) ? 1 : 0;
|
||||
constexpr int K6 = ((OPT & O_K6) && V4) ? 1 : 0;
|
||||
// A token's row in a page, and where in it V's codes, V's scales and K's scales start.
|
||||
constexpr int ROWB = K6 ? HEAD_DIM * 22 / 16 : V4 ? HEAD_DIM * 25 / 16 : 2 * HEAD_DIM;
|
||||
constexpr int VOFF = K6 ? HEAD_DIM * 3 / 4 : HEAD_DIM;
|
||||
constexpr int VSOFF = K6 ? HEAD_DIM * 21 / 16 : HEAD_DIM * 3 / 2;
|
||||
constexpr int KSOFF = HEAD_DIM * 5 / 4;
|
||||
constexpr int GPREV = ((OPT & O_GPREV) && !V4) ? 1 : 0;
|
||||
constexpr int LDSB = (OPT & O_LDSB) ? 1 : 0;
|
||||
constexpr int KWIDE = (OPT & O_KWIDE) ? 1 : 0;
|
||||
typedef DT16<F16> D;
|
||||
@@ -100,6 +110,7 @@ void r4d_attn_prefill_kernel(const R4DArgs a)
|
||||
constexpr float PSHIFT = D::SHIFT;
|
||||
constexpr float PGROW = D::GROW;
|
||||
static_assert(!KWIDE || KPASS == 1, "the wide K fetch is one straight-line pass");
|
||||
static_assert(!K6 || KWIDE, "k6_v4 K is staged sixteen dims a thread");
|
||||
|
||||
__shared__ uint16_t sK[TILE * KSTR];
|
||||
__shared__ uint16_t sV16[HEAD_DIM * VSTR];
|
||||
@@ -173,6 +184,69 @@ void r4d_attn_prefill_kernel(const R4DArgs a)
|
||||
const int vg = lane >> 3, vj = lane & 7;
|
||||
uint4 kreg[KREGS], vreg[VREGS];
|
||||
|
||||
// ---- fp8_v4 / k6_v4: V is int4 in groups of 32 with an f16 scale (absmax / 7): 16 bytes of
|
||||
// codes a group at VOFF (dim d at bits 4*(d%8) of word d/8), the group scales at VSOFF. A thread stages one key pair's 8 dims: two code words and the pair's scale,
|
||||
// fetched a tile ahead like GPREV's V, so the loads are in flight across the m-tile loop. They
|
||||
// widen into sV16 ([dim][key]) as a dword per dim holding both keys: with u = code ^ 8 (offset
|
||||
// binary), 0x6400 | u is the f16 1024 + u, so (x - 1032) * s is the value times s, rounded
|
||||
// once. Key pairs run fastest across lanes,
|
||||
// which keeps the dword stores of one dim row in distinct banks.
|
||||
static_assert(!V4 || F16, "fp8_v4 widens V to f16");
|
||||
constexpr int NP4 = (TILE / 2) * (HEAD_DIM / 8);
|
||||
constexpr int U4 = V4 ? (NP4 + NTHR - 1) / NTHR : 1;
|
||||
uint32_t v4c[U4][2], v4s[U4];
|
||||
auto fetchV4 = [&](int k0, const int* blks) {
|
||||
#pragma unroll
|
||||
for (int r = 0; r < U4; ++r) {
|
||||
const int u = tid + r * NTHR;
|
||||
if (NP4 % NTHR == 0 || u < NP4) {
|
||||
const int kp = u % (TILE / 2), o = u / (TILE / 2);
|
||||
uint32_t sc[2];
|
||||
#pragma unroll
|
||||
for (int j = 0; j < 2; ++j) {
|
||||
const int kk = min(k0 + 2 * kp + j, ctx - 1);
|
||||
const int bk = BTS ? blks[(kk - k0) / BS] : bt[kk / BS];
|
||||
const uint8_t* base = (const uint8_t*)a.kv + (size_t)bk * a.kv_block_stride
|
||||
+ (size_t)kvh * a.kv_head_stride + (size_t)(kk % BS) * ROWB;
|
||||
v4c[r][j] = *(const uint32_t*)(base + VOFF + o * 4);
|
||||
sc[j] = *(const uint16_t*)(base + VSOFF + (o >> 2) * 2);
|
||||
}
|
||||
v4s[r] = sc[0] | (sc[1] << 16);
|
||||
}
|
||||
}
|
||||
};
|
||||
auto storeV4 = [&]() {
|
||||
typedef _Float16 h2 __attribute__((ext_vector_type(2)));
|
||||
#pragma unroll
|
||||
for (int r = 0; r < U4; ++r) {
|
||||
const int u = tid + r * NTHR;
|
||||
if (NP4 % NTHR == 0 || u < NP4) {
|
||||
const int kp = u % (TILE / 2), o = u / (TILE / 2);
|
||||
// Codes are two's complement nibbles; ^ 8 makes them offset binary (value + 8).
|
||||
const uint32_t n0 = (v4c[r][0] & 0x0F0F0F0Fu) ^ 0x08080808u;
|
||||
const uint32_t m0 = ((v4c[r][0] >> 4) & 0x0F0F0F0Fu) ^ 0x08080808u;
|
||||
const uint32_t n1 = (v4c[r][1] & 0x0F0F0F0Fu) ^ 0x08080808u;
|
||||
const uint32_t m1 = ((v4c[r][1] >> 4) & 0x0F0F0F0Fu) ^ 0x08080808u;
|
||||
const h2 s2 = __builtin_bit_cast(h2, v4s[r]);
|
||||
const h2 bias = { (_Float16)1032.0f, (_Float16)1032.0f };
|
||||
uint16_t* dst = &sV16[(o * 8) * VSTR + 2 * kp];
|
||||
#pragma unroll
|
||||
for (int b = 0; b < 4; ++b) {
|
||||
// Byte b of the first key's word low, of the second key's word in the high half:
|
||||
// perm selects source 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 lo = __builtin_amdgcn_perm(n1, n0, sel) | 0x64006400u; // dim 2b
|
||||
const uint32_t hi = __builtin_amdgcn_perm(m1, m0, sel) | 0x64006400u; // dim 2b+1
|
||||
const h2 vl = (__builtin_bit_cast(h2, lo) - bias) * s2;
|
||||
const h2 vh = (__builtin_bit_cast(h2, hi) - bias) * s2;
|
||||
*(uint32_t*)(dst + (2 * b) * VSTR) = __builtin_bit_cast(uint32_t, vl);
|
||||
*(uint32_t*)(dst + (2 * b + 1) * VSTR) = __builtin_bit_cast(uint32_t, vh);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
auto fetchK = [&](int k0, const int* blks) {
|
||||
#pragma unroll
|
||||
for (int p = 0; p < KPASS; ++p) {
|
||||
@@ -184,12 +258,18 @@ void r4d_attn_prefill_kernel(const R4DArgs a)
|
||||
const int kk = min(k0 + key, ctx - 1);
|
||||
const int blk = BTS ? blks[(kk - k0) / BS] : bt[kk / BS];
|
||||
const size_t o = (size_t)blk * a.kv_block_stride
|
||||
+ (size_t)kvh * a.kv_head_stride + (size_t)(kk % BS) * (2 * HEAD_DIM);
|
||||
+ (size_t)kvh * a.kv_head_stride + (size_t)(kk % BS) * ROWB;
|
||||
if (KVP) {
|
||||
const uint16_t* g = (const uint16_t*)a.kv + o + ch * KEL;
|
||||
#pragma unroll
|
||||
for (int q = 0; q < (KVP ? (KEL / 8) : 1); ++q)
|
||||
kreg[p * (KEL / 8) + q] = *(const uint4*)(g + 8 * q);
|
||||
} else if (K6) {
|
||||
// k6_v4: the chunk's sixteen low nibbles, its high-bit word, its group's scale.
|
||||
const uint8_t* rp = (const uint8_t*)a.kv + o;
|
||||
const uint2 lo = *(const uint2*)(rp + ch * 8);
|
||||
kreg[p] = make_uint4(lo.x, lo.y, *(const uint32_t*)(rp + HEAD_DIM / 2 + ch * 4),
|
||||
*(const uint16_t*)(rp + KSOFF + (ch >> 1) * 2));
|
||||
} else if (KWIDE) {
|
||||
kreg[p] = *(const uint4*)((const uint8_t*)a.kv + o + ch * 16);
|
||||
} else {
|
||||
@@ -212,7 +292,7 @@ void r4d_attn_prefill_kernel(const R4DArgs a)
|
||||
const int blk = BTS ? blks[(kk - k0) / BS] : bt[kk / BS];
|
||||
const size_t o = (size_t)blk * a.kv_block_stride
|
||||
+ (size_t)kvh * a.kv_head_stride
|
||||
+ (size_t)(kk % BS) * (2 * HEAD_DIM) + HEAD_DIM + d0;
|
||||
+ (size_t)(kk % BS) * ROWB + HEAD_DIM + d0;
|
||||
const void* p = KVP ? (const void*)((const uint16_t*)a.kv + o)
|
||||
: (const void*)((const uint8_t*)a.kv + o);
|
||||
vreg[r] = __builtin_bit_cast(uint4, load_tr_b128(p));
|
||||
@@ -238,7 +318,8 @@ void r4d_attn_prefill_kernel(const R4DArgs a)
|
||||
w4[2] = D::from_bf16w(r.z); w4[3] = D::from_bf16w(r.w);
|
||||
} else {
|
||||
const uint4 r = kreg[p];
|
||||
fp8x8_to_16x4w<D>(q ? make_uint2(r.z, r.w) : make_uint2(r.x, r.y), w4);
|
||||
if constexpr (K6) k6x8_to_f16x4w(q ? r.y : r.x, r.z, q, r.w | (r.w << 16), w4);
|
||||
else fp8x8_to_16x4w<D>(q ? make_uint2(r.z, r.w) : make_uint2(r.x, r.y), w4);
|
||||
}
|
||||
*(uint2*)(kd + 8 * q) = make_uint2(w4[0], w4[1]);
|
||||
*(uint2*)(kd + 8 * q + 4) = make_uint2(w4[2], w4[3]);
|
||||
@@ -296,6 +377,7 @@ void r4d_attn_prefill_kernel(const R4DArgs a)
|
||||
int blk[NB];
|
||||
if (BTS) load_bt(tstart * NB, blk);
|
||||
if (GPREV) fetchV(tstart * TILE, blk);
|
||||
if (V4) fetchV4(tstart * TILE, blk);
|
||||
|
||||
for (int ti = tstart; ti < ntiles; ++ti) {
|
||||
const int k0 = ti * TILE;
|
||||
@@ -305,13 +387,14 @@ void r4d_attn_prefill_kernel(const R4DArgs a)
|
||||
}
|
||||
if (LDSB) lds_barrier(); else __syncthreads();
|
||||
fetchK(k0, blk);
|
||||
if (!GPREV) fetchV(k0, blk);
|
||||
storeV(); // V first: already in registers, so it covers K's load latency
|
||||
// V first: already in registers, so it covers K's load latency.
|
||||
if constexpr (V4) { storeV4(); } else { if (!GPREV) fetchV(k0, blk); storeV(); }
|
||||
storeK();
|
||||
if (LDSB) lds_barrier(); else __syncthreads();
|
||||
// Issue the next tile's V loads before the WMMAs below; they are consumed after the next
|
||||
// barrier.
|
||||
if (GPREV && ti + 1 < ntiles) fetchV(k0 + TILE, blkn);
|
||||
if (V4 && ti + 1 < ntiles) fetchV4(k0 + TILE, blkn);
|
||||
|
||||
#pragma unroll 1
|
||||
for (int mt = 0; mt < MT; ++mt) {
|
||||
|
||||
@@ -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
|
||||
|
||||
+109
-2
@@ -570,6 +570,83 @@ __global__ __launch_bounds__(kThreads) void kv_store_fp8_kernel(
|
||||
}
|
||||
}
|
||||
|
||||
// The fp8_v4 store: K as kv_store_fp8 writes it, V as int4 in groups of 32 with an f16 scale
|
||||
// (absmax / 7). Four columns a thread as above, so a group of 32 is 8 lanes; head_dim is a multiple
|
||||
// of 32, so a group never straddles two heads. A token's row is `row` bytes. K6 is the k6_v4 store:
|
||||
// K as int6 in groups of 32 (absmax / 31) in the row layout of abi/rad_builder.h's RAD_KVQ_K6V4, V
|
||||
// as above after it.
|
||||
template <int K6>
|
||||
__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) {
|
||||
const uint32_t t = blockIdx.x;
|
||||
const int32_t s = slot[t];
|
||||
if (s < 0 || (uint32_t)s / block_size >= n_blocks) return;
|
||||
const uint32_t blk = (uint32_t)s / block_size, off = (uint32_t)s % block_size;
|
||||
const size_t slot_base = ((size_t)blk * kv_heads * block_size + (size_t)off) * row;
|
||||
const size_t head_step = (size_t)block_size * row;
|
||||
|
||||
for (uint32_t j = threadIdx.x * 4u; j < kv_heads * head_dim; j += kThreads * 4u) {
|
||||
const uint32_t h = j / head_dim, d = j % head_dim;
|
||||
uint8_t* __restrict__ c = kv + slot_base + (size_t)h * head_step;
|
||||
float kf[4], vf[4];
|
||||
#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]);
|
||||
}
|
||||
const uint32_t voff = K6 ? head_dim * 3 / 4 : head_dim;
|
||||
const uint32_t vsoff = K6 ? head_dim * 21 / 16 : head_dim * 3 / 2;
|
||||
if constexpr (K6) {
|
||||
// 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);
|
||||
const float ksc = (float)k16;
|
||||
uint32_t q[4];
|
||||
#pragma unroll
|
||||
for (int e = 0; e < 4; ++e) {
|
||||
const float r = ksc > 0.0f ? rintf(kf[e] / ksc) : 0.0f;
|
||||
q[e] = (uint32_t)((int)fminf(fmaxf(r, -31.0f), 31.0f)) & 0x3Fu;
|
||||
}
|
||||
*(uint16_t*)(c + d / 2) = (uint16_t)((q[0] & 15u) | ((q[1] & 15u) << 4) |
|
||||
((q[2] & 15u) << 8) | ((q[3] & 15u) << 12));
|
||||
// The high bits of dims x..x+3 of a 16-dim chunk (x = d % 16 in 0, 4, 8, 12): bits
|
||||
// 0-3 of bytes x/2 and x/2+1 for x < 8, bits 4-7 of bytes (x-8)/2 and (x-8)/2+1 for
|
||||
// x >= 8. The lane two over holds the dims eight on, so the lower lane writes both.
|
||||
const uint32_t hb = ((q[0] >> 4) | ((q[1] >> 4) << 2)) | ((((q[2] >> 4) | ((q[3] >> 4) << 2))) << 8);
|
||||
const uint32_t up = (uint32_t)__shfl_xor((int)hb, 2, 32);
|
||||
if ((d & 15u) < 8u)
|
||||
*(uint16_t*)(c + head_dim / 2 + (d / 16) * 4 + (d & 7u) / 2) = (uint16_t)(hb | (up << 4));
|
||||
if ((d & 31u) == 0)
|
||||
*(uint16_t*)(c + head_dim * 5 / 4 + (d / 32) * 2) = __builtin_bit_cast(uint16_t, k16);
|
||||
} else {
|
||||
int pk = 0;
|
||||
pk = __builtin_amdgcn_cvt_pk_fp8_f32(fminf(fmaxf(kf[0], -448.0f), 448.0f),
|
||||
fminf(fmaxf(kf[1], -448.0f), 448.0f), pk, false);
|
||||
pk = __builtin_amdgcn_cvt_pk_fp8_f32(fminf(fmaxf(kf[2], -448.0f), 448.0f),
|
||||
fminf(fmaxf(kf[3], -448.0f), 448.0f), pk, true);
|
||||
*(uint32_t*)(c + d) = (uint32_t)pk;
|
||||
}
|
||||
|
||||
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);
|
||||
const float sc = (float)s16;
|
||||
uint32_t q[4];
|
||||
#pragma unroll
|
||||
for (int e = 0; e < 4; ++e) {
|
||||
const float r = sc > 0.0f ? rintf(vf[e] / sc) : 0.0f;
|
||||
q[e] = (uint32_t)((int)fminf(fmaxf(r, -7.0f), 7.0f)) & 0xFu;
|
||||
}
|
||||
*(uint16_t*)(c + voff + d / 2) = (uint16_t)(q[0] | (q[1] << 4) | (q[2] << 8) | (q[3] << 12));
|
||||
if ((d & 31u) == 0)
|
||||
*(uint16_t*)(c + vsoff + (d / 32) * 2) = __builtin_bit_cast(uint16_t, s16);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
// ================================================================================ entry points
|
||||
@@ -1709,7 +1786,7 @@ struct KvStoreGeom {
|
||||
long long head_dim, block_size, kv_heads, n_blocks, k_ld, v_ld, T;
|
||||
};
|
||||
|
||||
static int kv_store_geom(const RadArgs* a, KvStoreGeom* g) {
|
||||
static int kv_store_geom(const RadArgs* a, KvStoreGeom* g, long long row_per_hd16 = 32) {
|
||||
g->k = r4d_opt(a, KS_K);
|
||||
g->v = r4d_opt(a, KS_V);
|
||||
g->sm = r4d_opt(a, KS_SLOT);
|
||||
@@ -1728,7 +1805,7 @@ static int kv_store_geom(const RadArgs* a, KvStoreGeom* g) {
|
||||
* disagrees. */
|
||||
if (g->kv->rank != 4) return R4D_NO(RAD_E_SHAPE);
|
||||
if (g->kv->shape[1] != g->kv_heads || g->kv->shape[2] != g->block_size ||
|
||||
g->kv->shape[3] != 2 * g->head_dim) return R4D_NO(RAD_E_SHAPE);
|
||||
g->kv->shape[3] != g->head_dim * row_per_hd16 / 16) return R4D_NO(RAD_E_SHAPE);
|
||||
g->n_blocks = g->kv->shape[0];
|
||||
|
||||
const long long lane = g->kv_heads * g->head_dim;
|
||||
@@ -1774,6 +1851,36 @@ extern "C" int r4d_rad_kv_store_fp8(const RadArgs* a, RadStream stream) {
|
||||
(float)ks, (float)vs, stream) == 0 ? RAD_OK : RAD_E_DEVICE;
|
||||
}
|
||||
|
||||
/* The fp8_v4 and k6_v4 stores: the cache's last extent is a token's row, 25 or 22 * head_dim / 16
|
||||
* bytes. */
|
||||
static int kv_store_q4(const RadArgs* a, RadStream stream, int k6) {
|
||||
const long long r16 = k6 ? 22 : 25;
|
||||
KvStoreGeom g{};
|
||||
const int rc = kv_store_geom(a, &g, r16);
|
||||
if (rc != RAD_OK) return rc;
|
||||
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);
|
||||
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)
|
||||
return R4D_EMIT(stream, kv_store_fp8v4_kernel<1>, 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;
|
||||
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;
|
||||
}
|
||||
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); }
|
||||
|
||||
/* ------------------------------------------------------------------ logits_gemm
|
||||
*
|
||||
* gemm_nt's arithmetic with `n_vocab`/`n_embd` for N/K, but writing f32 (see the kernel above).
|
||||
|
||||
@@ -91,6 +91,12 @@ extern "C" {
|
||||
int r4d_rad_attn_prefill_h256_fp8 (const RadArgs*, RadStream);
|
||||
int r4d_rad_attn_prefill_h256_bf16(const RadArgs*, RadStream);
|
||||
int r4d_rad_attn_decode_h256_fp8 (const RadArgs*, RadStream);
|
||||
int r4d_rad_attn_decode_h256_fp8v4(const RadArgs*, RadStream);
|
||||
int r4d_rad_attn_prefill_h256_fp8v4(const RadArgs*, RadStream);
|
||||
int64_t r4d_rad_attn_decode_h256_v4_scratch(const RadArgs*);
|
||||
int r4d_rad_attn_decode_h256_k6v4(const RadArgs*, RadStream);
|
||||
int r4d_rad_attn_prefill_h256_k6v4(const RadArgs*, RadStream);
|
||||
int64_t r4d_rad_attn_decode_h256_k6_scratch(const RadArgs*);
|
||||
int r4d_rad_attn_decode_h256_bf16 (const RadArgs*, RadStream);
|
||||
int r4d_rad_attn_decode_h128_fp8 (const RadArgs*, RadStream);
|
||||
int r4d_rad_attn_decode_h128_bf16 (const RadArgs*, RadStream);
|
||||
@@ -410,6 +416,8 @@ int r4d_rad_gated_had_quant_i8 (const RadArgs*, RadStream);
|
||||
int r4d_rad_gdn_gated_norm_hq_i8 (const RadArgs*, RadStream);
|
||||
int r4d_rad_qk_norm_rope_gate (const RadArgs*, RadStream);
|
||||
int r4d_rad_qk_norm_rope_gate_kv_store(const RadArgs*, RadStream);
|
||||
int r4d_rad_qk_norm_rope_gate_kv_store_fp8v4(const RadArgs*, RadStream);
|
||||
int r4d_rad_qk_norm_rope_gate_kv_store_k6v4(const RadArgs*, RadStream);
|
||||
int r4d_rad_rope_table (const RadArgs*, RadStream);
|
||||
|
||||
/* -- block-scaled fp8, and the device sampler (r4d_gemm_fp8*.hip / r4d_fp8_layout.cpp) --
|
||||
@@ -607,6 +615,8 @@ int r4d_rad_gemm_nt_bias (const RadArgs*, RadStream);
|
||||
int r4d_rad_gemm_f32_nt_bias(const RadArgs*, RadStream);
|
||||
int r4d_rad_kv_store (const RadArgs*, RadStream);
|
||||
int r4d_rad_kv_store_fp8 (const RadArgs*, RadStream);
|
||||
int r4d_rad_kv_store_fp8v4 (const RadArgs*, RadStream);
|
||||
int r4d_rad_kv_store_k6v4 (const RadArgs*, RadStream);
|
||||
int r4d_rad_logits_gemm (const RadArgs*, RadStream);
|
||||
|
||||
/* -- the decomposed sampler chain (r4d_sample_stages_f32.hip) --
|
||||
|
||||
@@ -49,10 +49,11 @@ struct QkKvStore {
|
||||
void* cache;
|
||||
int v_pitch, block_size, n_blocks;
|
||||
float k_inv, v_inv;
|
||||
int row; /* fp8_v4 / k6_v4: a token's bytes in a page, 25 or 22 * HD / 16 */
|
||||
};
|
||||
|
||||
/* The cache forms KV selects. */
|
||||
enum { QK_KV_NONE = 0, QK_KV_FP8 = 1, QK_KV_BF16 = 2 };
|
||||
enum { QK_KV_NONE = 0, QK_KV_FP8 = 1, QK_KV_BF16 = 2, QK_KV_F8V4 = 3, QK_KV_K6V4 = 4 };
|
||||
|
||||
/* KV folds kv_store in: a k head's block, holding the token's rotated k one element per lane, also
|
||||
* writes that element and the head's v element into the cache at the token's slot, as E4M3 with
|
||||
@@ -153,14 +154,53 @@ __global__ __launch_bounds__(256) void r4d_qk_norm_rope_gate_kernel(
|
||||
if (sl >= 0 && (uint32_t)sl / (uint32_t)kvs.block_size < (uint32_t)kvs.n_blocks) {
|
||||
const uint32_t blk = (uint32_t)sl / (uint32_t)kvs.block_size;
|
||||
const uint32_t off = (uint32_t)sl % (uint32_t)kvs.block_size;
|
||||
const size_t at = ((size_t)blk * nkv * kvs.block_size + off) * 2 * HD +
|
||||
(size_t)lh * kvs.block_size * 2 * HD;
|
||||
const size_t rowb = (KV == QK_KV_F8V4 || KV == QK_KV_K6V4) ? (size_t)kvs.row : (size_t)(2 * HD);
|
||||
const size_t at = ((size_t)blk * nkv * kvs.block_size + off) * rowb +
|
||||
(size_t)lh * kvs.block_size * rowb;
|
||||
const __hip_bfloat16 kb = __float2bfloat16(res);
|
||||
const __hip_bfloat16 vb = kvs.v[(size_t)tok * kvs.v_pitch + (size_t)lh * HD + t];
|
||||
if constexpr (KV == QK_KV_BF16) {
|
||||
__hip_bfloat16* c = (__hip_bfloat16*)kvs.cache + at;
|
||||
c[t] = kb;
|
||||
c[HD + t] = vb;
|
||||
} else if constexpr (KV == QK_KV_F8V4 || KV == QK_KV_K6V4) {
|
||||
// fp8_v4: K as the E4M3 form writes it. k6_v4: K as int6 in groups of 32 (one wave)
|
||||
// with an f16 scale, absmax / 31, in kv_store_k6v4's layout. V as int4 in groups of 32
|
||||
// with an f16 scale, absmax / 7. The even lane of a pair writes the pair's nibble byte.
|
||||
unsigned char* c = (unsigned char*)kvs.cache + at;
|
||||
const float kf = __bfloat162float(kb) * kvs.k_inv;
|
||||
constexpr int voff = KV == QK_KV_K6V4 ? HD * 3 / 4 : HD;
|
||||
constexpr int vsoff = KV == QK_KV_K6V4 ? HD * 21 / 16 : HD * 3 / 2;
|
||||
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 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;
|
||||
const int qn = __shfl_xor(q & 15, 1, 32);
|
||||
if ((t & 1) == 0) c[t / 2] = (unsigned char)((q & 15) | (qn << 4));
|
||||
// High bits: a pair's four into bits 0-3 at its even lane, then the pair eight dims on
|
||||
// into bits 4-7; lane 2j of a 16-dim chunk writes byte j.
|
||||
const int h01 = (q >> 4) | (__shfl_xor(q >> 4, 1, 32) << 2);
|
||||
const int hb = h01 | (__shfl_xor(h01, 8, 32) << 4);
|
||||
if ((t & 1) == 0 && (t & 15) < 8) c[HD / 2 + (t / 16) * 4 + (t & 7) / 2] = (unsigned char)hb;
|
||||
if ((t & 31) == 0) *(uint16_t*)(c + HD * 5 / 4 + (t / 32) * 2) = __builtin_bit_cast(uint16_t, k16);
|
||||
} else {
|
||||
const int pk = __builtin_amdgcn_cvt_pk_fp8_f32(fminf(fmaxf(kf, -448.0f), 448.0f), 0.0f,
|
||||
0, false);
|
||||
c[t] = (unsigned char)(pk & 0xFF);
|
||||
}
|
||||
const float vf = __bfloat162float(vb);
|
||||
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 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;
|
||||
const int qn = __shfl_xor(q, 1, 32);
|
||||
if ((t & 1) == 0) c[voff + t / 2] = (unsigned char)(q | (qn << 4));
|
||||
if ((t & 31) == 0) *(uint16_t*)(c + vsoff + (t / 32) * 2) = __builtin_bit_cast(uint16_t, s16);
|
||||
} else {
|
||||
unsigned char* c = (unsigned char*)kvs.cache + at;
|
||||
const float kf = __bfloat162float(kb) * kvs.k_inv;
|
||||
@@ -346,6 +386,51 @@ extern "C" int r4d_rad_qk_norm_rope_gate_kv_store(const RadArgs* args, RadStream
|
||||
: qk_body<QK_KV_FP8>(args, stream, kvs);
|
||||
}
|
||||
|
||||
/* The fp8_v4 and k6_v4 forms: the same operands, the cache [n_blocks, kv_heads, block_size,
|
||||
* 25 or 22 * HD / 16] E4M3-typed bytes (fp8_v4: E4M3 K; k6_v4: int6 K; both int4 V, f16 scales). */
|
||||
static int qk_kv_q4(const RadArgs* args, RadStream stream, int k6) {
|
||||
const long long r16 = k6 ? 22 : 25;
|
||||
const RadTensor* k = r4d_opt(args, QK_K);
|
||||
const RadTensor* qg = r4d_opt(args, QK_QGATE);
|
||||
const RadTensor* v = r4d_opt(args, QK_V);
|
||||
const RadTensor* sm = r4d_opt(args, QK_SLOT);
|
||||
const RadTensor* kv = r4d_opt(args, QK_CACHE);
|
||||
if (!k || !v || !sm || !kv) return R4D_NO(RAD_E_INVAL);
|
||||
if (v->dtype != RAD_BF16 || sm->dtype != RAD_I32 || kv->dtype != RAD_F8E4M3)
|
||||
return R4D_NO(RAD_E_DTYPE);
|
||||
if (!r4d_contig(sm) || !r4d_contig(kv)) return R4D_NO(RAD_E_STRIDE);
|
||||
const long long hd = r4d_geti_or(args, "head_dim", 0);
|
||||
const long long nkv = r4d_geti_or(args, "n_head_kv", 0);
|
||||
const long long bs = r4d_geti_or(args, "block_size", 0);
|
||||
if (hd <= 0 || nkv <= 0 || bs <= 0 || hd % 32) return R4D_NO(RAD_E_SHAPE);
|
||||
if (kv->rank != 4 || kv->shape[1] != nkv || kv->shape[2] != bs || kv->shape[3] != hd * r16 / 16)
|
||||
return R4D_NO(RAD_E_SHAPE);
|
||||
const long long M = qg ? qg->shape[0] : k->shape[0];
|
||||
long long Mv = 0;
|
||||
const long long v_ld = r4d_rows_pitch(v, nkv * hd, &Mv);
|
||||
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);
|
||||
QkKvStore kvs{};
|
||||
kvs.v = (const __hip_bfloat16*)r4d_p(v);
|
||||
kvs.slot = (const int32_t*)r4d_p(sm);
|
||||
kvs.cache = r4d_wp(kv);
|
||||
kvs.v_pitch = (int)v_ld;
|
||||
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.row = (int)(hd * r16 / 16);
|
||||
return k6 ? qk_body<QK_KV_K6V4>(args, stream, kvs) : qk_body<QK_KV_F8V4>(args, stream, kvs);
|
||||
}
|
||||
extern "C" int r4d_rad_qk_norm_rope_gate_kv_store_fp8v4(const RadArgs* args, RadStream stream) {
|
||||
return qk_kv_q4(args, stream, 0);
|
||||
}
|
||||
extern "C" int r4d_rad_qk_norm_rope_gate_kv_store_k6v4(const RadArgs* args, RadStream stream) {
|
||||
return qk_kv_q4(args, stream, 1);
|
||||
}
|
||||
|
||||
// The cos/sin plane the fused prologue reads: one launch a step, against `rope`'s one per layer.
|
||||
// `qk_norm_rope_gate` takes a table, not a theta, so this row is what makes it usable.
|
||||
//
|
||||
|
||||
@@ -3130,6 +3130,24 @@ static const RadConstraint cAttnDecodeFp8[] = {
|
||||
C_EQ("window", 0), C_EQ("head_dim", 256), C_EQ("gqa", 6), C_EQ("block_size", 16),
|
||||
C_EQ("causal", 1), C_LE("q_len", 10), C_IN("q_dtype", "bf16"), C_IN("kv_dtype", "fp8_e4m3"),
|
||||
};
|
||||
/* The fp8_v4 cache (E4M3 K, int4 V in groups of 32 with f16 scales) and the k6_v4 cache (int6 K,
|
||||
* int4 V); the row layouts are RAD_KVQ_*'s (abi/rad_builder.h). */
|
||||
static const RadConstraint cAttnPrefillK6v4[] = {
|
||||
C_EQ("window", 0), C_EQ("head_dim", 256), C_EQ("gqa", 6), C_EQ("block_size", 16),
|
||||
C_EQ("causal", 1), C_IN("q_dtype", "bf16"), C_IN("kv_dtype", "k6_v4"),
|
||||
};
|
||||
static const RadConstraint cAttnDecodeK6v4[] = {
|
||||
C_EQ("window", 0), C_EQ("head_dim", 256), C_EQ("gqa", 6), C_EQ("block_size", 16),
|
||||
C_EQ("causal", 1), C_LE("q_len", 10), C_IN("q_dtype", "bf16"), C_IN("kv_dtype", "k6_v4"),
|
||||
};
|
||||
static const RadConstraint cAttnPrefillFp8v4[] = {
|
||||
C_EQ("window", 0), C_EQ("head_dim", 256), C_EQ("gqa", 6), C_EQ("block_size", 16),
|
||||
C_EQ("causal", 1), C_IN("q_dtype", "bf16"), C_IN("kv_dtype", "fp8_v4"),
|
||||
};
|
||||
static const RadConstraint cAttnDecodeFp8v4[] = {
|
||||
C_EQ("window", 0), C_EQ("head_dim", 256), C_EQ("gqa", 6), C_EQ("block_size", 16),
|
||||
C_EQ("causal", 1), C_LE("q_len", 10), C_IN("q_dtype", "bf16"), C_IN("kv_dtype", "fp8_v4"),
|
||||
};
|
||||
static const RadConstraint cAttnDecodeBf16[] = {
|
||||
C_EQ("window", 0), C_EQ("head_dim", 256), C_EQ("gqa", 6), C_EQ("block_size", 16),
|
||||
C_EQ("causal", 1), C_LE("q_len", 10), C_IN("q_dtype", "bf16"), C_IN("kv_dtype", "bf16"),
|
||||
@@ -3965,6 +3983,14 @@ static const RadConstraint cQkNormRopeGateKv[] = {
|
||||
C_IN("q_dtype", "bf16"), C_LE("head_dim", 256), C_DIV("head_dim", 32),
|
||||
C_GE("block_size", 1), C_IN("kv_dtype", "fp8_e4m3"),
|
||||
};
|
||||
static const RadConstraint cQkNormRopeGateKvFp8v4[] = {
|
||||
C_IN("q_dtype", "bf16"), C_LE("head_dim", 256), C_DIV("head_dim", 32),
|
||||
C_GE("block_size", 1), C_IN("kv_dtype", "fp8_v4"),
|
||||
};
|
||||
static const RadConstraint cQkNormRopeGateKvK6v4[] = {
|
||||
C_IN("q_dtype", "bf16"), C_LE("head_dim", 256), C_DIV("head_dim", 32),
|
||||
C_GE("block_size", 1), C_IN("kv_dtype", "k6_v4"),
|
||||
};
|
||||
static const RadConstraint cQkNormRopeGateKvBf16[] = {
|
||||
C_IN("q_dtype", "bf16"), C_LE("head_dim", 256), C_DIV("head_dim", 32),
|
||||
C_GE("block_size", 1), C_IN("kv_dtype", "bf16"),
|
||||
@@ -4248,6 +4274,10 @@ static const RadConstraint cCausalConv[] = { C_IN("dtype", "bf16"), C_GE("width"
|
||||
C_LE("width", 8) };
|
||||
static const RadConstraint cKvStore[] = { C_GE("block_size", 1), C_IN("kv_dtype", "bf16") };
|
||||
static const RadConstraint cKvStoreFp8[] = { C_GE("block_size", 1), C_IN("kv_dtype", "fp8_e4m3") };
|
||||
static const RadConstraint cKvStoreFp8v4[] = { C_GE("block_size", 1), C_DIV("head_dim", 32),
|
||||
C_IN("kv_dtype", "fp8_v4") };
|
||||
static const RadConstraint cKvStoreK6v4[] = { C_GE("block_size", 1), C_DIV("head_dim", 32),
|
||||
C_IN("kv_dtype", "k6_v4") };
|
||||
static const RadConstraint cRopeRow[] = { C_GE("head_dim", 1),
|
||||
C_IN("mode", "neox mrope gptj imrope axial") };
|
||||
/* The vision tower's rows. The GEMM stages 16-byte chunks, so K is a multiple of 8; the norm and
|
||||
@@ -4821,6 +4851,26 @@ static const RadKernelInfo kKernels[] = {
|
||||
nullptr, nullptr, r4d_rad_kv_store_fp8, nullptr, nullptr, nullptr,
|
||||
r4d_shape_kv_store },
|
||||
|
||||
{ "kv_store_fp8v4", "kv_store", "attn",
|
||||
"scatter k and v into a paged fp8_v4 cache, one workgroup a token",
|
||||
"cache exactly [n_blocks, kv_heads, block_size, 25*head_dim/16]; head_dim a multiple of 32; a "
|
||||
"negative or out-of-range slot stores nothing",
|
||||
"bf16 k / v, E4M3 K and int4 V in groups of 32 with f16 scales, i32 slot mapping",
|
||||
RAD_DOMAIN_DEVICE, R4D_PRIO_ONLY,
|
||||
ROW(cKvStoreFp8v4), NOTUNE,
|
||||
nullptr, nullptr, r4d_rad_kv_store_fp8v4, nullptr, nullptr, nullptr,
|
||||
nullptr },
|
||||
|
||||
{ "kv_store_k6v4", "kv_store", "attn",
|
||||
"scatter k and v into a paged k6_v4 cache, one workgroup a token",
|
||||
"cache exactly [n_blocks, kv_heads, block_size, 22*head_dim/16]; head_dim a multiple of 32; a "
|
||||
"negative or out-of-range slot stores nothing",
|
||||
"bf16 k / v, int6 K and int4 V in groups of 32 with f16 scales, i32 slot mapping",
|
||||
RAD_DOMAIN_DEVICE, R4D_PRIO_ONLY,
|
||||
ROW(cKvStoreK6v4), NOTUNE,
|
||||
nullptr, nullptr, r4d_rad_kv_store_k6v4, nullptr, nullptr, nullptr,
|
||||
nullptr },
|
||||
|
||||
{ "logits_gemm_bf16", "logits_gemm", "vocab",
|
||||
"the lm_head projection: C[M, n_vocab] f32 = x[M, n_embd] @ lm_head[n_vocab, n_embd]^T",
|
||||
"any M and any vocabulary; n_embd a multiple of 8. One workgroup a vocabulary column",
|
||||
@@ -5005,6 +5055,24 @@ static const RadKernelInfo kKernels[] = {
|
||||
nullptr, nullptr, r4d_rad_attn_prefill_h256_fp8, nullptr, nullptr, nullptr,
|
||||
r4d_shape_attn_paged },
|
||||
|
||||
{ "attn_prefill_h256_gqa6_fp8v4", "attn_paged", "attn",
|
||||
"paged causal attention, query-tiled (prefill / chunked prefill)",
|
||||
"head_dim 256, 6 queries per kv head, paged block 16, varlen",
|
||||
"bf16 query, fp8_v4 kv cache (E4M3 K, int4 V in groups of 32 with f16 scales), bf16 out",
|
||||
RAD_DOMAIN_DEVICE, R4D_PRIO_ATTN_PREFILL,
|
||||
ROW(cAttnPrefillFp8v4), NOTUNE,
|
||||
nullptr, nullptr, r4d_rad_attn_prefill_h256_fp8v4, nullptr, nullptr, nullptr,
|
||||
nullptr },
|
||||
|
||||
{ "attn_prefill_h256_gqa6_k6v4", "attn_paged", "attn",
|
||||
"paged causal attention, query-tiled (prefill / chunked prefill)",
|
||||
"head_dim 256, 6 queries per kv head, paged block 16, varlen",
|
||||
"bf16 query, k6_v4 kv cache (int6 K, int4 V, each in groups of 32 with f16 scales), bf16 out",
|
||||
RAD_DOMAIN_DEVICE, R4D_PRIO_ATTN_PREFILL,
|
||||
ROW(cAttnPrefillK6v4), NOTUNE,
|
||||
nullptr, nullptr, r4d_rad_attn_prefill_h256_k6v4, nullptr, nullptr, nullptr,
|
||||
nullptr },
|
||||
|
||||
{ "attn_prefill_h256_gqa6_bf16kv", "attn_paged", "attn",
|
||||
"paged causal attention, query-tiled (prefill / chunked prefill)",
|
||||
"head_dim 256, 6 queries per kv head, paged block 16, varlen",
|
||||
@@ -5024,6 +5092,26 @@ static const RadKernelInfo kKernels[] = {
|
||||
nullptr, nullptr,
|
||||
r4d_shape_attn_paged },
|
||||
|
||||
{ "attn_decode_h256_gqa6_fp8v4", "attn_paged", "attn",
|
||||
"paged causal attention, split-KV (decode / speculative verify)",
|
||||
"head_dim 256, 6 queries per kv head, paged block 16, q_len*gqa <= 64",
|
||||
"bf16 query, fp8_v4 kv cache (E4M3 K, int4 V in groups of 32 with f16 scales), bf16 out, f16 partials",
|
||||
RAD_DOMAIN_DEVICE, R4D_PRIO_ATTN_DECODE,
|
||||
ROW(cAttnDecodeFp8v4), ROW(tAttnSplit), nullptr,
|
||||
nullptr, nullptr, r4d_rad_attn_decode_h256_fp8v4, r4d_rad_attn_decode_h256_v4_scratch,
|
||||
nullptr, nullptr,
|
||||
nullptr },
|
||||
|
||||
{ "attn_decode_h256_gqa6_k6v4", "attn_paged", "attn",
|
||||
"paged causal attention, split-KV (decode / speculative verify)",
|
||||
"head_dim 256, 6 queries per kv head, paged block 16, q_len*gqa <= 64",
|
||||
"bf16 query, k6_v4 kv cache (int6 K, int4 V, each in groups of 32 with f16 scales), bf16 out, f16 partials",
|
||||
RAD_DOMAIN_DEVICE, R4D_PRIO_ATTN_DECODE,
|
||||
ROW(cAttnDecodeK6v4), ROW(tAttnSplit), nullptr,
|
||||
nullptr, nullptr, r4d_rad_attn_decode_h256_k6v4, r4d_rad_attn_decode_h256_k6_scratch,
|
||||
nullptr, nullptr,
|
||||
nullptr },
|
||||
|
||||
{ "attn_decode_h256_gqa6_bf16kv", "attn_paged", "attn",
|
||||
"paged causal attention, split-KV (decode / speculative verify)",
|
||||
"head_dim 256, 6 queries per kv head, paged block 16, q_len*gqa <= 64",
|
||||
@@ -6820,6 +6908,28 @@ R4D_WIN_PAIR(512, 1) R4D_WIN_PAIR(512, 2) R4D_WIN_PAIR(512, 4) R4D_WIN_PAIR(512,
|
||||
nullptr, nullptr, r4d_rad_qk_norm_rope_gate_kv_store, nullptr, nullptr, nullptr,
|
||||
r4d_shape_qk_norm_rope_gate_kv_store, nullptr, r4d_fuse_qk_norm_rope_gate_kv_store },
|
||||
|
||||
/* The fold into an fp8_v4 cache: E4M3 K, int4 V in groups of 32 (a wave) with f16 scales. */
|
||||
{ "qk_norm_rope_gate_kv_store_fp8v4", "qk_norm_rope_gate_kv_store", "attn",
|
||||
"qk_norm_rope_gate, and kv_store into an fp8_v4 paged cache from the same launch",
|
||||
"qk_norm_rope_gate's shapes; cache exactly [n_blocks, kv_heads, block_size, 25*head_dim/16], "
|
||||
"head_dim a multiple of 32, one slot a row; a negative or out-of-range slot stores nothing",
|
||||
"bf16 in and out, E4M3 K and int4 V with f16 scales, i32 slot mapping",
|
||||
RAD_DOMAIN_DEVICE, R4D_PRIO_ONLY,
|
||||
ROW(cQkNormRopeGateKvFp8v4), NOTUNE,
|
||||
nullptr, nullptr, r4d_rad_qk_norm_rope_gate_kv_store_fp8v4, nullptr, nullptr, nullptr,
|
||||
nullptr },
|
||||
|
||||
/* The fold into a k6_v4 cache: int6 K and int4 V in groups of 32 (a wave) with f16 scales. */
|
||||
{ "qk_norm_rope_gate_kv_store_k6v4", "qk_norm_rope_gate_kv_store", "attn",
|
||||
"qk_norm_rope_gate, and kv_store into a k6_v4 paged cache from the same launch",
|
||||
"qk_norm_rope_gate's shapes; cache exactly [n_blocks, kv_heads, block_size, 22*head_dim/16], "
|
||||
"head_dim a multiple of 32, one slot a row; a negative or out-of-range slot stores nothing",
|
||||
"bf16 in and out, int6 K and int4 V with f16 scales, i32 slot mapping",
|
||||
RAD_DOMAIN_DEVICE, R4D_PRIO_ONLY,
|
||||
ROW(cQkNormRopeGateKvK6v4), NOTUNE,
|
||||
nullptr, nullptr, r4d_rad_qk_norm_rope_gate_kv_store_k6v4, nullptr, nullptr, nullptr,
|
||||
nullptr },
|
||||
|
||||
/* The same fold into a bf16 cache: the store is the two values the pair's kv_store_bf16 copies. */
|
||||
{ "qk_norm_rope_gate_kv_store_bf16", "qk_norm_rope_gate_kv_store", "attn",
|
||||
"qk_norm_rope_gate, and kv_store into a bf16 paged cache from the same launch",
|
||||
|
||||
+193
-16
@@ -860,17 +860,39 @@ static void attn_oracle_one(const uint16_t* qh, const uint8_t* kv, int kv_is_fp8
|
||||
int64_t head_dim, int64_t kv_heads, int64_t block_size, int64_t kh,
|
||||
const int* block_table, int64_t max_blocks, int ctx,
|
||||
float kds, float vds, int causal, int window, float scale,
|
||||
int64_t qlen, int64_t i, float* out) {
|
||||
int64_t qlen, int64_t i, float* out, int kvq = 0) {
|
||||
const int ew = kv_is_fp8 ? 1 : 2;
|
||||
const int64_t pos = ctx - qlen + i;
|
||||
const int64_t sb = kv_heads * block_size * 2 * head_dim;
|
||||
const int64_t sh = block_size * 2 * head_dim;
|
||||
const int64_t so = 2 * head_dim;
|
||||
/* kvq 1, fp8_v4: a token's row is head_dim E4M3 K bytes, then V's codes (dim d in the nibble
|
||||
* at bit 4 * (d % 2) of byte d / 2), then an f16 scale per 32 dims; V is (code ^ 8) - 8 times
|
||||
* it. kvq 2, k6_v4: K's low nibbles, its high-bit words, V's codes, K's scales, V's scales
|
||||
* (RAD_KVQ_K6V4 in abi/rad_builder.h); K is (code ^ 32) - 32 times its scale. */
|
||||
const int v4 = kvq != 0;
|
||||
const int64_t so = kvq == 2 ? 22 * head_dim / 16 : v4 ? 25 * head_dim / 16 : 2 * head_dim;
|
||||
const int64_t voff = kvq == 2 ? head_dim * 3 / 4 : head_dim;
|
||||
const int64_t vsoff = kvq == 2 ? head_dim * 21 / 16 : head_dim * 3 / 2;
|
||||
const auto f16at = [](const uint8_t* p) {
|
||||
uint16_t b; std::memcpy(&b, p, 2); return (float)__builtin_bit_cast(_Float16, b);
|
||||
};
|
||||
const int64_t sb = kv_heads * block_size * so;
|
||||
const int64_t sh = block_size * so;
|
||||
const auto kslot = [&](int64_t blk, int64_t slot, int64_t d) {
|
||||
if (kvq == 2) {
|
||||
const uint8_t* row = kv + blk * sb + kh * sh + slot * so;
|
||||
const int lo = (row[d / 2] >> (4 * (d & 1))) & 0xF;
|
||||
const int x = (int)(d % 16);
|
||||
const int hi = (row[head_dim / 2 + (d / 16) * 4 + (x & 7) / 2] >> ((x & 1) * 2 + (x >> 3) * 4)) & 3;
|
||||
return (float)(((lo | (hi << 4)) ^ 32) - 32) * f16at(row + head_dim * 5 / 4 + (d / 32) * 2);
|
||||
}
|
||||
const uint8_t* p = kv + (blk * sb + kh * sh + slot * so + d) * ew;
|
||||
return kv_is_fp8 ? h_e4m3(p[0]) : h_bf16(*(const uint16_t*)p);
|
||||
};
|
||||
const auto vslot = [&](int64_t blk, int64_t slot, int64_t d) {
|
||||
if (v4) {
|
||||
const uint8_t* row = kv + blk * sb + kh * sh + slot * so;
|
||||
const int nib = (row[voff + d / 2] >> (4 * (d & 1))) & 0xF;
|
||||
return (float)((nib ^ 8) - 8) * f16at(row + vsoff + (d / 32) * 2);
|
||||
}
|
||||
const uint8_t* p = kv + (blk * sb + kh * sh + slot * so + head_dim + d) * ew;
|
||||
return kv_is_fp8 ? h_e4m3(p[0]) : h_bf16(*(const uint16_t*)p);
|
||||
};
|
||||
@@ -922,19 +944,42 @@ static void attn_numeric_case(const Plugin& pl, const char* name, int64_t head_d
|
||||
const int64_t max_blocks = ctx / bs;
|
||||
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;
|
||||
const int v4 = kvq != 0;
|
||||
const uint32_t kvdt = kv_is_fp8 ? RAD_F8E4M3 : RAD_BF16;
|
||||
const int ew = kv_is_fp8 ? 1 : 2;
|
||||
const int64_t rowe = kvq == 2 ? 22 * head_dim / 16 : v4 ? 25 * head_dim / 16 : 2 * head_dim;
|
||||
const int64_t voff = kvq == 2 ? head_dim * 3 / 4 : head_dim;
|
||||
const int64_t vsoff = kvq == 2 ? head_dim * 21 / 16 : head_dim * 3 / 2;
|
||||
const float scale = 1.0f / std::sqrt((float)head_dim);
|
||||
|
||||
std::vector<uint16_t> qh((size_t)total_q * q_heads * head_dim);
|
||||
for (size_t i = 0; i < qh.size(); ++i) qh[i] = st_rand_bf16(i);
|
||||
const int64_t nelem = max_blocks * kv_heads * bs * 2 * head_dim;
|
||||
const int64_t nelem = max_blocks * kv_heads * bs * rowe;
|
||||
std::vector<uint8_t> kv((size_t)nelem * ew);
|
||||
for (int64_t e = 0; e < nelem; ++e) {
|
||||
const size_t bo = (size_t)e * ew;
|
||||
if (kv_is_fp8) kv[bo] = st_rand_e4m3(e);
|
||||
else { const uint16_t h = st_rand_bf16(e); kv[bo] = (uint8_t)h; kv[bo + 1] = (uint8_t)(h >> 8); }
|
||||
}
|
||||
if (v4) /* V: random codes, and scales from 1/64 to 1/4 so V spans about +-2 */
|
||||
for (int64_t r0 = 0; r0 < nelem; r0 += rowe) {
|
||||
uint8_t* row = &kv[(size_t)r0];
|
||||
for (int64_t b = 0; b < head_dim / 2; ++b)
|
||||
row[voff + b] = (uint8_t)(st_rand_e4m3(r0 * 7 + b) * 37u + (uint32_t)b);
|
||||
for (int64_t g = 0; g < head_dim / 32; ++g) {
|
||||
const _Float16 s = (_Float16)(0.015625f * (float)(1 + (st_rand_e4m3(r0 + g) & 15)));
|
||||
std::memcpy(row + vsoff + g * 2, &s, 2);
|
||||
}
|
||||
if (kvq == 2) { /* K: random codes, scales from 1/256 to 1/16 so K spans about +-2 */
|
||||
for (int64_t b = 0; b < head_dim * 3 / 4; ++b)
|
||||
row[b] = (uint8_t)(st_rand_e4m3(r0 * 5 + b + 11) * 29u + (uint32_t)(3 * b));
|
||||
for (int64_t g = 0; g < head_dim / 32; ++g) {
|
||||
const _Float16 s = (_Float16)(0.00390625f * (float)(1 + (st_rand_e4m3(r0 + g + 7) & 15)));
|
||||
std::memcpy(row + head_dim * 5 / 4 + g * 2, &s, 2);
|
||||
}
|
||||
}
|
||||
}
|
||||
const std::vector<int32_t> bt = iota_i32(max_blocks);
|
||||
|
||||
void* d_q = dalloc((size_t)total_q * q_heads * head_dim * 2);
|
||||
@@ -945,7 +990,7 @@ static void attn_numeric_case(const Plugin& pl, const char* name, int64_t head_d
|
||||
|
||||
std::vector<RadTensor> ts = {
|
||||
T(d_q, RAD_BF16, { total_q, q_heads, head_dim }),
|
||||
T(d_kv, kvdt, { max_blocks, kv_heads, bs, 2 * head_dim }),
|
||||
T(d_kv, kvdt, { max_blocks, kv_heads, bs, rowe }),
|
||||
TI32({ seqs, max_blocks }, bt),
|
||||
TI32({ seqs }, rep_i32(seqs, (int32_t)ctx)),
|
||||
TF32({ seqs, kv_heads }, 1.0f),
|
||||
@@ -973,7 +1018,7 @@ static void attn_numeric_case(const Plugin& pl, const char* name, int64_t head_d
|
||||
float o[512];
|
||||
attn_oracle_one(&qh[(tok * q_heads + h) * head_dim], kv.data(), kv_is_fp8,
|
||||
head_dim, kv_heads, bs, kh, bt.data(), max_blocks, ctx,
|
||||
1.0f, 1.0f, 1, window, scale, q_len, tok, o);
|
||||
1.0f, 1.0f, 1, window, scale, q_len, tok, o, kvq);
|
||||
for (int64_t d = 0; d < head_dim; ++d) {
|
||||
const size_t idx = (size_t)(tok * q_heads + h) * head_dim + d;
|
||||
oref[idx] = o[d];
|
||||
@@ -1621,17 +1666,19 @@ static void qk_prologue_case(const Plugin& pl, uint32_t gain_dt, bool yarn = fal
|
||||
* The inputs span the clamp (v up to +-2000, k normed and rotated), reaching E4M3's +-448
|
||||
* saturation and its subnormals. A pad row (slot -1) and a slot past the cache's end must store
|
||||
* nothing. The engine's geometry: 12 query heads and 2 kv heads of 256, rot 64. */
|
||||
static void qk_kv_fold_case(const Plugin& pl, bool bf16) {
|
||||
const char* row = bf16 ? "qk_norm_rope_gate_kv_store_bf16" : "qk_norm_rope_gate_kv_store_fp8";
|
||||
static void qk_kv_fold_case(const Plugin& pl, bool bf16, int kvq = 0) {
|
||||
const char* row = bf16 ? "qk_norm_rope_gate_kv_store_bf16"
|
||||
: kvq == 1 ? "qk_norm_rope_gate_kv_store_fp8v4"
|
||||
: kvq == 2 ? "qk_norm_rope_gate_kv_store_k6v4" : "qk_norm_rope_gate_kv_store_fp8";
|
||||
if (!sel(row)) return;
|
||||
const char* name = bf16
|
||||
? "qk_norm_rope_gate_kv_store_bf16 is byte-identical to the prologue + kv_store"
|
||||
: "qk_norm_rope_gate_kv_store_fp8 is byte-identical to the prologue + kv_store";
|
||||
const std::string nm_s = std::string(row) + " is byte-identical to the prologue + kv_store";
|
||||
const char* name = nm_s.c_str();
|
||||
const uint32_t cdt = bf16 ? RAD_BF16 : RAD_F8E4M3;
|
||||
const char* cname = bf16 ? "bf16" : "fp8_e4m3";
|
||||
const char* cname = bf16 ? "bf16" : kvq == 1 ? "fp8_v4" : kvq == 2 ? "k6_v4" : "fp8_e4m3";
|
||||
const RadKernelInfo* rk = find(pl, row);
|
||||
const RadKernelInfo* rf = find(pl, "qk_norm_rope_gate");
|
||||
const RadKernelInfo* rs = find(pl, bf16 ? "kv_store_bf16" : "kv_store_fp8");
|
||||
const RadKernelInfo* rs = find(pl, bf16 ? "kv_store_bf16" : kvq == 1 ? "kv_store_fp8v4"
|
||||
: kvq == 2 ? "kv_store_k6v4" : "kv_store_fp8");
|
||||
const RadKernelInfo* rt = find(pl, "rope_table_f32");
|
||||
if (!rk || !rf || !rs || !rt) { ok(name, false, "row absent"); return; }
|
||||
|
||||
@@ -1657,7 +1704,8 @@ static void qk_kv_fold_case(const Plugin& pl, bool bf16) {
|
||||
for (auto& x : hv) x = h_to_bf16(rnd(2000.0f));
|
||||
std::vector<float> fwq((size_t)hd), fwk((size_t)hd);
|
||||
for (int64_t d = 0; d < hd; ++d) { fwq[(size_t)d] = rnd(0.5f); fwk[(size_t)d] = rnd(0.5f); }
|
||||
const size_t cbytes = (size_t)(nb * nkv * bs * 2 * hd) * (bf16 ? 2 : 1);
|
||||
const int64_t crow = kvq == 2 ? 22 * hd / 16 : kvq ? 25 * hd / 16 : 2 * hd; /* a token's row */
|
||||
const size_t cbytes = (size_t)(nb * nkv * bs * crow) * (bf16 ? 2 : 1);
|
||||
std::vector<uint8_t> hc(cbytes);
|
||||
for (auto& x : hc) x = (uint8_t)(rnd(128.0f) + 128.0f);
|
||||
|
||||
@@ -1699,7 +1747,7 @@ static void qk_kv_fold_case(const Plugin& pl, bool bf16) {
|
||||
T(q, RAD_BF16, { M, nq * hd }), T(k, RAD_BF16, { M, nkv * hd }),
|
||||
RadTensor{} };
|
||||
};
|
||||
auto cache = [&](void* c) { return T(c, cdt, { nb, nkv, bs, 2 * hd }); };
|
||||
auto cache = [&](void* c) { return T(c, cdt, { nb, nkv, bs, crow }); };
|
||||
|
||||
/* ---- the pair ---- */
|
||||
{
|
||||
@@ -2735,6 +2783,121 @@ static void attn_gq_fold_case(const Plugin& pl, int64_t S, bool bf16, bool i8 =
|
||||
* A negative or out-of-range slot (a pad row) must store nothing: the cache is prefilled with a
|
||||
* sentinel and those slots must still hold it. Slot 0 is a real slot (as in libref and
|
||||
* r4d_gdn_conv_w4_h128_bf16.hip) and is used deliberately. */
|
||||
/* kv_store into the fp8_v4 cache (E4M3 K, int4 V in groups of 32 with an f16 scale, absmax / 7),
|
||||
* against values the format holds exactly: K as in kv_store_case, V an integer from -7 to 7 times
|
||||
* a per-head step, with a 7 or -7 in every group, so the scale is the step and every code decodes
|
||||
* back to the input. A pad slot and one past the cache must leave the sentinel row untouched. */
|
||||
static void kv_store_v4_case(const Plugin& pl, bool k6 = false) {
|
||||
const char* name = k6 ? "kv_store_k6v4" : "kv_store_fp8v4";
|
||||
if (!sel(name)) return;
|
||||
const RadKernelInfo* r = find(pl, name);
|
||||
if (!r) { ok(name, false, "row absent"); return; }
|
||||
const int64_t head_dim = 256, kv_heads = 2, block_size = 16, n_blocks = 4;
|
||||
const int64_t lane = kv_heads * head_dim, rowb = (k6 ? 22 : 25) * head_dim / 16;
|
||||
const int64_t voff = k6 ? head_dim * 3 / 4 : head_dim;
|
||||
const int64_t vsoff = k6 ? head_dim * 21 / 16 : head_dim * 3 / 2;
|
||||
/* k6_v4's K: an integer from -31 to 31 times a per-(token, head) step, with a 31 and a -31 in
|
||||
* every group, so the scale is the step and every code decodes back to the input. */
|
||||
auto k6int = [&](int64_t t, int64_t h, int64_t d) -> int {
|
||||
if (d % 32 == 3) return -31;
|
||||
if (d % 32 == 26) return 31;
|
||||
return (int)((t * 7 + h * 11 + d) % 63) - 31;
|
||||
};
|
||||
auto kstep = [&](int64_t t, int64_t h) { return 0.0625f * (float)(1 + (t + 2 * h) % 4); };
|
||||
const std::vector<int32_t> slots = { 0, 17, 63, -1, (int32_t)(n_blocks * block_size), 33 };
|
||||
const int64_t T_tok = (int64_t)slots.size();
|
||||
auto kval = [&](int64_t t, int64_t h, int64_t d) {
|
||||
static const float mag[8] = { 1.0f, 1.5f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 8.0f };
|
||||
return k6 ? (float)k6int(t, h, d) * kstep(t, h) : mag[(size_t)((t * 3 + h * 5 + d) % 8)];
|
||||
};
|
||||
auto vint = [&](int64_t t, int64_t h, int64_t d) -> int {
|
||||
if (d % 32 == 0) return -7;
|
||||
if (d % 32 == 17) return 7;
|
||||
return (int)((t * 5 + h * 3 + d) % 15) - 7;
|
||||
};
|
||||
auto step = [&](int64_t t, int64_t h) { return 0.125f * (float)(1 + (t + h) % 4); };
|
||||
std::vector<uint16_t> hk((size_t)T_tok * lane), hv((size_t)T_tok * lane);
|
||||
for (int64_t t = 0; t < T_tok; ++t)
|
||||
for (int64_t h = 0; h < kv_heads; ++h)
|
||||
for (int64_t d = 0; d < head_dim; ++d) {
|
||||
hk[(size_t)(t * lane + h * head_dim + d)] = h_to_bf16(kval(t, h, d));
|
||||
hv[(size_t)(t * lane + h * head_dim + d)] = h_to_bf16((float)vint(t, h, d) * step(t, h));
|
||||
}
|
||||
const size_t cbytes = (size_t)(n_blocks * kv_heads * block_size * rowb);
|
||||
void* d_k = dalloc((size_t)T_tok * lane * 2);
|
||||
void* d_v = dalloc((size_t)T_tok * lane * 2);
|
||||
void* d_sm = dalloc((size_t)T_tok * 4);
|
||||
void* d_c = dalloc(cbytes);
|
||||
if (!d_k || !d_v || !d_sm || !d_c) { ok(name, false, "hipMalloc failed"); return; }
|
||||
st_memcpy(d_k, hk.data(), (size_t)T_tok * lane * 2, hipMemcpyHostToDevice);
|
||||
st_memcpy(d_v, hv.data(), (size_t)T_tok * lane * 2, hipMemcpyHostToDevice);
|
||||
st_memcpy(d_sm, slots.data(), (size_t)T_tok * 4, hipMemcpyHostToDevice);
|
||||
st_memset(d_c, 0x7f, cbytes);
|
||||
std::vector<RadTensor> ts = {
|
||||
T(d_k, RAD_BF16, { T_tok, lane }),
|
||||
T(d_v, RAD_BF16, { T_tok, lane }),
|
||||
T(d_sm, RAD_I32, { T_tok }),
|
||||
T(d_c, RAD_F8E4M3, { n_blocks, kv_heads, block_size, rowb }),
|
||||
};
|
||||
std::vector<RadParam> ps = {
|
||||
PI("M", T_tok), PI("head_dim", head_dim), PI("n_head_kv", kv_heads),
|
||||
PI("block_size", block_size), PS("kv_dtype", k6 ? "k6_v4" : "fp8_v4"),
|
||||
};
|
||||
run_case(r, ts, ps);
|
||||
std::vector<uint8_t> got(cbytes);
|
||||
st_memcpy(got.data(), d_c, cbytes, hipMemcpyDeviceToHost);
|
||||
|
||||
int bad = 0;
|
||||
std::string first;
|
||||
std::vector<bool> live_row((size_t)(n_blocks * block_size), false);
|
||||
for (int64_t t = 0; t < T_tok; ++t) {
|
||||
const int32_t s = slots[(size_t)t];
|
||||
if (s < 0 || s / block_size >= n_blocks) continue;
|
||||
live_row[(size_t)s] = true;
|
||||
for (int64_t h = 0; h < kv_heads; ++h) {
|
||||
const uint8_t* row = &got[(size_t)(((s / block_size) * kv_heads + h) * block_size
|
||||
+ s % block_size) * rowb];
|
||||
for (int64_t d = 0; d < head_dim && bad < 4; ++d) {
|
||||
float k;
|
||||
if (k6) {
|
||||
const int lo = (row[d / 2] >> (4 * (d & 1))) & 0xF;
|
||||
const int x = (int)(d % 16);
|
||||
const int hi = (row[head_dim / 2 + (d / 16) * 4 + (x & 7) / 2]
|
||||
>> ((x & 1) * 2 + (x >> 3) * 4)) & 3;
|
||||
uint16_t kb;
|
||||
std::memcpy(&kb, row + head_dim * 5 / 4 + (d / 32) * 2, 2);
|
||||
k = (float)(((lo | (hi << 4)) ^ 32) - 32) * (float)__builtin_bit_cast(_Float16, kb);
|
||||
} else {
|
||||
k = h_e4m3(row[d]);
|
||||
}
|
||||
const int nib = (row[voff + d / 2] >> (4 * (d & 1))) & 0xF;
|
||||
uint16_t sb;
|
||||
std::memcpy(&sb, row + vsoff + (d / 32) * 2, 2);
|
||||
const float sc = (float)__builtin_bit_cast(_Float16, sb);
|
||||
const float v = (float)((nib ^ 8) - 8) * sc;
|
||||
const float wv = (float)vint(t, h, d) * step(t, h);
|
||||
if (k != kval(t, h, d) || v != wv || sc != step(t, h)) {
|
||||
if (!bad) first = fmt("t%lld h%lld d%lld: k %g (want %g) v %g (want %g) scale %g",
|
||||
(long long)t, (long long)h, (long long)d, k,
|
||||
kval(t, h, d), v, wv, sc);
|
||||
++bad;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
int touched = 0;
|
||||
for (int64_t s = 0; s < n_blocks * block_size; ++s) {
|
||||
if (live_row[(size_t)s]) continue;
|
||||
for (int64_t h = 0; h < kv_heads; ++h) {
|
||||
const uint8_t* row = &got[(size_t)(((s / block_size) * kv_heads + h) * block_size
|
||||
+ s % block_size) * rowb];
|
||||
for (int64_t b = 0; b < rowb; ++b) touched += row[b] != 0x7f;
|
||||
}
|
||||
}
|
||||
ok(name, bad == 0 && touched == 0,
|
||||
bad ? first : fmt("every live row decodes exactly; %d bytes of other rows touched", touched));
|
||||
}
|
||||
|
||||
static void kv_store_case(const Plugin& pl, const char* name, const char* kv_dtype) {
|
||||
if (!sel(name)) return;
|
||||
const RadKernelInfo* r = find(pl, name);
|
||||
@@ -18351,6 +18514,8 @@ static void device_phase(const Plugin& pl) {
|
||||
/* --- the paged cache's write side --- */
|
||||
kv_store_case(pl, "kv_store_bf16", "bf16");
|
||||
kv_store_case(pl, "kv_store_fp8", "fp8_e4m3");
|
||||
kv_store_v4_case(pl);
|
||||
kv_store_v4_case(pl, true);
|
||||
|
||||
/* --- attention --- */
|
||||
attn_case(pl, "attn_prefill_h256_gqa6_fp8kv", 256, 6, 32, "fp8_e4m3", 1);
|
||||
@@ -18388,6 +18553,16 @@ static void device_phase(const Plugin& pl) {
|
||||
* tstart = 961/48 = 20. This exercises the window mask and the below-window tile skip (tiles
|
||||
* 0..19 are never walked). */
|
||||
attn_numeric_case(pl, "attn_prefill_h256_gqa2_fp8kv", 256, 2, 64, "fp8_e4m3", 1024, 2048);
|
||||
/* fp8_v4 (Qwen3.8-27B's h256 gqa6): a prefill of several tiles over a longer history, a
|
||||
* ragged last tile, and decode and verify rows. */
|
||||
attn_numeric_case(pl, "attn_prefill_h256_gqa6_fp8v4", 256, 6, 64, "fp8_v4", 0, 256);
|
||||
attn_numeric_case(pl, "attn_prefill_h256_gqa6_fp8v4", 256, 6, 100, "fp8_v4", 0, 1024);
|
||||
attn_numeric_case(pl, "attn_decode_h256_gqa6_fp8v4", 256, 6, 1, "fp8_v4", 0, 1024);
|
||||
attn_numeric_case(pl, "attn_decode_h256_gqa6_fp8v4", 256, 6, 4, "fp8_v4", 0, 2048);
|
||||
attn_numeric_case(pl, "attn_prefill_h256_gqa6_k6v4", 256, 6, 64, "k6_v4", 0, 256);
|
||||
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);
|
||||
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);
|
||||
@@ -19841,6 +20016,8 @@ static void device_phase(const Plugin& pl) {
|
||||
qk_prologue_case(pl, RAD_F32, true, 128);
|
||||
qk_kv_fold_case(pl, false);
|
||||
qk_kv_fold_case(pl, true);
|
||||
qk_kv_fold_case(pl, false, 1);
|
||||
qk_kv_fold_case(pl, false, 2);
|
||||
qsa_qprep_case(pl);
|
||||
topk_scatter_case(pl);
|
||||
router_gemm_fused_case(pl);
|
||||
|
||||
Reference in new issue
Block a user