libr4d: GLM-5.3's kernels -- mHC, KDA, the DSA indexer, MLA, f32-scaled FP8, MXFP4 experts

The device rows for GLM-5.3-Flash's ops, at its geometry and at TP4 and TP8 slices:

- mhc_pre / mhc_post / mhc_contract, and mhc_pre_partial / mhc_pre_finish, which run mhc_pre's
  own projection and finishing kernels with its chunk-sum order, so the pair is byte-identical
  to mhc_pre at n 4096, hc 4, chunk 2048.
- kda_update_k128_v128_bf16, one, two or four value rows a wave, both state forms.
- dsa_pool_fp8, dsa_query_fp8 and dsa_score_fp8 (fp8 WMMA, 32 heads of 128).
- mla_attn: a matrix row whose WMMA tile holds a query's heads, or pairs eight heads of two
  neighbouring queries over the union of their page tables, above the wave row; mla_project
  with up to eight rows a thread or wave.
- gemm_nt_f32 and router_sigmoid_topk in tiled and one-wave-a-row forms; gemm_fp8a8_f32scale
  and moe_gemm_fp8a8_f32scale read f32 block scales in place; moe_gemm_mxfp4a8 decodes e2m1 into
  exact E4M3 bytes for fp8 WMMA, with an expert-major decode form and a wide prefill form.
- swiglu_clamped, and the device-only fusions of GLM's MoE block: swiglu_clamped_quant_fp8,
  router_sigmoid_topk_scatter, and moe_scatter_inv / moe_gather_inv, which pass the gather's slot
  table from the sort instead of rebuilding it.

moe_gather takes f32 routing weights in its column-per-thread kernel. The sort and the gather
kernels move from r4d_moe.hip into r4d_moe_sort.h and r4d_moe_gather.h, so the fused router and
gather instantiate the same code.

Every form that replaces another gives its bytes: tests/r4d_emu runs the kernels on the host
(test_f32, test_fp8f32, test_kda, test_mhc, test_mla, test_moefuse, test_moemx), r4d_selftest
compares them on a card against libref and against each other, and plugin_test pins the
mla_attn band. docs/OPS.md states what libr4d serves.

Co-Authored-By: John McDonough <18016200+JohnCMcDonough@users.noreply.github.com>
This commit is contained in:
NunzeandJohn McDonough authored and StillDeadcode committed 2026-10-19 20:13:47 +02:00
1 parent da77f86332
commit 78c58841bf
31 files changed
+12371 -302

No files matched your search

