Skip to content

Commit b030c36

Browse files
committed
Migrate decoder configs to generic runtime type
Normalize every graph-representable decoder-only ORT GenAI config to the architecture-neutral decoder type. Preserve only runtime-specific cache behavior, LongRoPE, and auxiliary pipeline topologies, and keep emitted compatibility metadata aligned with the generated config.\n\nUpdate focused coverage, the Gemma4 text example, CLI documentation, and the ORT GenAI config skill to make the generic decoder contract the default.\n\nCo-authored-by: Copilot App <[email protected]> Signed-off-by: Justin Chu <[email protected]>
1 parent e591ecf commit b030c36

9 files changed

Lines changed: 189 additions & 37 deletions

File tree

‎.agents/skills/ort-genai-config/SKILL.md‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,22 @@ Key decoder fields: `filename`, `hidden_size`, `head_size`,
9797
### LLM (decoder-only → `DecoderOnly_Model`)
9898

9999
```
100-
chatglm, decoder, ernie4_5, gemma, gemma2, gemma3_text, gpt2,
101-
gptoss, granite, internlm2, llama, mistral, nemotron, olmo,
102-
phi, phimoe, phi3, phi3small, qwen2, qwen3, smollm3
100+
decoder
103101
```
104102

103+
Mobius emits `model.type = "decoder"` for every graph-representable,
104+
single-model decoder-only package, regardless of the HuggingFace architecture
105+
name. The optimized graph and config mappings define the runtime contract.
106+
107+
Specialized decoder-only exceptions are explicit:
108+
109+
- `gpt2`: selects `Gpt_Model`; Mobius rejects its separate key/value cache ABI
110+
because the runtime requires combined rank-5 cache tensors.
111+
- `lfm2`: selects `LFM2_Model` and its convolution-cache implementation.
112+
- `phi3`, `phi3small`, `phimoe`: used only for LongRoPE configs whose runtime
113+
must recompute caches after crossing the short-context threshold. Non-LongRoPE
114+
exports use `decoder`.
115+
105116
### VLM (vision-language → `MultiModalLanguageModel`)
106117

