Skip to content

Fix BF16 vision encoder: keep OneHot values as float32 - #252

Merged
justinchuby merged 1 commit into
mainfrom
fix-onehot-bf16
May 5, 2026
Merged

Fix BF16 vision encoder: keep OneHot values as float32#252
justinchuby merged 1 commit into
mainfrom
fix-onehot-bf16

Conversation

@justinchuby

Copy link
Copy Markdown
Member

Summary

ONNX OneHot op does not support bfloat16 for the values tensor. When building Gemma4 with --dtype bf16, the vision encoder fails to load because CastLike casts the OneHot values to bf16 before the op.

Fix

Keep OneHot values as float32 and cast the output to model dtype afterward for the subsequent MatMul.

Testing

  • 13 gemma4 tests pass
  • BF16 build succeeds: mobius build --model google/gemma-4-e2b-it --dtype bf16 --no-weights

ONNX OneHot op doesn't support bfloat16 for the values tensor.
Keep OneHot values as float32 and cast the output to model dtype
afterward for the subsequent MatMul.

Previously, CastLike cast the values to bf16 before OneHot, causing
the entire vision encoder to fail at model load time.

Tested: bf16 build succeeds for google/gemma-4-e2b-it (all 4 models).
Signed-off-by: Justin Chu <justinchu@microsoft.com>
@github-actions

github-actions Bot commented May 5, 2026

Copy link
Copy Markdown

Performance Comparison

Comparing 0d8a6357cf8bde

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.

@github-actions

github-actions Bot commented May 5, 2026

Copy link
Copy Markdown

🏗️ Architecture Diff

Comparing 0d8a6357cf8bde

Model Sub-model Changes Status
gemma4 (gemma4) decoder 0
gemma4 (gemma4) embedding 0
gemma4 (gemma4) vision_encoder 2 🟡
gemma4_text model 0
gemma4 (gemma4) / vision_encoder — 2 change(s)

Op summary: 202 → 202 nodes

--- base
+++ head
@@ -181,8 +181,8 @@
 Mul
 Add
 Constant
-CastLike
 OneHot
+CastLike
 Transpose
 MatMul
 Constant

Added nodes:

  • + CastLike

Removed nodes:

  • - CastLike

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

@codecov

codecov Bot commented May 5, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@titaiwangms titaiwangms 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.

Did copilot miss op spec when it's designing model? Maybe we need to re-enforce into SKILL.md about op spec?

@justinchuby

Copy link
Copy Markdown
Member Author

Did copilot miss op spec when it's designing model? Maybe we need to re-enforce into SKILL.md about op spec?

Onehot does support it. I think it is missing support in ort: https://onnx.ai/onnx/operators/onnx__OneHot.html

@justinchuby
justinchuby merged commit c1daeaa into main May 5, 2026
23 of 27 checks passed
@justinchuby
justinchuby deleted the fix-onehot-bf16 branch May 5, 2026 21:09
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