libr3d: register the fused gdn_conv_recurrent_update row

The kernel and launcher (libhalo's, r3dh_gdn_recurrent_update.hip) have
been in the tree since 1.16.0, but no row referenced them, so the op
always landed on libavx's CPU row. Register them:

- the op's schema (12 params, 18 operands: the convolution's inputs,
  then the recurrence's minus q, k and v, which the fused kernel never
  materialises), identical to libavx's and libref's;
- the constraint set: q_len >= 1, head_k 128, head_v 128, conv width 4;
- the row at R3D_PRIO_ONLY, NOTUNE (one kernel configuration), no
  layout hooks;
- the selftest case (hand-built, as the sibling GDN rows): T=4, Hg=2,
  H=4, K=V=128, window 4, state f32; o_q and o_scale absent;
- the README row, the table count (111 rows over 72 schemas) and the
  decode-path prose.

rad-schemas over the four libs: 244 shared ops, 0 disagree (the new
schema matches the canonical registries). gfx1102 (RX 7600):
r3d_selftest 118 rows / 0 failed, the new row runs with 8 output
planes finite; ref_test 153,146 checks / 294 cases / 0 failures.
This commit is contained in:
Asim Siddiqui authored and StillDeadcode committed 2026-11-05 09:49:06 +01:00
1 parent 87c604370d
commit 036bd8fec0
4 files changed
+167 -2

No files matched your search

