Skip to content

Commit 26f1c0c

Browse files
justinchubyCopilot
andcommitted
Extract _cast_encoder_input helper to DRY up f32 Cast pattern
Add _cast_encoder_input(op, tensor, config) to _base.py and replace all 13 inline if-config.dtype-Cast blocks across 8 task files. The helper encapsulates the pattern: vision/audio graph inputs are declared as FLOAT (matching processor output), and Cast to config.dtype is added when the model uses f16/bf16. Co-authored-by: Copilot <[email protected]> Signed-off-by: Justin Chu <[email protected]>
1 parent 63a6b0a commit 26f1c0c

9 files changed

Lines changed: 39 additions & 52 deletions

‎src/mobius/tasks/_audio_ctc.py‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,12 @@
2222

2323
from mobius._configs import ArchitectureConfig
2424
from mobius._model_package import ModelPackage
25-
from mobius.tasks._base import ModelTask, _make_graph, _make_model
25+
from mobius.tasks._base import (
26+
ModelTask,
27+
_cast_encoder_input,
28+
_make_graph,
29+
_make_model,
30+
)
2631

2732

2833
class AudioCTCTask(ModelTask):
@@ -46,10 +51,7 @@ def build(
4651
dtype=ir.DataType.FLOAT,
4752
shape=["batch", "time", input_dim],
4853
)
49-
# Audio input is always f32 (matching audio processor output).
50-
# Cast at graph entry for f16/bf16 builds.
51-
if config.dtype and config.dtype != ir.DataType.FLOAT:
52-
input_features = op.Cast(input_features, to=config.dtype)
54+
input_features = _cast_encoder_input(op, input_features, config)
5355
language_id = builder.input(
5456
"language_id",
5557
dtype=ir.DataType.INT64,

‎src/mobius/tasks/_base.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,18 @@ 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+
133145
class ModelTask(ABC):
134146
"""Abstract base defining how to wire a module into an ONNX graph.
135147

‎src/mobius/tasks/_fun_asr_speech_language.py‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from mobius.tasks._base import (
2727
ComponentSpec,
2828
ModelTask,
29+
_cast_encoder_input,
2930
_make_graph,
3031
_make_model,
3132
build_decoder_from_embeds,
@@ -93,15 +94,12 @@ def _build_audio_encoder(
9394
graph, builder = _make_graph(name="audio_encoder")
9495
op = builder.op
9596

96-
# Audio encoder input is always f32 (matching audio processor output).
97-
# Cast at graph entry for f16/bf16 builds.
9897
input_features = builder.input(
9998
"input_features",
10099
dtype=ir.DataType.FLOAT,
101100
shape=[batch, seq_len, input_dim],
102101
)
103-
if config.dtype and config.dtype != ir.DataType.FLOAT:
104-
input_features = op.Cast(input_features, to=config.dtype)
102+
input_features = _cast_encoder_input(op, input_features, config)
105103

106104
audio_features = audio_encoder(op, input_features)
107105

‎src/mobius/tasks/_gemma4.py‎

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
from mobius._model_package import ModelPackage
2929
from mobius.tasks._base import (
3030
ModelTask,
31+
_cast_encoder_input,
3132
_make_graph,
3233
_make_model,
3334
)
@@ -298,16 +299,12 @@ def _build_vision(
298299
graph, builder = _make_graph(name="vision_encoder")
299300
op = builder.op
300301

301-
# Vision encoder input is always f32 (matching GenAI's image processor
302-
# output). When the model uses f16/bf16, add a Cast at the graph entry
303-
# so weights can stay at the requested dtype for memory efficiency.
304302
pixel_values = builder.input(
305303
"pixel_values",
306304
dtype=ir.DataType.FLOAT,
307305
shape=[batch, num_patches, pixel_dim],
308306
)
309-
if config.dtype and config.dtype != ir.DataType.FLOAT:
310-
pixel_values = op.Cast(pixel_values, to=config.dtype)
307+
pixel_values = _cast_encoder_input(op, pixel_values, config)
311308
pixel_position_ids = builder.input(
312309
"pixel_position_ids",
313310
dtype=ir.DataType.INT64,
@@ -357,11 +354,10 @@ def _build_audio(
357354

358355
input_features = builder.input(
359356
"input_features",
360-
dtype=ir.DataType.FLOAT, # Always f32 (matching audio processor output)
357+
dtype=ir.DataType.FLOAT,
361358
shape=[batch, time, input_size],
362359
)
363-
if config.dtype and config.dtype != ir.DataType.FLOAT:
364-
input_features = op.Cast(input_features, to=config.dtype)
360+
input_features = _cast_encoder_input(op, input_features, config)
365361
input_features_mask = builder.input(
366362
"input_features_mask",
367363
dtype=ir.DataType.BOOL,

‎src/mobius/tasks/_multimodal.py‎

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from mobius._model_package import ModelPackage
1313
from mobius.tasks._base import (
1414
ModelTask,
15+
_cast_encoder_input,
1516
_make_graph,
1617
_make_model,
1718
)
@@ -77,20 +78,15 @@ def build(
7778
dtype=ir.DataType.FLOAT,
7879
shape=[batch, 3, image_size, image_size],
7980
)
80-
# Vision input is always f32 (matching image processor output).
81-
# Cast at graph entry for f16/bf16 builds.
82-
if config.dtype and config.dtype != ir.DataType.FLOAT:
83-
pixel_values = op.Cast(pixel_values, to=config.dtype)
81+
pixel_values = _cast_encoder_input(op, pixel_values, config)
8482

8583
audio_input_size = (config.audio.input_size if config.audio else None) or 80
8684
audio_features = builder.input(
8785
"audio_features",
8886
dtype=ir.DataType.FLOAT,
8987
shape=[batch, "audio_seq_len", audio_input_size],
9088
)
91-
# Audio input is always f32 (matching audio processor output).
92-
if config.dtype and config.dtype != ir.DataType.FLOAT:
93-
audio_features = op.Cast(audio_features, to=config.dtype)
89+
audio_features = _cast_encoder_input(op, audio_features, config)
9490

9591
past_key_values = _make_kv_cache_inputs(
9692
builder,

‎src/mobius/tasks/_phi4mm_multimodal.py‎

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
from mobius.tasks._base import (
3030
ComponentSpec,
3131
ModelTask,
32+
_cast_encoder_input,
3233
_make_graph,
3334
_make_model,
3435
build_decoder_from_embeds,
@@ -87,15 +88,12 @@ def _build_vision(
8788
graph, builder = _make_graph(name="vision_encoder")
8889
op = builder.op
8990

90-
# Vision encoder input is always f32 (matching GenAI's image processor
91-
# output). Cast at graph entry for f16/bf16 builds.
9291
pixel_values = builder.input(
9392
"pixel_values",
9493
dtype=ir.DataType.FLOAT,
9594
shape=[batch, 3, image_size, image_size],
9695
)
97-
if config.dtype and config.dtype != ir.DataType.FLOAT:
98-
pixel_values = op.Cast(pixel_values, to=config.dtype)
96+
pixel_values = _cast_encoder_input(op, pixel_values, config)
9997
image_sizes = builder.input(
10098
"image_sizes",
10199
dtype=ir.DataType.INT64,
@@ -125,15 +123,12 @@ def _build_speech(
125123
graph, builder = _make_graph(name="audio_encoder")
126124
op = builder.op
127125

128-
# Audio encoder input is always f32 (matching audio processor output).
129-
# Cast at graph entry for f16/bf16 builds.
130126
audio_embeds = builder.input(
131127
"audio_embeds",
132128
dtype=ir.DataType.FLOAT,
133129
shape=[batch, audio_seq_len, input_size],
134130
)
135-
if config.dtype and config.dtype != ir.DataType.FLOAT:
136-
audio_embeds = op.Cast(audio_embeds, to=config.dtype)
131+
audio_embeds = _cast_encoder_input(op, audio_embeds, config)
137132
audio_sizes = builder.input(
138133
"audio_sizes",
139134
dtype=ir.DataType.INT64,

‎src/mobius/tasks/_speech_language.py‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from mobius.tasks._base import (
2424
ComponentSpec,
2525
ModelTask,
26+
_cast_encoder_input,
2627
_make_graph,
2728
_make_model,
2829
build_decoder_from_embeds,
@@ -85,15 +86,12 @@ def _build_audio_encoder(
8586
graph, builder = _make_graph(name="audio_encoder")
8687
op = builder.op
8788

88-
# Audio encoder input is always f32 (matching audio processor output).
89-
# Cast at graph entry for f16/bf16 builds.
9089
input_features = builder.input(
9190
"input_features",
9291
dtype=ir.DataType.FLOAT,
9392
shape=[batch, n_mels, mel_seq],
9493
)
95-
if config.dtype and config.dtype != ir.DataType.FLOAT:
96-
input_features = op.Cast(input_features, to=config.dtype)
94+
input_features = _cast_encoder_input(op, input_features, config)
9795

9896
audio_features = audio_encoder(op, input_features)
9997

‎src/mobius/tasks/_vision_language.py‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from mobius._model_package import ModelPackage
1313
from mobius.tasks._base import (
1414
ModelTask,
15+
_cast_encoder_input,
1516
_make_graph,
1617
_make_model,
1718
)
@@ -76,10 +77,7 @@ def build(
7677
dtype=ir.DataType.FLOAT,
7778
shape=[total_patches, pixel_dim],
7879
)
79-
# Vision input is always f32 (matching image processor output).
80-
# Cast at graph entry for f16/bf16 builds.
81-
if config.dtype and config.dtype != ir.DataType.FLOAT:
82-
pixel_values = op.Cast(pixel_values, to=config.dtype)
80+
pixel_values = _cast_encoder_input(op, pixel_values, config)
8381
# Image grid dimensions for position embedding interpolation
8482
num_images = ir.SymbolicDim("num_images")
8583
grid_thw = builder.input(

‎src/mobius/tasks/_vision_language_3model.py‎

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from mobius.tasks._base import (
2222
ComponentSpec,
2323
ModelTask,
24+
_cast_encoder_input,
2425
_make_graph,
2526
_make_model,
2627
build_decoder_from_embeds,
@@ -84,15 +85,12 @@ def _build_vision(
8485

8586
graph, builder = _make_graph(name="vision_encoder")
8687
op = builder.op
87-
# Vision encoder input is always f32 (matching GenAI's image processor
88-
# output). Cast at graph entry for f16/bf16 builds.
8988
pixel_values = builder.input(
9089
"pixel_values",
9190
dtype=ir.DataType.FLOAT,
9291
shape=[batch, 3, image_size, image_size],
9392
)
94-
if config.dtype and config.dtype != ir.DataType.FLOAT:
95-
pixel_values = op.Cast(pixel_values, to=config.dtype)
93+
pixel_values = _cast_encoder_input(op, pixel_values, config)
9694
image_features = vision(op, pixel_values=pixel_values)
9795

9896
builder.add_output(image_features, "image_features")
@@ -140,15 +138,12 @@ def _build_vision(
140138

141139
graph, builder = _make_graph(name="vision_encoder")
142140
op = builder.op
143-
# Vision encoder input is always f32 (matching GenAI's image processor
144-
# output). Cast at graph entry for f16/bf16 builds.
145141
pixel_values = builder.input(
146142
"pixel_values",
147143
dtype=ir.DataType.FLOAT,
148144
shape=[total_patches, pixel_dim],
149145
)
150-
if config.dtype and config.dtype != ir.DataType.FLOAT:
151-
pixel_values = op.Cast(pixel_values, to=config.dtype)
146+
pixel_values = _cast_encoder_input(op, pixel_values, config)
152147
image_grid_thw = builder.input(
153148
"image_grid_thw",
154149
dtype=ir.DataType.INT64,
@@ -220,15 +215,12 @@ def _build_vision(
220215

221216
graph, builder = _make_graph(name="vision_encoder")
222217
op = builder.op
223-
# Vision encoder input is always f32 (matching GenAI's image processor
224-
# output). Cast at graph entry for f16/bf16 builds.
225218
pixel_values = builder.input(
226219
"pixel_values",
227220
dtype=ir.DataType.FLOAT,
228221
shape=[batch, 3, height, width],
229222
)
230-
if config.dtype and config.dtype != ir.DataType.FLOAT:
231-
pixel_values = op.Cast(pixel_values, to=config.dtype)
223+
pixel_values = _cast_encoder_input(op, pixel_values, config)
232224

233225
image_features = vision(
234226
op,

0 commit comments

Comments
 (0)