unlimitedocr: the decoder plugin, the fused router ops and an h128 MHA attention leg

The text side of Unlimited-OCR: a 12-layer, 1280-wide decoder, 10 query heads over 10 KV
heads at head_dim 128, a dense layer 0 and 64 routed experts (top-6, softmax, no
renormalisation) plus one ungated shared arm in layers 1-11. arch/unlimitedocr_bf16/
carries its own plain-Llama bf16 attention, the shared MoE block in its bf16
external-input mode, and the tied LM head; data/recipes/unlimited-ocr-bf16.recipe is the
bf16 passthrough convert. core/format/checkpoint.cpp learns n_routed_experts, so n_expert
is read from the checkpoint's own key.

The router is issued as one op rather than a selection and a sort: libref gains
router_topk_scatter and router_gemm_topk_scatter (the second with the shared expert's
one-column gate at N = 1), libavx composes both from its router/gemm/scatter kernels, and
both get sweep geometries and the gate-absent/gate-present tests.

libr4d had no head_dim 128 leg at ratio 1, so every attn_paged band of this geometry
resolved to no kernel and the server refused to start on device.
r4d_attn_paged_h128_gqa1.hip instantiates the generic body at GQ 1 for both legs; the
rows and the selftest cases come with it.

Device numbers, one R9700 (gfx1201, HIP 7.16), bf16 weights and KV, no reference-kernel
fallback: a 523-token prompt prefills at 35.0-35.8k tok/s (15 ms) and a 4043-token prompt
at 54.2k tok/s (75 ms); decode is 351-354 tok/s (2.82-2.85 ms/token) at short context and
315 tok/s at 4k. r4d_selftest is 0 failures; the host suite is 86/87, the miss being
mm_test's lossless-webp decode, which fails on this box in code this change does not touch.
This commit is contained in:
Juup committed 2026-10-20 07:03:49 +00:00
1 parent 685ec5bd85
commit 543cba7416
20 files changed
+1179 -5

No files matched your search

