arch/common: group-limited sigmoid routing and a SwiGLU clipped after the SiLU, for Ling-3.0

GlmRouter takes `groups` and `topk_groups` and issues router_sigmoid_group_topk above one group.
MoeFP8 passes them on (a grouped router runs the separate selection, not the single-group fused
one), takes a clamp of its own for the shared arm, and with `swiglu_step` clips both arms after
the SiLU; GateQuantFP8 issues swiglu_step for that, without the fused clamp-and-quantise. Every
default is the existing behaviour, so no other graph changes.
This commit is contained in:
keptn42 authored and StillDeadcode committed 2026-11-03 19:07:34 +01:00
1 parent 523349dd8f
commit 1c2f55027c
3 files changed
+51 -21

No files matched your search

+25 -12
View File
@@ -1,5 +1,6 @@
/* GLM noaux_tc router. Correction bias changes selection only; mixture weights
* come from the uncorrected sigmoid probabilities. All routing arithmetic is F32. */
* come from the uncorrected sigmoid probabilities. All routing arithmetic is F32. With `groups`
* above 1 the selection is group-limited (router_sigmoid_group_topk, Ling-3.0's 8 groups, 4 kept). */
#ifndef RAD_BLOCK_GLM_ROUTER_H
#define RAD_BLOCK_GLM_ROUTER_H
#include "rad_glm_tp.h"
@@ -9,7 +10,7 @@ namespace rad {
namespace arch {
struct GlmRouter {
struct Config {
int64_t experts = 288, top_k = 8, rows = 32;
int64_t experts = 288, top_k = 8, rows = 32, groups = 1, topk_groups = 1;
int norm = 1;
double route_scale = 2.5;
};
@@ -33,8 +34,9 @@ struct GlmRouter {
w = wire;
if (!b || !name || !src.weight || !src.correction || g.n_embd < 1 || c.experts < 1 ||
c.experts > 512 || c.top_k < 1 || c.top_k > c.experts || c.rows < 1 ||
c.rows > g.max_tok || !std::isfinite(c.route_scale) || c.route_scale <= 0 || !w.input ||
!w.logits || !w.ids || !w.weights || w.logits == w.weights)
c.rows > g.max_tok || !std::isfinite(c.route_scale) || c.route_scale <= 0 ||
c.groups < 1 || c.experts % c.groups || c.topk_groups < 1 || c.topk_groups > c.groups ||
!w.input || !w.logits || !w.ids || !w.weights || w.logits == w.weights)
return RAD_E_INVAL;
const char *pn = nm.f("%s.weight", name), *cn = nm.f("%s.correction", name);
RAD_ARCH_TRY(map_copy(b, pn, src.weight));
@@ -52,14 +54,25 @@ struct GlmRouter {
{ w.input }, { w.logits });
RAD_ARCH_TRY(split.declare(b, nm, g, name, c.rows, c.experts, g.n_embd, projection, w.input,
w.logits));
op_select =
rw(b,
RAD_OP(b, "router_sigmoid_topk",
RAD_PARAMS(RAD_RANGE("M", 1, c.rows), RAD_INT("n_expert", c.experts),
RAD_INT("top_k", c.top_k), RAD_INT("norm", c.norm),
RAD_F64("route_scale", c.route_scale), RAD_STR("dtype", "f32")),
RAD_WEIGHTS(correction)),
{ w.logits }, { w.ids, w.weights });
if (c.groups > 1)
op_select =
rw(b,
RAD_OP(b, "router_sigmoid_group_topk",
RAD_PARAMS(RAD_RANGE("M", 1, c.rows), RAD_INT("n_expert", c.experts),
RAD_INT("top_k", c.top_k), RAD_INT("n_group", c.groups),
RAD_INT("topk_group", c.topk_groups), RAD_INT("norm", c.norm),
RAD_F64("route_scale", c.route_scale), RAD_STR("dtype", "f32")),
RAD_WEIGHTS(correction)),
{ w.logits }, { w.ids, w.weights });
else
op_select =
rw(b,
RAD_OP(b, "router_sigmoid_topk",
RAD_PARAMS(RAD_RANGE("M", 1, c.rows), RAD_INT("n_expert", c.experts),
RAD_INT("top_k", c.top_k), RAD_INT("norm", c.norm),
RAD_F64("route_scale", c.route_scale), RAD_STR("dtype", "f32")),
RAD_WEIGHTS(correction)),
{ w.logits }, { w.ids, w.weights });
return op_project && op_select ? RAD_OK : RAD_E_INVAL;
}
void project(RadCtx* ctx, int64_t first, int64_t rows, int64_t output_first = 0) const {
+20 -5
View File
@@ -182,8 +182,19 @@ struct MoeFP8 {
/* Block-fp8 experts whose scale plane is f32 (format 9, dtype fp8a8f32). */
int expert_f32_scale = 0;
double route_scale = 1.0;
/* With `sigmoid_router`: group-limited selection over `n_group` groups, `topk_group` kept
* (router_sigmoid_group_topk; Ling-3.0). 1 is GLM's single group. The fused selection and
* sort is single-group, so a grouped router runs the separate pair. */
int64_t n_group = 1, topk_group = 1;
/* Clamp of the SwiGLU product (swiglu_clamped); 0 is none. */
double swiglu_limit = 0.0;
/* The shared arm's clamp where it differs from the routed one (Ling-3.0 clamps its shared
* expert at 5 or 7 and its routed ones at 4, in different layers); negative: the routed
* arm's. */
double shared_swiglu_limit = -1.0;
/* Clip after the SiLU (`swiglu_step`) rather than before it (`swiglu_clamped`), for both
* arms. */
int swiglu_step = 0;
/* With `swiglu_limit`: the clamped product and its quantiser as one launch
* (GateQuantFP8::fuse_clamped), for the routed and the shared arm. */
int swiglu_quant = 0;
@@ -618,6 +629,8 @@ inline int MoeFP8::declare(RadBuilder* b, Names& nm, const Geom& geom, int l,
int ar_in, bool ar_out_) {
g = geom; c = src.cfg; w = wire; layer = l;
ar_out = ar_out_;
if (c.n_group > 1) c.router_fuse = 0;
const double sh_limit = c.shared_swiglu_limit < 0 ? c.swiglu_limit : c.shared_swiglu_limit;
/* 0 means not expert-parallel: this rank owns every expert. */
if (c.n_expert_all <= 0) c.n_expert_all = c.n_expert;
if (c.n_expert <= 0 || c.top_k <= 0 || c.top_k > c.n_expert_all || c.n_ff_exp <= 0)
@@ -764,6 +777,8 @@ inline int MoeFP8::declare(RadBuilder* b, Names& nm, const Geom& geom, int l,
rc.rows = c.rows;
rc.norm = c.norm_topk;
rc.route_scale = c.route_scale;
rc.groups = c.n_group;
rc.topk_groups = c.topk_group;
const GlmRouter::Wire wire{ w.h.x, w.logits, w.ids, w.ew };
const GlmRouter::Src rs{ src.router, src.router_bias };
RAD_ARCH_TRY(glm_router.declare(b, nm, g, l, nm.f("blk.%d.glm_router", l), rc, wire, rs));
@@ -997,7 +1012,7 @@ inline int MoeFP8::declare(RadBuilder* b, Names& nm, const Geom& geom, int l,
Geom gx = g;
gx.w_bf16 = true;
RAD_ARCH_TRY(gq_exp.declare(b, gx, w.egu, w.eff, c.n_ff_exp, "silu", c.rows * c.top_k,
true, false, c.swiglu_limit));
true, false, c.swiglu_limit, c.swiglu_step != 0));
op_dn = rw(b, rad_decl_op(b, "moe_gemm",
RAD_PARAMS(RAD_RANGE("M", 1, c.rows), RAD_INT("N", g.n_embd),
RAD_INT("K", c.n_ff_exp), RAD_INT("n_expert", ne),
@@ -1131,7 +1146,7 @@ inline int MoeFP8::declare(RadBuilder* b, Names& nm, const Geom& geom, int l,
* the rotated op at no extra launch. */
gq_exp.fuse_clamped = c.swiglu_quant != 0;
RAD_ARCH_TRY(gq_exp.declare(b, g, w.egu, w.eff, c.n_ff_exp, "silu", c.rows * c.top_k,
false, rot, c.swiglu_limit));
false, rot, c.swiglu_limit, c.swiglu_step != 0));
{
const std::vector<rad_weight> tab = tables(w_dn, w_dns);
rad_op h = ncls == 4
@@ -1234,7 +1249,7 @@ inline int MoeFP8::declare(RadBuilder* b, Names& nm, const Geom& geom, int l,
Geom gx = g;
gx.w_bf16 = true;
RAD_ARCH_TRY(gq_prot.declare(b, gx, b_pgu, pff, wp, "silu", c.rows * c.top_k, true,
false, c.swiglu_limit));
false, c.swiglu_limit, c.swiglu_step != 0));
op_pdn = rw(b, rad_decl_op(b, "moe_gemm",
RAD_PARAMS(RAD_RANGE("M", 1, c.rows), RAD_INT("N", g.n_embd),
RAD_INT("K", wp), RAD_INT("n_expert", np),
@@ -1374,7 +1389,7 @@ inline int MoeFP8::declare(RadBuilder* b, Names& nm, const Geom& geom, int l,
RAD_ARCH_TRY(rad_buf_concurrent(b, w.sff.q8));
RAD_ARCH_TRY(rad_buf_concurrent(b, w.sff.s8));
}
if (!w.sff.q || c.swiglu_limit>0 || (sh_down.reads_x && !(w.sff.q8_fed && sh_down.probed_i8)))
if (!w.sff.q || sh_limit>0 || (sh_down.reads_x && !(w.sff.q8_fed && sh_down.probed_i8)))
RAD_ARCH_TRY(rad_buf_concurrent(b, w.sff.x));
RAD_ARCH_TRY(rad_buf_concurrent(b, w.sout));
RAD_ARCH_TRY(rad_buf_concurrent(b, w.sgate));
@@ -1391,7 +1406,7 @@ inline int MoeFP8::declare(RadBuilder* b, Names& nm, const Geom& geom, int l,
gq_sh.fuse_clamped = c.swiglu_quant != 0;
RAD_ARCH_TRY(gq_sh.declare(b, g, w.sgu, w.sff, c.n_ff_shared, "silu", c.rows,
sh_down.reads_x && !(w.sff.q8_fed && sh_down.probed_i8), false,
c.swiglu_limit));
sh_limit, c.swiglu_step != 0));
RAD_ARCH_TRY(sh_down.declare(b, nm, g, down_base,
g.n_embd, c.n_ff_shared, RAD_SHARD_COL, grp_layer(l),
{ shared_down },
+6 -4
View File
@@ -533,9 +533,11 @@ struct GateQuantFP8 {
* quantises its input itself (an int8 linear's op_q). */
bool silu_only = false;
/* `step`: with `limit`, clip after the SiLU (`swiglu_step`, Ling-3.0) rather than before it
* (`swiglu_clamped`, GLM-5.3). It has no fused quantiser, so the pair runs. */
int declare(RadBuilder* b, const Geom& g, rad_buf gate_up, const ActFP8& act, int64_t n,
const char* act_name = "silu", int64_t max_rows = 0, bool bf16_out = true,
bool rot = false, double limit = 0);
bool rot = false, double limit = 0, bool step = false);
void step(RadCtx* c, rad_buf gate_up, int64_t T, int64_t r0 = 0, int64_t rows = 0) const;
};
@@ -544,7 +546,7 @@ struct GateQuantFP8 {
* bf16 output is unrotated either way. */
inline int GateQuantFP8::declare(RadBuilder* b, const Geom& g, rad_buf gate_up, const ActFP8& act,
int64_t n, const char* act_name, int64_t max_rows,
bool bf16_out, bool rot, double limit) {
bool bf16_out, bool rot, double limit, bool step) {
a = act;
want_x = bf16_out;
op_quant = 0;
@@ -562,7 +564,7 @@ inline int GateQuantFP8::declare(RadBuilder* b, const Geom& g, rad_buf gate_up,
std::strcmp(act_name, "silu"))
return RAD_E_INVAL;
const int64_t rows = max_rows > 0 ? max_rows : g.max_tok;
if (fuse_clamped && !bf16 && !a.q8_fed && a.cq() && a.cs() &&
if (fuse_clamped && !step && !bf16 && !a.q8_fed && a.cq() && a.cs() &&
rad_op_declared(b, "swiglu_clamped_quant_fp8")) {
op =
RAD_OP(b, "swiglu_clamped_quant_fp8",
@@ -579,7 +581,7 @@ inline int GateQuantFP8::declare(RadBuilder* b, const Geom& g, rad_buf gate_up,
}
want_x = true;
op = rw(b,
RAD_OP(b, "swiglu_clamped",
RAD_OP(b, step ? "swiglu_step" : "swiglu_clamped",
RAD_PARAMS(RAD_RANGE("M", 1, rows), RAD_INT("n", n), RAD_F64("limit", limit),
RAD_STR("dtype", g.dtype)),
RAD_NOWEIGHTS),