Repository navigation
Commit 2e5b433
committed
fix(moe): fix QMoE routing/layout bugs found via actual onnxruntime-genai execution
Structural graph export (Olive RTN + MobiusBuilder) previously succeeded for
Qwen3.5-MoE models, but the exported model.onnx had never actually been
loaded/run with onnxruntime-genai. Doing so surfaced five real, pre-existing
bugs in the QMoE fused-expert path and genai_config generation:
1. Router gate weight orphaned as an unqualified "weight" graph input.
MoELayer._qmoe_forward() called self.gate.qmoe_routing() directly instead
of through self.gate(op, ...), bypassing Module.__call__'s parameter
realization (which qualifies "weight" into "<prefix>.gate.weight").
Fixed by manually pushing the gate's module scope and realizing its
parameters first, mirroring the existing pattern in gemma4_assistant.py.
2. router_probs was hard-cast to float32, but QMoE's router_probs input
shares type constraint "T" with the hidden_states input (per
contrib_defs.cc) -- it must match hidden_states' actual dtype (e.g. fp16).
Fixed with CastLike instead of a fixed Cast(..., FLOAT).
3. swiglu_fusion=2 (concatenated gate/up layout) is not supported by the CPU
QMoE kernel in this ORT build (only swiglu_fusion=1, interleaved, is).
Fixed by reordering fc1_experts_weights/fc1_scales/fc1_experts_zero_points
rows from HF's concatenated [gate;up] layout to the interleaved
[g_0,u_0,g_1,u_1,...] layout via a graph-level reshape/transpose/reshape
(folded away at session load since it's applied to a constant
initializer), matching the existing swiglu_fusion=1 path in gemma4.py.
4. router_probs/router_weights were left 3D (batch, seq, num_experts) when
QMoE requires strictly 2D (num_tokens, num_experts). Added
_flatten_to_2d() to collapse leading dims before the QMoE call.
5. genai_config's past_present_share_buffer auto-detection only checked for
GroupQueryAttention nodes, defaulting to False for hybrid models that mix
LinearAttention (recurrent state, e.g. Qwen3.5's GatedDeltaNet) with
standard Attention. LinearAttention's recurrent state requires
past_present_share_buffer=True unconditionally, so detection now also
checks for LinearAttention nodes.
All five were verified against actual onnxruntime-genai execution of an
Olive RTN + MobiusBuilder export of a tiny Qwen3.5-MoE test model, fixed
one at a time, and each confirmed to move execution further before the next
bug surfaced. Not related to PR #493 (linear_class threading) or #491
(suffix mismatch) -- these bugs exist independently in the base QMoE/
genai-config code and were simply never exercised by prior structural-only
export tests.
Updated _moe_test.py's swiglu_fusion assertions from 2 to 1 to match.1 parent 01008cf commit 2e5b433
3 files changed
Lines changed: 110 additions & 14 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
18 | 57 | | |
19 | 58 | | |
20 | 59 | | |
| |||
56 | 95 | | |
57 | 96 | | |
58 | 97 | | |
59 | | - | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
60 | 102 | | |
61 | 103 | | |
62 | 104 | | |
| |||
92 | 134 | | |
93 | 135 | | |
94 | 136 | | |
95 | | - | |
| 137 | + | |
96 | 138 | | |
97 | 139 | | |
98 | 140 | | |
| |||
105 | 147 | | |
106 | 148 | | |
107 | 149 | | |
108 | | - | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
109 | 154 | | |
110 | 155 | | |
111 | 156 | | |
| |||
154 | 199 | | |
155 | 200 | | |
156 | 201 | | |
157 | | - | |
| 202 | + | |
158 | 203 | | |
159 | 204 | | |
160 | 205 | | |
| |||
164 | 209 | | |
165 | 210 | | |
166 | 211 | | |
167 | | - | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
168 | 216 | | |
169 | 217 | | |
170 | 218 | | |
| |||
298 | 346 | | |
299 | 347 | | |
300 | 348 | | |
| 349 | + | |
301 | 350 | | |
302 | 351 | | |
303 | 352 | | |
| |||
341 | 390 | | |
342 | 391 | | |
343 | 392 | | |
344 | | - | |
345 | | - | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
346 | 429 | | |
347 | 430 | | |
348 | 431 | | |
349 | 432 | | |
350 | | - | |
351 | | - | |
| 433 | + | |
| 434 | + | |
352 | 435 | | |
353 | 436 | | |
354 | 437 | | |
355 | 438 | | |
356 | 439 | | |
357 | 440 | | |
358 | 441 | | |
359 | | - | |
| 442 | + | |
360 | 443 | | |
361 | 444 | | |
362 | 445 | | |
| |||
365 | 448 | | |
366 | 449 | | |
367 | 450 | | |
368 | | - | |
| 451 | + | |
369 | 452 | | |
370 | 453 | | |
371 | 454 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
158 | 158 | | |
159 | 159 | | |
160 | 160 | | |
161 | | - | |
| 161 | + | |
162 | 162 | | |
163 | 163 | | |
164 | 164 | | |
| |||
210 | 210 | | |
211 | 211 | | |
212 | 212 | | |
213 | | - | |
| 213 | + | |
214 | 214 | | |
215 | 215 | | |
216 | 216 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1009 | 1009 | | |
1010 | 1010 | | |
1011 | 1011 | | |
| 1012 | + | |
| 1013 | + | |
| 1014 | + | |
| 1015 | + | |
| 1016 | + | |
| 1017 | + | |
| 1018 | + | |
| 1019 | + | |
1012 | 1020 | | |
1013 | 1021 | | |
1014 | 1022 | | |
1015 | | - | |
| 1023 | + | |
1016 | 1024 | | |
1017 | 1025 | | |
1018 | 1026 | | |
| 1027 | + | |
| 1028 | + | |
| 1029 | + | |
| 1030 | + | |
| 1031 | + | |
1019 | 1032 | | |
1020 | 1033 | | |
1021 | 1034 | | |
| |||
0 commit comments