Skip to content

Enhance embedding introspection for GenAI config - #321

Merged
justinchuby merged 4 commits into
mainfrom
asonawane/gemma
May 29, 2026
Merged

Enhance embedding introspection for GenAI config#321
justinchuby merged 4 commits into
mainfrom
asonawane/gemma

Conversation

@apsonawane

@apsonawane apsonawane commented May 28, 2026

Copy link
Copy Markdown
Contributor

Summary

Two fixes for Gemma4 multimodal export and genai_config generation.

Introspect embedding model outputs in genai_config.json

The Gemma4 embedding model produces per_layer_inputs alongside inputs_embeds, but genai_config.json only listed inputs_embeds in the embedding outputs section. This caused onnxruntime-genai to not bind the per_layer_inputs output, so ORT allocated a separate buffer and the decoder received uninitialized data.

Added _introspect_outputs() to auto-discover embedding model outputs from the ONNX graph, and embedding_output_names parameter to GenaiConfigGenerator.with_vision().

Files: integrations/ort_genai/auto_export.py, integrations/ort_genai/genai_config.py

Pass --ep to mobius CLI in export script

build_and_export() in gemma4_genai.py did not pass the execution provider to mobius build. This caused all exports to use the default EP (which has empty gqa_dtypes), so KV-shared layers fell back to standard Attention instead of GQA — leading to past_present_share_buffer failures at runtime.

Now --device cuda correctly maps to --ep cuda, producing all-GQA models.

Files: examples/gemma4_genai.py

Testing

  • CPU fp32 export: GQA=35, Attention=0 ✅
  • CUDA fp16 export: GQA=35, Attention=0 ✅
  • Text-only generation (CPU): clean output ✅
  • Multimodal image+text generation: no NaN, pixel_position_ids extracted correctly ✅
  • per_layer_inputs forwarded from embedding to decoder ✅
  • Backward compatible: existing non-Gemma4 models unaffected ✅

@github-actions

github-actions Bot commented May 28, 2026

Copy link
Copy Markdown

Performance Comparison

Comparing ccf96d62159047

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 28, 2026

Copy link
Copy Markdown

The author of this PR, apsonawane, is not an activated member of this organization on Codecov.
Please activate this user on Codecov to display this PR comment.
Coverage data is still being uploaded to Codecov.io for purposes of overall coverage calculations.
Please don't hesitate to email us at support@codecov.io with any questions.

@github-actions

github-actions Bot commented May 28, 2026

Copy link
Copy Markdown

🏗️ Architecture Diff

Comparing ccf96d62159047

Model Sub-model Changes Status

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

This PR addresses Gemma4 multimodal ORT GenAI export issues by aligning vision input dtype handling, ensuring embedding outputs are represented in genai_config, and passing the selected execution provider through the example export flow.

Changes:

  • Declares Gemma4 vision pixel_values as float32 and casts internally to model dtype.
  • Adds embedding output-name overrides and graph-output introspection for ORT GenAI config generation.
  • Propagates --device to mobius build --ep in the Gemma4 example script.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/mobius/tasks/_gemma4.py Changes Gemma4 vision encoder pixel_values input dtype to float32.
src/mobius/models/gemma4.py Casts float32 vision inputs to model dtype inside the vision encoder.
src/mobius/integrations/ort_genai/genai_config.py Adds embedding_output_names support to vision config generation.
src/mobius/integrations/ort_genai/auto_export.py Introspects embedding model outputs and forwards them into genai_config.
examples/gemma4_genai.py Passes the selected device as the Mobius CLI execution provider.

Comment thread src/mobius/integrations/ort_genai/genai_config.py
Comment thread src/mobius/integrations/ort_genai/auto_export.py
@justinchuby

Copy link
Copy Markdown
Member

Wasn't the decision to cast inputs in genai? I just merged a PR for that behavior. Or is there a technical difficulty with vision (overflow etc. )?

@justinchuby justinchuby left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving to unblock. LMK the state of the input contract

@apsonawane

Copy link
Copy Markdown
Contributor Author

yes, sorry for the confusion. Casting is happening in genai. I reverted it here

Copilot AI requested a review from justinchuby May 29, 2026 05:05
@justinchuby
justinchuby enabled auto-merge (squash) May 29, 2026 14:48
@justinchuby justinchuby changed the title Accept float32 pixel_values and enhance embedding introspection Enhance embedding introspection for GenAI config May 29, 2026
@justinchuby
justinchuby disabled auto-merge May 29, 2026 14:52
@justinchuby
justinchuby enabled auto-merge (squash) May 29, 2026 14:52
@justinchuby
justinchuby merged commit ef17fcb into main May 29, 2026
20 of 21 checks passed
@justinchuby
justinchuby deleted the asonawane/gemma branch May 29, 2026 14:52
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.

4 participants