Skip to content

Commit cc84612

Browse files
authored
Merge branch 'main' into copilot/sensenova-canonical-metadata
Signed-off-by: Justin Chu <[email protected]>
2 parents bfb7d2d + 8b92428 commit cc84612

9 files changed

Lines changed: 166 additions & 0 deletions

File tree

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -831,6 +831,7 @@ def build_shared_state_pixel_flow_workflow_metadata(
831831
"adapter_abis": {"onnx-genai.image-preprocess": "1"},
832832
"capabilities": [
833833
"workflow_ssa",
834+
"linear_effects",
834835
"typed_emit",
835836
"nested_control_flow",
836837
"loop_induction_values",

‎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(

‎src/mobius/models/sensenova_u1_test.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,7 @@ def test_emits_complete_hashless_shared_state_workflow(self):
513513
package, config, num_inference_steps=2
514514
)
515515
workflow = metadata["pipeline"]["workflow"]
516+
assert "linear_effects" in workflow["manifest"]["capabilities"]
516517
assert set(workflow["components"]) >= {
517518
"embedding",
518519
"vision_encoder",
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/fixtures/onnx_genai_workflows/shared_state_pixel_flow/inference_metadata.yaml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ pipeline:
5454
onnx-genai.image-preprocess: '1'
5555
capabilities:
5656
- workflow_ssa
57+
- linear_effects
5758
- typed_emit
5859
- nested_control_flow
5960
- loop_induction_values

‎tests/generate_onnx_genai_validation_packages.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,11 @@ def _executable_diffusion_package() -> ModelPackage:
609609
vae_builder.op.Constant(value_ints=[3]),
610610
vae_builder.op.Constant(value_ints=[1]),
611611
)
612+
# Standard VAE decoder semantic: bound the decoded pixels to [-1, 1] with
613+
# Tanh so the graph's numeric output actually honors the workflow
614+
# metadata's declared `value_range: negative_one_to_one` for this
615+
# synthetic conformance package (not just a documentation claim).
616+
image = vae_builder.op.Tanh(image)
612617
vae_builder.add_output(
613618
_typed(
614619
image,

0 commit comments

Comments
 (0)