+4 -2
View File
@@ -65,10 +65,10 @@ The table groups the rows by family; `r3d_rows.cpp` and `r3d.h` hold the full se
| `quant` | `quant_act_fp8_bf16`, `quant_act_i8_bf16`, `quant_act_i8g_bf16`, `had_quant_act_{i8,fp8}_bf16` and `had_quant_act_i8g_bf16` (the same with a block Walsh-Hadamard rotation), `gated_had_quant_{i8,i8g,fp8}_bf16`, `gelu_mul_quant_i8_bf16`, `quant_cols_fp8_bf16`, `rmsnorm_quant_{fp8,i8}_bf16` (norm and quantiser in one pass), `gated_quant_fp8_bf16`, `dequant_fp8`, `dequant_e5m2`, `dequant_i8` |
| `sample` | pre-filters `sample_{penalties,dry,ngram_ban,mask,logprobs}_f32`; spine `sample_{argmax,temp,topk,pick,topp,minp,typical,xtc}_f32` — f32 logits in and out, the draw the reference's in its order, the top-R scan single-pass (the plane read once) |
| `moe` | `expert_scale_bf16` (the renormalised top-k weight times the expert's per-expert scale, in place), `moe_router_topk_bf16` (softmax top-k, renormalised if `norm`), `moe_router_topk_scatter` (the top-k and the counting sort as one op), `moe_router_sigmoid_topk` (the GLM-style router: sigmoid scores, selection on scores + bias, the kept weights scaled by `route_scale`), `moe_scatter_small` (the routed slots' stable counting sort: one workgroup up to 1024 slots, three passes over 256-slot tiles above), `moe_gather` (fold the expert outputs back to the token, with the optional shared arm), `moe_gemm_q_e4m3` (grouped E4M3 GEMM; the `impl` axis: 16x16 table decode, 16x16 ALU decode, 16x128 block with the F32 scales folded), `moe_gemm_bf16` (unquantised experts, wave matrix), `moe_gemm_mxfp4a8` (canonical MXFP4 experts), `moe_gemm_w4a8` (int8 activation, packed two's-complement int4 experts: the i32 wave matrix, an f32 a-scale and a bf16 w-scale per (row, 128 of K), token- and sorted-order `a`), `moe_gemm_w4a4` (int4 activation, packed two's-complement int4 experts: both widen to int8 on the i32 wave matrix, an f32 a-scale and a bf16 w-scale per (row, 128 of K), token- and sorted-order `a`), `row_topk_{bf16,f32}` (the reference files the op under `moe`, so the row does too) |
| `gdn` | `gdn_kkt_solve_c64` (`A = (I + strict_lower(diag(beta) K K^T e^{g_i-g_j}))^-1` per 64-chunk; libhalo's kernel), `gdn_chunk_scan_k128_v128_c64` (the chunked scan: WY recompute, state recurrence and output in one kernel; libhalo's), `gdn_gated_rmsnorm_h128`, `gdn_gated_norm_had_quant_i8` (the gated norm fused with the rotation and quantiser), `gdn_conv_prep_w4_h128` (causal conv, split, l2norm, gating and the per-chunk gate cumsum in one prefill kernel), `gdn_conv_update_w4_h128` (the same convolution at decode over a rolling conv-state buffer), `gdn_recurrent_update_k128_v128` (the decode-time recurrent state update, per_candidate or anchor; libhalo's) |
| `gdn` | `gdn_kkt_solve_c64` (`A = (I + strict_lower(diag(beta) K K^T e^{g_i-g_j}))^-1` per 64-chunk; libhalo's kernel), `gdn_chunk_scan_k128_v128_c64` (the chunked scan: WY recompute, state recurrence and output in one kernel; libhalo's), `gdn_gated_rmsnorm_h128`, `gdn_gated_norm_had_quant_i8` (the gated norm fused with the rotation and quantiser), `gdn_conv_prep_w4_h128` (causal conv, split, l2norm, gating and the per-chunk gate cumsum in one prefill kernel), `gdn_conv_update_w4_h128` (the same convolution at decode over a rolling conv-state buffer), `gdn_recurrent_update_k128_v128` (the decode-time recurrent state update, per_candidate or anchor; libhalo's), `gdn_conv_recurrent_update_k128_v128` (the convolution and the recurrent update as one decode kernel, q/k/v never materialised; libhalo's) |
| `draft` | `dflash_select_bf16`: the drafter's accept step, a walk over the draft block's candidate lattice |
The table holds 110 rows over 71 schemas; `r3d_selftest` checks every schema slot for slot against
The table holds 111 rows over 72 schemas; `r3d_selftest` checks every schema slot for slot against
the `libref.so` beside the plugin. Fourteen rows carry tune axes (see
[Tunable params](#tunable-params)); the rest are single-implementation.
@@ -430,6 +430,8 @@ the qk l2norm, the gating and the per-chunk gate cumsum in one prefill kernel, a
`gdn_conv_update_w4_h128` / `gdn_recurrent_update_k128_v128` are the same convolution and the
recurrent state update at decode, over a rolling conv-state buffer and a paged f32 or f16 state
cache, per candidate token or as one committed state with a replay scratch slot (`anchor`).
`gdn_conv_recurrent_update_k128_v128` is the two as one kernel: the fold holds q, k and v in
registers, so they are never written and the step is one launch instead of two.
The QSA rows are the indexer's tail: `kv_len_trim_i32` trims each sequence's context lengths by
`trim` (the slice's own last row), and `qsa_tail_store_bf16` writes the QSA indexer's tail into a
+1
View File
@@ -187,6 +187,7 @@ int r3d_rad_gdn_conv_update(const RadArgs* a, RadStream s);
/* --- r3dh_gdn_recurrent_update.hip (libhalo's): the gated-delta-net recurrent state update at
* decode against the paged state cache, in either state form (per_candidate or anchor). */
extern "C" int r3dh_rad_gdn_recurrent_update(const RadArgs* a, RadStream s);
extern "C" int r3dh_rad_gdn_conv_recurrent_update(const RadArgs* a, RadStream s);
/* --- r3d_expert_scale_bf16.hip: each routing weight times its expert's own scale, in place. */
int r3d_rad_expert_scale_bf16(const RadArgs* a, RadStream s);
+46
View File
@@ -393,6 +393,32 @@ static const RadOperandSpec oGdnRecurrent[] = { { "q", RAD_OPD_IN, 0 },
{ "o_q", RAD_OPD_OUT, 1 },
{ "o_scale", RAD_OPD_OUT, 1 } };
/* gdn_conv_recurrent_update: the decode conv and the recurrent update as one kernel, the
* convolution's operands then the recurrence's minus q, k and v (never materialised). */
static const RadParamSpec pGdnConvRecurrent[] = { P_INT("q_len"), P_INT("head_k"), P_INT("head_v"),
P_INT("conv_width"), P_INTO("n_head_v"),
P_F64O("scale"), P_F64O("eps"), P_F64O("l2_eps"),
P_F64O("softplus_thr"), P_STRO("act"),
P_INTO("state_fp16"), P_STRO("state_form") };
static const RadOperandSpec oGdnConvRecurrent[] = { { "x", RAD_OPD_IN, 0 },
{ "conv_w", RAD_OPD_WEIGHT, 0 },
{ "conv_b", RAD_OPD_WEIGHT, 1 },
{ "conv_state", RAD_OPD_INOUT, 0 },
{ "conv_idx", RAD_OPD_IN, 0 },
{ "a", RAD_OPD_IN, 0 },
{ "b", RAD_OPD_IN, 0 },
{ "A_log", RAD_OPD_WEIGHT, 0 },
{ "dt_bias", RAD_OPD_WEIGHT, 0 },
{ "state", RAD_OPD_INOUT, 0 },
{ "state_idx", RAD_OPD_IN, 0 },
{ "num_accepted", RAD_OPD_IN, 0 },
{ "cu", RAD_OPD_IN, 0 },
{ "z", RAD_OPD_IN, 0 },
{ "norm_w", RAD_OPD_WEIGHT, 0 },
{ "o", RAD_OPD_OUT, 0 },
{ "o_q", RAD_OPD_OUT, 1 },
{ "o_scale", RAD_OPD_OUT, 1 } };
/* ================================================================== the vocab and the sampler
*
@@ -704,6 +730,11 @@ static const RadOpSchema kSchemas[] = {
"dt_bias), beta = sigmoid(b), q/k l2-normalised, S *= e^g, u = beta (v - S k), S += u "
"k^T, o = S q, per token against a paged fp32 state cache; the gated RMS norm is "
"folded in when `z` and `norm_w` are present."),
SCHEMA("gdn_conv_recurrent_update", pGdnConvRecurrent, oGdnConvRecurrent,
"gdn_conv_update followed by gdn_recurrent_update, with q, k and v never written: the "
"operands are the convolution's (`conv_idx` is its state_idx) and then the recurrence's "
"without those three."),
SCHEMA("expert_scale", pExpertScale, oExpertScale,
"Each routing weight times its expert's own per-expert scale, in place: the router's "
"renormalised top-k weight scaled by the [n_expert] table, an out-of-table id reading "
@@ -1268,6 +1299,8 @@ static const RadConstraint cGdnConvUpdW4[] = { C_GE("q_len", 1), C_EQ("head_k",
C_EQ("head_v", 128), C_EQ("conv_width", 4) };
static const RadConstraint cGdnRecurrent[] = { C_GE("q_len", 1), C_EQ("head_k", 128),
C_EQ("head_v", 128) };
static const RadConstraint cGdnConvRecurrent[] = { C_GE("q_len", 1), C_EQ("head_k", 128),
C_EQ("head_v", 128), C_EQ("conv_width", 4) };
static const RadConstraint cExpertScaleBf16[] = { C_IN("dtype", "bf16"), C_GE("top_k", 1),
C_LE("top_k", 64), C_GE("n_expert", 1) };
static const RadConstraint cMoeRouterTopkBf16[] = { C_IN("dtype", "bf16"), C_GE("top_k", 1),
@@ -2360,6 +2393,19 @@ static const RadKernelInfo kKernels[] = {
nullptr, nullptr, nullptr,
nullptr, nullptr },
{ "gdn_conv_recurrent_update_k128_v128", "gdn_conv_recurrent_update", "gdn",
"The gated-delta-net decode step as one kernel: the rolling-window conv and the recurrent state "
"update, with q, k and v never materialised",
"head_k 128, head_v 128, conv width 4; the speculative window (state_len_max - 2) must fit the "
"register buffer; the gated RMS norm is folded in (z and norm_w are required); the state cache "
"is f32 or f16 (libhalo's kernel, r3dh_gdn_recurrent_update.hip)",
"bf16 x and o, f32 a, b, A_log, dt_bias and norm_w, the conv state bf16, the state f32 or f16",
RAD_DOMAIN_DEVICE, R3D_PRIO_ONLY,
ROW(cGdnConvRecurrent), NOTUNE,
nullptr, nullptr, r3dh_rad_gdn_conv_recurrent_update,
nullptr, nullptr, nullptr,
nullptr, nullptr },
/* ------------------------------------------------------------------ r3d_gemm_fp8a16_nt_m1.hip
*
* The same shape against a block-scaled weight. The E4M3 codes are widened to f16 exactly and
+116
View File
@@ -2293,6 +2293,120 @@ static int build_gdn_recurrent(Case& c, const RadOpSchema* s) {
return 0;
}
/* gdn_conv_recurrent_update: the conv's inputs (as build_gdn_conv_update) then the recurrence's
* minus q, k and v (as build_gdn_recurrent), one geometry across both halves. */
static int build_gdn_conv_recurrent(Case& c, const RadOpSchema* s) {
const long long T = 4, Hg = 2, H = 4, K = 128, V = 128, W = 4, SL = 4, SLOTS = 2;
const long long units = 2 * Hg + H, XD = units * K;
if (s->n_operands != 18) { fail("gdn_conv_recurrent_update: the schema has %d operands, not 18",
s->n_operands); return 1; }
c.n_p = 0;
const RadParam pp[] = { { "q_len", RAD_P_INT, T, 0, nullptr, 0.0 },
{ "head_k", RAD_P_INT, K, 0, nullptr, 0.0 },
{ "head_v", RAD_P_INT, V, 0, nullptr, 0.0 },
{ "conv_width", RAD_P_INT, W, 0, nullptr, 0.0 },
{ "eps", RAD_P_F64, 0, 0, nullptr, 1e-6 } };
for (const RadParam& q : pp) c.p[c.n_p++] = q;
uint32_t st = 0x5C1D8E3Fu;
auto rndf = [&]() { st = st * 1664525u + 1013904223u;
return (float)((st >> 8) & 0xffffu) / 65536.0f; };
std::vector<uint16_t> hx(T * XD), hw(XD * W), hb(XD);
for (auto& x : hx) x = f32_to_bf16((rndf() - 0.5f) * 2.0f);
for (auto& x : hw) x = f32_to_bf16((rndf() - 0.5f) * 0.5f);
for (auto& x : hb) x = f32_to_bf16((rndf() - 0.5f) * 0.2f);
std::vector<uint16_t> hcs(XD * SL, 0);
for (auto& x : hcs) x = f32_to_bf16((rndf() - 0.5f) * 2.0f);
std::vector<uint16_t> ha(T * H), hbb(T * H), hz(T * H * V);
for (auto& x : ha) x = f32_to_bf16((rndf() - 0.5f) * 4.0f);
for (auto& x : hbb) x = f32_to_bf16((rndf() - 0.5f) * 4.0f);
for (auto& x : hz) x = f32_to_bf16((rndf() - 0.5f) * 4.0f);
std::vector<float> halog(H), hdtb(H), hstate(SLOTS * H * V * K), hnw(V);
for (auto& x : halog) x = logf(0.1f + rndf());
for (auto& x : hdtb) x = (rndf() - 0.5f) * 4.0f;
for (auto& x : hstate) x = (rndf() - 0.5f) * 0.1f;
for (auto& x : hnw) x = (rndf() - 0.5f) * 2.0f;
std::vector<uint16_t> ho(T * H * V, 0x7fc0u);
const int cidxv[1] = { 0 }, sidxv[T] = { 0, 0, 1, 1 };
/* num_accepted counts from 1 (docs/OPS.md): the window's history is read at column
* num_accepted - 1, so 1 is the handover out of a prefill. */
const int nav[1] = { 1 };
const int cuv[2] = { 0, (int)T };
struct { uint32_t dt; long long nel; } qd[18] = {
{ RAD_BF16, T * XD }, // x
{ RAD_BF16, XD * W }, // conv_w
{ RAD_BF16, XD }, // conv_b
{ RAD_BF16, XD * SL }, // conv_state [XD, 1, SL]
{ RAD_I32, 1 }, // conv_idx
{ RAD_BF16, T * H }, // a
{ RAD_BF16, T * H }, // b
{ RAD_F32, H }, // A_log
{ RAD_F32, H }, // dt_bias
{ RAD_F32, SLOTS * H * V * K }, // state
{ RAD_I32, T }, // state_idx [1, T]
{ RAD_I32, 1 }, // num_accepted
{ RAD_I32, 2 }, // cu
{ RAD_BF16, T * H * V }, // z
{ RAD_F32, V }, // norm_w
{ RAD_BF16, T * H * V }, // o
{ RAD_I8, 0 }, // o_q: absent
{ RAD_F32, 0 } // o_scale: absent
};
for (int o = 0; o < 16; ++o) {
const size_t bytes = (size_t)qd[o].nel * dt_bytes(qd[o].dt);
void* d = nullptr;
if (hipMalloc(&d, bytes) != hipSuccess) { fail("gdn_conv_recurrent: hipMalloc(%zu) failed",
bytes); return 1; }
c.dev.push_back(d);
switch (o) {
case 0: std::memcpy(d, hx.data(), bytes); break;
case 1: std::memcpy(d, hw.data(), bytes); break;
case 2: std::memcpy(d, hb.data(), bytes); break;
case 3: std::memcpy(d, hcs.data(), bytes); break;
case 4: std::memcpy(d, cidxv, sizeof cidxv); break;
case 5: std::memcpy(d, ha.data(), bytes); break;
case 6: std::memcpy(d, hbb.data(), bytes); break;
case 7: std::memcpy(d, halog.data(), bytes); break;
case 8: std::memcpy(d, hdtb.data(), bytes); break;
case 9: std::memcpy(d, hstate.data(), bytes); break;
case 10: std::memcpy(d, sidxv, sizeof sidxv); break;
case 11: std::memcpy(d, nav, sizeof nav); break;
case 12: std::memcpy(d, cuv, sizeof cuv); break;
case 13: std::memcpy(d, hz.data(), bytes); break;
case 14: std::memcpy(d, hnw.data(), bytes); break;
default: std::memcpy(d, ho.data(), bytes); break;
}
RadTensor& t = c.t[o];
t = RadTensor{};
t.data = d; t.dtype = qd[o].dt;
switch (o) {
case 0: t.rank = 2; t.shape[0] = T; t.shape[1] = XD; t.stride[1] = 1; t.stride[0] = XD; break;
case 1: t.rank = 2; t.shape[0] = XD; t.shape[1] = W; t.stride[1] = 1; t.stride[0] = W; break;
case 2: t.rank = 1; t.shape[0] = XD; t.stride[0] = 1; break;
case 3: t.rank = 3; t.shape[0] = XD; t.shape[1] = 1; t.shape[2] = SL;
t.stride[2] = 1; t.stride[1] = SL; t.stride[0] = SL; break;
case 4: t.rank = 1; t.shape[0] = 1; t.stride[0] = 1; break;
case 5: case 6: t.rank = 2; t.shape[0] = T; t.shape[1] = H; t.stride[1] = 1; t.stride[0] = H; break;
case 7: case 8: t.rank = 1; t.shape[0] = H; t.stride[0] = 1; break;
case 9: t.rank = 4; t.shape[0] = SLOTS; t.shape[1] = H; t.shape[2] = V; t.shape[3] = K;
t.stride[3] = 1; t.stride[2] = K; t.stride[1] = V * K; t.stride[0] = H * V * K; break;
case 10: t.rank = 2; t.shape[0] = 1; t.shape[1] = T; t.stride[1] = 1; t.stride[0] = T; break;
case 11: t.rank = 1; t.shape[0] = 1; t.stride[0] = 1; break;
case 12: t.rank = 1; t.shape[0] = 2; t.stride[0] = 1; break;
case 13: t.rank = 3; t.shape[0] = T; t.shape[1] = H; t.shape[2] = V;
t.stride[2] = 1; t.stride[1] = V; t.stride[0] = H * V; break;
case 14: t.rank = 1; t.shape[0] = V; t.stride[0] = 1; break;
default: t.rank = 3; t.shape[0] = T; t.shape[1] = H; t.shape[2] = V;
t.stride[2] = 1; t.stride[1] = V; t.stride[0] = H * V; break;
}
}
c.t[16] = RadTensor{}; // o_q absent
c.t[17] = RadTensor{}; // o_scale absent
return 0;
}
static int run_row(int i) {
const RadKernelInfo* k = row_at(i);
if (!k) return 1;
@@ -2319,6 +2433,7 @@ static int run_row(int i) {
: (0 == std::strcmp(k->name, "gdn_conv_prep_w4_h128")) ? 9
: (0 == std::strcmp(k->name, "gdn_conv_update_w4_h128")) ? 10
: (0 == std::strcmp(k->name, "gdn_recurrent_update_k128_v128")) ? 11
: (0 == std::strcmp(k->name, "gdn_conv_recurrent_update_k128_v128")) ? 23
: (0 == std::strcmp(k->name, "expert_scale_bf16")) ? 12
: (0 == std::strcmp(k->name, "moe_router_topk_bf16")) ? 13
: (0 == std::strcmp(k->name, "moe_scatter_small")) ? 14
@@ -2349,6 +2464,7 @@ static int run_row(int i) {
: hand == 9 ? build_gdn_conv_prep(c, s)
: hand == 10 ? build_gdn_conv_update(c, s)
: hand == 11 ? build_gdn_recurrent(c, s)
: hand == 23 ? build_gdn_conv_recurrent(c, s)
: hand == 12 ? build_expert_scale(c, s)
: hand == 13 ? build_moe_router_topk(c, s)
: hand == 14 ? build_moe_scatter(c, s)