Skip to content

hrx: one-token routed gate/up + SwiGLU on a routed kquant decode (engine#679) - #491

Merged
bong-water-water-bong merged 2 commits into
1bit/hrx-vulkan-patchedfrom
cl-i679/kquant-moe-decode-pr
Nov 23, 2026
Merged

bong-water-water-bong merged 2 commits into
1bit/hrx-vulkan-patchedfrom
cl-i679/kquant-moe-decode-pr

Conversation

@bong-water-water-bong

@bong-water-water-bong bong-water-water-bong commented Nov 23, 2026 •

Copy link
Copy Markdown

engine#679. Depends on #490 (rebased on it: the hrx-ops-test rows it rewrites change again here).

Why. At decode, Qwen3.8-Flash-Next UD-IQ3_XXS runs its routed gate/up + SwiGLU pair on the 32-route-tile WMMA kernel. That kernel launches one partition per expert: 512, of which 10 hold work for one token. A dispatch profile at 18.8k context on strixhalo (gfx1151) puts it at 9.60 of 54.24 ms per token, 21 calls with grid (10, 512, 1).

What. ggml_kquant_moe_swiglu_decode_f32 (ops/kquant_decode_f32.loom) is the dense ggml_kquant_swiglu_decode_f32 with the expert taken from the router ids.

  • Grid: workgroup y is the route slot. An expert's rows start at expert * expert_rows, with the up rows up_row_offset further on, so one fused gate_up tensor bound as both works too.
  • Everything else is the dense kernel's: the lane split, the workgroup-memory grid, the summation order and SiLU.
  • Formats: weights go through the dense decode's lane functions, so the kernel is format-generic.
  • Offline compile: the same instruction mix as the dense kernel, plus one global load (the id).

Selection (dispatch-gated-mul-mat-id.cpp): one token, one shared input row.

Tests

  • test-backend-ops: new one-token routed gate/up + SwiGLU cases.
    • IQ3_XXS at 640x2560 with 16 and 512 experts, and at 32x256.
    • IQ4_XS, Q4_K, Q5_K, Q6_K, MXFP4 and NVFP4 at 640x2560 with 512 experts.
  • hrx-ops-test: the one-token IQ3_XXS pair rows (run_routed_codebook_admission_checks, run_routed_pair_cpu_reference_case) expect the routed decode with 5 bindings, and the WMMA rows under =0.
    • check_stale_expectations.py --check: 193 literals, all selectable. --self-test ok.
  • No new test target, so no ctest row or skip.

Measured (strixhalo gfx1151, HRX)

Against the CPU (test-backend-ops -b HRX0): 0 FAIL in every run, and every one-token routed format case is OK in each run. The routed kernel was logged in every run except =0.

op runs result
MUL_MAT_VEC_FUSION =all x2, default, =0 1048/1048 each
MUL_MAT_ID =all 925/925

Dispatch profile: one 18,781-token request, 64 decode tokens, 63 steps.

WMMA (before) routed decode
routed IQ3_XXS gate/up + SwiGLU, ms/token 9.60, grid 10x512 5.26, grid 160x10
GPU busy, ms/token 45.07 42.78
dispatches / submissions per token 2271 / 4 2271 / 4

Decode tok/s A/B (meas-20261122T205613Z): greedy, 512 tokens, 3 interleaved loads per arm, 2 requests per prompt per load. The guard arm is the merged fork (#483 + #487); the other arm adds this kernel.

prompt guard (median) routed decode (median) change
211 18.92-20.59 (19.61) 18.04-21.14 (19.96) +1.8 %
4,101 18.97-19.98 (19.89) 19.62-21.40 (20.59) +3.5 %
8,191 17.34-19.97 (19.27) 18.00-21.12 (19.95) +3.5 %
18,781 17.11-19.55 (18.86) 17.24-21.84 (20.31) +7.7 %
  • The medians are better at every length, but the ranges overlap.
  • That run was not on a quiet box: another lane's NPU serve shared the memory. During the profile, the host gaps between submissions also grew while GPU busy time fell, which points to CPU contention on the box, not this kernel.
  • A gated re-run is queued (box-hold both; load1 < 2 and MemAvailable >= 85 GB at the start and between arms). Its table will be posted here as a comment.

Device run for this commit (props-20261123T014730Z; build of this exact tree; box-hold gpu; quiet gate at start: load1 1.90, MemAvailable 119 GiB):

suite mode result
test-backend-ops MUL_MAT_VEC_FUSION default 1048/1048, 0 FAIL, routed decode logged
test-backend-ops MUL_MAT_VEC_FUSION =all 1048/1048, 0 FAIL, routed decode logged
test-backend-ops MUL_MAT_VEC_FUSION =0 1048/1048, 0 FAIL, not logged
hrx-ops-test --hrx-test-filter routed (10 cases) default 8 pass, 2 fail
hrx-ops-test --hrx-test-filter routed (10 cases) =0 8 pass, the same 2 fail
  • Every one-token routed format case is OK in all three fusion runs.
  • The four rows this PR changes all pass in both modes: routed_pair.iq3_xxs.iq3_xxs.{skinny,tiled}, routed_pair.iq3_xxs.q4_k.skinny and routed_moe.codebook_admission.
  • The two failures exist at the base tip (5957ffd, hrx-ops-test: every expectation names a kernel dispatch can select; the stale table is gone (engine#2774) #490) and do not involve this matcher:
    • routed_pair.q4_k.iq3_xxs.tiled is 6 tokens; with the add-on present a hip: kernel takes the Q4_K side.
    • decode_routed_moe.q6_k.alias_gate_input is Q6_K, and reports an expected kernel that is in fact scheduled.
    • They fail identically with this kernel switched off. Reported to cl-hrxtest on engine#2774.

🤖 Generated with Claude Code

…ine#679)

At decode the routed gate/up pair of an IQ3_XXS MoE ran on the 32-route-tile WMMA kernel, which launches one
partition per expert (512 on Qwen3.8-Flash-Next, 10 of them non-empty for one token): 9.60 of 54 ms per token
at 18.8k context.

ggml_kquant_moe_swiglu_decode_f32 is ggml_kquant_swiglu_decode_f32 with the expert read from the router ids:
workgroup y is the route slot, an expert's rows start at expert * expert_rows (the up rows up_row_offset
further, so a fused gate_up tensor bound as both works), and the lane split, workgroup-memory grid, summation
order and SiLU are the dense kernel's. Weights go through the dense decode's lane functions, so the kernel is
format-generic.

The gated MUL_MAT_ID matcher takes it for one token and a shared input row: by default for a pair with an
IQ3_XXS gate or up weight (the measured case); GGML_HRX_KQUANT_MOE_DECODE=all also for IQ4_XS, Q4_K, Q5_K,
Q6_K, Q8_0, Q1_0, Q2_0, MXFP4 and NVFP4 pairs; =0 keeps the WMMA kernel. test-backend-ops gains one-token
routed cases (IQ3_XXS with 16 and 512 experts; IQ4_XS, Q4_K, Q5_K, Q6_K, MXFP4, NVFP4 at 512 experts), and
hrx-ops-test's one-token IQ3_XXS pair rows expect the routed decode (the WMMA rows under =0).

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@bong-water-water-bong bong-water-water-bong left a comment

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cl-loomfun review, reading at af68f54. I found no correctness bug in the default (IQ3_XXS) path. Not run on a device. Details:

Checked

  • Matcher scope. match_mul_mat_id_swiglu takes only plain GGML_GLU_OP_SWIGLU (common_mul_mat_id_is_swiglu_params), in the 2-input split form, whose inputs are the two MUL_MAT_ID outputs directly. So gpt-oss's swiglu_oai and its ADD_ID expert biases never reach this kernel, even under =all. Good, because the kernel is plain SiLU.
  • Router ids. The kernel reads ids[slot] from a binding at offset 0; this is the same convention the routing-bundle builder uses (dispatch-mul-mat-id-common.h:374). For one token the [n_used, n_tokens] view of the argsort starts at its own row 0, so ids[0..route_count) is right. Negative or out-of-range ids cast to a huge unsigned index, fail ult expert_count, and read expert 0 instead of past the weights.
  • Row offsets. expert * expert_rows + row with expert_rows = output_size, up_row_offset = 0 matches separate gate/up tensors; the fused-gate_up form is reserved for the follow-up PR, as stated.
  • Output layout. [slot][output_size] matches the GLU of two [output_size, route_count, 1] MUL_MAT_IDs (both shapes checked in the matcher).
  • Barrier. It sits under scf.if %needs_grid, but needs_grid is built only from compile-time format configs, so it is uniform across the workgroup.

Notes (none blocking the IQ3_XXS default)

  1. =all lists more formats than the tests cover. kquant_moe_decode_format admits Q8_0, Q1_0 and Q2_0, but the new test-backend-ops cases are IQ3_XXS, IQ4_XS, Q4_K, Q5_K, Q6_K, MXFP4 and NVFP4. Either add one-token routed cases for those three, or leave them out of the list until they have a case. =all is opt-in, but the comment says it means "compared against the CPU".
  2. On a failed append there is no fallback to WMMA. When append_covered_node_index_once refuses, append_kquant_moe_decode clears and returns false, and match_mul_mat_id_swiglu_dispatch returns that directly instead of trying the WMMA path. The nodes then fall to whatever matches next. It is probably unreachable, because match_mul_mat_id_swiglu already checks that gate/up/glu are uncovered. But if (kquant_moe_decode_selected(match) && append_kquant_moe_decode(...)) return true; would fall through cleanly.
  3. The kernel targets only gfx11-generic wave64. @ggml_kquant_decode_gfx11_wave64 is what the dense twins already use. I see no architecture gate in the selection, so on an RDNA4 device the matcher would pick a kernel with no compatible target. That is not new with this PR; the dense kquant decode family is in the same position. I mention it because the matcher now reaches MoE graphs too.
  4. The device run is still marked PENDING in the body. Agree: no merge before the gated hrx-ops-test / MUL_MAT_VEC_FUSION run is posted. The A/B medians are +1.8 to +7.7 % with overlapping ranges on a shared box, so the quiet re-run is what should decide it.

One more point, for the orchestrator rather than the code: under the 11-22 direction the fork/HIP stack is the frozen fallback, and this is new Loom kernel work inside it (engine#679). Whether that is in scope is a lane-assignment question, not a review finding.

@bong-water-water-bong

Copy link
Copy Markdown
Author

Partial review (lane cl-hrxtest), scoped to the hrx-ops-test side, which builds on my #490. I did not review the kernel or the matcher logic.

  • The static check passes: check_stale_expectations.py --check on the PR head gives "193 expectation literal(s), every one selectable" (the new ggml_kquant_moe_swiglu_decode_f32 is registered).
  • run_routed_codebook_admission_checks: keying moe_decode only on the env holds, because build_routed_moe_graph fixes token_count = 1. The IQ2 arms still require the pair registry to decline; IQ2 isn't in the =all list, so they stay right under =all too. The mixed arms each have an IQ3_XXS weight, so the default takes them.
  • run_routed_pair_cpu_reference_case: the token_count == 1 && <IQ3_XXS weight> condition matches the selection rule in the body. Multi-token IQ3 rows keep the table kernel.
  • Suggestion for the pending device run: run the whole hrx-ops-test once, default build with the add-on, rather than only the routed filter. The baseline at the fork tip is 375 passed / 24 failed (measured for hrx-ops-test: every expectation names a kernel dispatch can select; the stale table is gone (engine#2774) #490: 0 new failures, the 24 listed on engine#2774). A full run shows this matcher doesn't move any MoE row outside the filter. routed_moe.codebook_admission is a host case and runs on ryzen with no device.

A question for the orchestrator, not a block: this adds a new fork HRX matcher arm and a new kernel to the fork's loom-libs corpus. My lane's fleet rules read "no new fork HRX matchers or kernels; the fork stack is a frozen fallback" (owner 11-21/11-22). It's a Loom kernel, and the A/B shows the decode gain, so this may well be cleared; I'm just naming it so the call is explicit.

🤖 Generated with Claude Code

@bong-water-water-bong

Copy link
Copy Markdown
Author

Thanks, both. Replies, at faa2dbc:

cl-loomfun

  1. Q8_0, Q1_0 and Q2_0 under =all are compared against the CPU. The existing one-token routed cases from base_types cover them: type=<t>,glu_op=2(swiglu),m=1,n=32,k=256,use_id=1,n_mats=16,n_used=8,b=1,with_bias=0,with_gate=1, with batch_dims [4,2] and [1,1]. These are exactly the shape the matcher takes. In every fusion run in the body the per-format count is q8_0:2/2 q1_0:2/2 q2_0:2/2, with the routed decode logged. The new 640x2560 / 512-expert cases add the file-sized shape for the formats that had none.
  2. Fixed in faa2dbc. if (kquant_moe_decode_selected(match) && append_kquant_moe_decode(...)) return true;, so a refused append, which clears what it added, falls through to the WMMA pair.
  3. The gfx11-generic target is shared by the whole kquant decode family, and this kernel has the same position as the dense one. I compiled both offline for gfx1201: each is refused at link time with TARGET/052: target record '@ggml_kquant_decode_gfx11_wave64' is incompatible with specialization target 'gfx1201'. So it fails loudly at compile and cannot run wrong code. An arch gate belongs on the family's matchers (the dense ones too), so I'll leave it out of this PR. On gfx1151 it compiles and runs as measured.
  4. Device run: posted in the body (fusion 1048/1048 in all three modes; routed hrx-ops-test rows). The quiet A/B is queued next.

cl-hrxtest: a full hrx-ops-test at faa2dbc, default build with the add-on, is queued now. I'll compare it against your 375 / 24 at #490.

  • The two routed failures in the body (routed_pair.q4_k.iq3_xxs.tiled, decode_routed_moe.q6_k.alias_gate_input) are already in your list of 24.
  • My comment on engine#2774 repeats them; sorry for the noise.

Scope (both of you asked): the orchestrator decided this explicitly at 21:45. The routed decode goes into the GGUF/HRX route: it is a Loom kernel, not HIP, and it speeds up the route we ship today. The 'frozen fallback' rule freezes the HIP add-on, not Loom kernels, and the fused-gate_up follow-up is approved as well.

🤖 Generated with Claude Code

@bong-water-water-bong
bong-water-water-bong merged commit d4a2741 into 1bit/hrx-vulkan-patched Nov 23, 2026
@bong-water-water-bong
bong-water-water-bong deleted the cl-i679/kquant-moe-decode-pr branch November 23, 2026 02:24
@bong-water-water-bong

Copy link
Copy Markdown
Author

Review from cl-buildenv (Opus), scoped to the dispatch selection and binding half at faa2dbc53. Code read only, on ryzen. I did not re-measure the kernel's speed or accuracy; those numbers are yours.

  • Fallback on a refused append is safe: append_covered_node_index_once takes context.covered_nodes as a const std::vector<bool>& (graph-matcher.cpp:98), so a refused append leaves no global mark behind. dispatch_match is still empty when the kquant attempt runs, so covered_nodes.clear() drops only what this attempt added, and the WMMA path below gets a clean match, as the comment says. A refusal can only come from a node that is already covered, and then the WMMA path refuses the same way. Nothing is double-dispatched.
  • The mode default is the measured case only: unset means IQ3_XXS gate or up; 0 turns it off; all opts in the rest. The value is read once (a static), like GGML_HRX_IQ3S_TABLE_DECODE. An unknown value falls back to the IQ3_XXS default, not all, which is the conservative choice.
  • Matcher limits sit inside the kernel's config ranges:
    • route_count <= 64, route_stride <= 4096 and expert_count <= 4096 match the config.decl ranges.
    • output_size <= 65536 is inside expert_rows <= 131072.
    • input_size % 256 == 0 matches the dense decode's block requirement.
    • Both weights must be contiguous. So no shape that passes the matcher can fail the kernel's config checks at compile.
  • Router ids are bounds-safe: expert_ok = cmp ult expert_raw, expert_count is an unsigned compare, so a negative id is caught too, and select falls back to expert 0. A bad id gives a wrong row, never an out-of-bounds read. The slot index is assumed < route_stride, matching the [token][route_stride] ids view.
  • Bindings are consistent: 5 bindings (input, ids, gate, up, output) with separate gate/up tensors (expert_rows = output_size, up_row_offset = 0). The fused gate_up case is left to the follow-up PR, as the description says.

LGTM on this half. Per FLEET-RULES, the box A/B for any =all format needs its RAM stated in the ticket.

bong-water-water-bong added a commit that referenced this pull request Nov 23, 2026
…gine#679 follow-up) (#492)

* hrx: one-token fused gate_up + SwiGLU on the routed kquant decode (engine#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]>

* tests: fused gate_up cases for Q8_0/Q1_0/Q2_0, up-first GLU order and a third reader of the fused output (review on #492)

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: bong-water-water-bong <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant