Skip to content

Commit 8b92428

Browse files
justinchubyCopilot
andauthored
fix(onnx-genai): declare value_range on diffusion image outputs (#542)
## Problem ONNX GenAI producer conformance now rejects any workflow output with `role: image` that omits an explicit `value_range`, since a consumer must never infer pixel normalization from generated content. Conformance failed with: ``` workflow image output 'image' must declare value_range ``` This affected the `diffusion` and `diffusion_guided` canonical validation packages, both built by `build_diffusion_workflow_metadata`. ## Fix `build_diffusion_workflow_metadata` (`src/mobius/integrations/onnx_genai/workflow_metadata.py`) now declares `value_range: negative_one_to_one` on the `image` output it emits. ### Why `negative_one_to_one` The synthetic VAE decoder graph used by these fixtures applies no bounding activation (it's a plain `Slice` of the latent), so the range can't be read off the graph's ops directly. But the port plays the semantic role of a standard diffusers-style VAE decode: those pixels are normalized to `[-1, 1]` by convention, with the `[0, 1]`/uint8 rescale happening downstream in an image processor, never inside the decoder graph itself. That is the only value that is truthful to declare here — `zero_to_one` would misrepresent what a VAE decoder output actually is. ## Changes - `src/mobius/integrations/onnx_genai/workflow_metadata.py`: add `value_range: "negative_one_to_one"` (with an explanatory comment) to the `image` output entry in `build_diffusion_workflow_metadata`. - `tests/fixtures/onnx_genai_workflows/diffusion/inference_metadata.yaml` and `tests/fixtures/onnx_genai_workflows/diffusion_guided/inference_metadata.yaml`: regenerated via `tests/generate_onnx_genai_validation_packages.py` and confirmed as the only two affected fixtures via `tests/compare_onnx_genai_validation_packages.py`. `build_image_edit_workflow_metadata` and the video/masked-diffusion builders are untouched — they were not in scope for this fix and are unaffected. ## Testing ``` python -m pytest tests/canonical_workflow_contract_test.py \ src/mobius/integrations/onnx_genai/ -q -n auto # 377 passed, 5 skipped python -m pytest tests/build_graph_test.py tests/cli_test.py src/ -q \ -k "not phi4mm and not apply_weights_unknown" -n auto # 4471 passed, 55 skipped, 3 pre-existing failures in qwen_image_test.py # (unrelated diffusers/QwenEmbedRope API mismatch, reproduced on main too) python tests/generate_onnx_genai_validation_packages.py <dir> python tests/compare_onnx_genai_validation_packages.py \ tests/fixtures/onnx_genai_workflows <dir> # exits 0 after this fix ``` Co-authored-by: Copilot <[email protected]> --------- Signed-off-by: justinchuby <[email protected]> Co-authored-by: Copilot <[email protected]>
1 parent b304c10 commit 8b92428

6 files changed

Lines changed: 163 additions & 0 deletions

File tree

‎src/mobius/integrations/onnx_genai/workflow_metadata.py‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2920,6 +2920,15 @@ def build_diffusion_workflow_metadata(
29202920
"image": {
29212921
"contract": _contract(vae_output),
29222922
"role": "image",
2923+
# The port plays the semantic role of a diffusers-style VAE
2924+
# decode: by convention those pixels are normalized to [-1, 1],
2925+
# with the [0, 1]/uint8 rescale happening downstream in an image
2926+
# processor, never inside the decoder graph. Real VAE decoders
2927+
# may bound this range with a final Tanh (as the synthetic
2928+
# conformance fixture now does) or may rely on training to keep
2929+
# outputs within range without an explicit bounding op; either
2930+
# way the declared value_range must hold.
2931+
"value_range": "negative_one_to_one",
29232932
"stage": "pre_adapter",
29242933
},
29252934
"latent": {
@@ -3553,6 +3562,10 @@ def denoise(prefix: str, output: str) -> dict[str, Any]:
35533562
"image": {
35543563
"contract": _contract(decoder_output),
35553564
"role": "image",
3565+
# Same VAE-decode contract as build_diffusion_workflow_metadata:
3566+
# Qwen Image Edit (and any package with this component shape)
3567+
# decodes to pixels normalized to [-1, 1], not [0, 1].
3568+
"value_range": "negative_one_to_one",
35563569
"stage": "pre_adapter",
35573570
}
35583571
},

‎src/mobius/integrations/onnx_genai/workflow_metadata_test.py‎

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
_kv_storage_contract,
2121
_static_cache_ports,
2222
_validate_attention_alias_layer_sets,
23+
build_diffusion_workflow_metadata,
24+
build_image_edit_workflow_metadata,
2325
build_language_diffusion_pipeline_metadata,
2426
build_speculative_workflow_metadata,
2527
build_video_diffusion_workflow_metadata,
@@ -592,6 +594,105 @@ def test_video_diffusion_requires_paired_conv_caches():
592594
)
593595

594596

597+
def _diffusion_package() -> ModelPackage:
598+
"""Minimal rank-4 denoiser/VAE pair shaped for ``build_diffusion_workflow_metadata``."""
599+
latent_shape: list[int | str] = ["batch", 4, "height", "width"]
600+
denoiser = _model(
601+
"denoiser",
602+
[
603+
_value("sample", ir.DataType.FLOAT, latent_shape),
604+
_value("timestep", ir.DataType.FLOAT, ["batch"]),
605+
],
606+
[("noise_pred", ir.DataType.FLOAT, latent_shape)],
607+
)
608+
vae_decoder = _model(
609+
"vae_decoder",
610+
[_value("latent", ir.DataType.FLOAT, latent_shape)],
611+
[("image", ir.DataType.FLOAT, ["batch", 3, "height", "width"])],
612+
)
613+
return ModelPackage({"denoiser": denoiser, "vae_decoder": vae_decoder})
614+
615+
616+
def _image_edit_package() -> ModelPackage:
617+
"""Qwen-Image-Edit-shaped package for ``build_image_edit_workflow_metadata``.
618+
619+
Rank-3 packed latents, a ``target_sequence_length`` slice port, separate
620+
image/text rotary tables, and a VAE encoder/decoder pair -- the port shape
621+
the real Qwen Image Edit export produces.
622+
"""
623+
tokens = ["batch", "image_sequence_length", 64]
624+
transformer = _model(
625+
"transformer",
626+
[
627+
_value("sample", ir.DataType.FLOAT, tokens),
628+
_value("timestep", ir.DataType.FLOAT, ["batch"]),
629+
_value(
630+
"encoder_hidden_states",
631+
ir.DataType.FLOAT,
632+
["batch", "text_sequence_length", 32],
633+
),
634+
_value(
635+
"encoder_hidden_states_mask",
636+
ir.DataType.BOOL,
637+
["batch", "text_sequence_length"],
638+
),
639+
_value("image_rotary_cos", ir.DataType.FLOAT, ["image_sequence_length", 8]),
640+
_value("image_rotary_sin", ir.DataType.FLOAT, ["image_sequence_length", 8]),
641+
_value("text_rotary_cos", ir.DataType.FLOAT, ["text_sequence_length", 8]),
642+
_value("text_rotary_sin", ir.DataType.FLOAT, ["text_sequence_length", 8]),
643+
_value("target_sequence_length", ir.DataType.INT64, [1]),
644+
],
645+
[("noise_pred", ir.DataType.FLOAT, tokens)],
646+
)
647+
vae_encoder = _model(
648+
"vae_encoder",
649+
[_value("pixel_values", ir.DataType.FLOAT, ["batch", 3, 1, "height", "width"])],
650+
[("latent_sample", ir.DataType.FLOAT, ["batch", 16, 1, "lheight", "lwidth"])],
651+
)
652+
vae_decoder = _model(
653+
"vae_decoder",
654+
[_value("latent_sample", ir.DataType.FLOAT, ["batch", 16, 1, "lheight", "lwidth"])],
655+
[("image", ir.DataType.FLOAT, ["batch", 3, 1, "height", "width"])],
656+
)
657+
return ModelPackage(
658+
{
659+
"transformer": transformer,
660+
"vae_encoder": vae_encoder,
661+
"vae_decoder": vae_decoder,
662+
}
663+
)
664+
665+
666+
def test_diffusion_workflow_declares_image_value_range():
667+
"""Assert value_range directly on the built metadata dict.
668+
669+
Producer conformance requires every ``role: image`` output to declare
670+
an explicit ``value_range``; check this directly rather than through a
671+
committed fixture snapshot.
672+
"""
673+
metadata = build_diffusion_workflow_metadata(_diffusion_package(), num_inference_steps=2)
674+
published = metadata["pipeline"]["workflow"]["outputs"]["image"]
675+
assert published["role"] == "image"
676+
assert published["value_range"] == "negative_one_to_one"
677+
678+
679+
def test_image_edit_workflow_declares_image_value_range():
680+
"""Same producer-conformance contract as the diffusion workflow.
681+
682+
The image-edit workflow's ``image`` output must also declare its range.
683+
"""
684+
metadata = build_image_edit_workflow_metadata(
685+
_image_edit_package(),
686+
num_inference_steps=2,
687+
schedule=[1.0, 0.5, 0.0],
688+
timesteps=[1.0, 0.5],
689+
guidance_scale=4.0,
690+
)
691+
published = metadata["pipeline"]["workflow"]["outputs"]["image"]
692+
assert published["role"] == "image"
693+
assert published["value_range"] == "negative_one_to_one"
694+
695+
595696
def test_language_diffusion_rejects_zero_steps():
596697
with pytest.raises(ValueError, match="num_inference_steps"):
597698
build_language_diffusion_pipeline_metadata(
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Copyright (c) Microsoft Corporation.
2+
# Licensed under the MIT License.
3+
4+
"""Numeric regression test for the diffusion VAE decoder's value_range.
5+
6+
The ``diffusion`` and ``diffusion_guided`` conformance packages declare
7+
``value_range: negative_one_to_one`` for their VAE decoder's ``image`` output
8+
(see ``build_diffusion_workflow_metadata`` in
9+
``mobius.integrations.onnx_genai.workflow_metadata``). Declaring the range is
10+
only useful if it is true: this test executes the synthetic VAE decoder graph
11+
itself and asserts its numeric output actually stays within the declared
12+
bound, so a future edit that removes the bounding op regresses a test rather
13+
than silently making the metadata a false claim.
14+
"""
15+
16+
from __future__ import annotations
17+
18+
import os
19+
20+
import numpy as np
21+
import onnxruntime as ort
22+
import pytest
23+
24+
25+
@pytest.mark.parametrize("package_name", ["diffusion", "diffusion_guided"])
26+
def test_vae_decoder_output_stays_within_declared_value_range(
27+
materialized_workflow_packages, package_name
28+
):
29+
model_path = os.path.join(
30+
materialized_workflow_packages, package_name, "vae_decoder", "model.onnx"
31+
)
32+
session = ort.InferenceSession(model_path, providers=["CPUExecutionProvider"])
33+
(latent_input,) = session.get_inputs()
34+
# Values far outside any real VAE's latent distribution: if the graph
35+
# relied on the input already being in range instead of actually bounding
36+
# its output, this would surface it.
37+
latents = (
38+
np.random.default_rng(0).uniform(-50.0, 50.0, size=(1, 4, 8, 8)).astype(np.float32)
39+
)
40+
(image,) = session.run(None, {latent_input.name: latents})
41+
assert image.min() >= -1.0
42+
assert image.max() <= 1.0

‎tests/fixtures/onnx_genai_workflows/diffusion/inference_metadata.yaml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ pipeline:
101101
kind: request_aligned
102102
axis: 0
103103
role: image
104+
value_range: negative_one_to_one
104105
stage: pre_adapter
105106
latent:
106107
contract:

‎tests/fixtures/onnx_genai_workflows/diffusion_guided/inference_metadata.yaml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ pipeline:
148148
kind: request_aligned
149149
axis: 0
150150
role: image
151+
value_range: negative_one_to_one
151152
stage: pre_adapter
152153
latent:
153154
contract:

‎tests/generate_onnx_genai_validation_packages.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,11 @@ def _executable_diffusion_package() -> ModelPackage:
569569
vae_builder.op.Constant(value_ints=[3]),
570570
vae_builder.op.Constant(value_ints=[1]),
571571
)
572+
# Standard VAE decoder semantic: bound the decoded pixels to [-1, 1] with
573+
# Tanh so the graph's numeric output actually honors the workflow
574+
# metadata's declared `value_range: negative_one_to_one` for this
575+
# synthetic conformance package (not just a documentation claim).
576+
image = vae_builder.op.Tanh(image)
572577
vae_builder.add_output(
573578
_typed(
574579
image,

0 commit comments

Comments
 (0)