Repository navigation
feat: add input_features_mask to Gemma4 audio encoder - #207
Conversation
Add bool mask input to the audio encoder ONNX graph for proper variable-length audio handling in batched inference. The mask tracks valid positions through conv subsampling layers and prevents padded positions from corrupting Conformer attention features. Matches the onnx-community/gemma-4-E2B-it-ONNX audio encoder interface. Co-authored-by: Copilot <[email protected]> Signed-off-by: Justin Chu <[email protected]>
Performance Comparison
|
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
🏗️ Architecture Diff
No architecture changes detected. ✅ Legend: ⚪ No change · 🔵 Minor (attrs/inits) · 🟡 Moderate (nodes added/removed) · 🔴 Major (interface changed) |
There was a problem hiding this comment.
Pull request overview
Adds an input_features_mask: BOOL [B, T] input to the Gemma4 audio encoder ONNX graph and threads it through the audio Conformer stack to support variable-length, batched audio inference.
Changes:
- Extend Gemma4 audio task/model interfaces to accept
input_features_maskas a graph input and forward it through the audio encoder. - Implement mask propagation through conv subsampling (zero padded frames + stride-2 mask downsampling) and apply a key-padding additive bias in attention.
- Update graph-build tests to assert the new audio input is present.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
tests/build_graph_test.py |
Asserts input_features_mask is present as an audio graph input. |
src/mobius/tasks/_gemma4.py |
Adds input_features_mask as an ONNX graph input and passes it into the audio module. |
src/mobius/models/gemma4.py |
Extends audio encoder forward signature to accept and forward input_features_mask. |
src/mobius/components/_gemma4_audio.py |
Implements mask downsampling/zeroing in conv subsampling and padding bias in attention; threads mask through audio encoder layers. |
src/mobius/components/_gemma4_audio_test.py |
Adjusts conv subsampling test for the new (features, mask) return signature. |
Co-authored-by: Copilot <[email protected]> Signed-off-by: Justin Chu <[email protected]>
- gemma4_multimodal.py: pass all-True mask for single-clip audio - Update any golden/integration tests that feed the audio model - Add TODO for exposing downsampled mask as output for batched inference Co-authored-by: Copilot <[email protected]> Signed-off-by: Justin Chu <[email protected]>
|
I think it would be helpful to clarify whether the mask can be arbitrary or if there is an assumption/requirement that it is all Trues up to some length and all False afterwards. |
The mask is produced by HF Gemma4AudioFeatureExtractor which pads audio to equal batch lengths — all True (valid) frames precede all False (padding) frames. Document this requirement on both the task docstring and the ConvSubsampling component. Co-authored-by: Copilot <[email protected]> Signed-off-by: Justin Chu <[email protected]>
Signed-off-by: Justin Chu <[email protected]>
Remove because it is outdated. Signed-off-by: Justin Chu <[email protected]>
Signed-off-by: Justin Chu <[email protected]>
#209) ## Summary Updates the ort-genai runtime integration to generate the correct image processor config format for Gemma4 models. ## Problem The `OrtxCreateProcessor` API in onnxruntime-extensions requires a JSON config with a `processor.transforms` array defining an ordered pipeline of image processing operations. Previously, Gemma4 models generated a HuggingFace-style `processor_config.json` with raw config values (`image_size`, `patch_size`, etc.), which caused a runtime crash: RuntimeError: [json.exception.out_of_range.403] key 'transforms' not found ## Changes **`src/mobius/integrations/ort_genai/auto_export.py`** - `_write_processor_config()`: For `gemma4`/`gemma4_text` models, generates `image_processor.json` with the onnxruntime-extensions transforms pipeline format (`DecodeImage` → `Gemma4ImageTransform`) instead of the HF-style `processor_config.json`. Reads `patch_size`, `max_soft_tokens`, and `pooling_kernel_size` from the model config. - `_write_genai_config()`: Sets `vision.config_filename` to `"image_processor.json"` for Gemma4 models so the genai runtime points to the correct file. Non-Gemma4 models are unaffected and continue generating `processor_config.json`. ## Generated `image_processor.json` example ```json { "processor": { "name": "gemma_4_image_processing", "transforms": [ { "operation": { "name": "decode_image", "type": "DecodeImage", "attrs": { "color_space": "RGB" } } }, { "operation": { "name": "gemma4_image_transform", "type": "Gemma4ImageTransform", "attrs": { "patch_size": 16, "max_soft_tokens": 280, "pooling_kernel_size": 3 } } } ] } }
Thread the downsampled mask through the encoder and expose it as a second graph output. Callers can use it to strip padding rows before scattering audio features into text embeddings during batched variable-length inference. Audio encoder outputs: - audio_features [B, T//4, text_hidden_size] - audio_features_mask [B, T//4] (bool) Co-authored-by: Copilot <[email protected]> Signed-off-by: Justin Chu <[email protected]>
Vision: padded patches with (-1,-1) sentinel position IDs. Audio: contiguous bool mask, downsampled through conv layers. Decoder: standard attention_mask for KV cache padding. Co-authored-by: Copilot <[email protected]> Signed-off-by: Justin Chu <[email protected]>
Both demo_audio and demo_vision_audio now capture audio_features_mask from the encoder output and use it to remove padding rows before counting tokens and scattering into text embeddings. Co-authored-by: Copilot <[email protected]> Signed-off-by: Justin Chu <[email protected]>
|
Examples validated |
Add bool mask input (
input_features_mask: BOOL [B, T]) to the audio encoder ONNX graph for proper variable-length audio handling in batched inference.Changes
Gemma4ConvSubsampling): Acceptsinput_features_mask, zeros out padded positions before each conv layer, downsamples the mask by stride 2 after each stage (matching HFGemma4AudioSubSampleConvProjectionLayer)Gemma4Attention): Accepts optionalattention_maskand applies additive padding bias to prevent attending to padded key positionsGemma4AudioLayer,Gemma4AudioEncoder): Thread the mask through the full Conformer stackGemma4Task._build_audio): Addsinput_features_maskas a graph inputMatches the onnx-community/gemma-4-E2B-it-ONNX audio encoder interface.