Skip to content

fix: Gemma4 genai config - vision inputs, decoder input_ids, processor - #199

Merged
justinchuby merged 5 commits into
mainfrom
justinchu/gemma4-genai-config
Apr 23, 2026
Merged

fix: Gemma4 genai config - vision inputs, decoder input_ids, processor#199
justinchuby merged 5 commits into
mainfrom
justinchu/gemma4-genai-config

Conversation

@justinchuby

Copy link
Copy Markdown
Member

Summary

Fix Gemma4 ORT GenAI config generation to produce correct genai_config.json and processor_config.json.

Changes

1. Vision inputs (auto_export.py)

Gemma4 uses pixel_values + pixel_position_ids (not image_grid_thw). Added Gemma4-specific branch in _write_genai_config() that sets the correct input names and disables spatial_merge_size.

2. Decoder input_ids (genai_config.py)

Gemma4 decoders need input_ids alongside inputs_embeds for per-layer token embeddings (E2B architecture). Added with_extra_decoder_inputs() method to GenaiConfigGenerator and wired it for Gemma4 in _write_genai_config().

3. Processor config (auto_export.py)

Updated _write_processor_config() to detect Gemma4 and write the correct format with name, tokens_per_image, mean, and std fields wrapped under a processor key, matching the ort-extensions expected format.

4. Reference config updated

Added input_ids to decoder inputs in examples/gemma4/ort_genai/vlm/genai_config.json.

Testing

  • All 85 ORT GenAI tests pass
  • All 12 Gemma4 build graph tests pass
  • Full test suite: 2557 passed, 41 skipped
  • Linter clean

1. Vision inputs: use pixel_values + pixel_position_ids (not image_grid_thw)
2. Decoder inputs: include input_ids alongside inputs_embeds
   (needed for per-layer token embeddings in Gemma4 E2B)
3. Processor config: add Gemma4-specific fields (name, tokens_per_image)
4. Update reference config in examples/

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Signed-off-by: Justin Chu <justinchu@microsoft.com>
@github-actions

github-actions Bot commented Apr 23, 2026

Copy link
Copy Markdown

🏗️ Architecture Diff

Comparing 1045792864906b

Model Sub-model Changes Status

No architecture changes detected.


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

@github-actions

github-actions Bot commented Apr 23, 2026

Copy link
Copy Markdown

Performance Comparison

Comparing 1045792864906b

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 Apr 23, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 95.12195% with 10 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/mobius/integrations/ort_genai/auto_export.py 72.97% 4 Missing and 6 partials ⚠️

📢 Thoughts on this report? Let us know!

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 fixes ORT GenAI config generation for Gemma4 exports so that genai_config.json and processor_config.json match Gemma4’s actual model IO contracts (vision inputs, decoder inputs) and the expected ort-extensions processor schema.

Changes:

  • Add a Gemma4-specific ORT GenAI config path to use pixel_values + pixel_position_ids for the vision model and omit spatial_merge_size.
  • Extend GenaiConfigGenerator to support adding extra decoder inputs (used to include input_ids alongside inputs_embeds for Gemma4 VL).
  • Update Gemma4 processor config output format and update the example reference genai_config.json.

Reviewed changes

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

File Description
src/mobius/integrations/ort_genai/genai_config.py Adds with_extra_decoder_inputs() and merges extra decoder inputs into the generated decoder section.
src/mobius/integrations/ort_genai/auto_export.py Gemma4-specific processor config schema and Gemma4-specific VLM genai_config vision + decoder input wiring.
examples/gemma4/ort_genai/vlm/genai_config.json Updates example reference config to include input_ids in decoder inputs.

Comment thread src/mobius/integrations/ort_genai/auto_export.py Outdated
Comment thread src/mobius/integrations/ort_genai/genai_config.py Outdated
Comment thread src/mobius/integrations/ort_genai/auto_export.py Outdated
Comment thread src/mobius/integrations/ort_genai/auto_export.py Outdated
Comment thread src/mobius/integrations/ort_genai/auto_export.py Outdated
justinchuby and others added 4 commits April 23, 2026 19:02
- Use mm_tokens_per_image instead of max_soft_tokens for VisionConfig
- Coalesce None to defaults for image_size/patch_size
- Add unit tests for with_extra_decoder_inputs, Gemma4 vision
  mapping, and decoder input_ids

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Signed-off-by: Justin Chu <justinchu@microsoft.com>
Replace hard-coded model-specific input name mappings with dynamic
introspection of the actual ONNX graph inputs from the ModelPackage.
This eliminates the need for per-model if/elif branches and ensures
the genai config always matches the built graph.

The ModelPackage is threaded from write_ort_genai_config() through
to the config generator. Decoder, vision, and embedding input names
are extracted from graph.inputs, filtering out KV cache entries.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Signed-off-by: Justin Chu <justinchu@microsoft.com>
Add Stage 5 to optimize_model() that removes graph inputs with zero
consumers. After EP-aware fusion (e.g. GQA absorbs RoPE), some inputs
like position_ids become dead. Removing them produces cleaner models
and avoids requiring the runtime to provide dummy values.

Also remove incorrect mean/std from Gemma4 processor_config — Gemma4
rescales pixels to [0,1] without ImageNet normalization (per
ort-extensions Gemma4ImageTransform).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Signed-off-by: Justin Chu <justinchu@microsoft.com>
Move dead graph input removal from inline code in optimize_model()
to RemoveDeadGraphInputsPass in _passes/. Add it to the Stage 4
fold PassManager so it runs alongside other cleanup passes.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Signed-off-by: Justin Chu <justinchu@microsoft.com>
@justinchuby
justinchuby force-pushed the justinchu/gemma4-genai-config branch from 530b27b to 864906b Compare April 23, 2026 19:39
@justinchuby
justinchuby merged commit e845a3d into main Apr 23, 2026
6 checks passed
@justinchuby
justinchuby deleted the justinchu/gemma4-genai-config branch April 23, 2026 19:40
This was referenced Apr 23, 2026
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