fix: Gemma4 genai config - vision inputs, decoder input_ids, processor - #199
Merged
Conversation
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>
Performance Comparison
|
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
Contributor
There was a problem hiding this comment.
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_idsfor the vision model and omitspatial_merge_size. - Extend
GenaiConfigGeneratorto support adding extra decoder inputs (used to includeinput_idsalongsideinputs_embedsfor 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. |
- 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
force-pushed
the
justinchu/gemma4-genai-config
branch
from
April 23, 2026 19:39
530b27b to
864906b
Compare
This was referenced Apr 23, 2026
Closed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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(notimage_grid_thw). Added Gemma4-specific branch in_write_genai_config()that sets the correct input names and disablesspatial_merge_size.2. Decoder input_ids (genai_config.py)
Gemma4 decoders need
input_idsalongsideinputs_embedsfor per-layer token embeddings (E2B architecture). Addedwith_extra_decoder_inputs()method toGenaiConfigGeneratorand 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 withname,tokens_per_image,mean, andstdfields wrapped under aprocessorkey, matching the ort-extensions expected format.4. Reference config updated
Added
input_idsto decoder inputs inexamples/gemma4/ort_genai/vlm/genai_config.json.Testing