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

+191
View File
@@ -66,6 +66,7 @@
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include "r4d_plugin.h"
#include "rad_sample.h"
@@ -364,6 +365,91 @@ extern "C" int r4d_shape_gemm_nt_bias(const RadParam* p, int n_p, int operand, R
R4D_PICK(l);
}
extern "C" int r4d_shape_moe_mxfp4(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
const int64_t M = pi(p, n_p, "M"), N = pi(p, n_p, "N"), K = pi(p, n_p, "K");
const int64_t N1 = rad_param_geti(p, n_p, "N_odd", N), K1 = rad_param_geti(p, n_p, "K_odd", K);
const int64_t ne = pi(p, n_p, "n_expert"), top = pi(p, n_p, "top_k");
const char* order = rad_param_gets(p, n_p, "a_order", "");
const int64_t ar = !std::strcmp(order, "sorted") ? M * top : M;
const int64_t nk = std::max(K, K1), nn = std::max(N, N1);
RadOpdDesc s0 = opd((uint32_t)RAD_U8, (ne + 1) / 2, N, K / 32);
RadOpdDesc s1 = opd((uint32_t)RAD_U8, ne / 2, N1, K1 / 32);
s0.fill = s1.fill = RAD_FILL_INDEX;
s0.idx_const = s1.idx_const = 127;
const RadOpdDesc l[] = { opd((uint32_t)RAD_F8E4M3, ar, nk),
opd((uint32_t)RAD_F32, ar, nk / 128),
opd((uint32_t)RAD_FP4E2M1, (ne + 1) / 2, N, K),
s0,
opd_idx(M * top, 0, M * top),
opd_idx(ne + 1, 0, M * top + 1),
opd((uint32_t)RAD_FP4E2M1, ne / 2, N1, K1),
s1,
opd((uint32_t)RAD_BF16, M * top, nn) };
R4D_PICK(l);
}
extern "C" int r4d_shape_kda_update(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
const char* form = rad_param_gets(p, n_p, "state_form", "");
if (!std::strcmp(form, "anchor")) return RAD_E_UNSUPPORTED;
const int64_t M = pi(p, n_p, "M"), H = pi(p, n_p, "heads");
const int64_t K = pi(p, n_p, "head_k"), V = pi(p, n_p, "head_v");
const uint32_t D = dt_act(p, n_p, "dtype");
const char* mode = rad_param_gets(p, n_p, "mode", "");
const int64_t history = !std::strcmp(mode, "verify") ? M : 1;
const RadOpdDesc l[] = {
opd(D, M, H, K), opd(D, M, H, K), opd(D, M, H, V), opd(D, M, H, K), opd(D, M, H),
opd((uint32_t)RAD_F32, H), opd((uint32_t)RAD_F32, H * K),
opd((uint32_t)RAD_F32, history + 1, H, V, K),
opd_idx(1, history, history + 1, -1, RAD_OPD_F_IDX_UNIQUE),
opd_idx(2, 0, M, -1, RAD_OPD_F_IDX_CU),
opd_idx(1, 0, 0, 1), opd_idx(1, 0, 0, 1), opd(D, M, H, V),
};
R4D_PICK(l);
}
extern "C" int r4d_shape_mhc_pre(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
const int64_t M = pi(p, n_p, "M"), n = pi(p, n_p, "n"), hc = pi(p, n_p, "hc");
const uint32_t D = dt_act(p, n_p, "dtype");
const RadOpdDesc l[] = { opd(D, M, hc * n), opd((uint32_t)RAD_F32, hc * (hc + 2), hc * n),
opd((uint32_t)RAD_F32, 3), opd((uint32_t)RAD_F32, hc * (hc + 2)), opd(D, M, n),
opd((uint32_t)RAD_F32, M, hc), opd((uint32_t)RAD_F32, M, hc * hc) };
R4D_PICK(l);
}
/* The partial matrix: per chunk the hc*(hc+2) coefficient partials and the sum of squares, padded to
* a multiple of four floats (docs/OPS.md). */
static int64_t mhc_part_stride(int64_t hc) { return (hc * (hc + 2) + 1 + 3) / 4 * 4; }
extern "C" int r4d_shape_mhc_pre_partial(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
const int64_t M = pi(p, n_p, "M"), n = pi(p, n_p, "n"), hc = pi(p, n_p, "hc");
const int64_t chunks = pi(p, n_p, "chunks");
const uint32_t D = dt_act(p, n_p, "dtype");
const RadOpdDesc l[] = { opd(D, M, hc * n), opd((uint32_t)RAD_F32, hc * (hc + 2), hc * n),
opd((uint32_t)RAD_F32, M, chunks * mhc_part_stride(hc)) };
R4D_PICK(l);
}
extern "C" int r4d_shape_mhc_pre_finish(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
const int64_t M = pi(p, n_p, "M"), n = pi(p, n_p, "n"), hc = pi(p, n_p, "hc");
const int64_t chunk = pi(p, n_p, "chunk");
const int64_t chunks = chunk > 0 ? hc * n / chunk : 0;
const uint32_t D = dt_act(p, n_p, "dtype");
const RadOpdDesc l[] = { opd((uint32_t)RAD_F32, M, chunks * mhc_part_stride(hc)),
opd(D, M, hc * n), opd((uint32_t)RAD_F32, 3), opd((uint32_t)RAD_F32, hc * (hc + 2)),
opd(D, M, n), opd((uint32_t)RAD_F32, M, hc), opd((uint32_t)RAD_F32, M, hc * hc) };
R4D_PICK(l);
}
extern "C" int r4d_shape_mhc_post(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
const int64_t M = pi(p, n_p, "M"), n = pi(p, n_p, "n"), hc = pi(p, n_p, "hc");
const uint32_t D = dt_act(p, n_p, "dtype");
const RadOpdDesc l[] = { opd(D, M, n), opd(D, M, hc * n), opd((uint32_t)RAD_F32, M, hc),
opd((uint32_t)RAD_F32, M, hc * hc), opd(D, M, hc * n) };
R4D_PICK(l);
}
extern "C" int r4d_shape_mhc_contract(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
const int64_t M = pi(p, n_p, "M"), n = pi(p, n_p, "n"), hc = pi(p, n_p, "hc");
const uint32_t D = dt_act(p, n_p, "dtype");
const RadOpdDesc l[] = { opd(D, M, hc * n), opd(D, M, n) };
R4D_PICK(l);
}
/* ---- the gated residual (r4d_hc_bf16.hip's three shims). `inject` and `group` default on, so a
* query naming neither describes what every trunk connection runs; 0 for either describes one of
* the two exceptions: the final mixer, and a read whose consumer wants bf16. */
@@ -496,6 +582,83 @@ extern "C" int r4d_shape_silu_mul(const RadParam* p, int n_p, int operand,
const RadOpdDesc l[] = { opd(D, M, 2 * n), opd(D, M, n) };
R4D_PICK(l);
}
extern "C" int r4d_shape_router_sigmoid_topk(const RadParam* p, int n_p, int operand,
RadOpdDesc* out) {
const int64_t M = pi(p, n_p, "M"), ne = pi(p, n_p, "n_expert"), k = pi(p, n_p, "top_k");
const uint32_t D = dt_act(p, n_p, "dtype");
const RadOpdDesc l[] = { opd(D, M, ne), opd((uint32_t)RAD_F32, ne), opd_idx(M, k, ne),
opd(D, M, k) };
R4D_PICK(l);
}
extern "C" int r4d_shape_router_sigmoid_topk_scatter(const RadParam* p, int n_p, int operand,
RadOpdDesc* out) {
const int64_t M = pi(p, n_p, "M"), ne = pi(p, n_p, "n_expert"), k = pi(p, n_p, "top_k");
const int64_t nl = pi(p, n_p, "n_local", ne);
if (ne <= 0 || ne > 512 || k <= 0 || k > ne || nl <= 0 || nl > ne || M * k > 1024)
return RAD_E_UNSUPPORTED;
const uint32_t D = dt_act(p, n_p, "dtype");
const RadOpdDesc l[] = { opd(D, M, ne),
opd((uint32_t)RAD_F32, ne),
opd_idx(M, k, ne),
opd(D, M, k),
opd((uint32_t)RAD_I32, M * k),
opd((uint32_t)RAD_I32, nl + 1),
opd((uint32_t)RAD_I32, nl),
opd((uint32_t)RAD_BF16, M, 1),
opd((uint32_t)RAD_I32, M * k) };
R4D_PICK(l);
}
extern "C" int r4d_shape_dsa_pool(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
const int64_t M = pi(p, n_p, "M"), n = pi(p, n_p, "n"), ratio = pi(p, n_p, "ratio");
const RadOpdDesc l[] = { opd((uint32_t)RAD_BF16, M * ratio, 2 * n),
opd((uint32_t)RAD_F32, ratio, n),
opd((uint32_t)RAD_F8E4M3, M, n),
opd((uint32_t)RAD_F32, M, 1),
opd_none(),
opd_none() };
R4D_PICK(l);
}
extern "C" int r4d_shape_dsa_query(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
const int64_t M = pi(p, n_p, "M"), n = pi(p, n_p, "n");
const RadOpdDesc l[] = { opd((uint32_t)RAD_BF16, M, n), opd((uint32_t)RAD_F8E4M3, M, n),
opd((uint32_t)RAD_F32, M, 1) };
R4D_PICK(l);
}
extern "C" int r4d_shape_mla_project(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
const int64_t M = pi(p, n_p, "M"), h = pi(p, n_p, "heads"), d = pi(p, n_p, "head_dim"),
l = pi(p, n_p, "latent_dim");
const char* mode = r4d_pgets(p, n_p, "mode");
const bool absorb = mode && !std::strcmp(mode, "absorb"),
expand = mode && !std::strcmp(mode, "expand");
if (!absorb && !expand) return RAD_E_UNSUPPORTED;
const RadOpdDesc list[] = { opd((uint32_t)RAD_BF16, M, h, absorb ? d : l),
opd((uint32_t)RAD_BF16, h * 2 * d, l),
opd((uint32_t)RAD_BF16, M, h, absorb ? l : d) };
R4D_PICK(list);
}
extern "C" int r4d_shape_dsa_score(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
const int64_t M = pi(p, n_p, "M"), n = pi(p, n_p, "n"), H = pi(p, n_p, "heads");
const int64_t B = pi(p, n_p, "blocks"), S = pi(p, n_p, "seqs"), pages = S * B;
const RadOpdDesc l[] = { opd((uint32_t)RAD_F8E4M3, M, H, n),
opd((uint32_t)RAD_F32, M, H),
opd((uint32_t)RAD_F32, M, H),
opd((uint32_t)RAD_F8E4M3, pages, n),
opd((uint32_t)RAD_F32, pages),
opd_idx(S, B, pages),
opd_idx(S + 1, 0, M, -1, RAD_OPD_F_IDX_CU),
opd_idx(S, 0, 0, B),
opd((uint32_t)RAD_F32, M, B) };
R4D_PICK(l);
}
extern "C" int r4d_shape_mla_attn(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
const int64_t M = pi(p, n_p, "M"), D = pi(p, n_p, "latent_dim"), H = pi(p, n_p, "heads");
const int64_t B = pi(p, n_p, "blocks"), bs = pi(p, n_p, "block_size"),
pages = std::max((int64_t)2, B * M);
const uint32_t dt = dt_act(p, n_p, "dtype");
const RadOpdDesc l[] = { opd(dt, M, H, D), opd(dt, pages, 1, bs, D), opd_idx(M, B, pages),
opd_idx(M, 0, 0, B * bs), opd(dt, M, H, D) };
R4D_PICK(l);
}
extern "C" int r4d_shape_rope(const RadParam* p, int n_p, int operand,
RadOpdDesc* out) {
@@ -1020,6 +1183,12 @@ extern "C" int r4d_shape_ar_rmsnorm_quant_fp8(const RadParam* p, int n_p, int op
* r4d_rowtopk_bf16.hip).
* ========================================================================================= */
extern "C" int r4d_shape_gemm_nt_f32(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
const int64_t M = pi(p, n_p, "M"), N = pi(p, n_p, "N"), K = pi(p, n_p, "K");
const RadOpdDesc l[] = { opd((uint32_t)RAD_BF16, M, K), opd((uint32_t)RAD_F32, N, K),
opd((uint32_t)RAD_F32, M, N) };
R4D_PICK(l);
}
extern "C" int r4d_shape_gemm_nt(const RadParam* p, int n_p, int operand,
RadOpdDesc* out) {
@@ -1243,6 +1412,22 @@ extern "C" int r4d_shape_moe_scatter(const RadParam* p, int n_p, int operand, Ra
R4D_PICK(l);
}
extern "C" int r4d_shape_moe_scatter_inv(const RadParam* p, int n_p, int operand,
RadOpdDesc* out) {
const int64_t M = pi(p, n_p, "M");
const int64_t ne = pi(p, n_p, "n_expert");
const int64_t k = pi(p, n_p, "top_k");
if (ne <= 0 || ne > 512 || k <= 0) return RAD_E_UNSUPPORTED;
const RadOpdDesc l[] = {
opd_idx(M, k, ne, -1),
opd((uint32_t)RAD_I32, M * k),
opd((uint32_t)RAD_I32, ne + 1),
opd((uint32_t)RAD_I32, ne),
opd((uint32_t)RAD_I32, M * k),
};
R4D_PICK(l);
}
extern "C" int r4d_shape_moe_gemm_q(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
const int64_t M = pi(p, n_p, "M");
const int64_t N0 = pi(p, n_p, "N"), K0 = pi(p, n_p, "K");
@@ -1339,6 +1524,12 @@ extern "C" int r4d_shape_moe_gather(const RadParam* p, int n_p, int operand, Rad
R4D_PICK(l);
}
/* moe_gather's list with the slot table in place of `sorted_tok`: the same draw, since a valid
* table, like a valid sort, holds distinct rows of `y_expert` (and -1 for a dropped slot). */
extern "C" int r4d_shape_moe_gather_inv(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
return r4d_shape_moe_gather(p, n_p, operand, out);
}
/* row_topk -- exact per-row top-R of a wide bf16 matrix (shim in r4d_rowtopk_bf16.hip). R is at
* most 32, stage 2's PT. */
extern "C" int r4d_shape_row_topk(const RadParam* p, int n_p, int operand,