107118
```
@@ -141,7 +152,7 @@ phi3small_pipeline, qwen2_5_vl_pipeline
141152
```json
142153
{
143154
"model": {
144-
"type": "llama",
155+
"type": "decoder",
145156
"vocab_size": 32000,
146157
"context_length": 4096,
147158
"eos_token_id": 2,

‎.agents/skills/ort-genai-config/references/genai-config-fields.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ parent [SKILL.md](../SKILL.md).
1010

1111
| Field | Type | Required | Description |
1212
|---|---|---|---|
13-
| `type` | string | **yes** | Model type identifier (see registry in SKILL.md) |
13+
| `type` | string | **yes** | `decoder` for graph-representable decoder-only packages; a specialized type only for the explicit runtime/topology exceptions in SKILL.md |
1414
| `vocab_size` | int | yes | Vocabulary size |
1515
| `context_length` | int | **yes** | Maximum context length; must be > 0 |
1616
| `bos_token_id` | int | no | Beginning-of-sequence token |

‎docs/cli_reference.md‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,10 @@ tokenizer files to the output directory:
161161
- With `--config` (local directory): tokenizer files are copied from that
162162
directory.
163163

164-
For a single-model decoder-only text graph, Mobius emits the architecture-neutral
165-
`model.type: "decoder"` contract supported by onnxruntime-genai 0.14.0 and newer.
164+
For a graph-representable, single-model decoder-only text graph, Mobius emits the
165+
architecture-neutral `model.type: "decoder"` contract supported by
166+
onnxruntime-genai 0.14.0 and newer. Validation targets only the latest stable
167+
release, currently 0.15.2.
166168
The graph determines the exact semantic input names, output names, cache templates,
167169
and global cache indices, so dense, MoE, tied-weight, quantized, and unknown
168170
architecture names do not need a runtime registry entry.
@@ -171,9 +173,10 @@ Architecture-specific types remain only where the runtime selects different
171173
behavior. `lfm2` uses its legacy convolution-cache implementation. `gpt2` selects
172174
`Gpt_Model`, but Mobius's separate rank-4 key/value cache ABI does not match that
173175
runtime's rank-5 combined-cache contract, so config generation currently fails
174-
closed. `phi3`, `phimoe`, and `phi3small` retain their names because the released
175-
generator uses them to recompute LongRoPE caches when generation crosses the
176-
short-context threshold. Multimodal, audio, encoder-decoder, special-position-ID,
176+
closed. `phi3`, `phimoe`, and `phi3small` retain their names only when their config selects
177+
LongRoPE, because the released generator uses those names to recompute caches when
178+
generation crosses the short-context threshold. Ordinary Phi-3-family graphs use
179+
`decoder`. Multimodal, audio, encoder-decoder, special-position-ID,
177180
and split pipeline packages remain outside the generic path and require their
178181
dedicated types and schemas. These exceptions follow the
179182
[v0.15.2 runtime model factory](https://github.com/microsoft/onnxruntime-genai/blob/v0.15.2/src/models/model.cpp#L874-L907),

‎examples/gemma4/ort_genai/README.md‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,16 +127,16 @@ Output: `audio_features [batch, time/4, 1536]` — projected to text hidden_size
127127

128128
## ORT GenAI support required
129129

130-
These configs use the `gemma4` model type which is not yet in a released
131-
ORT GenAI build:
130+
The decoder-only config uses the generic `decoder` type. Multimodal configs
131+
retain `gemma4` because that value selects the vision/audio pipeline:
132132

133133
| Config | `model.type` | Pipeline variant |
134134
|---|---|---|
135-
| `text/genai_config.json` | `gemma4` | Decoder-only (text) |
135+
| `text/genai_config.json` | `decoder` | Decoder-only (text) |
136136
| `vlm/genai_config.json` | `gemma4` | Multimodal (vision + audio + text) |
137137

138-
The runtime auto-detects the pipeline variant from which ONNX files are
139-
present.
138+
The specialized multimodal runtime selects its pipeline variant from which
139+
ONNX files are present.
140140

141141
---
142142

‎examples/gemma4/ort_genai/text/genai_config.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"model": {
3-
"type": "gemma4",
3+
"type": "decoder",
44
"vocab_size": 262144,
55
"context_length": 131072,
66
"bos_token_id": 2,

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

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,8 @@ def _revision_kwargs(revision: str | None) -> dict[str, str]:
144144
"gpt2": "gpt2",
145145
"lfm2": "lfm2",
146146
"lfm2_vl": "lfm2",
147-
"phi3": "phi3",
148-
"phi3small": "phi3small",
149-
"phimoe": "phimoe",
150147
}
148+
_LONGROPE_TEXT_TYPES = frozenset({"phi3", "phi3small", "phimoe"})
151149
_GENERIC_DECODER_MIN_VERSION = (0, 14, 0)
152150
_GENERIC_DECODER_TESTED_VERSIONS = ("0.15.2",)
153151
_DECODER_SEMANTIC_INPUTS = frozenset(
@@ -269,14 +267,15 @@ def _select_ort_model_type(
269267
hf_model_type: str | None,
270268
*,
271269
is_decoder_only: bool,
270+
rope_type: str | None = None,
272271
) -> str:
273272
"""Choose the ORT-GenAI model type for an exported package.
274273
275-
Released ORT GenAI 0.14+ dispatches ``decoder`` to its generic
274+
Released ORT GenAI dispatches ``decoder`` to its generic
276275
``DecoderOnly_Model``. Decoder-only packages therefore use that type unless
277276
the runtime has genuinely different behavior: ``gpt2`` selects ``Gpt_Model``,
278-
``lfm2`` selects ``LFM2_Model``/``LFM2Cache``, and Phi-3 family names enable
279-
LongRoPE cache recomputation after the short-context threshold.
277+
``lfm2`` selects ``LFM2_Model``/``LFM2Cache``, and Phi-3 family names are
278+
retained only for LongRoPE cache recomputation after the short-context threshold.
280279
281280
Multimodal and encoder-decoder packages retain their architecture-specific
282281
type because those values select distinct runtime pipelines and position-ID
@@ -287,6 +286,8 @@ def _select_ort_model_type(
287286
resolved = _resolve_ort_genai_model_type(source_type or "unknown")
288287
if resolved in _ARCHITECTURE_SPECIFIC_TEXT_TYPES:
289288
return _ARCHITECTURE_SPECIFIC_TEXT_TYPES[resolved]
289+
if resolved in _LONGROPE_TEXT_TYPES and rope_type == "longrope":
290+
return resolved
290291
return "decoder"
291292
return _resolve_ort_genai_model_type(hf_model_type or "unknown")
292293

@@ -1546,6 +1547,7 @@ def _write_genai_config(
15461547
else _count_cache_layer_slots(decoder_model)
15471548
),
15481549
sliding_window=sliding_window,
1550+
has_specialized_topology=not _is_single_model_decoder_package(pkg),
15491551
)
15501552
generator.with_special_tokens(
15511553
**_special_token_ids_from_tokenizer_config(output_dir, config.vocab_size)
@@ -1865,7 +1867,10 @@ def write_ort_genai_config(
18651867
# See _select_ort_model_type: decoder-only packages prefer the package's
18661868
# own config.model_type; multimodal packages keep the HF parent type.
18671869
ort_model_type = _select_ort_model_type(
1868-
cfg_model_type, model_type, is_decoder_only=is_decoder_only
1870+
cfg_model_type,
1871+
model_type,
1872+
is_decoder_only=is_decoder_only,
1873+
rope_type=getattr(config, "rope_type", None),
18691874
)
18701875
# Token IDs may live on the parent config or the text sub-config
18711876
# (e.g. Gemma4Config has text_config with bos_token_id=2).
@@ -1901,7 +1906,10 @@ def write_ort_genai_config(
19011906
ort_model_type = "gemma3n"
19021907
else:
19031908
ort_model_type = _select_ort_model_type(
1904-
raw_type, raw_type, is_decoder_only=is_decoder_only
1909+
raw_type,
1910+
raw_type,
1911+
is_decoder_only=is_decoder_only,
1912+
rope_type=getattr(config, "rope_type", None),
19051913
)
19061914
if ort_model_type == "unknown":
19071915
logger.warning(
@@ -1948,7 +1956,6 @@ def write_ort_genai_config(
19481956
"Generic ORT GenAI decoder packages require onnxruntime-genai >= 0.14.0; "
19491957
f"requested {runtime_version}"
19501958
)
1951-
19521959
result: dict[str, str] = {}
19531960

19541961
if "mtp" in pkg:
@@ -2022,9 +2029,11 @@ def write_ort_genai_config(
20222029
has_speech=has_speech,
20232030
)
20242031
result["genai_config"] = genai_path
2032+
with open(genai_path, encoding="utf-8") as handle:
2033+
emitted_model_type = json.load(handle)["model"]["type"]
20252034
compatibility_path = _write_runtime_compatibility(
20262035
directory,
2027-
model_type=ort_model_type,
2036+
model_type=emitted_model_type,
20282037
runtime_version=runtime_version,
20292038
)
20302039
result["runtime_compatibility"] = compatibility_path

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

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -202,14 +202,29 @@ def test_decoder_only_preserves_specialized_hf_fallback(self):
202202
("gpt2", "gpt2"),
203203
("lfm2", "lfm2"),
204204
("lfm2_vl", "lfm2"),
205-
("phi3", "phi3"),
206-
("phi3small", "phi3small"),
207-
("phimoe", "phimoe"),
208205
],
209206
)
210207
def test_specialized_decoder_type_is_preserved(self, model_type, expected):
211208
assert _select_ort_model_type(model_type, model_type, is_decoder_only=True) == expected
212209

210+
@pytest.mark.parametrize("model_type", ["phi3", "phi3small", "phimoe"])
211+
def test_phi3_family_uses_generic_decoder_without_longrope(self, model_type):
212+
assert (
213+
_select_ort_model_type(model_type, model_type, is_decoder_only=True) == "decoder"
214+
)
215+
216+
@pytest.mark.parametrize("model_type", ["phi3", "phi3small", "phimoe"])
217+
def test_phi3_family_preserves_specialized_type_for_longrope(self, model_type):
218+
assert (
219+
_select_ort_model_type(
220+
model_type,
221+
model_type,
222+
is_decoder_only=True,
223+
rope_type="longrope",
224+
)
225+
== model_type
226+
)
227+
213228

214229
class TestWriteProcessorConfig:
215230
def test_no_vision_returns_none(self, tmp_path):
@@ -3409,6 +3424,20 @@ def test_generic_decoder_runtime_compatibility_metadata(tmp_path):
34093424
}
34103425

34113426

3427+
def test_decoder_sidecar_preserves_type_and_matching_compatibility_metadata(tmp_path):
3428+
pkg = _make_fake_llm_pkg("qwen2")
3429+
pkg["mtp"] = _mock_model(inputs=["hidden_states"], outputs=["draft_logits"])
3430+
3431+
result = write_ort_genai_config(pkg, str(tmp_path))
3432+
with open(result["genai_config"], encoding="utf-8") as handle:
3433+
config = json.load(handle)
3434+
with open(result["runtime_compatibility"], encoding="utf-8") as handle:
3435+
compatibility = json.load(handle)
3436+
3437+
assert config["model"]["type"] == "qwen2"
3438+
assert compatibility["model_type"] == config["model"]["type"]
3439+
3440+
34123441
@pytest.mark.parametrize(
34133442
("model_type", "config_overrides"),
34143443
[
@@ -3470,12 +3499,22 @@ def test_gpt2_specialized_runtime_rejects_separate_cache_graph(tmp_path):
34703499

34713500
@pytest.mark.parametrize("model_type", ["phi3", "phi3small", "phimoe"])
34723501
def test_phi3_longrope_runtime_type_is_preserved(tmp_path, model_type):
3473-
result = write_ort_genai_config(_make_fake_llm_pkg(model_type), str(tmp_path))
3502+
pkg = _make_fake_llm_pkg(model_type)
3503+
pkg.config.rope_type = "longrope"
3504+
result = write_ort_genai_config(pkg, str(tmp_path))
34743505
with open(result["genai_config"], encoding="utf-8") as handle:
34753506
config = json.load(handle)
34763507
assert config["model"]["type"] == model_type
34773508

34783509

3510+
@pytest.mark.parametrize("model_type", ["phi3", "phi3small", "phimoe"])
3511+
def test_phi3_without_longrope_emits_generic_decoder(tmp_path, model_type):
3512+
result = write_ort_genai_config(_make_fake_llm_pkg(model_type), str(tmp_path))
3513+
with open(result["genai_config"], encoding="utf-8") as handle:
3514+
config = json.load(handle)
3515+
assert config["model"]["type"] == "decoder"
3516+
3517+
34793518
class TestGemma4RealModel:
34803519
"""Build a real tiny Gemma4 model and verify genai config inputs."""
34813520

@@ -3819,7 +3858,7 @@ def test_auto_export_produces_genai_config(self, tmp_path):
38193858
genai_config = gen.generate()
38203859

38213860
assert "model" in genai_config
3822-
assert genai_config["model"]["type"] == "qwen2"
3861+
assert genai_config["model"]["type"] == "decoder"
38233862
assert genai_config["model"]["vocab_size"] == 256
38243863
assert genai_config["model"]["decoder"]["num_hidden_layers"] == 2
38253864

@@ -3834,7 +3873,7 @@ def test_auto_export_produces_genai_config(self, tmp_path):
38343873

38353874
with open(os.path.join(output_dir, "genai_config.json")) as f:
38363875
saved = json.load(f)
3837-
assert saved["model"]["type"] == "qwen2"
3876+
assert saved["model"]["type"] == "decoder"
38383877

38393878
def test_phi4mm_detection_and_config(self, tmp_path):
38403879
"""Simulate phi4mm auto-export: verify detection and config."""

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

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@
1515
import os
1616
from typing import Any
1717

18+
_SPECIALIZED_DECODER_MODEL_TYPES = {
19+
"gpt2": "gpt2",
20+
"lfm2": "lfm2",
21+
"lfm2_vl": "lfm2",
22+
}
23+
_LONGROPE_DECODER_MODEL_TYPES = frozenset({"phi3", "phi3small", "phimoe"})
24+
1825

1926
def _default_decoder_inputs(
2027
*,
@@ -138,8 +145,10 @@ class GenaiConfigGenerator:
138145
and assembles the nested dict structure that ORT-GenAI expects.
139146
140147
Args:
141-
model_type: The ORT-GenAI model type string (e.g. ``"qwen2"``,
142-
``"llama"``, ``"qwen2_5_vl"``).
148+
model_type: The source architecture or specialized ORT-GenAI model type.
149+
Decoder-only configs emit ``"decoder"`` unless this value identifies
150+
a runtime-specific state ABI, or LongRoPE is explicitly requested.
151+
Multimodal configs retain the supplied pipeline type.
143152
vocab_size: Model vocabulary size.
144153
hidden_size: Decoder hidden dimension.
145154
num_hidden_layers: Number of decoder transformer layers.
@@ -163,6 +172,10 @@ class GenaiConfigGenerator:
163172
``past_value_names``).
164173
decoder_outputs: Explicit decoder output mapping derived from the
165174
graph, including logits and present-cache templates.
175+
uses_longrope: Preserve a Phi-3-family specialized type because the
176+
runtime must recompute LongRoPE caches across the context threshold.
177+
has_specialized_topology: Preserve the supplied type for packages with
178+
auxiliary graphs or runtime-managed pipelines.
166179
"""
167180

168181
def __init__(
@@ -188,6 +201,8 @@ def __init__(
188201
layer_types: list[str] | None = None,
189202
conv_cache_size: int | None = None,
190203
sliding_window: dict[str, Any] | None = None,
204+
uses_longrope: bool = False,
205+
has_specialized_topology: bool = False,
191206
):
192207
self.model_type = model_type
193208
self.vocab_size = vocab_size
@@ -217,6 +232,8 @@ def __init__(
217232
self._layer_types = layer_types
218233
self._conv_cache_size = conv_cache_size
219234
self._sliding_window = sliding_window
235+
self._uses_longrope = uses_longrope
236+
self._has_specialized_topology = has_specialized_topology
220237

221238
# Optional VLM fields (set via with_vision())
222239
self._vision: dict[str, Any] | None = None
@@ -247,6 +264,7 @@ def from_config(
247264
supports_in_place_kv_cache: bool | None = None,
248265
num_cache_layer_slots: int | None = None,
249266
sliding_window: dict[str, Any] | None = None,
267+
has_specialized_topology: bool = False,
250268
) -> GenaiConfigGenerator:
251269
"""Create a generator from a BaseModelConfig-like dataclass.
252270
@@ -302,6 +320,11 @@ def from_config(
302320
else None
303321
),
304322
sliding_window=sliding_window,
323+
uses_longrope=(
324+
model_type in _LONGROPE_DECODER_MODEL_TYPES
325+
and getattr(config, "rope_type", None) == "longrope"
326+
),
327+
has_specialized_topology=has_specialized_topology,
305328
)
306329

307330
def with_vision(
@@ -524,6 +547,14 @@ def with_special_tokens(self, **token_ids: int) -> GenaiConfigGenerator:
524547
def generate(self) -> dict[str, Any]:
525548
"""Generate the full genai_config.json dict."""
526549
is_multimodal = self._vision is not None or self._audio is not None
550+
if is_multimodal or self._has_specialized_topology:
551+
emitted_model_type = self.model_type
552+
elif self.model_type in _SPECIALIZED_DECODER_MODEL_TYPES:
553+
emitted_model_type = _SPECIALIZED_DECODER_MODEL_TYPES[self.model_type]
554+
elif self.model_type in _LONGROPE_DECODER_MODEL_TYPES and self._uses_longrope:
555+
emitted_model_type = self.model_type
556+
else:
557+
emitted_model_type = "decoder"
527558

528559
# Decoder section — use explicit inputs when available (from
529560
# graph introspection), otherwise fall back to defaults.
@@ -562,7 +593,7 @@ def generate(self) -> dict[str, Any]:
562593

563594
# Model section
564595
model: dict[str, Any] = {
565-
"type": self.model_type,
596+
"type": emitted_model_type,
566597
"vocab_size": self.vocab_size,
567598
"context_length": self.context_length,
568599
"decoder": decoder,

0 commit comments

Comments
 (0)