+78
View File
@@ -147,6 +147,84 @@ int ref_moe_scatter(const RadArgs* a, RadStream) {
return RAD_OK;
}
/* ------------------------------------------------------------------ router_topk_scatter */
/* THE PAIR AS ONE OP, and the reference is the pair: the fused device kernel's whole claim is that
* it is byte-identical to these two launches, so the oracle that checks that claim is exactly
* these two, in order, over the operands the fused op carries. The selection runs over the whole
* router's `n_expert` logits; the sort runs over THIS RANK'S SLICE, which is the fused op's
* `n_local` standing where `moe_scatter`'s `n_expert` names the slice it owns. */
int ref_router_topk_scatter(const RadArgs* a, RadStream s) {
if (!have(a, 6)) return RAD_E_INVAL;
const int64_t ne = p_int(a, "n_expert", 0);
const int64_t nl = p_int(a, "n_local", ne);
const int64_t k = p_int(a, "top_k", 1);
if (ne <= 0 || k <= 0 || k > ne || nl <= 0 || nl > ne) return RAD_E_SHAPE;
const RadParam rp[] = { RAD_INT("n_expert", ne), RAD_INT("top_k", k),
RAD_INT("norm", p_int(a, "norm", 0)),
RAD_STR("dtype", p_str(a, "dtype", "bf16")) };
const RadArgs ra{ a->t, 3, rp, 4, nullptr, 0, nullptr, a->rank, a->world_size };
const int rc = ref_router_topk(&ra, s);
if (rc != RAD_OK) return rc;
/* The fused op's operands with `expert_w` removed -- the sort's input is the selection's
* output, named once, and `moe_scatter` takes it in operand slot 0. RadArgs reads a
* contiguous tensor array, so the four descriptors are laid out here, by value. */
const RadTensor t2[] = { a->t[1], a->t[3], a->t[4], a->t[5] };
const RadParam sp[] = { RAD_INT("n_expert", nl), RAD_INT("top_k", k),
RAD_INT("expert_base", p_int(a, "expert_base", 0)),
RAD_STR("protect", p_str(a, "protect", nullptr)) };
const RadArgs sa{ t2, 4, sp, 4, nullptr, 0, nullptr, a->rank, a->world_size };
return ref_moe_scatter(&sa, s);
}
/* ------------------------------------------------------------------ router_gemm_topk_scatter */
/* THE ROUTER GEMM IN FRONT OF THE PAIR, and with `gate_b`/`gate` present the shared expert's
* one-column gate over the same input. As with the pair, the reference IS the launches it stands
* in for: the router's `gemm_nt`, the gate's `gemm_nt` at N = 1, then `router_topk_scatter` over
* the logits -- the fused kernel's claim is byte-identity to exactly these. `gate_b` and `gate`
* come together or not at all, and the operand count says which. */
int ref_router_gemm_topk_scatter(const RadArgs* a, RadStream s) {
/* NOT `have`: the required operands are not a prefix -- `gate_b` sits between the router's
* weight and the outputs, and it and `gate` are null slots in the gate-absent form. The nine
* required slots are checked by name; the pair is checked to come together or not at all. */
if (!a || !a->t || a->n_t < 9) return RAD_E_INVAL;
const bool gate_b = a->n_t > 2 && a->t[2].data != nullptr;
const bool gate = a->n_t > 9 && a->t[9].data != nullptr;
if (gate_b != gate) return RAD_E_INVAL;
for (int i : {0, 1, 3, 4, 5, 6, 7, 8})
if (!a->t[i].data) return RAD_E_INVAL;
const int64_t ne = p_int(a, "n_expert", 0);
const int64_t nl = p_int(a, "n_local", ne);
const int64_t k = p_int(a, "top_k", 1);
const int64_t K = p_int(a, "K", 0);
if (ne <= 0 || k <= 0 || k > ne || nl <= 0 || nl > ne || K <= 0) return RAD_E_SHAPE;
const char* dt = p_str(a, "dtype", "bf16");
/* The router: a [M, K] x b [n_expert, K]^T -> logits. gemm_nt takes a, b, y contiguously. */
const RadTensor g3[] = { a->t[0], a->t[1], a->t[3] };
const RadParam gp[] = { RAD_INT("N", ne), RAD_INT("K", K), RAD_STR("dtype", dt) };
const RadArgs ga{ g3, 3, gp, 3, nullptr, 0, nullptr, a->rank, a->world_size };
const int gc = ref_gemm_nt(&ga, s);
if (gc != RAD_OK) return gc;
/* The shared expert's gate: the same arithmetic at N = 1, beside the router, over the same
* `a`. The pair was checked to come together. */
if (gate) {
const RadTensor p3[] = { a->t[0], a->t[2], a->t[9] };
const RadParam pp[] = { RAD_INT("N", 1), RAD_INT("K", K), RAD_STR("dtype", dt) };
const RadArgs pa{ p3, 3, pp, 3, nullptr, 0, nullptr, a->rank, a->world_size };
const int pc = ref_gemm_nt(&pa, s);
if (pc != RAD_OK) return pc;
}
/* The pair over the logits the GEMM just wrote. Every parameter the pair reads is a parameter
* of this op, under the same name -- the fused lists are unions, so `a`'s list passes through. */
const RadTensor t6[] = { a->t[3], a->t[4], a->t[5], a->t[6], a->t[7], a->t[8] };
const RadArgs fa{ t6, 6, a->p, a->n_p, nullptr, 0, nullptr, a->rank, a->world_size };
return ref_router_topk_scatter(&fa, s);
}
/* ------------------------------------------------------------------ moe_gemm, moe_gemm_q */
/* y[i, :] = a[row(i), :] @ w[e]^T, e the expert whose offset range contains i. `a_order` picks
* row(i): `sorted_tok[i] / top_k` for one activation row per token, i itself for one per
+4
View File
@@ -237,6 +237,8 @@ int ref_dflash_conv (const RadArgs*, RadStream);
/* ---- mixture of experts */
int ref_router_topk (const RadArgs*, RadStream);
int ref_moe_scatter (const RadArgs*, RadStream);
int ref_router_topk_scatter(const RadArgs*, RadStream);
int ref_router_gemm_topk_scatter(const RadArgs*, RadStream);
int ref_moe_gemm (const RadArgs*, RadStream);
int ref_moe_gemm_q (const RadArgs*, RadStream);
int ref_moe_gemm_q_mod4 (const RadArgs*, RadStream);
@@ -469,6 +471,8 @@ REF_SHAPE_FN(ref_dflash_conv);
REF_SHAPE_FN(ref_router_topk);
REF_SHAPE_FN(ref_moe_scatter);
REF_SHAPE_FN(ref_router_topk_scatter);
REF_SHAPE_FN(ref_router_gemm_topk_scatter);
REF_SHAPE_FN(ref_moe_gemm);
REF_SHAPE_FN(ref_moe_gemm_q);
REF_SHAPE_FN(ref_moe_gemm_q_mod4);
+39
View File
@@ -472,6 +472,29 @@ static const RadParamSpec pScatter[] = { P_INT("M"), P_INT("n_expert"), P_INT("t
static const RadOperandSpec oScatter[] = { OPD("expert_ids"), OUT("sorted_tok"),
OUT("expert_offset"), OUT("expert_count") };
/* THE PAIR AS ONE OP: the union of the two parameter lists, and their operands with the shared
* `expert_ids` named once, as an output. Same schema as libr4d's fused row. */
static const RadParamSpec pRouterTopkScatter[] = { P_INT("M"), P_INT("n_expert"),
P_INTO("n_local"), P_INT("top_k"),
P_INT("norm"), P_STR("dtype"),
P_INTO("expert_base"),
P_STRO("protect") };
static const RadOperandSpec oRouterTopkScatter[] = { OPD("logits"), OUT("expert_ids"),
OUT("expert_w"), OUT("sorted_tok"),
OUT("expert_offset"),
OUT("expert_count") };
/* THE ROUTER GEMM IN FRONT OF THE PAIR, and the gate's one-column GEMM beside it: the union of
* the three operand lists with `logits` named once, as the GEMM's output. */
static const RadParamSpec pRouterGemmTopkScatter[] = { P_INT("M"), P_INT("K"),
P_INT("n_expert"), P_INTO("n_local"),
P_INT("top_k"), P_INT("norm"),
P_STR("dtype"), P_INTO("expert_base"),
P_STRO("protect") };
static const RadOperandSpec oRouterGemmTopkScatter[] = {
OPD("a"), WGT("b"), WGT_O("gate_b"), OUT("logits"), OUT("expert_ids"), OUT("expert_w"),
OUT("sorted_tok"), OUT("expert_offset"), OUT("expert_count"), OUT_O("gate") };
/* `a_order` says which rows the activation has; a routed block's two grouped GEMMs differ. gate_up
* reads the block input, one row per token, so sorted row i gathers `a[sorted_tok[i] / top_k]`.
* down reads gate_up's output, already one row per (token, slot) in scatter order, so row i is row
@@ -1356,6 +1379,16 @@ static const RadOpSchema kSchemas[] = {
"sorts only its own slice, and the slots that went elsewhere simply contribute nothing to this "
"rank's partial -- which the block's end-of-block all-reduce then sums. `expert_base` absent "
"or 0 with n_expert the model's count is the whole-model case." },
{ "router_topk_scatter", ARR(pRouterTopkScatter), ARR(oRouterTopkScatter),
"`router_topk` then `moe_scatter`, in one launch: exactly those two and in that order, over "
"the operands they share. The reference IS the pair -- the fused device kernel claims to be "
"byte-identical to these two launches, and this is what it is measured against. The sort's "
"outputs are sized by `n_local`, this rank's slice; absent it is the router's count." },
{ "router_gemm_topk_scatter", ARR(pRouterGemmTopkScatter), ARR(oRouterGemmTopkScatter),
"`gemm_nt` of `a` against the router `b`, then `router_topk_scatter` over those logits, and "
"with `gate_b`/`gate` present the shared expert's `gemm_nt` at N = 1 over the same `a`. As "
"with the pair, the reference IS the launches: the gate weight and output come together or not "
"at all, and the operand count says which." },
{ "moe_gemm", ARR(pMoeGemm), ARR(oMoeGemm),
"y[i] = a[row(i)] @ w[e]^T in SORTED order, e being the expert whose offset range contains i. "
"`a_order` picks row(i): \"token\" gathers a[sorted_tok[i] / top_k] from an activation with one "
@@ -2100,6 +2133,12 @@ SIMPLE("ref_router_topk", "router_topk", "moe", "softmax then exact top-k routin
ref_router_topk),
SIMPLE("ref_moe_scatter", "moe_scatter", "moe", "stable counting sort of routing slots by expert",
"i32 indices", ref_moe_scatter),
SIMPLE("ref_router_topk_scatter", "router_topk_scatter", "moe",
"the selection and the sort in one op, run as exactly the pair", DT_FLOAT,
ref_router_topk_scatter),
SIMPLE("ref_router_gemm_topk_scatter", "router_gemm_topk_scatter", "moe",
"the router GEMM, the selection and the sort in one op, run as exactly the launches",
DT_FLOAT, ref_router_gemm_topk_scatter),
SIMPLE("ref_moe_gemm", "moe_gemm", "moe", "grouped GEMM over the expert-sorted rows", DT_FLOAT,
ref_moe_gemm),
ROW_CB("ref_moe_gemm_q", "moe_gemm_q", "moe",
+49 -2
View File
@@ -1653,8 +1653,55 @@ int ref_moe_scatter_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* o
return o.finish("moe_scatter", operand, out);
}
/* `a_order` decides the activation's row count: one per token, or one per (token, slot) in
* scatter order. */
/* The two shape lists concatenated, with `expert_ids` named ONCE as an output -- it is what the
* selection writes and what the sort reads. The sort's three outputs are sized by THIS RANK'S
* SLICE (`n_local`, absent means the slice is the router), exactly as in libr4d's shape. */
int ref_router_topk_scatter_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
const int64_t M = gi(p, n_p, "M", 0);
const int64_t ne = gi(p, n_p, "n_expert", 0);
const int64_t k = gi(p, n_p, "top_k", 0);
const int64_t nl = gi(p, n_p, "n_local", ne);
if (M <= 0 || ne <= 0 || k <= 0 || k > ne || nl <= 0 || nl > ne) return RAD_E_UNSUPPORTED;
const uint32_t D = act_dt(p, n_p);
Opd o;
o.t(D, { M, ne }); /* logits */
o.t((uint32_t)RAD_I32, { M, k }); /* expert_ids */
o.t(D, { M, k }); /* expert_w */
o.t((uint32_t)RAD_I32, { M * k }); /* sorted_tok */
o.t((uint32_t)RAD_I32, { nl + 1 }); /* expert_offset */
o.t((uint32_t)RAD_I32, { nl }); /* expert_count */
return o.finish("router_topk_scatter", operand, out);
}
/* The router GEMM's list, the pair's list, and -- with the gate present -- the gate's one-column
* list, with `logits` named once as the GEMM's output. `gate_b`/`gate` come together or not: the
* operand count carries it, as in libr4d's shape. */
int ref_router_gemm_topk_scatter_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
const int64_t M = gi(p, n_p, "M", 0);
const int64_t K = gi(p, n_p, "K", 0);
const int64_t ne = gi(p, n_p, "n_expert", 0);
const int64_t k = gi(p, n_p, "top_k", 0);
const int64_t nl = gi(p, n_p, "n_local", ne);
if (M <= 0 || K <= 0 || ne <= 0 || k <= 0 || k > ne || nl <= 0 || nl > ne)
return RAD_E_UNSUPPORTED;
const uint32_t D = act_dt(p, n_p);
Opd o;
o.t(D, { M, K }); /* a */
o.t(D, { ne, K }); /* b: the router */
o.t(D, { 1, K }); /* gate_b: the shared expert's gate */
o.t(D, { M, ne }); /* logits */
o.t((uint32_t)RAD_I32, { M, k }); /* expert_ids */
o.t(D, { M, k }); /* expert_w */
o.t((uint32_t)RAD_I32, { M * k }); /* sorted_tok */
o.t((uint32_t)RAD_I32, { nl + 1 }); /* expert_offset */
o.t((uint32_t)RAD_I32, { nl }); /* expert_count */
o.t(D, { M, 1 }); /* gate */
return o.finish("router_gemm_topk_scatter", operand, out);
}
/* `a_order` decides the activation's row count: one per token, or one per (token, slot) already
* in the scatter's order. A described case that got this wrong would build the operand at the
* extent the WRONG reading wants, and the fault it names would go unseen. */
static inline bool sorted_a_order(const RadParam* p, int n_p) {
const char* s = gs(p, n_p, "a_order");
return s && std::strcmp(s, "sorted") == 0;