libxe2: fast decode kernels for Qwen3.8-27B-PARO-M2XFP4 -- 0.1 -> 12.1 tok/s on the B65

- m2xfp4 GEMV (M <= 16): e2m1 decoded to fp16 by bits alone ((mag << 9) | sign << 15 is exactly
  e2m1 * 2^-14; the card keeps fp16 subnormals), K permuted (u, u + 4) inside each 8 so a code
  pair is one shift and mask, the activation as fp16(a * a_scale / R * 2^14) in that order, half2
  subgroup and group sums, f32 across groups; four columns a sub-group at decode. The scalar
  form was ALU-bound (~10 ops a weight, 80 ms a token); this one runs at 415-525 GB/s on the
  27B's shapes in a standalone probe. rad-kbench against libref on the container: rel_l2 ~5e-5 to
  ~3e-3, tolerance 4e-2.
- m2xfp4 prefill (M >= 17): the weight decoded to bf16 (exact) and the activation to
  bf16(e4m3 * scale), then the bf16 XMX GEMM per part.
- givens_dense_quant: a sub-group an output, the rotation row read coalesced, libref's double sum
  as a float pair (two-product and two-sum, ~48 bits); the double form stays for XE2_EXACT=1.
  146 -> 5.4 ms a token.
- the joined fp8 head (logits_gemm, M <= 16): E4M3 to fp16 by bits ((b & 0x7F) << 7 is E4M3 *
  2^-8), bytes (0, 2), (1, 3) as half pairs; an i8 head or a narrow scale block element by element.
  43 -> 3.9 ms.
- gated delta net, still bit-identical to libref: the decode step split over 16-column blocks
  (384 work-groups, not 48) with the gated norm in a second kernel and the anchor replay its own
  launch (block 0 rewrites the factors other blocks read); the chunked scan and the kkt solve
  work-group parallel; persistent staging (workspace slots) instead of a malloc and a wait a call.
- rmsnorm_quant_fp8: the row in local memory, loads issued before stores, the ordered sum fed by
  sub-group broadcasts -- still libref's chain (a probe puts that chain's floor at ~32 us for 5120).
- round_dt converts the float dtypes directly.

rad-kbench: 1410/1410 on the fixture; the 27B container's M <= 32 bands 77/77. Decode (greedy, no
speculation) 12.1 tok/s, coherent text; with the DFlash2 drafter 5.3 tok/s and the same text.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018EpFgqQidSnH8zo7boVZC1
This commit is contained in:
claudeandClaude Opus 5.5 committed 2026-10-24 03:22:48 +02:00
1 parent 3158324bdb
commit 15944be7e3
9 files changed
+1030 -35

No files matched your search

+29
View File
@@ -26,6 +26,12 @@ const RadConstraint cFp8Gated[] = { RAD_CIN("dtype", "fp8a8 fp8a8f32 fp8a16"), R
const RadConstraint cZpI8Dtype[] = { RAD_CIN("dtype", "w2a8 w4a8_asym") };
/* The M2XFP4 grid over givens_dense_quant's fp8 activation (xe2_gemm_m2.cpp). */
const RadConstraint cM2Dtype[] = { RAD_CIN("dtype", "m2xfp4") };
/* The fp8 head's decode band. */
const RadConstraint cHeadGemv[] = { RAD_CLE("M", 16) };
/* Its decode / verify band (xe2_gemv_m2.cpp). */
const RadConstraint cM2Gemv[] = { RAD_CIN("dtype", "m2xfp4"), RAD_CLE("M", 16) };
/* Its prefill band (bf16 XMX, xe2_gemv_m2.cpp). */
const RadConstraint cM2Xmx[] = { RAD_CIN("dtype", "m2xfp4"), RAD_CGE("M", 17) };
/* Rows by field name (C++20 designated initialisers must follow declaration order, so the compiler
* checks it), unlike a positional table where a shifted field can still compile. */
@@ -263,6 +269,22 @@ const RadKernelInfo kKernels[] = {
.domain = RAD_DOMAIN_DEVICE, .constraints = cZpI8Dtype, .n_constraints = 1,
.launch = xe2::gemm_nt_q_zp, .layout = xe2::gemm_zp_layout,
.relayout = xe2::gemm_zp_relayout, .unrelayout = xe2::gemm_zp_unrelayout },
{ .name = "xe2_gemm_nt_q_m2_gemv", .op = "gemm_nt_q", .family = "gemm",
.computes = "Y = A B^T over the M2XFP4 grid for M <= 16: e2m1 to fp16 by bits, fp16 activation "
"and subgroup sums, f32 group sums; libref's products regrouped",
.shape = "M <= 16; K a multiple of 32, group 32",
.dtypes = "m2xfp4 weight, E4M3 activation with a scale per 128 columns",
.domain = RAD_DOMAIN_DEVICE, .priority = 10, .constraints = cM2Gemv, .n_constraints = 2,
.launch = xe2::gemm_nt_q_m2_gemv, .layout = xe2::gemm_m2_layout,
.relayout = xe2::gemm_m2_relayout, .unrelayout = xe2::gemm_m2_unrelayout },
{ .name = "xe2_gemm_nt_q_m2_xmx", .op = "gemm_nt_q", .family = "gemm",
.computes = "Y = A B^T over the M2XFP4 grid for M >= 17: the weight decoded to bf16 (exact), "
"the activation to bf16(e4m3 * scale), then the bf16 XMX GEMM per part",
.shape = "M >= 17; K a multiple of 32, group 32",
.dtypes = "m2xfp4 weight, E4M3 activation with a scale per 128 columns",
.domain = RAD_DOMAIN_DEVICE, .priority = 5, .constraints = cM2Xmx, .n_constraints = 2,
.launch = xe2::gemm_nt_q_m2_xmx, .layout = xe2::gemm_m2_layout,
.relayout = xe2::gemm_m2_relayout, .unrelayout = xe2::gemm_m2_unrelayout },
{ .name = "xe2_gemm_nt_q_m2_ref", .op = "gemm_nt_q", .family = "gemm",
.computes = "Y = A B^T over the M2XFP4 grid against givens_dense_quant's fp8 activation, "
"libref's gemm_m2 per output; the correctness-first form",
@@ -300,6 +322,13 @@ const RadKernelInfo kKernels[] = {
.computes = "Y = A B^T + bias, then gelu / gelu_tanh and a residual; libref's order",
.shape = "any M, N, K", .dtypes = "float A and B",
.domain = RAD_DOMAIN_DEVICE, .launch = xe2::gemm_nt_bias },
{ .name = "xe2_logits_gemm_head_gemv", .op = "logits_gemm", .family = "vocab",
.computes = "the lm_head over a joined E4M3 / int8 head at M <= 16: a sub-group a column, "
"16 codes a lane step inside a scale block",
.shape = "M <= 16, n_embd a multiple of 128", .dtypes = "float A; E4M3 codes, float block scale",
.domain = RAD_DOMAIN_DEVICE, .priority = 10, .constraints = cHeadGemv, .n_constraints = 1,
.launch = xe2::logits_gemm_head_gemv, .layout = xe2::head_layout,
.relayout = xe2::head_relayout, .unrelayout = xe2::head_unrelayout },
{ .name = "xe2_logits_gemm_head_ref", .op = "logits_gemm", .family = "vocab",
.computes = "the lm_head over a head of E4M3 / int8 codes with a scale plane, joined; each "
"weight decoded to code * scale, then libref's plain product",