Repository navigation
hrx: the fused F16 routed-FFN decodes the table with the layout it was written with (engine#2861) - #445
Conversation
…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.
|
VERDICT: READY "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:
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. |
|
VERDICT at The finding is real and it was invisible from the outside
And the router picks Qwen for 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:
|
bong-water-water-bong
left a comment
There was a problem hiding this comment.
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:116passing them intounpack_expert_partition_descriptor— so hard-coding511/9/15here was decoding a Qwen-laid table with the common constants, which is "engine#2861's failure one consumer over." I reviewed #438'smoe_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 > 0rather thanrecorded != 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
left a comment
There was a problem hiding this comment.
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.
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
qwen3_moefused (q4k)scalar.andi %descriptor, 127,shrui 7,shrui 13—kernel-corpus/kernels/qwen_moe/qwen3_moe/routed_gate_up_swiglu_q4k.loom:559-573loom_libsgeneric (mul_mat_id_swiglu_f16_f16_wmma, the fused F16 path)f16_pair_schedule.loom:116hands%descriptor_expert_mask/…_partition_shift/…_row_count_shifttounpack_expert_partition_descriptorSo 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/15whenever the router wrote Qwen, which happens atexpert_count <= 128with no plainmul_mat_idconsumer.The fix
The bundle already records the layout it was written with (
engine#438added those three fields for this question), so the dispatch passes those: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 plainmul_mat_idpath fixed in#438.Verified
-fsyntax-onlyclean ondispatch-routed-ffn.cpp(bench tree's generated.incon the include path), and the header-only test still green.Refs ggml-org#2861, #438