Skip to content

Commit 4a10564

Browse files
committed
tasks: declare vision/audio encoder inputs at config.dtype, revert f32 cast pattern
Revert PR #275 (and the matching original change for _gemma4.py and _vision_language_3model.py). Per discussion with the ORT GenAI team, the runtime does not require encoder inputs to be float32 — it can deliver vision / audio inputs at the same dtype the model was built with (f16 / bf16). Declaring the graph input at config.dtype matches the model's internal compute and removes the extra Cast node at graph entry. Changes: * tasks: replace the f32+Cast pattern with dtype=config.dtype across _vision_language.py, _vision_language_3model.py, _multimodal.py, _audio_ctc.py, _speech_language.py, _hunyuan_vl_mot.py, _phi4mm_multimodal.py, _fun_asr_speech_language.py, _gemma4.py. * tasks/_base.py: drop the now-unused _cast_encoder_input helper. * tests/build_graph_test.py: rewrite test_multimodal_encoder_inputs_* and test_gemma4_encoder_inputs_* to assert encoder graph inputs match config.dtype and that there is no Cast at graph entry. Single-model encoder-only tasks (_image_classification.py, _audio_feature_extraction.py) still declare FLOAT because they have no separate compute dtype — the whole model is f32 and the input goes straight to the encoder. L1 + unit suite: 2763 passed, 43 skipped (no regressions). Signed-off-by: justinchuby <[email protected]>
1 parent 3ce0444 commit 4a10564

11 files changed

Lines changed: 33 additions & 67 deletions

