Skip to content

Ensure model package type correctness - #477

Merged
justinchuby merged 2 commits into
mainfrom
cohaagen/onnx-genai-io-metadata-robustness
Aug 14, 2026
Merged

justinchuby merged 2 commits into
mainfrom
cohaagen/onnx-genai-io-metadata-robustness

Conversation

@justinchuby

@justinchuby justinchuby commented Aug 11, 2026 •

Copy link
Copy Markdown
Member

Ensure model package type

`_add_explicit_io_to_file` augments an emitted `inference_metadata.yaml`
sidecar with the explicit `model.io` port contract (kv_inputs/kv_outputs/
state_pairs, token/logits roles) derived from the actual ONNX graph ports.
It previously bailed out silently when *any* in-memory package component
lacked a `.graph` attribute:

    if not models or any(not hasattr(model, "graph") for model in models):
        return   # silent no-op -> ships THIN metadata

Large decoders built with external-data / streamed weights do not retain
the `ir.Graph` in memory, so this guard shipped thin metadata (only
`model.attention` + `kv_cache`, no `io`). This is exactly what produced the
Qwen3.6-27B int4 CUDA sidecar, forcing downstream runtimes to derive I/O
from the graph at load (onnx-genai native EP workaround) and breaking
ORT-genai, which could not resolve `model.io.token_input`.

Fix: when a component's graph is not in memory, reload it from the
`model.onnx` written next to the sidecar via `onnx_ir.load` (external data
resolves relative to the model file) and derive ports from that. The
disk-loaded `ir.Model` is a drop-in for `.graph` access. Only when the
graph is genuinely unavailable (missing in memory *and* on disk) do we
skip augmentation -- and then loudly via `logging.warning`, never silently
shipping thin metadata. The in-memory `.graph` fast path is preserved
(no unnecessary reload).

Attribute-driven: no model-name gates. Verified end-to-end against the real
Qwen3.6-27B graph -- the re-emitted sidecar gains a full `io` block (32 kv
entries + 96 conv/recurrent state_pairs).

Regression tests cover: graphless decoder reloads a hybrid (linear-attention
conv_state/recurrent_state + GQA KV) contract from disk; graphless with no
disk model warns and skips; in-memory `.graph` is never reloaded.

Co-authored-by: Copilot <[email protected]>
@justinchuby
justinchuby requested review from a team and a lite review from Copilot August 11, 2026 17:17
@justinchuby
justinchuby force-pushed the cohaagen/onnx-genai-io-metadata-robustness branch from af4f38a to e930574 Compare August 11, 2026 17:19
@github-actions

github-actions Bot commented Aug 11, 2026 •

Copy link
Copy Markdown

🏗️ Architecture Diff

Comparing f57f294 → 30ff64a

Model Sub-model Changes Status

No architecture changes detected. ✅


Legend: ⚪ No change · 🔵 Minor (attrs/inits) · 🟡 Moderate (nodes added/removed) · 🔴 Major (interface changed)

@github-actions

github-actions Bot commented Aug 11, 2026 •

Copy link
Copy Markdown

Performance Comparison

Comparing f57f294 → 30ff64a

