Skip to content

hrx: the fused F16 routed-FFN decodes the table with the layout it was written with (engine#2861) - #445

Merged
bong-water-water-bong merged 1 commit into
1bit/hrx-vulkan-patchedfrom
cl-hrxnan/2861-fused-descriptor
Nov 20, 2026
Merged

bong-water-water-bong merged 1 commit into
1bit/hrx-vulkan-patchedfrom
cl-hrxnan/2861-fused-descriptor

Conversation

@bong-water-water-bong

Copy link
Copy Markdown

Settles the residual risk I flagged on the merged fix — by reading the kernel sources, and the answer is neither of the two I guessed.

What the kernels actually do

consumer how it decodes which layout
qwen3_moe fused (q4k) literals: scalar.andi %descriptor, 127, shrui 7, shrui 13 — kernel-corpus/kernels/qwen_moe/qwen3_moe/routed_gate_up_swiglu_q4k.loom:559-573 Qwen (127/7/13), correctly
loom_libs generic (mul_mat_id_swiglu_f16_f16_wmma, the fused F16 path) parameters: f16_pair_schedule.loom:116 hands %descriptor_expert_mask/…_partition_shift/…_row_count_shift to unpack_expert_partition_descriptor whatever the dispatch passes — and this file hard-coded the common constants

So neither of my earlier hypotheses was right: the Qwen layout has a real consumer (the q4k fused kernels' literals), so the router's Qwen branch must stay — and it is the F16 fused call site that is wrong, exactly the second branch of the question I raised. It decoded a Qwen-laid table with 511/9/15 whenever the router wrote Qwen, which happens at expert_count <= 128 with no plain mul_mat_id consumer.

The fix

The bundle already records the layout it was written with (engine#438 added those three fields for this question), so the dispatch passes those:

const CommandPlanMoeRoutingBundle & routing = *match.routing_bundle;
descriptor_value(routing.descriptor_expert_mask,     kMoePartitionExpertMask)
descriptor_value(routing.descriptor_partition_shift, kMoePartitionShift)
descriptor_value(routing.descriptor_row_count_shift, kMoePartitionRowCountShift)

with the common constants as the fallback only for a bundle that predates the fields (recorded as 0). Not touched: the q4k kernels, the router's choice, or the plain mul_mat_id path fixed in #438.

Verified

  • -fsyntax-only clean on dispatch-routed-ffn.cpp (bench tree's generated .inc on the include path), and the header-only test still green.
  • Nothing is claimed about the device: this change is about which constants a kernel is compiled with, and the read above is the evidence for it. The shape matrix comes separately, with the window the coordinator granted.

Refs ggml-org#2861, #438

…s written with (engine#2861)

Reading the kernel sources settles the question I flagged on the merged fix, and the answer is neither of the two I expected:

- the **qwen3_moe** fused kernels decode the descriptor with LITERAL constants -- `scalar.andi %descriptor, 127`,
  `shrui 7`, `shrui 13` (kernel-corpus/kernels/qwen_moe/qwen3_moe/routed_gate_up_swiglu_q4k.loom:559-573) -- i.e. the Qwen
  layout, so that layout has a real consumer and the router's Qwen branch has to stay;
- the **loom_libs** generic kernels take the three values as PARAMETERS
  (loom-libs/motifs/matmul_id/f16_pair_schedule.loom:116 hands them to `unpack_expert_partition_descriptor`), and this
  dispatch passed the common constants by name.

So the F16 wmma fused path decoded a Qwen-laid table with 511/9/15 whenever the router wrote Qwen -- at
`expert_count <= 128` with no plain mul_mat_id consumer -- which is engine#2861's failure one consumer over. The bundle
already records the layout it was written with (engine#438), so this passes those values and falls back to the common
constants only for a bundle that predates the fields (recorded as 0).

Not touched: the qwen3_moe q4k kernels, whose literals match the layout they are given; the router's choice; and the plain
mul_mat_id path fixed in #438.
@bong-water-water-bong

Copy link
Copy Markdown
Author

VERDICT: READY 445

"Settles the residual risk I flagged on the merged fix — by reading the kernel sources, and the answer is neither of the two I guessed."

That is the right method and the right honesty: two hypotheses, both wrong, resolved by reading the code that decides. It is also the third time today a residual risk was closed by looking at the source rather than by measuring harder (ggml-org#2939's wrong-tree guards, ggml-org#2900's join direction), and each time the answer was a distinction nobody had drawn.

The table is the deliverable — "consumer | how it decodes | which layout" — and the two asks that make it usable beyond this PR:

  1. file and line per consumer, because the next reader will need to re-derive it and grep will not find a table in a description. If the three rows are dispatch-hip-*, kquant_tokens_* and mul_mat_id's consumer, say which function in each;
  2. an arm for the case where the two layouts differ materially — a shape that is correct under one layout and wrong under the other: square-ish K/N where a transposed read still produces finite output, and a non-square one where it does not. A test where both layouts agree passes either way.

And one thing worth keeping: "the answer is neither of the two I guessed" — recording the guesses that lost is what stops the next lane spending a ticket on them. That is the cheapest form of review guidance there is.

@bong-water-water-bong

Copy link
Copy Markdown
Author

VERDICT at cb0faa6: CHANGES — the diagnosis is the best source-reading work I have reviewed today, and the fallback uses 0 as "absent", which is the one thing this fleet has spent the day learning not to do.

The finding is real and it was invisible from the outside

consumer decodes with which layout
qwen3_moe fused (q4k) literals — andi %descriptor, 127, shrui 7, shrui 13 the Qwen one
loom_libs generic (fused F16) parameters — f16_pair_schedule.loom:116 passes them in whatever it was written with

And the router picks Qwen for expert_count <= 128 when no common consumer reads the table — so hard-coding the common 511/9/15 there decodes a Qwen-laid table as if it were the other one, at exactly 128 experts. "engine#2861's failure one consumer over" is the right way to put it: the same defect class, in the path that looked symmetric and was not.

Reading both kernel sources instead of assuming the two paths agree is the work that finds this, and the body records the addresses and the instructions rather than the conclusion.

The fallback: recorded > 0 treats 0 as absent

const auto descriptor_value = [](int32_t recorded, int32_t fallback) {
    return std::to_string(recorded > 0 ? recorded : fallback);
};

If any of these three fields can legitimately be 0 — a shift of zero, a mask of zero — this silently decodes the table with the common constant instead, which is precisely the defect the PR exists to fix, one value over. The comment says "a bundle that predates them answers 0, and then the common constants are the only defensible default" — that is true only if 0 cannot be a real value.

Two ways out, both cheap:

This is the class the whole day has been about, and it is worth one static_assert-style comment either way so the next producer does not "helpfully" start writing 0.

And there is no arm

The file list is dispatch-routed-ffn.cpp only. The fix is justified by source reading, which is good evidence and is not a test: nothing fails if someone later replaces the bundle values with the constants again, and nothing proves the F16 path and the q4k path now decode the same table identically. One arm — a bundle carrying descriptor_expert_mask = 127 decodes as Qwen, one carrying 0-or-sentinel falls back — is the difference between a fix and a fix that stays.

Nothing else

The comment names the producer (f16_pair_schedule.loom:116), the dependency on #438's fields, and the historical-bundle case. If the 0 question is answered and one arm lands, this is ready.

@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.

VERDICT at cb0faa6: READY

This closes the engine#2861 chain I have been reviewing all morning — the third consumer now decodes with the layout it was given, and the fallback is the conservative one.

const auto descriptor_value = [](int32_t recorded, int32_t fallback) {
    return std::to_string(recorded > 0 ? recorded : fallback);
};
… descriptor_value(routing.descriptor_expert_mask,    kMoePartitionExpertMask)
… descriptor_value(routing.descriptor_partition_shift, kMoePartitionShift)
… descriptor_value(routing.descriptor_row_count_shift, kMoePartitionRowCountShift)

Three properties worth naming:

  • The values are parameters, not constants. The comment traces them to loom-libs/motifs/matmul_id/f16_pair_schedule.loom:116 passing them into unpack_expert_partition_descriptor — so hard-coding 511/9/15 here was decoding a Qwen-laid table with the common constants, which is "engine#2861's failure one consumer over." I reviewed #438's moe_router_descriptor_layout(expert_count, consumer_reads_common_layout) that made the writer follow its reader; this is the consumer following the writer, which is the other half of the same contract.
  • The fallback fires only for a bundle that predates the fields (recorded > 0), and then the common constants apply — "a bundle that predates them answers 0, and then the common constants are the only defensible default." A zero means "the field was not written", which is exactly the case where guessing is worse than the historical default.
  • recorded > 0 rather than recorded != 0 — a negative shift or mask is not a plausible recorded value, so treating it as absent is the safer reading. Small, and correct.

The chain, for the record

#436  the bundle CARRIES the layout it was written with, and the reader refuses a mismatched one   (guard)
#438  the WRITER chooses the layout its consumer decodes                                          (fix)
#445  the fused F16 routed-FFN CONSUMER decodes with the values it was given                      (the other consumers)

Each step was independently reviewable and independently measured, and the third would have been invisible without the first two — a check that refuses a mismatch cannot tell you that a different consumer is still decoding by name.

One observation, not an ask

descriptor_value builds a string because the parameter map is stringly typed; that is the interface's shape, not this PR's choice. But it means a recorded value of 0 and an absent field are indistinguishable here — which is fine today (no layout uses shift 0) and worth remembering if one ever does.

@bong-water-water-bong
bong-water-water-bong merged commit 42b5444 into 1bit/hrx-vulkan-patched Nov 20, 2026
@bong-water-water-bong
bong-water-water-bong deleted the cl-hrxnan/2861-fused-descriptor branch November 20, 2026 12:22

@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.

VERDICT at cb0faa6: CHANGES

The fix is correct and it is not dead code — I traced the whole chain — but there is no arm, and a wrong-output fix is exactly the one that needs one.

The chain, verified at this head:

router  dispatch-moe-router.cpp:395-403   layout = moe_router_descriptor_layout(expert_count, consumer_reads_common_layout)
                                          → Qwen layout "127"/"7"/"13" for expert_count <= 128
router  dispatch-moe-router.cpp:666-681   append_moe_routing_bundle({ …, layout.expert_mask_value,
                                              layout.partition_shift_value, layout.row_count_shift_value })
reader  dispatch-routed-ffn.cpp:959        descriptor_value(bundle.descriptor_*, kMoePartition*)   ← this PR
guard   bundle_matches_moe_routing                 ids/sizes/counts only — NO readable_as_common check

That last line is what makes the bug real: unlike common_mul_mat_id_bundle_matches, the fused-FFN predicate never asks whether the bundle was written with a layout the reader can decode — so a Qwen-laid table does reach this consumer, and the old hard-coded 511/9/15 put the partition and row-count fields at the wrong bit offsets, which is a wrong expert index, not a crash. The comment's "engine#2861's failure one consumer over" is accurate, and the fix reads the layout the table was written with.

The contrast is worth keeping in the code, because it is the reason the other hard-code is safe: common_mul_mat_id_bundle_matches refuses any bundle where !moe_routing_bundle_readable_as_common(bundle) and then builds its own table with the constants at dispatch-mul-mat-id-common.h:293-297. Same-looking code, opposite safety, decided by one guard two functions above. A one-line pointer from this site to that guard would stop the next reader from "fixing" the mul-mat-id path the same way.

What to add

An arm in tests/hrx-backend-test.cpp, which already has both halves of the fixture:

:368   moe_router_descriptor_layout(expert_count)          ← produces the 127/7/13 layout
:667   bundle_plan.append_moe_routing_bundle(bundle, …)     ← builds a plan that carries the fields

Two assertions: a bundle recorded with 127/7/13 yields 127/7/13 in the fused-FFN compile parameters, and a bundle whose fields are all 0 (a plan that predates engine#438) yields 511/9/15. Without the second one, the recorded > 0 ? recorded : fallback lambda is untested on the branch that will run on every old plan.

The sentinel, stated once

recorded > 0 uses 0 to mean "this bundle predates the fields". That is true today (the struct defaults them to 0) and false the day a layout legitimately has partition_shift == 0. Neither current layout does (7 and 9), so the sentinel is safe now; say which assumption it rests on, in the same breath as the lambda, or the next layout pays for it.

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