Skip to content

Keep vision/audio inputs f32 in single-model tasks for f16/bf16 builds - #275

Merged
justinchuby merged 2 commits into
mainfrom
migrate-f32-encoder-inputs
May 6, 2026
Merged

justinchuby merged 2 commits into
mainfrom
migrate-f32-encoder-inputs

Conversation

@justinchuby

Copy link
Copy Markdown
Member

Summary

Apply the f32+Cast-at-input pattern to 3 remaining task files that still used config.dtype for vision/audio graph inputs.

Problem

When building with --dtype bf16 or --dtype f16, image/audio processor outputs are always f32. If graph inputs are declared at config.dtype (e.g. bf16), ORT raises type mismatches at load time.

Fix

Declare vision/audio graph inputs as ir.DataType.FLOAT and add a Cast to config.dtype at graph entry, matching the pattern already used in _gemma4.py and _vision_language_3model.py.

Files changed:

  • _vision_language.py: pixel_values (Qwen3-VL single-model task)
  • _multimodal.py: pixel_values + audio_features (generic multimodal task)
  • _audio_ctc.py: input_features (SenseVoiceSmall CTC task)

Tests

1204 passed, 40 skipped — no regressions.

Apply the f32+Cast-at-input pattern to 3 remaining task files:

- _vision_language.py: pixel_values (Qwen3-VL single-model)
- _multimodal.py: pixel_values + audio_features (generic multimodal)
- _audio_ctc.py: input_features (SenseVoiceSmall CTC)

Image/audio processors output f32. Declaring graph inputs as FLOAT
with a Cast to config.dtype at entry ensures correct type matching
regardless of the model's compute precision.

Co-authored-by: Copilot <[email protected]>
Signed-off-by: Justin Chu <[email protected]>
@github-actions

github-actions Bot commented May 6, 2026 •

Copy link
Copy Markdown

Performance Comparison

Comparing 6025fbb → 26f1c0c

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 408 408 +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 6, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 88.88889% with 2 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/mobius/tasks/_multimodal.py 0.00% 2 Missing ⚠️

📢 Thoughts on this report? Let us know!

@github-actions

github-actions Bot commented May 6, 2026 •

Copy link
Copy Markdown

🏗️ Architecture Diff

Comparing 6025fbb → 26f1c0c

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)

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]>
@justinchuby

Copy link
Copy Markdown
Member Author

Added DRY refactor: extracted _cast_encoder_input(op, tensor, config) helper to _base.py and replaced all 13 inline Cast blocks across 8 task files. Net -13 lines (39 added, 52 removed).

Commit: 26f1c0c

@justinchuby
justinchuby merged commit 5860be3 into main May 6, 2026
20 checks passed
@justinchuby
justinchuby deleted the migrate-f32-encoder-inputs branch May 6, 2026 16:37
justinchuby added a commit that referenced this pull request May 27, 2026
## 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

```python
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

```python
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.

Signed-off-by: justinchuby <[email protected]>
Co-authored-by: justinchuby <[email protected]>
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.

1 participant