+87 -1
View File
@@ -1,7 +1,7 @@
/* r4d_fuse.cpp -- the chain of unfused ops each fused kernel computes in one launch.
*
* `RadKernelInfo::replaces` (rad_abi.h) makes a fused row's claim to be byte-identical to the ops
* it replaces executable. Eleven of libr4d's fusions have no libref kernel, so this is their only
* it replaces executable. Most of libr4d's fusions have no libref kernel, so this is their only
* oracle: a caller runs the fused row against its chain built from this plugin's own unfused rows,
* and expects identical bytes, not a tolerance.
*
@@ -149,6 +149,16 @@ struct Step {
return *this;
}
Step& pf64(const char* key, double v) {
if (s->n_p >= RAD_MAX_FUSE_PARAMS) { ok = false; return *this; }
RadParam q{};
q.key = key;
q.kind = RAD_P_F64;
q.dval = v;
s->p[s->n_p++] = q;
return *this;
}
Step& pstr(const char* key, const char* v) {
if (!v || s->n_p >= RAD_MAX_FUSE_PARAMS) { ok = false; return *this; }
RadParam q{};
@@ -306,6 +316,82 @@ extern "C" int r4d_fuse_gated_quant_fp8(const RadParam* p, int n_p, uint64_t pre
}
}
/* ==========================================================================================
* swiglu_clamped_quant_fp8 -- GLM's clipped SwiGLU and the fp8 quantiser in one pass.
* (kernel: libr4d/r4d_glm.hip, swiglu_clamped_quant_kernel)
*
* Operands: [0] gate_up [1] q [2] scale [3] out_bf16?
*
* Each element is `swiglu_clamped_bf16`'s expression, rounded to bf16 once, and the codes are
* quant_act_fp8's over that rounded value, so the chain is the two rows with the product between.
*/
extern "C" int r4d_fuse_swiglu_clamped_quant_fp8(const RadParam* p, int n_p, uint64_t present,
int step, RadFuseStep* out) {
if (!out) return RAD_E_INVAL;
const int64_t M = pi(p, n_p, "M"), n = pi(p, n_p, "n");
if (!fp8_geometry_ok(p, n_p, M, n)) return RAD_E_UNSUPPORTED;
const int16_t mid = RAD_FUSE_HAS(present, 3) ? (int16_t)3 : RAD_FUSE_TMP(0);
switch (step) {
case 0: {
Step s(out, "swiglu_clamped");
s.mirror(p, n_p, "M").mirror(p, n_p, "n").mirror(p, n_p, "limit").mirror(p, n_p, "dtype");
return s.wire({ 0, mid }); /* gate_up, y */
}
case 1: {
Step s(out, "quant_act_fp8");
s.mirror(p, n_p, "M").mirror(p, n_p, "n").mirror(p, n_p, "group").mirror(p, n_p, "dtype");
return s.wire({ mid, 1, 2 }); /* x, q, scale */
}
default:
return RAD_E_NOTFOUND;
}
}
/* ==========================================================================================
* router_sigmoid_topk_scatter -- GLM's selection, the sort over its ids and the shared gate's fill.
* (kernel: libr4d/r4d_glm.hip, router_sigmoid_scatter_kernel)
*
* Operands: [0] logits [1] bias [2] ids [3] weights [4] sorted_tok [5] expert_offset
* [6] expert_count [7] gate? [8] inverse?
*
* The sort runs over this rank's slice, so `moe_scatter`'s `n_expert` is the fused op's `n_local`;
* with the inverse it is `moe_scatter_inv`. The fill steps in only with the gate.
*/
extern "C" int r4d_fuse_router_sigmoid_topk_scatter(const RadParam* p, int n_p, uint64_t present,
int step, RadFuseStep* out) {
if (!out) return RAD_E_INVAL;
const int64_t ne = pi(p, n_p, "n_expert"), nl = pi(p, n_p, "n_local", ne);
if (ne <= 0 || nl <= 0 || nl > ne) return RAD_E_UNSUPPORTED;
switch (step) {
case 0: {
Step s(out, "router_sigmoid_topk");
s.mirror(p, n_p, "M").mirror(p, n_p, "n_expert").mirror(p, n_p, "top_k")
.mirror(p, n_p, "norm").mirror(p, n_p, "route_scale").mirror(p, n_p, "dtype");
return s.wire({ 0, 1, 2, 3 }); /* logits, bias, ids, weights */
}
case 1: {
const bool inv = RAD_FUSE_HAS(present, 8) != 0;
Step s(out, inv ? "moe_scatter_inv" : "moe_scatter");
s.mirror(p, n_p, "M").pint("n_expert", nl).mirror(p, n_p, "top_k")
.mirror_opt(p, n_p, "expert_base").mirror_opt(p, n_p, "protect");
if (inv) return s.wire({ 2, 4, 5, 6, 8 }); /* expert_ids, sorted_tok, offset, count, inverse */
return s.wire({ 2, 4, 5, 6 }); /* expert_ids, sorted_tok, offset, count */
}
case 2: {
if (!RAD_FUSE_HAS(present, 7)) return RAD_E_NOTFOUND;
Step s(out, "fill");
s.mirror(p, n_p, "M").pint("n", 1).pstr("dtype", "bf16").pf64("value", 1.0);
return s.wire({ 7 }); /* y */
}
default:
return RAD_E_NOTFOUND;
}
}
/* ==========================================================================================
* gate_quant_fp8 -- the same epilogue with the gate in its own operand.
* (kernel: libr4d/r4d_fused_quant_fp8.hip, gate_quant_fp8_kernel)