Skip to content

Commit cea2ac1

Browse files
titaiwangmsCopilot
andcommitted
Gate opset 29 QMoE lowering behind experimental opt-in
Add the default-off experimental_opset29 feature flag and its MOBIUS_EXPERIMENTAL_OPSET29 environment setting. Reject requested QMoE conversion before mutation unless explicitly enabled, while leaving no-QMoE models, native exports and the opset 24 default unchanged. Document that opting in permits explicit lowering but does not imply runtime kernel support. Cover default/env flag behavior, atomic rejection and no-op paths; flag and QMoE suites pass 266 tests with four existing direct-runtime skips. Co-authored-by: Copilot <[email protected]> Signed-off-by: titaiwangms <[email protected]>
1 parent da0dd8c commit cea2ac1

5 files changed

Lines changed: 82 additions & 3 deletions

File tree

‎docs/qmoe-lowering.md‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,25 @@
22

33
```python
44
import onnx_ir as ir
5+
from mobius._flags import override_flags
56
from mobius.functions import lower_qmoe
67

78
model = ir.load("native.onnx")
8-
lower_qmoe(model) # in-place, also returns model
9+
with override_flags(experimental_opset29=True):
10+
lower_qmoe(model) # in-place, also returns model
911
ir.save(model, "standard.onnx")
1012
```
1113

14+
**Experimental, disabled by default.** Enable `experimental_opset29` explicitly,
15+
or set `MOBIUS_EXPERIMENTAL_OPSET29=1` before importing mobius. A QMoE conversion
16+
without this opt-in raises `ValueError` before changing the model. Models without
17+
QMoE remain unchanged, regardless of the flag.
18+
19+
The flag permits experimental lowering; it does not automatically upgrade models
20+
or imply ORT/EP kernel support. Schema/checker support alone does not establish
21+
runtime execution support. In particular, the tested ORT 1.31.1 cannot execute
22+
these opset-29 graphs directly.
23+
1224
This opt-in pass replaces `com.microsoft::QMoE` with standard ONNX blocked
1325
`DequantizeLinear` and two `GroupedMatMul-29` calls. When at least one node is
1426
lowered, it upgrades the model and compatible existing function/subgraph imports

‎src/mobius/_flags.py‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@ class _Flags:
8484
- ``False``
8585
- Lower the ONNX opset declaration to 23 for non-CPU EPs
8686
(ORT <=1.24.x workaround). Disabled by default.
87+
* - ``experimental_opset29``
88+
- ``MOBIUS_EXPERIMENTAL_OPSET29``
89+
- ``False``
90+
- Permit explicit experimental opset-29 lowering.
91+
Does not imply runtime kernel support or upgrade ordinary exports.
8792
* - ``tencent_q1_0_use_native_2bit``
8893
- ``MOBIUS_TENCENT_Q1_0_USE_NATIVE_2BIT``
8994
- ``False``
@@ -134,6 +139,21 @@ class _Flags:
134139
an older ORT build without opset 24 kernel registration.
135140
"""
136141

142+
experimental_opset29: bool = dataclasses.field(
143+
default_factory=lambda: _env_bool("MOBIUS_EXPERIMENTAL_OPSET29", False)
144+
)
145+
"""Permit explicit experimental opset-29 lowering, disabled by default.
146+
147+
Set ``MOBIUS_EXPERIMENTAL_OPSET29=1`` before importing mobius, or use
148+
``override_flags(experimental_opset29=True)``, to allow
149+
:func:`~mobius.functions.lower_qmoe` to produce an opset-29 graph.
150+
Without this opt-in, an attempted QMoE conversion raises before mutation.
151+
152+
This flag does not automatically upgrade models, change native exports,
153+
or imply ORT/EP kernel support. Opset 29 is experimental and may require
154+
source-built ONNX schemas and a separately qualified runtime.
155+
"""
156+
137157
tencent_q1_0_use_native_2bit: bool = dataclasses.field(
138158
default_factory=lambda: _env_bool("MOBIUS_TENCENT_Q1_0_USE_NATIVE_2BIT", False)
139159
)

‎src/mobius/_flags_test.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ def test_gemma4_custom_geglu_default_off(self, monkeypatch):
2222
monkeypatch.delenv("MOBIUS_GEMMA4_CUSTOM_GEGLU", raising=False)
2323
assert _flags._Flags().gemma4_custom_geglu is False
2424

25+
def test_experimental_opset29_default_off(self, monkeypatch):
26+
monkeypatch.delenv("MOBIUS_EXPERIMENTAL_OPSET29", raising=False)
27+
assert _flags._Flags().experimental_opset29 is False
28+
2529

2630
class TestEnvVarOverride:
2731
"""Flags are read from environment variables at construction time."""
@@ -50,6 +54,13 @@ def test_gemma4_custom_geglu_opt_in(self, monkeypatch, value, expected):
5054
monkeypatch.setenv("MOBIUS_GEMMA4_CUSTOM_GEGLU", value)
5155
assert _flags._Flags().gemma4_custom_geglu is expected
5256

57+
@pytest.mark.parametrize(
58+
("value", "expected"), [("1", True), ("true", True), ("0", False), ("maybe", False)]
59+
)
60+
def test_experimental_opset29_opt_in(self, monkeypatch, value, expected):
61+
monkeypatch.setenv("MOBIUS_EXPERIMENTAL_OPSET29", value)
62+
assert _flags._Flags().experimental_opset29 is expected
63+
5364

5465
class TestProgrammaticOverride:
5566
"""Flags can be assigned directly on the singleton."""

‎src/mobius/functions/qmoe.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import numpy as np
1616
import onnx_ir as ir
1717

18+
from mobius._flags import flags
1819
from mobius.functions._qmoe_opset import plan_opset29
1920

2021

@@ -342,13 +343,23 @@ def lower_qmoe(model: ir.Model) -> ir.Model:
342343
Supports FLOAT/FLOAT16 inputs with CPU-reference FLOAT compute, raw UINT8
343344
INT4 weights, blocked scales, interleaved SwiGLU, and rank-2/3 hidden input.
344345
See ``docs/qmoe-lowering.md``.
346+
Requires ``experimental_opset29=True``; otherwise a QMoE conversion raises
347+
``ValueError`` before mutation. Enabling the flag does not trigger lowering
348+
automatically or imply runtime kernel support.
345349
Requires a runtime supporting GroupedMatMul-29 (or function expansion).
346350
"""
347351
roots = [model.graph, *(function.graph for function in model.functions.values())]
348352
plans = []
349353
for root in roots:
350354
for node in root.all_nodes():
351355
if node.domain == "com.microsoft" and node.op_type == "QMoE":
356+
if not flags.experimental_opset29:
357+
raise _error(
358+
"opset 29 is experimental; set MOBIUS_EXPERIMENTAL_OPSET29=1 "
359+
"before importing mobius or use "
360+
"override_flags(experimental_opset29=True). "
361+
"This opt-in does not imply runtime kernel support."
362+
)
352363
if node.op_identifier() in model.functions:
353364
raise _error("caller-defined QMoE functions cannot be overridden")
354365
plans.append((node, _validate(node)))