Model Metric Baseline Current Delta
bert (feature-extraction) model_size_bytes 359 KB 359 KB +0.0% ⚪
bert (feature-extraction) num_nodes 60 60 +0.0% ⚪
falcon model_size_bytes 364 KB 364 KB +0.0% ⚪
falcon num_nodes 68 68 +0.0% ⚪
gemma2 model_size_bytes 428 KB 428 KB +0.0% ⚪
gemma2 num_nodes 107 107 +0.0% ⚪
gpt2 model_size_bytes 388 KB 388 KB +0.0% ⚪
gpt2 num_nodes 54 54 +0.0% ⚪
llama model_size_bytes 425 KB 425 KB +0.0% ⚪
llama num_nodes 62 62 +0.0% ⚪
llama (static-cache) model_size_bytes 425 KB 425 KB +0.0% ⚪
llama (static-cache) num_nodes 58 58 +0.0% ⚪
mamba (ssm-text-generation) model_size_bytes 296 KB 296 KB +0.0% ⚪
mamba (ssm-text-generation) num_nodes 98 98 +0.0% ⚪
phi3 model_size_bytes 421 KB 421 KB +0.0% ⚪
phi3 num_nodes 60 60 +0.0% ⚪
phi3 (static-cache) model_size_bytes 421 KB 421 KB +0.0% ⚪
phi3 (static-cache) num_nodes 56 56 +0.0% ⚪
qwen2 model_size_bytes 425 KB 425 KB +0.0% ⚪
qwen2 num_nodes 62 62 +0.0% ⚪
qwen2 (static-cache) model_size_bytes 425 KB 425 KB +0.0% ⚪
qwen2 (static-cache) num_nodes 58 58 +0.0% ⚪
qwen3_5_moe (hybrid-text-generation) model_size_bytes 506 KB 506 KB +0.0% ⚪
qwen3_5_moe (hybrid-text-generation) num_nodes 275 275 +0.0% ⚪
qwen3_5_text (hybrid-text-generation) model_size_bytes 458 KB 458 KB +0.0% ⚪
qwen3_5_text (hybrid-text-generation) num_nodes 129 129 +0.0% ⚪
qwen3_5_vl (hybrid-qwen-vl) model_size_bytes 977 KB 977 KB +0.0% ⚪
qwen3_5_vl (hybrid-qwen-vl) num_nodes 431 431 +0.0% ⚪
t5 (seq2seq) model_size_bytes 836 KB 836 KB +0.0% ⚪
t5 (seq2seq) num_nodes 166 166 +0.0% ⚪
whisper (speech-to-text) model_size_bytes 1008 KB 1008 KB +0.0% ⚪
whisper (speech-to-text) num_nodes 128 128 +0.0% ⚪

No performance regressions.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes cases where onnx-genai inference_metadata.yaml is emitted without an explicit model.io contract when the in-memory ModelPackage components don’t retain an ir.Graph (notably for large external-data / streamed-weight decoders). It preserves the in-memory fast path, and otherwise reloads the component graph from the emitted model.onnx on disk to derive kv_inputs/kv_outputs/state_pairs and token/logits roles; when neither in-memory nor disk graphs are available, it now logs a warning instead of silently producing thin metadata.

Changes:

  • Reload missing component graphs from disk via onnx_ir.load() when augmenting sidecar model.io metadata.
  • Emit loud warnings (instead of silent no-ops) when graph reload isn’t possible.
  • Add regression tests covering disk-reload behavior, warning/skip behavior, and preserving the in-memory fast path.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/mobius/integrations/onnx_genai/auto_export.py Reloads component graphs from disk to derive explicit model.io, preserving fast path and warning on failure.
src/mobius/integrations/onnx_genai/auto_export_test.py Adds regression tests for graphless decoders (reload succeeds, reload missing warns/skips, fast path doesn’t reload).

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +50 to +53
graph in memory, in which case we reload the graph from the ``model.onnx``
file already written next to the sidecar. Only when a component's graph is
truly unavailable (missing in memory *and* on disk) do we skip it — and then
loudly, never silently shipping thin metadata.
@justinchuby

Copy link
Copy Markdown
Member Author

@copilot can you instead uphold the invariant that the graph is available, and have a test to guard all new models?

Require every ModelPackage component to retain an in-memory ONNX graph, so runtime metadata generation does not rely on reloading exported models.

Signed-off-by: GitHub <[email protected]>

Co-authored-by: justinchuby <[email protected]>
@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you all sign our Contributor License Agreement before we can accept your contribution.
1 out of 2 committers have signed the CLA.

✅ justinchuby
❌ Copilot
You have signed the CLA already but the status is still pending? Let us recheck it.

Copilot AI commented Aug 12, 2026

Copy link
Copy Markdown
Contributor

@copilot can you instead uphold the invariant that the graph is available, and have a test to guard all new models?

Implemented in 30ff64a. ModelPackage now rejects components without an in-memory ir.Model.graph, with constructor/insertion and save-retention regression coverage; the disk-reload workaround was removed.

assert pkg["new"] is m

def test_rejects_component_without_graph(self):
with pytest.raises(TypeError, match="must be an ir.Model with a graph"):
ModelPackage({"invalid": object()}) # type: ignore[arg-type]

pkg = ModelPackage()
with pytest.raises(TypeError, match="must be an ir.Model with a graph"):
@justinchuby justinchuby changed the title Reload decoder graph from disk when augmenting onnx-genai model.io Ensure model package type correctness Aug 14, 2026
@justinchuby
justinchuby merged commit b108254 into main Aug 14, 2026
12 of 21 checks passed
@justinchuby
justinchuby deleted the cohaagen/onnx-genai-io-metadata-robustness branch August 14, 2026 00:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants