Archived
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:
1 parent
685ec5bd85
commit
543cba7416
20 files changed
+1179
-5
No files matched your search
@@ -557,6 +557,18 @@ std::vector<Case> build_cases() {
|
||||
HS("dtype", dt) });
|
||||
add("router_topk", { HI("M", 9), HI("n_expert", 8), HI("top_k", 2), HI("norm", 0),
|
||||
HS("dtype", dt) });
|
||||
/* The pair and the pair behind the router GEMM: the whole-model form and a rank's slice
|
||||
* with a nonzero expert base, since the sort's slicing is the part the fuse adds. */
|
||||
add("router_topk_scatter", { HI("M", 9), HI("n_expert", 8), HI("top_k", 2),
|
||||
HI("norm", 1), HS("dtype", dt) });
|
||||
add("router_topk_scatter", { HI("M", 9), HI("n_expert", 8), HI("n_local", 4),
|
||||
HI("top_k", 2), HI("norm", 0), HI("expert_base", 2),
|
||||
HS("dtype", dt) });
|
||||
add("router_gemm_topk_scatter", { HI("M", 9), HI("K", 128), HI("n_expert", 8),
|
||||
HI("top_k", 2), HI("norm", 0), HS("dtype", dt) });
|
||||
add("router_gemm_topk_scatter", { HI("M", 9), HI("K", 128), HI("n_expert", 8),
|
||||
HI("n_local", 4), HI("top_k", 2), HI("norm", 1),
|
||||
HI("expert_base", 2), HS("dtype", dt) });
|
||||
add("moe_gather", { HI("M", 9), HI("n", 128), HI("top_k", 2), HS("dtype", dt) });
|
||||
/* The shared-expert fold: `shared`/`shared_gate` are always described, so always
|
||||
* present; this case uses sigmoid, the one above "none". */
|
||||
|
||||
+2
-1
@@ -106,7 +106,8 @@ int avx_par_force(int threads);
|
||||
/* ---- mixture of experts */ \
|
||||
X(router_topk) X(moe_scatter) X(moe_gemm) X(moe_gemm_q) \
|
||||
X(moe_gather) X(row_topk) X(row_topk_merge) X(logit_rerank) \
|
||||
X(moe_gemm_q_mod4) X(pair_argmax) \
|
||||
X(moe_gemm_q_mod4) X(pair_argmax) \
|
||||
X(router_topk_scatter) X(router_gemm_topk_scatter) \
|
||||
/* ---- gated delta net */ \
|
||||
X(gdn_conv_prep) X(gdn_conv_update) X(gdn_kkt_solve) X(gdn_chunk_scan) \
|
||||
X(gdn_recurrent_update) X(gdn_conv_recurrent_update) X(gdn_gated_rmsnorm) \
|
||||
|
||||
@@ -1218,10 +1218,14 @@ AVX_KERNEL(moe_gemm_q_mod4) { return moe_gemm_impl(a, true, 4); }
|
||||
/* y[t] = sum over the token's slots of expert_w[t, j] * y_expert[position of (t, j)], summed in
|
||||
* ascending slot j (see the file header). The inverse permutation is built once per batch. */
|
||||
AVX_KERNEL(moe_gather) {
|
||||
/* NOT `rad_args_have(a, 6)`: the required operands are not a prefix -- the shared pair at 3
|
||||
* and 4 is a NULL SLOTS form for a layer whose shared arm runs beside the block, and the
|
||||
* prefix test reads a legal unfolded call as a missing operand. */
|
||||
if (!a || !a->t || a->n_t < 6 || !rad_args_have(a, 3) || !rad_arg_in(a, 5)) return RAD_E_INVAL;
|
||||
const RadTensor *ye = &a->t[0], *ew = &a->t[1], *sorted = &a->t[2], *Y = &a->t[5];
|
||||
/* Optional shared-expert fold: y = narrow(shared * act(gate)) + narrow(gathered). Both
|
||||
* operands or neither, as in libref. */
|
||||
if (!ye->data || !ew->data || !sorted->data || !Y->data) return RAD_E_INVAL;
|
||||
const RadTensor *sh = rad_arg_in(a, 3), *sg = rad_arg_in(a, 4);
|
||||
if ((sh != nullptr) != (sg != nullptr)) return RAD_E_INVAL;
|
||||
const char* act = p_str(a, "act", "none");
|
||||
@@ -1492,3 +1496,76 @@ AVX_KERNEL(pair_argmax) {
|
||||
}
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ router_topk_scatter */
|
||||
/* THE PAIR AS ONE OP: `router_topk` then `moe_scatter` over the ids it just wrote. The selection
|
||||
* reads 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.
|
||||
*
|
||||
* The operands line up with `router_topk`'s own first three slots (logits, ids, weights), so the
|
||||
* selection is the sibling kernel called on this very RadArgs -- no copy, no re-derivation of the
|
||||
* softmax -- and the sort is `moe_scatter` over the four operands it takes. */
|
||||
AVX_KERNEL(router_topk_scatter) {
|
||||
if (!rad_args_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 int rc = AVX_FN(avx_router_topk)(a, stream);
|
||||
if (rc != RAD_OK) return rc;
|
||||
|
||||
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, a->scratch, a->scratch_bytes, a->instance, a->rank,
|
||||
a->world_size };
|
||||
return AVX_FN(avx_moe_scatter)(&sa, stream);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ 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, at N = 1. `gate_b` and `gate` come together or not at all,
|
||||
* and the operand count says which. */
|
||||
extern "C" int AVX_FN(avx_gemm_nt)(const RadArgs*, RadStream);
|
||||
|
||||
AVX_KERNEL(router_gemm_topk_scatter) {
|
||||
/* NOT `rad_args_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. */
|
||||
if (!a || !a->t || a->n_t < 9) return RAD_E_INVAL;
|
||||
const bool gate_b = 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");
|
||||
|
||||
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, a->scratch, a->scratch_bytes, a->instance, a->rank,
|
||||
a->world_size };
|
||||
const int gc = AVX_FN(avx_gemm_nt)(&ga, stream);
|
||||
if (gc != RAD_OK) return gc;
|
||||
|
||||
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, a->scratch, a->scratch_bytes, a->instance, a->rank,
|
||||
a->world_size };
|
||||
const int pc = AVX_FN(avx_gemm_nt)(&pa, stream);
|
||||
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. */
|
||||
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, a->scratch, a->scratch_bytes, a->instance, a->rank,
|
||||
a->world_size };
|
||||
return AVX_FN(avx_router_topk_scatter)(&fa, stream);
|
||||
}
|
||||
@@ -387,6 +387,26 @@ static const RadParamSpec pScatter[] = { P_INT("M"), P_INT("n_expert"), P_INT(
|
||||
static const RadOperandSpec oScatter[] = { OPD("expert_ids"), OUT("sorted_tok"),
|
||||
OUT("expert_offset"), OUT("expert_count") };
|
||||
|
||||
/* THE PAIR AS ONE OP, and the router GEMM in front of it: libref's schemas for the same two rows,
|
||||
* where the operand lists are unions and `expert_ids`/`logits` are named once as outputs. */
|
||||
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") };
|
||||
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") };
|
||||
|
||||
static const RadParamSpec pMoeGemm[] = { P_INT("M"), P_INT("N"), P_INT("K"), P_INT("n_expert"),
|
||||
P_INT("top_k"), P_STR("a_order"), P_STR("dtype") };
|
||||
static const RadOperandSpec oMoeGemm[] = { OPD("a"), WTAB("w"), OPD("sorted_tok"),
|
||||
@@ -979,6 +999,10 @@ static const RadOpSchema kSchemas[] = {
|
||||
{ "router_sigmoid_topk", ARR(pRouterSigmoid), ARR(oRouterSigmoid),
|
||||
"Single-group sigmoid routing; correction bias selects experts, original scores weight them." },
|
||||
{ "router_topk", ARR(pRouter), ARR(oRouter), "softmax then exact top-k routing." },
|
||||
{ "router_topk_scatter", ARR(pRouterTopkScatter), ARR(oRouterTopkScatter),
|
||||
"the selection and the sort in one op, run as exactly the pair." },
|
||||
{ "router_gemm_topk_scatter", ARR(pRouterGemmTopkScatter), ARR(oRouterGemmTopkScatter),
|
||||
"the router GEMM, the shared expert's gate GEMM and the pair, run as exactly the launches." },
|
||||
{ "moe_scatter", ARR(pScatter), ARR(oScatter),
|
||||
"stable counting sort of routing slots by expert." },
|
||||
{ "moe_gemm", ARR(pMoeGemm), ARR(oMoeGemm), "grouped GEMM over the expert-sorted rows." },
|
||||
@@ -1514,6 +1538,12 @@ SIMPLE("avx_router_topk", "router_topk", "moe", "softmax then exact top-k routin
|
||||
L(router_topk)),
|
||||
SIMPLE("avx_router_sigmoid_topk", "router_sigmoid_topk", "moe", "sigmoid correction-bias routing", DT_FLOAT,
|
||||
L(router_sigmoid_topk)),
|
||||
SIMPLE("avx_router_topk_scatter", "router_topk_scatter", "moe",
|
||||
"the selection and the sort in one op, run as exactly the pair", DT_FLOAT,
|
||||
L(router_topk_scatter)),
|
||||
SIMPLE("avx_router_gemm_topk_scatter", "router_gemm_topk_scatter", "moe",
|
||||
"the router GEMM, the shared expert's gate GEMM and the pair, run as exactly the launches",
|
||||
DT_FLOAT, L(router_gemm_topk_scatter)),
|
||||
SIMPLE("avx_moe_scatter", "moe_scatter", "moe", "stable counting sort of routing slots by expert",
|
||||
"i32 indices", L(moe_scatter)),
|
||||
SIMPLE("avx_moe_gemm", "moe_gemm", "moe", "grouped GEMM over the expert-sorted rows", DT_FLOAT,
|
||||
|
||||
Reference in new issue
Block a user