libr4d: opt-in fp8 QK and PV legs for the h256/gqa6 prefill attention (R4D_ATTN_FP8)

Ports the 8-bit prefill legs of radiance-vllm-mxfp4's r4d_radiance_extras.patch (commit afa21b5
there) to the h256/gqa6 prefill kernel as two separable bits, off by default:

- O_QK8 (32): Q is quantised to e4m3 once at load (after the folded scale), K stays raw bytes in
  LDS, and Q.K^T runs as fp8 WMMA.
- O_PV8 (64): P is packed to e4m3, V stays raw bytes in LDS, and P.V runs as fp8 WMMA. PGROW
  tightens to 8 octaves (e4m3 max 448) and DOT2 is off.

Both need CONTIG and are compiled out for bf16 KV. Byte tiles halve the K/V LDS footprint.
prefill_launch reads R4D_ATTN_FP8 once: 0 = f16 (unchanged), 1 = QK8, 2 = PV8, 3 = both.

Measured with radiance 1.9.0 on 2x R9700 (230 W), Qwen3.8-27B MXFP4, TP 2, fp8 KV,
--tp-wire wht6:
- kernel, ctx 200K, chunk 6592: 161.8 ms -> 113.9 ms with 3 (x1.42); per-row relative error
  against fp32 1.7e-3 -> 3.1e-2
- prompt throughput at 200K: 2,621 -> 3,082-3,127 t/s; at 25K: 4,746 -> 4,861-5,011 t/s
- --kld-ref against the unpatched build: R4D_ATTN_FP8=0 is bit-identical (KL 0 on 10,198
  positions); 3 gives mean KL 0.0076, p99.9 0.94, max 14.3, top-1 98.4 %, PPL x1.0024
- decode and DFlash2 acceptance unchanged (the decode kernel keeps its f16 legs)
This commit is contained in:
Marc Siebenhaar authored and StillDeadcode committed 2026-10-21 09:46:23 +02:00
1 parent fa4a7a75a4
commit cdc0ae26df
2 files changed
+95 -13

No files matched your search

+20 -3
View File
@@ -19,6 +19,7 @@
#include "r4d_attn_args.h"
#include "r4d_common.h"
#include <cmath>
#include <cstdlib>
#include <algorithm>
using std::max;
#include "r4d_attn_prefill_h256_gqa6.hip"
@@ -56,9 +57,25 @@ static int prefill_launch(const R4DArgs* a, hipStream_t s) {
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 | XOPT>),
grid, dim3(P_NWARPS * 32), 0, s, *a);
// R4D_ATTN_FP8: 0 = f16 (default), 1 = O_QK8, 2 = O_PV8, 3 = both; fp8 KV without V4/K6 only.
int fp8mode = 0;
if constexpr (KVP == 0 && XOPT == 0) {
static const int m = [] { const char* e = getenv("R4D_ATTN_FP8"); return e ? atoi(e) : 0; }();
fp8mode = m;
}
#define P_LAUNCH(OPTX) \
hipLaunchKernelGGL((r4d_attn_prefill_kernel<P_NWARPS, P_TILE, A_HEAD_DIM, A_GQA, A_BLOCK_SIZE, \
KVP, OPTX>), \
grid, dim3(P_NWARPS * 32), 0, s, *a)
if constexpr (KVP == 0 && XOPT == 0) {
if (fp8mode == 3) P_LAUNCH(P_OPT | O_QK8 | O_PV8);
else if (fp8mode == 2) P_LAUNCH(P_OPT | O_PV8);
else if (fp8mode == 1) P_LAUNCH(P_OPT | O_QK8);
else P_LAUNCH(P_OPT | XOPT);
} else {
P_LAUNCH(P_OPT | XOPT);
}
#undef P_LAUNCH
CHK();
return 0;
}