libr4d: decode tile arms for the int4/int5 x int8 GEMM

gemm_w4a8i8_tiled / gemm_w5a8i8_tiled ran one 256 x 64 tile, built for
2048-row prefill chunks. At decode row counts its time is the weight's
trip through the block and is flat in M (N=5120 K=17408: 272 us at
M=32, 284 at M=256): a 128-row step computes half padding, and N/64
blocks (80 at N=5120) leave most of an R9700 idle.

The kernel is now templated on its tile (TM x TN fragments a wave,
WM x WN waves a block), with the body unchanged, and the rows gain one
tunable axis, `arm`: 0 is the prefill tile (the old constants, so an
untuned install runs the same kernel), 1-8 are 128-, 64- and 32-row
tiles. tools/opshapes.cpp describes the int8-activation operands so
rad-tune can walk the axis, and r4d_selftest checks every arm against
the host reference for int4 and int5, with ragged M and a partitioned
weight; --perfwpa takes R4D_PERF_ARM.

Tuned on an R9700 at the trunk shapes of Qwen3.8-27B ParoQuant int5
(rad-tune, us, arm 0 -> the tuned arm):

  shape             M=32         M=64         M=128
  N=16384 K=5120    258 -> 72    229 -> 97    233 -> 151
  N=5120  K=6144    101 -> 38    101 -> 42    101 -> 62
  N=34816 K=5120    497 -> 217   494 -> 234   504 -> 309
  N=5120  K=17408   273 -> 89    275 -> 103   277 -> 168
  N=12288 K=5120    167 -> 52    167 -> 72    168 -> 112
  N=1024  K=5120     36 -> 21     36 -> 22     37 -> 24

The prefill band (M=2048) keeps arm 0. Which rows reach this kernel is
unchanged: the qwen35 plugin still hands the rotated int4/int5 linears
to it above 64 rows, so the M=32/64 columns only bound what a lower
handover could gain.

The int4 rows share the kernel and gain alike (int4 x int8 at M=128,
arm 0 -> best: N=5120 K=17408 267 -> 150 us, N=34816 K=5120 444 -> 248,
N=16384 K=5120 211 -> 118, N=12288 K=5120 160 -> 91).

End to end, Qwen3.8-27B ParoQuant int5 + DFlash2 on one R9700 (k6_v4
cache, f16 state, depth 3, 32 sequences of ~3k shared + ~2.5k unique
prompt, 3000 tokens out, so 128-row verify steps), against origin/main
on the same boot and tune cache: mean decode step 111.0 -> 93.8 ms,
decode 467 -> 614 tok/s, wall 345 -> 418, no preemptions. Decode at up
to 64 rows (one to eight sequences at depth 7) and prefill (arm 0) run
the same kernels as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019BxxGmKGSAyx2h88G3sEGp
This commit is contained in:
KeziandClaude Opus 5.5 authored and StillDeadcode committed 2026-11-11 22:41:02 +01:00
1 parent 7da9ccf38b
commit 60bcc30e5e
5 files changed
+96 -22

No files matched your search

+21
View File
@@ -193,6 +193,27 @@ int rad_operand_shapes(const std::string& op, const Geometry& g, int n_opd, int6
add((uint32_t)RAD_BF16, {M, N}); /* y */
add((uint32_t)RAD_F32, {K / group, (M + 15) / 16 * 16}); /* a_sum, group-major */
skip_opd(); /* b_ref */
} else if (q && (std::strcmp(q, "w5a8i8") == 0 || std::strcmp(q, "w4a8i8_asym") == 0)) {
/* The same grids against an int8 activation (libr4d/r4d_gemm_wpa8i8_tiled.hip):
* givens_dense_quant's int8 codes, parts side by side, with an f32 scale per (row,
* part, 128 of K). The weight is libr4d/r4d_layout.cpp's int4 / int5 form: a nibble
* plane of N*K/2 bytes, then (int5 only) a fifth-bit plane of N*K/8, and one dword per
* row and 128 of K holding the f16 scale and zero. Random bytes suffice for timing. */
if (N <= 0 || K <= 0 || group != 128 || (K % 128) || (N % 16))
return RAD_E_UNSUPPORTED;
const int64_t pb1 = gi("pb1", 0), pb2 = gi("pb2", 0);
const int64_t e1 = pb1 > 0 && pb1 < N ? pb1 : N;
int64_t e2 = pb2 > 0 && pb2 < N ? pb2 : N;
if (e2 < e1) e2 = e1;
const int64_t parts = e2 < N ? 3 : (e1 < N ? 2 : 1);
const bool five = std::strcmp(q, "w5a8i8") == 0;
add((uint32_t)RAD_I8, {M, parts * K}); /* a */
add((uint32_t)RAD_F32, {M, parts * (K / 128)}); /* a_scale */
add((uint32_t)RAD_U8, {N, five ? K * 5 / 8 : K / 2}); /* b */
add((uint32_t)RAD_U32, {N, K / group}); /* b_scale */
add((uint32_t)RAD_BF16, {M, N}); /* y */
skip_opd(); /* a_sum */
skip_opd(); /* b_ref */
} else {
const bool a8 = q && std::strcmp(q, "fp8a8") == 0;
const bool a16 = q && std::strcmp(q, "fp8a16") == 0;