Repository navigation
Commit d6d45b7
Add experimental opset 29 function fallbacks for the MoE family (#814)
## Summary
Add **experimental, default-off opset-29 function fallbacks** for native
`com.microsoft::MoE` and raw INT4 `com.microsoft::QMoE`.
Use the existing **function factory → registry → EP-controlled public
`InlinePass`** mechanism. `moe(config)` and `qmoe(config)` return real,
configuration-specialized `ir.Function` bodies. No separate lowering
pass,
replacement emitter, `_opset29.py` auditor, or additional framework
remains.
```python
from mobius import build_from_module
from mobius._flags import override_flags
with override_flags(experimental_opset29=True):
package = build_from_module(module, config, execution_provider="onnx-standard")
```
`MOBIUS_EXPERIMENTAL_OPSET29=1` also enables the flag before import.
| Build path | Behavior |
|---|---|
| Flag off | Existing opset-24/default behavior, except the QNN
capability correction below |
| Flag on, native-capable EP | Existing opset-24 native calls; no
fallback ABI restrictions |
| Flag on, fallback EP | New graph/functions constructed at 29;
supported native calls registered and inlined |
| Existing opset-24 graph requesting the new fallback | Explicit rebuild
error, not import relabeling/version conversion |
The global `OPSET_VERSION` remains 24. Generic dense `MoELayer`
implementations,
arbitrary routing gates, and Qwen4 routing algebra are unchanged.
**Gemma4's model source is restored, not extended:** `gemma4.py` is
byte-identical to PR base `858ead` and the reviewed main snapshot
`e475e4871b0430f58654bc25b13fd17806b2f9c5`. Non-native EPs retain its
dense
Einsum path at both opset 24 and 29. Existing native/quantized branch
implementations are unchanged. This PR does not add direct GroupedMatMul
Gemma dispatch.
**QNN is an intentional flag-off exception:** correcting its profile to
`supports_fused_moe=False` changes capability-based model dispatch even
when
`experimental_opset29=False`. Qwen4 selects its existing
`_standard_dispatch`;
eligible unquantized Gemma4 configurations that previously selected
native MoE
select the existing dense path instead. QNN still builds at opset 24
with the
flag off, without registering these MoE/QMoE fallback bodies. This fixes
an
incorrect kernel capability, not a new model dispatch implementation,
and
does not qualify QNN device execution of opset 29 or quantized Gemma4.
## Supported semantics
- **MoE:** row-major expert weights, FLOAT/FLOAT16, optional biases,
ReLU/tanh-GELU/SiLU/identity/interleaved SwiGLU, full-expert FLOAT
softmax
before TopK, higher-expert-ID probability ties, selected normalization,
positive-route masking, CPU-reference HALF rounding, FLOAT weighted
reduction.
- **Raw INT4 QMoE:** low-nibble-first UINT8 weights, blocked scales,
optional packed asymmetric zero points (**omission means 8**),
`weights_prepacked=0`, interleaved SwiGLU, logit-first CPU routing,
input-14 router weights, `<=1e-8` route suppression, and FLOAT
dequantization/GEMMs before any final HALF cast.
- Rank-2/rank-3 hidden inputs, dynamic/empty tokens, original packed
initializer names, optional input positions, and compatible output
annotations/docs/metadata are preserved.
CPU source supplies the **semantic reference**, not a CPU-only execution
restriction or an all-provider equivalence claim. Unsupported ABIs fail
explicitly. BFLOAT16, FP4/MXFP4, prepacked weights, arbitrary gates,
CUDA-specific routing, and fused-kernel performance are not qualified.
Registration validates every target, containing-root imports, ownership,
and collisions before body construction/mutation. Binary32-canonical
specialization identities survive save/load/re-registration.
Caller-owned
functions remain unmodified. Public inliner preconditions remain
authoritative;
distinct alias-return boundaries prevent clobbering input/upstream
metadata.
## Copilot review 5880527221: four confirmed issues repaired
| Issue | Repair and regression evidence |
|---|---|
| QNN incorrectly inherits fused MoE capability | Set
`supports_fused_moe=False`. Test flag-off 24 / flag-on 29 construction
and actual optimizer registration/public inlining of raw INT4 QMoE. **No
QNN runtime qualification is implied.** |
| Empty-key-only core-opset lookups reject valid aliases | Use one
read-only resolver for model roots, containing functions,
source/destination specializations, and ordinary factories. Accept sole
`ai.onnx` and matching dual aliases; reject conflicts atomically without
normalizing imports. Preserve flag-first validation and public inliner
mismatch checks. |
| Added Gemma branch changes tied TopK selection | Remove the added
integration instead of reversing model tensors. Original lower-ID dense
TopK remains; add flag-off/on, preinitialized/scale-only top-1 numerical
cases. |
| Folding live expert scale into W2 overflows early | Restore original
contribution-side scaling and dense dispatch. Add the same matrix with
W2=`2e38`, live scale=`2`, and intermediate approximately `0.1`; require
finite output approximately `4e37`. |
Lead independently executed the **old e217b50 graphs**, not merely
structural
assertions: tied inputs changed `0.43863517` to `-3.72839904` with the
flag;
the overflow example changed finite `3.99999972e37` to infinity. Both
were
reproduced for preinitialized weights and scale-only updates.
The repaired graph's numerical regressions pass against independent
references.
No tolerance weakening, weight reversal, or compensating routing
transform.
The original proposed Gemma FLOAT scale-folding repair is superseded by
this
restoration; it is not retained or claimed as a fix.
## Copilot review 5900254968: documentation and coverage follow-up
| Issue | Repair |
|---|---|
| QNN flag-off dispatch exception was undocumented | Both lowering
guides and this description now explicitly distinguish the QNN profile
correction from the default-off opset-29 feature. Existing Qwen4/Gemma
branches are selected differently on QNN even with the flag off; no new
model implementation is added. |
| Multi-layer post-build quantized-weight binding was untested | Add two
consecutive shared raw-INT4 `MoELayer`s through `build_from_module(...,
task="feature-extraction", execution_provider="onnx-standard")`, with
every parameter initially unbound. Apply original packed checkpoint
names only to the returned package, assert every binding is consumed,
and compare a test-only expanded copy against independent NumPy
dequantization/routing over two batches and three tokens. Production
imports remain 29. |
| Flag-off test used a native-capable profile | Use QNN, assert its
capability is false, and check unchanged native inputs/attributes plus
absence of fallback functions. A gate-only in-memory mutation produces
two expected failures, demonstrating that the corrected regression
detects removal of the flag guard. |
This follow-up changes only two existing test files and the two lowering
guides; no production dispatch, optimizer, or lowering implementation
changes.
The released-schema environment skips the new standard-pipeline case
because
its final public validator needs the official GroupedMatMul-29 schema. A
QNN-only diagnostic is not treated as acceptance for the requested
`onnx-standard` path; that path is validated with the exact schema pin.
### November 14 focused validation
Command: `python -m pytest src/mobius/functions/moe_registration_test.py
src/mobius/functions/qmoe_test.py -m opset29 -q -rs`.
| Check | Result |
|---|---|
| Released ONNX 1.19, current IR/ONNXScript | **394 passed, 7 skipped**;
includes the schema-gated new export case |
| Exact pinned ONNX source, current IR/ONNXScript, integrated root
checkout | **397 passed, 4 skipped**; the real standard-export
multi-layer case executes and passes |
| Flag-guard-only mutation | **2 expected failures** with the corrected
fallback-profile test; mutation restored |
| Ruff lint/format and complete four-file diff check | Passed |
The four pinned skips are existing direct opset-29 runtime probes,
unsupported
by ORT 1.31.1. The multi-layer fixture found no production metadata or
binding
bug: it verifies existing behavior that previously lacked end-to-end
coverage.
Lead reviewed the complete four-file follow-up; this is not a new
unified
review of the cumulative 46-file PR.
## Justin's eleven review comments
Implementation: `d9c4acc4848e4dfbeb66d5071dd0fa9e9f1b1062`.
Comment-only readability follow-up:
`982a05412c3361ed58aa8426827bf28e2a1fd511`.
Cold-collection dependency fix:
`e217b5093b4dbd9584cc0c25f89f16cadc544c11`.
| Request | Change |
|---|---|
| Select CI tests by marker | Register `opset29`; both phases use
`pytest src/mobius tests/ep_optimization_test.py -m opset29`. At that
revision, all 832 previous cases retained and 40 added. Collection
dependencies `httpx`/`gguf`/`jsonschema` are explicit. |
| Remove redundant docs section | Remove QMoE's Runtime/validation
section and repair the inbound MoE link. |
| Bound inlining | At most initial function count + 1 rounds, with
leaf-retirement progress checks and explicit failure. Preserve public
validation and the existing unused-leaf clean exit. |
| Avoid installed-schema dependence | Remove the redundant GroupedMatMul
availability gate. Public construction, registration, and inlining work
on actual ONNX 1.19; checking/export auditing remains schema-dependent.
|
| Use SwiGLU | Standard SwiGLU-28 in both bodies, retaining clipping,
alpha/beta, and HALF boundaries. |
| Use Swish | Standard Swish-24 for MoE SiLU. |
| Expand variable names | Spell out QMoE internals without changing the
native 15-input positional ABI. |
| TODO for unpack | Add TODO; no current standard raw nibble-unpack op.
BitCast requires equal widths; DequantizePacked's GGUF formats do not
match raw QMoE. |
| Is zero-point construction redundant? | Retain explicit default 8:
omitted ONNX DQ zeros instead mean 0. Numerical tests compare omission
with explicit 8 and distinguish explicit 3. |
| Simplify routing/pattern matching | Remove the redundant routing
Where. Retain safe zero-total normalization and the final contribution
Where, which suppresses inactive expert NaNs. No fusion/performance
promise. |
| Use Slice | Negative-step expert reversal and stride-2 gate/up slicing
replace index lists in both bodies. Comments explain endpoints and
interleaving. |
Operator versions were compared with the official ONNX catalog at
`94d0e6056931a8c1dad84104c2e567e0011d04f2` and the pinned schema
revision
below; the relevant versions agree.
### Earlier review and public IR repairs
Retain binary32 identity roundtrips, alias-output metadata isolation,
direct
MoE activation validation, direct QMoE configuration/block validation,
and
snapshotted-opset dispatch. Gemma HALF rounding and remaining live
scales
are protected by retaining its original dense implementation, not by
adding
new native-call integration. Earlier code-quality suggestions remain
addressed.
Use public `Function.clone`, `Graph.append`, `Function.outputs`,
`Shape.copy`,
graph traversal, `InlinePass.requires`/execution,
`ir.convenience.extract`,
`ir.convenience.replace_nodes_and_values`, and `ir.save`/`ir.load`.
No new private graph-state mutation, explicit protobuf operation,
production
activation expansion, or production opset relabeling was added.
## Dependencies and runtime limits
**Construction/registration does not require the installed opset-29
schemas.**
Normal export validation, ONNX checking, and schema inference still
audit
real schemas; removing the registration gate does not bypass them.
The experimental schema environment builds official ONNX revision
`82721c0908fa965dd84e674c006147791313f3e1` (reporting 1.24), with
IR 1.1.0 / onnxscript 0.7.2. ONNX 1.19 is the released-schema
compatibility
environment. Default dependency minimums remain unchanged.
IR 1.0.0 cannot parse the pinned source build's newer `FLOAT6E2M3` enum,
also reproduced on the unchanged earlier parent with ordinary opset-29
MatMulNBits construction. Minimum API availability does not imply the
minimum packages can parse every experimental schema. The actual
ONNX 1.19 + minimum IR 1.0.0 / onnxscript 0.7.1 combination is tested
below.
ORT 1.31.1 does **not** execute these opset-29 graphs directly. Generic
MoE/QMoE numerical tests expand GroupedMatMul and SwiGLU on a
**test-only
copy** for older kernels, using Swish-24 plus Mul for SwiGLU. Production
retains standard operators and original imports. Restored dense Gemma
regressions use ReferenceEvaluator at their original 24/29 imports.
Native HALF MoE tests have explicit capability skips on the FLOAT-only
CPU
registration; independent rounded-HALF coverage stays active.
## Prior full-cohort validation at 1a45c28
Broad command: `python -m pytest src/mobius
tests/ep_optimization_test.py -m opset29 -q -rs`.
| Check | Result |
|---|---|
| Final repair, actual ONNX 1.19 + IR 1.1.0 / onnxscript 0.7.2 | **893
passed, 23 skipped, 9995 deselected** |
| Same repair, actual ONNX 1.19 + minimum IR 1.0.0 / onnxscript 0.7.1 |
**893 passed, 23 skipped, 9995 deselected** |
| Same repair, exact pinned ONNX schema source + IR 1.1.0 / onnxscript
0.7.2 | **905 passed, 11 skipped, 9995 deselected** |
| Marker coverage | **916 selected:** 865 unchanged IDs, 7 explicitly
renamed Gemma cases for restored dispatch, 44 new cases; no prior
parameter case missing |
| Existing targeted Gemma MoE/expert/preprocessing/precision suite |
**68 passed** |
| Fail-before regressions, developer | Gemma **5 failed / 9 passed**;
QNN/aliases **21 failed / 14 passed** |
| Independent old-head numerical reproduction, lead | Both actual
wrong-expert and finite-to-infinity failures reproduced |
| Ruff lint/format on all five changed Python files; diff check | Passed
|
| Mypy on the three changed production sources versus exact e217b50
baseline | **65 pre-existing Gemma errors, zero added/removed
diagnostics**, exact message/column/unchanged-line comparison; resolver
and provider clean |
The 11 pinned skips are five unavailable native MoE HALF kernels, four
unsupported direct opset-29 runtime cases, and two optional OpenVINO
cases.
Released-schema runs additionally skip schema-dependent checks, not the
new dense Gemma export test. Local results are not overall hosted-CI
success.
## Hosted CI diagnosis
At e217b50, the dedicated experimental job **passed both stages**:
released ONNX **848 passed / 24 skipped**, pinned schemas
**861 passed / 11 skipped**. The previously pending pinned stage
completed.
The three failures in run `43282577546` (Integration fast, L4, L5)
occurred
**before tests**, while installing ORT GPU from external Azure feed/blob
endpoints: read timeouts/connection resets, not model assertions.
Those installation commands are unchanged versus exact PR base `858ead`.
On that exact base, Integration fast **passed** and L4/L5 were
**cancelled**;
these are **not claimed to be matching base failures**.
No unrelated workflow retry/tolerance/skip change is included.
The completed run `43300507986` at `1a45c280` passed the experimental
stages
(**893 passed / 23 skipped** released; **905 passed / 11 skipped**
pinned),
the Linux/Windows test matrix, Lint, L1, L3, and fast CPU OGA.
L4/L5 were **cancelled**, not passed. The next repair push starts fresh
CI;
its status must be checked separately.
## Review, dissent, and exclusions
Historical static review of d9c4acc: Code and Critical used actual
`gpt-6.1-sol`; Deep and Readability used actual `claude-sonnet-5.5`,
verified
from runtime events. This bounded follow-up received direct lead review
and
developer/lead execution evidence; **no fresh full-team approval is
claimed**.
| ID | Severity | Raised by | Finding | Disposition |
|---|---|---|---|---|
| C1 | Major | Copilot | QNN native capability | **Confirmed; fixed** |
| C2 | Major | Copilot | Core import aliases | **Confirmed; fixed** |
| C3 | Major | Copilot | Gemma tie ordering | **Confirmed; fixed by
removing added integration** |
| C4 | Major | Copilot | Gemma remaining-scale overflow | **Confirmed;
fixed by removing added integration** |
| F1, historical | Major | Deep | Uncalled selected functions allegedly
reach the no-progress error | **Rejected; reviewer withdrew.** Existing
no-annotation early return handles them; reproduced on IR 1.0.0 and
1.1.0. |
**Minority/correction record:** F1 was disproved by its concrete trace
and
existing branch, then withdrawn. Deep also corrected reversed-diff
claims
about the unpack TODO and explicit-zero comment. No disagreement remains
on F1. C3/C4's bugs were accepted; the minimal restoration replaces the
suggested tensor-reversal workaround.
**Attributed questions:** Readability's active-mask placement question
remains
non-blocking; its placement keeps routing-threshold semantics together.
Deep withdrew the raw-unpack question after confirming the TODO and
absence
of a standard raw-unpack operator.
**Coverage gap:** Historical Integration review remains blocked by the
authored/runtime mismatch (`mai-code-2.x1` versus `mai-code-2`); its
coverage
is not credited or replaced. Historical Deep/Readability excluded
numeric
test bodies; runtime evidence comes from lead/developer. Critical's
threat
model was N/A; this follow-up adds no privileged execution boundary.
Earlier non-blocking limits remain: bypassing Gemma weight preprocessing
is
not newly qualified; QMoE accepts a bounded power-of-two block subset;
direct configs require Python integers; uninlined optional arguments in
other consumers are not qualified.
**Residual risk:** no direct opset-29 kernels, QNN device execution,
CUDA/other-provider parity, performance/load measurements, GPU/L5
full-model
generation, arbitrary gates, BFLOAT16/FP4/prepacked qualification,
exhaustive
HALF proof, or whole-repository integration clearance. Whole-repository
Mypy is not clean. Task-created caches/intermediate models are removed
after validation; small evidence reports are retained.
The full cumulative 46-file diff has not received a unified latest-head
internal review; previous snapshot and bounded follow-up reviews are not
claimed as equivalent coverage.
## Scope and history
This consolidates the earlier MoE/QMoE work into #814; #815 remains
closed
unmerged. Earlier thread resolutions remain scoped to their cited
evidence,
including containing-root import authority and public IR alias handling.
No private-domain workaround was added.
See [float MoE](docs/moe-lowering.md) and
[INT4 QMoE](docs/qmoe-lowering.md) for qualified ABIs and runtime
exclusions.
---------
Signed-off-by: titaiwangms <[email protected]>
Co-authored-by: Copilot <[email protected]>1 parent fe7043a commit d6d45b7
46 files changed
Lines changed: 5175 additions & 61 deletions
File tree
- .github/workflows
- docs
- src/mobius
- _testing
- components
- functions
- models
- tasks
- tests
Some content is hidden
Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
27 | 27 | | |
28 | 28 | | |
29 | 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 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
30 | 66 | | |
31 | 67 | | |
32 | 68 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
32 | 32 | | |
33 | 33 | | |
34 | 34 | | |
| 35 | + | |
| 36 | + | |
35 | 37 | | |
36 | 38 | | |
37 | 39 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 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 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
0 commit comments