1717from mobius ._flags import override_flags
1818from mobius .integrations .gguf ._builder_test_utils import _write_quantized_gguf
1919
20- pytestmark = [
21- pytest .mark .opset29 ,
22- pytest .mark .skipif (
23- not all (onnx .defs .has (name , 29 ) for name in ("DequantizePacked" , "GroupedMatMul" )),
24- reason = "Official ONNX opset-29 packed/grouped schemas require current ONNX main." ,
25- ),
26- ]
20+ pytestmark = pytest .mark .opset29
21+
22+
23+ def _has_schema29 (name : str ) -> bool :
24+ return onnx .defs .has (name , 29 ) and onnx .defs .get_schema (name , 29 ).since_version == 29
25+
26+
27+ requires_packed_schema = pytest .mark .skipif (
28+ not _has_schema29 ("DequantizePacked" ),
29+ reason = "DequantizePacked-29 is required for checking and reference execution." ,
30+ )
31+ requires_grouped_schema = pytest .mark .skipif (
32+ not _has_schema29 ("GroupedMatMul" ),
33+ reason = "GroupedMatMul-29 is required for checking and reference execution." ,
34+ )
2735
2836
2937@pytest .mark .parametrize (
@@ -84,6 +92,11 @@ def packed_sources(tmp_path_factory: pytest.TempPathFactory) -> dict[str, Path]:
8492 return paths
8593
8694
95+ @pytest .mark .parametrize (
96+ "validate_schema" ,
97+ [False , pytest .param (True , marks = requires_packed_schema )],
98+ ids = ["binding-roundtrip" , "reference" ],
99+ )
87100@pytest .mark .parametrize (
88101 "qtype" ,
89102 [
@@ -113,7 +126,7 @@ def packed_sources(tmp_path_factory: pytest.TempPathFactory) -> dict[str, Path]:
113126 "Q1_0" ,
114127 ],
115128)
116- def test_all_stored_reader_formats (tmp_path : Path , qtype : str ) -> None :
129+ def test_all_stored_reader_formats (tmp_path : Path , qtype : str , validate_schema : bool ) -> None :
117130 from types import SimpleNamespace
118131
119132 import gguf
@@ -184,6 +197,15 @@ def test_all_stored_reader_formats(tmp_path: Path, qtype: str) -> None:
184197 package .save (str (tmp_path ))
185198 restored = ModelPackage .load (str (tmp_path ))
186199 assert [value .name for value in restored ["model" ].graph .outputs ] == ["out" ]
200+ payload = next (
201+ v .const_value
202+ for v in restored ["model" ].graph .initializers .values ()
203+ if isinstance (v .const_value , ir .ExternalTensor )
204+ )
205+ assert payload .numpy ().tobytes () == raw .tobytes ()
206+ assert any (n .op_type == "Scan" for n in model .graph .all_nodes ())
207+ if not validate_schema :
208+ return
187209 onnx .checker .check_model (str (tmp_path / "model.onnx" ), full_check = True )
188210 evaluator = ReferenceEvaluator (str (tmp_path / "model.onnx" ))
189211 indices = np .array ([[1 , 0 , 1 ], [- 1 , - 2 , 0 ]], dtype = np .int64 )
@@ -201,13 +223,6 @@ def test_all_stored_reader_formats(tmp_path: Path, qtype: str) -> None:
201223 expected = gguf .dequantize (raw , kind )
202224 assert np .isfinite (expected ).all () and np .count_nonzero (expected )
203225 np .testing .assert_allclose (actual , expected [indices ], rtol = 1e-6 , atol = 1e-7 )
204- payload = next (
205- v .const_value
206- for v in model .graph .initializers .values ()
207- if isinstance (v .const_value , ir .ExternalTensor )
208- )
209- assert payload .numpy ().tobytes () == raw .tobytes ()
210- assert any (n .op_type == "Scan" for n in model .graph .all_nodes ())
211226
212227
213228@pytest .mark .parametrize ("architecture" , ["qwen2" , "llama" ])
@@ -221,7 +236,6 @@ def test_packed_build_roundtrip(packed_sources: dict[str, Path], architecture: s
221236 assert package .gguf_quantization_report .source_fidelity
222237 assert any (n .op_type == "DequantizePacked" for n in package ["model" ].graph )
223238 package .save (str (tmp_path ))
224- onnx .checker .check_model (str (tmp_path / "model.onnx" ), full_check = True )
225239 restored = ModelPackage .load (str (tmp_path ))
226240 restored .save (str (tmp_path ))
227241 assert not (tmp_path / "model.onnx.data" ).exists ()
@@ -251,7 +265,6 @@ def test_split_shards_and_bf16_preserve_external_ranges(
251265 and v .const_value .dtype == ir .DataType .BFLOAT16
252266 for v in package ["model" ].graph .initializers .values ()
253267 )
254- onnx .checker .check_model (str (tmp_path / "model.onnx" ), full_check = True )
255268 ModelPackage .load (str (tmp_path )).save (str (tmp_path ))
256269
257270
@@ -317,6 +330,7 @@ def test_packed_save_rejects_missing_generated_initializer(
317330 package .save (str (source .parent ))
318331
319332
333+ @requires_packed_schema
320334def test_optimization_cannot_fold_original_external_weights (
321335 packed_sources : dict [str , Path ],
322336) -> None :
@@ -332,7 +346,10 @@ def test_optimization_cannot_fold_original_external_weights(
332346 assert not (tmp_path / "model.onnx.data" ).exists ()
333347
334348
335- @pytest .mark .parametrize ("architecture" , ["qwen2moe" , "qwen3moe" ])
349+ @pytest .mark .parametrize (
350+ "architecture" ,
351+ ["qwen2moe" , pytest .param ("qwen3moe" , marks = requires_grouped_schema )],
352+ )
336353def test_packed_expert_layout_and_standard_moe (
337354 packed_sources : dict [str , Path ], architecture : str
338355) -> None :
@@ -347,10 +364,32 @@ def test_packed_expert_layout_and_standard_moe(
347364 v .name .endswith ("ffn_up_exps.weight" ) and isinstance (v .const_value , ir .ExternalTensor )
348365 for v in package ["model" ].graph .initializers .values ()
349366 )
350- onnx .checker .check_model (str (tmp_path / "model.onnx" ), full_check = True )
351367
352368
353- @pytest .mark .parametrize ("architecture" , ["parity" , "qwen2moe" , "qwen3moe" ])
369+ @requires_packed_schema
370+ @pytest .mark .parametrize (
371+ "architecture" ,
372+ [
373+ "qwen2" ,
374+ "llama" ,
375+ "split" ,
376+ "qwen2moe" ,
377+ pytest .param ("qwen3moe" , marks = requires_grouped_schema ),
378+ ],
379+ )
380+ def test_packed_model_schema (packed_sources : dict [str , Path ], architecture : str ) -> None :
381+ source = packed_sources [architecture ]
382+ with override_flags (experimental_opset29 = True ):
383+ package = build_from_gguf (source , execution_provider = "onnx-standard" )
384+ package .save (str (source .parent ))
385+ onnx .checker .check_model (str (source .parent / "model.onnx" ), full_check = True )
386+
387+
388+ @requires_packed_schema
389+ @pytest .mark .parametrize (
390+ "architecture" ,
391+ ["parity" , "qwen2moe" , pytest .param ("qwen3moe" , marks = requires_grouped_schema )],
392+ )
354393def test_packed_next_token_matches_float_source (
355394 packed_sources : dict [str , Path ], architecture : str
356395) -> None :
0 commit comments