Skip to content

tasks: declare vision/audio encoder inputs at config.dtype - #319

Merged
justinchuby merged 1 commit into
mainfrom
revert-encoder-input-f32-cast
May 27, 2026
Merged

justinchuby merged 1 commit into
mainfrom
revert-encoder-input-f32-cast

Conversation

@justinchuby

Copy link
Copy Markdown
Member

Summary

Revert PR #275 (and the matching original change for _gemma4.py / _vision_language_3model.py) that declared vision/audio encoder graph inputs as FLOAT with a Cast at graph entry. 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 directly:

  • matches the model's internal compute dtype
  • removes the extra Cast node at graph entry (one fewer op per inference)
  • simplifies test harnesses that previously had to cast feature-extractor output to np.float32

Before

pixel_values = builder.input("pixel_values", dtype=ir.DataType.FLOAT, shape=[...])
pixel_values = _cast_encoder_input(op, pixel_values, config)   # adds Cast when config.dtype != FLOAT

After

pixel_values = builder.input("pixel_values", dtype=config.dtype, shape=[...])

Files changed

  • tasks/_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: replace f32+Cast pattern with dtype=config.dtype
  • tasks/_base.py: drop the now-unused _cast_encoder_input helper
  • tests/build_graph_test.py: rewrite test_multimodal_encoder_inputs_* / test_gemma4_encoder_inputs_* to assert encoder inputs match config.dtype and that there is no Cast at graph entry

Out of scope

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 inputs go straight to the encoder.

Tests

L1 + unit suite: 2763 passed, 43 skipped (no regressions). ruff check + ruff format --check both pass.

…2 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]>
@github-actions

Copy link
Copy Markdown

Performance Comparison

Comparing 3ce0444 → 4a10564

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 66 66 +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 53 53 +0.0% ⚪
llama model_size_bytes 425 KB 425 KB +0.0% ⚪
llama num_nodes 61 61 +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 59 59 +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 61 61 +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 413 413 +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.

@codecov

codecov Bot commented May 27, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@github-actions

Copy link
Copy Markdown

🏗️ Architecture Diff

Comparing 3ce0444 → 4a10564

Model Sub-model Changes Status
bert (feature-extraction) model 0 ⚪
falcon model 0 ⚪
gemma2 model 0 ⚪
gemma4 (gemma4) decoder 0 ⚪
gemma4 (gemma4) embedding 0 ⚪
gemma4 (gemma4) vision_encoder 0 ⚪
gemma4_text model 0 ⚪
gpt2 model 0 ⚪
llama model 0 ⚪
llama (static-cache) model 0 ⚪
mamba (ssm-text-generation) model 0 ⚪
phi3 model 0 ⚪
phi3 (static-cache) model 0 ⚪
qwen model 0 ⚪
qwen (static-cache) model 0 ⚪
qwen2 model 0 ⚪
qwen2 (static-cache) model 0 ⚪
qwen2_moe model 0 ⚪
qwen2_moe (static-cache) model 0 ⚪
qwen3 model 0 ⚪
qwen3 (static-cache) model 0 ⚪
qwen3_5_moe (hybrid-text-generation) model 0 ⚪
qwen3_5_text (hybrid-text-generation) model 0 ⚪
qwen3_5_vl (hybrid-qwen-vl) decoder 0 ⚪
qwen3_5_vl (hybrid-qwen-vl) embedding 0 ⚪
qwen3_5_vl (hybrid-qwen-vl) vision_encoder 0 ⚪
qwen3_moe model 0 ⚪
qwen3_moe (static-cache) model 0 ⚪
qwen3_next (hybrid-text-generation) model 0 ⚪
t5 (seq2seq) decoder 0 ⚪
t5 (seq2seq) encoder 0 ⚪
whisper (speech-to-text) decoder 0 ⚪
whisper (speech-to-text) encoder 0 ⚪

No architecture changes detected. ✅


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

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

Reverts the prior pattern of declaring vision/audio encoder graph inputs as FLOAT with a Cast at graph entry. Per the ORT GenAI team, the runtime can deliver vision/audio inputs at the model's compute dtype directly, so declaring inputs at config.dtype removes an extra Cast op per inference and aligns encoder inputs with the convention already used for decoder/embedding/KV cache inputs.

Changes:

  • Replace dtype=ir.DataType.FLOAT + _cast_encoder_input(...) with dtype=config.dtype across all multi-model vision/audio task builders.
  • Drop the now-unused _cast_encoder_input helper from tasks/_base.py.
  • Rewrite the test_*_encoder_inputs_* tests to assert encoder inputs match config.dtype and that there is no Cast consuming pixel_values at graph entry.

Reviewed changes

Copilot reviewed 11 out of 11 changed files in this pull request and generated no comments.

Show a summary per file
File Description
src/mobius/tasks/_base.py Removes the _cast_encoder_input helper.
src/mobius/tasks/_vision_language.py pixel_values declared at config.dtype; helper import dropped.
src/mobius/tasks/_vision_language_3model.py Base/Qwen/Pixtral VL pixel_values declared at config.dtype.
src/mobius/tasks/_multimodal.py pixel_values and audio_features declared at config.dtype.
src/mobius/tasks/_speech_language.py input_features declared at config.dtype.
src/mobius/tasks/_audio_ctc.py input_features declared at config.dtype.
src/mobius/tasks/_fun_asr_speech_language.py input_features declared at config.dtype.
src/mobius/tasks/_hunyuan_vl_mot.py pixel_values declared at config.dtype.
src/mobius/tasks/_phi4mm_multimodal.py Vision pixel_values and speech audio_embeds declared at config.dtype.
src/mobius/tasks/_gemma4.py Vision pixel_values and audio input_features declared at config.dtype.
tests/build_graph_test.py Tests now assert encoder inputs match config.dtype with no entry-Cast.

@justinchuby
justinchuby merged commit 95baf2a into main May 27, 2026
24 checks passed
@justinchuby
justinchuby deleted the revert-encoder-input-f32-cast branch May 27, 2026 14:58
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.

2 participants