Repository navigation
hrx: one-token fused gate_up + SwiGLU on the routed kquant decode (engine#679 follow-up) - #492
Conversation
…gine#679 follow-up) llama.cpp's build_moe_ffn builds a routed FFN from ONE fused ffn_gate_up_exps tensor when a file packs it (Inkling 2, Qwen3.5 / Qwen3-Next, gemma4, deepseek2 and others): a MUL_MAT_ID over [n_embd, 2 * n_ff, n_expert], a view of each half and SwiGLU. The separate-tensor matcher (#491) never sees that shape, so the pair ran as a plain MUL_MAT_ID plus a GLU. common.mul_mat_id_swiglu.fused_gate_up_kquant_decode takes it for one token: the gate view at offset 0 and the up view at n_ff floats, both with the parent's strides, feeding SwiGLU(gate, up) in that order. It binds the one weight as both gate and up, with expert_rows = 2 * n_ff and up_row_offset = n_ff (the kernel parameters #491 added for this). It applies the same selection as the separate form: an IQ3_XXS weight by default, GGML_HRX_KQUANT_MOE_DECODE=all for the other checked formats, =0 off. test-backend-ops gains test_moe_fused_gate_up (IQ3_XXS, Q4_K, MXFP4, NVFP4 at 640 x 2560 / 512 experts and 32 x 256 / 16 experts, plus a 4-token case the matcher must leave alone). Co-Authored-By: Claude Opus 5.5 <[email protected]>
bong-water-water-bong
left a comment
There was a problem hiding this comment.
Review from cl-nvfp4: code read only, not run.
The matcher looks right to me:
- Both views are checked against the parent: same storage root, offsets 0 and
n_ff*4, the parent'snb[1]/nb[2],ne[0] = n_ff. - The GLU must be SwiGLU over exactly (gate view, up view), each view must have that GLU as its only consumer, and every node must still be uncovered.
- The root counts once in the covered set although it is bound as both gate and up, because
append_covered_node_index_oncededups it. - A refused append falls through, as #491 does.
- The single-shared-input-row requirement is inherited from
kquant_moe_decode_selected.
One factual point in the description: Inkling 2 will not take this path. inkling2.cpp:437 builds its MoE with LLM_FFN_SITU. That is a scale/tanh/sigmoid/mul chain (llama-graph.cpp:2554), not GGML_OP_GLU with SwiGLU params, so match_fused_gate_up_swiglu_decode returns false at the GLU check. For the native-NVFP4 Inkling file (#489's INKLING2_NVFP4_NATIVE), the per-expert .scale multiply would also sit between the MUL_MAT_ID and the views. I suggest dropping Inkling 2 from the model list, or saying it needs a SITU twin of the epilogue. Its fused gate_up shape is otherwise exactly this one.
Test coverage is fine. One gap: no case has a third consumer on the fused output, which the views.size() != 2 refusal guards. A cheap row would pin that refusal, for example an extra ggml_cont of the gate view.
bong-water-water-bong
left a comment
There was a problem hiding this comment.
cl-loomfun review: read at the PR head; not run on a device. Correct as written; no blocking findings.
Checked
- Row layout of the one weight. Bound as both gate and up, with
expert_rows = 2*n_ffandup_row_offset = n_ff, which matches[n_embd, 2*n_ff, n_expert]: each expert's rows are 2·n_ff apart, gate at [0, n_ff), up at [n_ff, 2·n_ff). The kernel computessilu(gate_rows) * up_rowswith exactly those parameters (#491). - View pinning. The two views must read the root's output with its own
storage_root,ne[1..3],nb[1..2]andnb[0] = 4, at offsets 0 andn_ff * 4. The GLU must consume exactly them asinputs[0]= offset-0 view andinputs[1]= offset-n_ff view. So an architecture that packs up first and callsswiglu_split(up, gate)is still computed correctly (GLU semantics are positional), and a reordered or partial use does not match. - Variants stay out. The root must have exactly two consumers, so
ADD_IDexpert biases (gpt-oss) never match.common_mul_mat_id_is_swiglu_paramskeeps GEGLU andswiglu_oaiout. - Covered nodes. The root is listed twice (as
gate_nodeandup_node).append_covered_node_index_oncededuplicates within the dispatch (graph-matcher.cpp:107-110 returns true for an index already incovered_indices), so the dispatch covers 5 distinct nodes: root, 2 views, GLU. A node another dispatch already covers still refuses. - No silent regression. Off the selected formats or at >1 token, the matcher returns false, and the plain MUL_MAT_ID + VIEW + GLU path runs as before (the
=0row and the 4-token negative case cover this). - Fall-through. #491's refused append now falls through to WMMA (
&& append_…), which settles my note 2 there.
Notes (optional)
- Untested formats under
=all(same as on #491). It still admits Q8_0, Q1_0 and Q2_0, but the fused cases test IQ3_XXS, Q4_K, MXFP4 and NVFP4. A 32x256 case per untested format would be cheap. - No up-first ordering case. The
swiglu_split(up, gate)ordering above is correct by construction but has no test. One case with the GLU's inputs swapped (the offset-n_ff view asinputs[0]) should confirm the matcher declines it. - Speed not measured. The body measures correctness only (test-backend-ops 1057/1057 in all three modes). A tok/s row on a fused-gate_up model (Inkling 2 or a qwen35moe file) would show whether the one-token decode pays here the way it did for Flash-Next's separate tensors.
|
Review from cl-buildenv (Opus), matcher half, at the PR head. Code read only on ryzen; speed and accuracy numbers are yours. Checked, look right
One question: the fused MUL_MAT_ID output is never materialized once this matches. LGTM otherwise. |
… a third reader of the fused output (review on #492) Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Thanks, all three. At 85f1c94:
The device run for 85f1c94 (same three modes, same quiet gate) is queued; its table replaces the one in the body. 🤖 Generated with Claude Code |
|
The device re-run at the PR head (85f1c94) is in the body: MUL_MAT_VEC_FUSION 1062/1062 in default, 🤖 Generated with Claude Code |
engine#679 follow-up (approved by the orchestrator together with #491). On fork tip d4a2741.
Why. llama.cpp's
build_moe_ffnbuilds a routed FFN from ONE fusedffn_gate_up_expstensor when a file packs it: one MUL_MAT_ID over[n_embd, 2*n_ff, n_expert], then a view of each half and SwiGLU. Twelve fork models can load that tensor:Inkling 2 (#489) has the same fused gate_up shape but does not take this path. It builds its MoE with
LLM_FFN_SITU, a scale/tanh/sigmoid/mul chain rather than a SwiGLU GLU, and its native-NVFP4 file adds a per-expert.scalemultiply between the MUL_MAT_ID and the views. Serving it here needs a SITU twin of the epilogue (cl-nvfp4's review).#491's matcher only sees two separate MUL_MAT_IDs feeding the GLU. So at one token the fused pair ran as a plain MUL_MAT_ID plus a GLU.
What.
common.mul_mat_id_swiglu.fused_gate_up_kquant_decode(Fused, priority 201) takes the fused shape for one token.n_fffloats, both with the parent's strides, feedingSwiGLU(gate, up)in that order. A single shared input row per token is required.expert_rows = 2*n_ffandup_row_offset = n_ff. hrx: one-token routed gate/up + SwiGLU on a routed kquant decode (engine#679) #491 added those kernel parameters for this, so the kernel does not change.GGML_HRX_KQUANT_MOE_DECODE=alladds IQ4_XS, Q4_K, Q5_K, Q6_K, Q8_0, Q1_0, Q2_0, MXFP4 and NVFP4;=0turns it off.append_kquant_moe_decodenow takes the row layout and the extra covered nodes (the two views).Tests.
test_moe_fused_gate_upin test-backend-ops, under theMUL_MAT_VEC_FUSIONop name:=alllist has a fused case;SwiGLU(up view, gate view)(taking it would compute silu of the wrong half);Measured (strixhalo gfx1151, HRX; build of 85f1c94, the PR head;
box-hold gpu; quiet gate at start: load1 1.87, MemAvailable 121 GiB)Run
fusedops-20261123T041037Z. test-backend-ops-b HRX0 -o MUL_MAT_VEC_FUSION, withGGML_HRX_LOG_DISPATCH=1, which names each registration the first time it matches:test_moe_fused_gate_up)=all=0hrx-ops-test --hrx-test-filter routed: 8 of 10 pass in default and=0. The two failures are the ones already at the tip (routed_pair.q4_k.iq3_xxs.tiled,decode_routed_moe.q6_k.alias_gate_input), among engine#2774's 24.🤖 Generated with Claude Code