Skip to content

Commit c8803af

Browse files
authored
Fix Qwen3.5 standalone ORT GenAI model types (#631)
## Summary - normalize standalone Qwen3.5/Qwen3.6 text packages to ORT GenAI's released generic `decoder` runtime - preserve distinct `qwen3_5` and `qwen3_5_moe` runtime types for dense and MoE multimodal packages - recover the correct VLM parent type when config-mode exports expose only an unwrapped text subconfig - add regression coverage across decoder-only, HF-parent multimodal, and config-mode multimodal paths ## Motivation Mobius previously mapped every Qwen3.5/Qwen3.6 variant to `qwen3_5`. For a standalone Qwen3.6 text export, that selects ORT GenAI's multimodal runtime and requires vision and embedding sessions that do not exist in a single-component package. Standalone graphs use the generic `decoder` contract already supported by released ORT GenAI. Full multimodal packages still require architecture-specific runtime types: `qwen3_5` for dense models and `qwen3_5_moe` for MoE models. This blocks the standalone text-generation recipe being prepared in microsoft/olive-recipes#588. ## Validation - `python -m pytest src/mobius/integrations/ort_genai/auto_export_test.py src/mobius/integrations/ort_genai/genai_config_test.py` (252 passed) - targeted decoder-only, HF-parent multimodal, and config-mode multimodal tests (14 passed) - `ruff check` and `ruff format --check` on affected files - `git diff --check` --------- Copilot-Session: 72aa2acc-7ef6-4fe4-b760-921d80d29dd7
1 parent 133ab10 commit c8803af

3 files changed

Lines changed: 155 additions & 22 deletions

File tree

‎src/mobius/integrations/ort_genai/auto_export.py‎

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -117,16 +117,18 @@ def _revision_kwargs(revision: str | None) -> dict[str, str]:
117117
"qwen2_vl": "qwen2_5_vl",
118118
"qwen3_vl": "qwen3_vl",
119119
"qwen3_vl_text": "qwen3_vl",
120-
# Qwen3.5 / Qwen3.6 use the Qwen-VL auxiliary vision+embedding pipeline
121-
# but a hybrid DeltaNet/full-attention decoder, so they must select the
122-
# native ORT GenAI qwen3_5 model type rather than the Qwen2.5-VL decoder.
120+
# Preserve Qwen3.5 / Qwen3.6 source architecture identities here so package
121+
# topology selection can distinguish standalone text from dense and MoE
122+
# multimodal parents. Standalone text packages are normalized later to the
123+
# released generic "decoder" type; multimodal variants retain the matching
124+
# Qwen-VL type so the runtime constructs the vision+embedding pipeline.
123125
"qwen3_5": "qwen3_5",
124-
"qwen3_5_text": "qwen3_5",
126+
"qwen3_5_text": "qwen3_5_text",
125127
"qwen3_5_vl": "qwen3_5",
126128
"qwen3_5_vl_text": "qwen3_5",
127-
"qwen3_5_moe": "qwen3_5",
128-
"qwen3_5_moe_text": "qwen3_5",
129-
"qwen3_5_moe_vl": "qwen3_5",
129+
"qwen3_5_moe": "qwen3_5_moe",
130+
"qwen3_5_moe_text": "qwen3_5_moe_text",
131+
"qwen3_5_moe_vl": "qwen3_5_moe",
130132
# GLM-OCR uses the Qwen2.5-VL three-model runtime contract: packed image
131133
# patches, M-RoPE position IDs, an embedding mixer, and a cached decoder.
132134
"glm_ocr": "qwen2_5_vl",
@@ -145,6 +147,18 @@ def _revision_kwargs(revision: str | None) -> dict[str, str]:
145147
"lfm2": "lfm2",
146148
"lfm2_vl": "lfm2",
147149
}
150+
# Composite configs are unwrapped to their text sub-config during config-mode
151+
# builds. Recover the parent runtime type when the exported package still has
152+
# the full multimodal topology.
153+
_UNWRAPPED_VLM_MODEL_TYPES = {
154+
"gemma3_text": "gemma3",
155+
# Gemma3n must retain its own pipeline: it binds per-layer inputs that the
156+
# Gemma3 runtime does not support.
157+
"gemma3n_text": "gemma3n",
158+
"qwen3_5_text": "qwen3_5",
159+
"qwen3_5_vl_text": "qwen3_5",
160+
"qwen3_5_moe_text": "qwen3_5_moe",
161+
}
148162
_LONGROPE_TEXT_TYPES = frozenset({"phi3", "phi3small", "phimoe"})
149163
_GENERIC_DECODER_MIN_VERSION = (0, 14, 0)
150164
_GENERIC_DECODER_TESTED_VERSIONS = ("0.15.2",)
@@ -275,7 +289,10 @@ def _select_ort_model_type(
275289
``DecoderOnly_Model``. Decoder-only packages therefore use that type unless
276290
the runtime has genuinely different behavior: ``gpt2`` selects ``Gpt_Model``,
277291
``lfm2`` selects ``LFM2_Model``/``LFM2Cache``, and Phi-3 family names are
278-
retained only for LongRoPE cache recomputation after the short-context threshold.
292+
retained only for LongRoPE cache recomputation after the short-context
293+
threshold. Standalone Qwen3.5 text configs also normalize to ``decoder``:
294+
their specialized names dispatch to the same ``DecoderOnly_Model`` and are
295+
not available in the latest released ORT GenAI.
279296
280297
Multimodal and encoder-decoder packages retain their architecture-specific
281298
type because those values select distinct runtime pipelines and position-ID
@@ -1894,16 +1911,8 @@ def write_ort_genai_config(
18941911
# Fall back to fields stored in ArchitectureConfig (set by from_transformers()).
18951912
# This path is taken when hf_model_id is not provided (e.g. --config mode).
18961913
raw_type = getattr(config, "model_type", None) or "unknown"
1897-
if is_vlm and raw_type == "gemma3_text":
1898-
# Gemma3 multimodal configs are unwrapped to the text sub-config
1899-
# during build, but ORT GenAI needs the multimodal parent type.
1900-
ort_model_type = "gemma3"
1901-
elif is_vlm and raw_type == "gemma3n_text":
1902-
# Same unwrapping for Gemma3n, whose parent type is "gemma3n".
1903-
# Deliberately *not* aliased to "gemma3": the package threads
1904-
# per_layer_inputs (and optional audio) that gemma3's ORT pipeline
1905-
# does not bind, so borrowing that type would mis-wire the graph.
1906-
ort_model_type = "gemma3n"
1914+
if is_vlm and raw_type in _UNWRAPPED_VLM_MODEL_TYPES:
1915+
ort_model_type = _UNWRAPPED_VLM_MODEL_TYPES[raw_type]
19071916
else:
19081917
ort_model_type = _select_ort_model_type(
19091918
raw_type,

‎src/mobius/integrations/ort_genai/auto_export_test.py‎

Lines changed: 117 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,21 @@ def test_gemma4_unified_model_types(self):
159159
assert _resolve_ort_genai_model_type("gemma4") == "gemma4"
160160
assert _resolve_ort_genai_model_type("gemma4_text") == "gemma4_text"
161161

162+
@pytest.mark.parametrize(
163+
("source_type", "expected"),
164+
[
165+
("qwen3_5_text", "qwen3_5_text"),
166+
("qwen3_5_moe_text", "qwen3_5_moe_text"),
167+
("qwen3_5", "qwen3_5"),
168+
("qwen3_5_vl", "qwen3_5"),
169+
("qwen3_5_vl_text", "qwen3_5"),
170+
("qwen3_5_moe", "qwen3_5_moe"),
171+
("qwen3_5_moe_vl", "qwen3_5_moe"),
172+
],
173+
)
174+
def test_qwen35_source_and_multimodal_model_type_mapping(self, source_type, expected):
175+
assert _resolve_ort_genai_model_type(source_type) == expected
176+
162177

163178
class TestSelectOrtModelType:
164179
"""Text-only / multimodal ORT model type selection (PR: text_only export)."""
@@ -191,6 +206,32 @@ def test_decoder_only_unknown_config_uses_generic_decoder(self):
191206
== "decoder"
192207
)
193208

209+
@pytest.mark.parametrize("model_type", ["qwen3_5_text", "qwen3_5_moe_text"])
210+
def test_decoder_only_qwen35_uses_released_generic_decoder(self, model_type):
211+
assert (
212+
_select_ort_model_type(model_type, "qwen3_5_moe", is_decoder_only=True)
213+
== "decoder"
214+
)
215+
216+
@pytest.mark.parametrize(
217+
("config_model_type", "hf_model_type", "expected"),
218+
[
219+
("qwen3_5_text", "qwen3_5", "qwen3_5"),
220+
("qwen3_5_moe_text", "qwen3_5_moe", "qwen3_5_moe"),
221+
],
222+
)
223+
def test_multimodal_qwen35_preserves_parent_vlm_runtime_type(
224+
self, config_model_type, hf_model_type, expected
225+
):
226+
assert (
227+
_select_ort_model_type(
228+
config_model_type,
229+
hf_model_type,
230+
is_decoder_only=False,
231+
)
232+
== expected
233+
)
234+
194235
def test_decoder_only_preserves_specialized_hf_fallback(self):
195236
assert (
196237
_select_ort_model_type("not_a_real_type", "gpt2", is_decoder_only=True) == "gpt2"
@@ -1060,6 +1101,18 @@ class TestWriteOrtGenaiConfigLocalDir:
10601101
def _make_pkg():
10611102
return _make_fake_llm_pkg("llama")
10621103

1104+
def test_qwen35_moe_text_emits_standalone_llm_config(self, tmp_path):
1105+
pkg = _make_fake_llm_pkg("qwen3_5_moe_text")
1106+
1107+
result = write_ort_genai_config(pkg, str(tmp_path))
1108+
1109+
with open(result["genai_config"]) as f:
1110+
model = json.load(f)["model"]
1111+
assert model["type"] == "decoder"
1112+
assert model["decoder"]["filename"] == "model.onnx"
1113+
assert "vision" not in model
1114+
assert "embedding" not in model
1115+
10631116
def test_local_config_dir_copies_tokenizer_files(self, tmp_path):
10641117
"""When local_config_dir is set, tokenizer files are copied from it."""
10651118
src = tmp_path / "local_model"
@@ -1535,7 +1588,7 @@ class FakeConfig:
15351588
assert resize["min_pixels"] == 12544
15361589
assert resize["max_pixels"] == 9633792
15371590

1538-
def test_qwen36_vl_hf_parent_type_maps_to_qwen35_runtime(self, tmp_path):
1591+
def test_qwen36_vl_hf_parent_type_maps_to_qwen35_moe_runtime(self, tmp_path):
15391592
import dataclasses
15401593

15411594
from mobius._model_package import ModelPackage
@@ -1551,7 +1604,7 @@ class FakeVision:
15511604
class FakeConfig:
15521605
# build_transformers_model unwraps Qwen3.6 VL to the text sub-config,
15531606
# but write_ort_genai_config must preserve the multimodal HF parent
1554-
# type and map it to ORT GenAI's native qwen3_5 runtime.
1607+
# type and map it to ORT GenAI's native qwen3_5_moe VLM runtime.
15551608
model_type: str = "qwen3_5_moe_text"
15561609
vocab_size: int = 248064
15571610
hidden_size: int = 2048
@@ -1601,7 +1654,7 @@ class FakeConfig:
16011654

16021655
with open(result["genai_config"]) as f:
16031656
data = json.load(f)
1604-
assert data["model"]["type"] == "qwen3_5"
1657+
assert data["model"]["type"] == "qwen3_5_moe"
16051658
assert data["model"]["vision"]["patch_size"] == 16
16061659

16071660
def test_mage_vl_is_rejected_before_writing_runtime_artifacts(self, tmp_path):
@@ -2256,6 +2309,67 @@ class FakeConfig:
22562309
data = json.load(f)
22572310
assert data["model"]["type"] == "gemma3n"
22582311

2312+
@pytest.mark.parametrize(
2313+
("text_model_type", "expected_runtime_type"),
2314+
[
2315+
("qwen3_5_text", "qwen3_5"),
2316+
("qwen3_5_vl_text", "qwen3_5"),
2317+
("qwen3_5_moe_text", "qwen3_5_moe"),
2318+
],
2319+
)
2320+
def test_config_mode_qwen35_text_vlm_recovers_multimodal_type(
2321+
self,
2322+
tmp_path,
2323+
text_model_type,
2324+
expected_runtime_type,
2325+
):
2326+
"""Unwrapped Qwen3.5/3.6 VLM configs retain their VLM runtime pipeline."""
2327+
import dataclasses
2328+
2329+
from mobius._model_package import ModelPackage
2330+
from mobius.integrations.ort_genai.auto_export import write_ort_genai_config
2331+
2332+
@dataclasses.dataclass
2333+
class FakeVision:
2334+
image_size: int = 448
2335+
patch_size: int = 16
2336+
spatial_merge_size: int = 2
2337+
window_size: int = 112
2338+
2339+
@dataclasses.dataclass
2340+
class FakeConfig:
2341+
model_type: str
2342+
vocab_size: int = 248064
2343+
hidden_size: int = 2048
2344+
num_hidden_layers: int = 2
2345+
num_attention_heads: int = 16
2346+
num_key_value_heads: int = 8
2347+
head_dim: int = 128
2348+
max_position_embeddings: int = 128
2349+
image_token_id: int = 248056
2350+
vision_start_token_id: int = 248053
2351+
video_token_id: int = 248057
2352+
tokens_per_second: float = 2.0
2353+
temporal_patch_size: int = 2
2354+
vision: FakeVision = dataclasses.field(default_factory=FakeVision)
2355+
2356+
pkg = ModelPackage(
2357+
{
2358+
"decoder": _mock_model_with_inputs(["inputs_embeds", "attention_mask"]),
2359+
"vision_encoder": _mock_model_with_inputs(["pixel_values"]),
2360+
"embedding": _mock_model_with_inputs(["input_ids", "image_features"]),
2361+
},
2362+
config=FakeConfig(model_type=text_model_type),
2363+
)
2364+
2365+
result = write_ort_genai_config(pkg, str(tmp_path), hf_model_id=None)
2366+
2367+
with open(result["genai_config"]) as f:
2368+
model = json.load(f)["model"]
2369+
assert model["type"] == expected_runtime_type
2370+
assert model["vision"]["filename"] == "vision_encoder/model.onnx"
2371+
assert model["embedding"]["filename"] == "embedding/model.onnx"
2372+
22592373
def test_gemma3n_references_only_processor_files_that_exist(self, tmp_path):
22602374
"""Every processor file genai_config.json names must be on disk.
22612375

‎src/mobius/integrations/ort_genai/genai_config_test.py‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,17 @@ def test_minimal_llm_config(self):
4444
assert decoder["head_size"] == 128
4545
assert decoder["filename"] == "model.onnx"
4646

47-
@pytest.mark.parametrize("model_type", ["llama", "qwen2", "gemma4_text", "custom"])
47+
@pytest.mark.parametrize(
48+
"model_type",
49+
[
50+
"llama",
51+
"qwen2",
52+
"gemma4_text",
53+
"qwen3_5_text",
54+
"qwen3_5_moe_text",
55+
"custom",
56+
],
57+
)
4858
def test_decoder_only_types_are_normalized(self, model_type):
4959
gen = GenaiConfigGenerator(
5060
model_type,

0 commit comments

Comments
 (0)