‎src/mobius/tasks/_audio_ctc.py‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
from mobius._model_package import ModelPackage
2525
from mobius.tasks._base import (
2626
ModelTask,
27-
_cast_encoder_input,
2827
_make_graph,
2928
_make_model,
3029
)
@@ -48,10 +47,9 @@ def build(
4847

4948
input_features = builder.input(
5049
"input_features",
51-
dtype=ir.DataType.FLOAT,
50+
dtype=config.dtype,
5251
shape=["batch", "time", input_dim],
5352
)
54-
input_features = _cast_encoder_input(op, input_features, config)
5553
language_id = builder.input(
5654
"language_id",
5755
dtype=ir.DataType.INT64,

‎src/mobius/tasks/_base.py‎

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -130,18 +130,6 @@ def _make_model(graph: ir.Graph) -> ir.Model:
130130
return model
131131

132132

133-
def _cast_encoder_input(op, tensor, config: BaseModelConfig):
134-
"""Cast an f32 encoder input to the model's compute dtype if needed.
135-
136-
Vision/audio processors output float32. Encoder graph inputs are
137-
declared as FLOAT, and this helper adds a Cast when the model uses
138-
f16/bf16 so that the first encoder op receives matching types.
139-
"""
140-
if config.dtype and config.dtype != ir.DataType.FLOAT:
141-
return op.Cast(tensor, to=config.dtype)
142-
return tensor
143-
144-
145133
class ModelTask(ABC):
146134
"""Abstract base defining how to wire a module into an ONNX graph.
147135

‎src/mobius/tasks/_fun_asr_speech_language.py‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
from mobius.tasks._base import (
2727
ComponentSpec,
2828
ModelTask,
29-
_cast_encoder_input,
3029
_make_graph,
3130
_make_model,
3231
build_decoder_from_embeds,
@@ -96,10 +95,9 @@ def _build_audio_encoder(
9695

9796
input_features = builder.input(
9897
"input_features",
99-
dtype=ir.DataType.FLOAT,
98+
dtype=config.dtype,
10099
shape=[batch, seq_len, input_dim],
101100
)
102-
input_features = _cast_encoder_input(op, input_features, config)
103101

104102
audio_features = audio_encoder(op, input_features)
105103

‎src/mobius/tasks/_gemma4.py‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
from mobius._model_package import ModelPackage
2929
from mobius.tasks._base import (
3030
ModelTask,
31-
_cast_encoder_input,
3231
_make_graph,
3332
_make_model,
3433
)
@@ -305,10 +304,9 @@ def _build_vision(
305304

306305
pixel_values = builder.input(
307306
"pixel_values",
308-
dtype=ir.DataType.FLOAT,
307+
dtype=config.dtype,
309308
shape=[batch, num_patches, pixel_dim],
310309
)
311-
pixel_values = _cast_encoder_input(op, pixel_values, config)
312310
pixel_position_ids = builder.input(
313311
"pixel_position_ids",
314312
dtype=ir.DataType.INT64,
@@ -358,10 +356,9 @@ def _build_audio(
358356

359357
input_features = builder.input(
360358
"input_features",
361-
dtype=ir.DataType.FLOAT,
359+
dtype=config.dtype,
362360
shape=[batch, time, input_size],
363361
)
364-
input_features = _cast_encoder_input(op, input_features, config)
365362
input_features_mask = builder.input(
366363
"input_features_mask",
367364
dtype=ir.DataType.BOOL,

‎src/mobius/tasks/_hunyuan_vl_mot.py‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
from mobius.tasks._base import (
2121
ComponentSpec,
2222
ModelTask,
23-
_cast_encoder_input,
2423
_make_graph,
2524
_make_model,
2625
build_embedding_from_features,
@@ -139,10 +138,9 @@ def _build_vision(
139138
op = builder.op
140139
pixel_values = builder.input(
141140
"pixel_values",
142-
dtype=ir.DataType.FLOAT,
141+
dtype=config.dtype,
143142
shape=[batch, 3, image_size, image_size],
144143
)
145-
pixel_values = _cast_encoder_input(op, pixel_values, config)
146144
image_features = vision(op, pixel_values=pixel_values)
147145

148146
builder.add_output(image_features, "image_features")

‎src/mobius/tasks/_multimodal.py‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
from mobius._model_package import ModelPackage
1313
from mobius.tasks._base import (
1414
ModelTask,
15-
_cast_encoder_input,
1615
_make_graph,
1716
_make_model,
1817
)
@@ -75,18 +74,16 @@ def build(
7574
image_size = config.vision.image_size or 224 if config.vision else 224
7675
pixel_values = builder.input(
7776
"pixel_values",
78-
dtype=ir.DataType.FLOAT,
77+
dtype=config.dtype,
7978
shape=[batch, 3, image_size, image_size],
8079
)
81-
pixel_values = _cast_encoder_input(op, pixel_values, config)
8280

8381
audio_input_size = (config.audio.input_size if config.audio else None) or 80
8482
audio_features = builder.input(
8583
"audio_features",
86-
dtype=ir.DataType.FLOAT,
84+
dtype=config.dtype,
8785
shape=[batch, "audio_seq_len", audio_input_size],
8886
)
89-
audio_features = _cast_encoder_input(op, audio_features, config)
9087

9188
past_key_values = _make_kv_cache_inputs(
9289
builder,

‎src/mobius/tasks/_phi4mm_multimodal.py‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
from mobius.tasks._base import (
3030
ComponentSpec,
3131
ModelTask,
32-
_cast_encoder_input,
3332
_make_graph,
3433
_make_model,
3534
build_decoder_from_embeds,
@@ -90,10 +89,9 @@ def _build_vision(
9089

9190
pixel_values = builder.input(
9291
"pixel_values",
93-
dtype=ir.DataType.FLOAT,
92+
dtype=config.dtype,
9493
shape=[batch, 3, image_size, image_size],
9594
)
96-
pixel_values = _cast_encoder_input(op, pixel_values, config)
9795
image_sizes = builder.input(
9896
"image_sizes",
9997
dtype=ir.DataType.INT64,
@@ -125,10 +123,9 @@ def _build_speech(
125123

126124
audio_embeds = builder.input(
127125
"audio_embeds",
128-
dtype=ir.DataType.FLOAT,
126+
dtype=config.dtype,
129127
shape=[batch, audio_seq_len, input_size],
130128
)
131-
audio_embeds = _cast_encoder_input(op, audio_embeds, config)
132129
audio_sizes = builder.input(
133130
"audio_sizes",
134131
dtype=ir.DataType.INT64,

‎src/mobius/tasks/_speech_language.py‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
from mobius.tasks._base import (
2424
ComponentSpec,
2525
ModelTask,
26-
_cast_encoder_input,
2726
_make_graph,
2827
_make_model,
2928
build_decoder_from_embeds,
@@ -88,10 +87,9 @@ def _build_audio_encoder(
8887

8988
input_features = builder.input(
9089
"input_features",
91-
dtype=ir.DataType.FLOAT,
90+
dtype=config.dtype,
9291
shape=[batch, n_mels, mel_seq],
9392
)
94-
input_features = _cast_encoder_input(op, input_features, config)
9593
# Mask is required: without it the encoder consumes padded mel
9694
# frames as if they were real audio, which causes the
9795
# downstream LLM to emit degenerate loops on any input padded

‎src/mobius/tasks/_vision_language.py‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
from mobius._model_package import ModelPackage
1313
from mobius.tasks._base import (
1414
ModelTask,
15-
_cast_encoder_input,
1615
_make_graph,
1716
_make_model,
1817
)
@@ -74,10 +73,9 @@ def build(
7473
pixel_dim = in_channels * temporal_patch_size * patch_size * patch_size
7574
pixel_values = builder.input(
7675
"pixel_values",
77-
dtype=ir.DataType.FLOAT,
76+
dtype=config.dtype,
7877
shape=[total_patches, pixel_dim],
7978
)
80-
pixel_values = _cast_encoder_input(op, pixel_values, config)
8179
# Image grid dimensions for position embedding interpolation
8280
num_images = ir.SymbolicDim("num_images")
8381
grid_thw = builder.input(

‎src/mobius/tasks/_vision_language_3model.py‎

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
from mobius.tasks._base import (
2222
ComponentSpec,
2323
ModelTask,
24-
_cast_encoder_input,
2524
_make_graph,
2625
_make_model,
2726
build_decoder_from_embeds,
@@ -87,10 +86,9 @@ def _build_vision(
8786
op = builder.op
8887
pixel_values = builder.input(
8988
"pixel_values",
90-
dtype=ir.DataType.FLOAT,
89+
dtype=config.dtype,
9190
shape=[batch, 3, image_size, image_size],
9291
)
93-
pixel_values = _cast_encoder_input(op, pixel_values, config)
9492
image_features = vision(op, pixel_values=pixel_values)
9593

9694
builder.add_output(image_features, "image_features")
@@ -140,10 +138,9 @@ def _build_vision(
140138
op = builder.op
141139
pixel_values = builder.input(
142140
"pixel_values",
143-
dtype=ir.DataType.FLOAT,
141+
dtype=config.dtype,
144142
shape=[total_patches, pixel_dim],
145143
)
146-
pixel_values = _cast_encoder_input(op, pixel_values, config)
147144
image_grid_thw = builder.input(
148145
"image_grid_thw",
149146
dtype=ir.DataType.INT64,
@@ -217,10 +214,9 @@ def _build_vision(
217214
op = builder.op
218215
pixel_values = builder.input(
219216
"pixel_values",
220-
dtype=ir.DataType.FLOAT,
217+
dtype=config.dtype,
221218
shape=[batch, 3, height, width],
222219
)
223-
pixel_values = _cast_encoder_input(op, pixel_values, config)
224220

225221
image_features = vision(
226222
op,

0 commit comments

Comments
 (0)