‎src/mobius/functions/qmoe_test.py‎

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from onnxscript._internal.builder import build_function
1616

1717
from mobius._constants import OPSET_VERSION
18+
from mobius._flags import override_flags
1819
from mobius.functions import lower_qmoe, register_function_bodies
1920

2021
ort = pytest.importorskip("onnxruntime")
@@ -32,6 +33,12 @@
3233
pytestmark = _requires_opset29
3334

3435

36+
@pytest.fixture(autouse=True)
37+
def _enable_experimental_opset29():
38+
with override_flags(experimental_opset29=True):
39+
yield
40+
41+
3542
def _value(name, array):
3643
tensor = ir.tensor(array)
3744
return ir.Value(
@@ -457,6 +464,22 @@ def test_reject_shapes_and_types(index, shape, dtype, message):
457464
assert model.graph.opset_imports[""] == 24
458465

459466

467+
def test_lowering_requires_experimental_opset29_before_mutation(tmp_path):
468+
model = _model()
469+
register_function_bodies(model)
470+
before, after = tmp_path / "before.onnx", tmp_path / "after.onnx"
471+
ir.save(model, before)
472+
473+
with (
474+
override_flags(experimental_opset29=False),
475+
pytest.raises(ValueError, match="MOBIUS_EXPERIMENTAL_OPSET29=1"),
476+
):
477+
lower_qmoe(model)
478+
479+
ir.save(model, after)
480+
assert before.read_bytes() == after.read_bytes()
481+
482+
460483
def test_native_serialization_no_automatic_opset29(tmp_path):
461484
model = _model()
462485
path = tmp_path / "native.onnx"
@@ -715,13 +738,15 @@ def test_compatible_caller_function_preserved_and_aligned(tmp_path):
715738
np.testing.assert_array_equal(actual, feeds["caller_feed"])
716739

717740

718-
def test_no_qmoe_does_not_align_or_reject_caller_functions(tmp_path):
741+
@pytest.mark.parametrize("enabled", [False, True])
742+
def test_no_qmoe_does_not_align_or_reject_caller_functions(tmp_path, enabled):
719743
function = _caller_function("Softmax", opset=11)
720744
graph = ir.Graph([], [], nodes=[], name="no_qmoe", opset_imports={"": 24})
721745
model = ir.Model(graph, ir_version=10, functions=[function])
722746
before, after = tmp_path / "before.onnx", tmp_path / "after.onnx"
723747
ir.save(model, before)
724-
lower_qmoe(model)
748+
with override_flags(experimental_opset29=enabled):
749+
lower_qmoe(model)
725750
ir.save(model, after)
726751
assert before.read_bytes() == after.read_bytes()
727752
assert function.opset_imports[""] == 11

0 commit comments

Comments
 (0)