Skip to content

feat: add input_features_mask to Gemma4 audio encoder - #207

Merged
justinchuby merged 13 commits into
mainfrom
justinchu/audio-mask
Apr 25, 2026
Merged

justinchuby merged 13 commits into
mainfrom
justinchu/audio-mask

Conversation

@justinchuby

Copy link
Copy Markdown
Member

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

  • Conv subsampling (Gemma4ConvSubsampling): Accepts input_features_mask, zeros out padded positions before each conv layer, downsamples the mask by stride 2 after each stage (matching HF Gemma4AudioSubSampleConvProjectionLayer)
  • Attention (Gemma4Attention): Accepts optional attention_mask and applies additive padding bias to prevent attending to padded key positions
  • Encoder layer/encoder (Gemma4AudioLayer, Gemma4AudioEncoder): Thread the mask through the full Conformer stack
  • Task (Gemma4Task._build_audio): Adds input_features_mask as a graph input
  • Tests: Updated to verify the new input

Matches the onnx-community/gemma-4-E2B-it-ONNX audio encoder interface.

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]>
@github-actions

github-actions Bot commented Apr 24, 2026 •

Copy link
Copy Markdown

Performance Comparison

Comparing 661b235 → 3adb5f6

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 24, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.82609% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/mobius/tasks/_gemma4.py 83.33% 0 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

@github-actions

github-actions Bot commented Apr 24, 2026 •

Copy link
Copy Markdown

🏗️ Architecture Diff

Comparing 661b235 → 3adb5f6

Model Sub-model Changes Status
bert (feature-extraction) model 0 ⚪
falcon model 0 ⚪
gemma2 model 0 ⚪
gemma4 (gemma4) decoder 0 ⚪
gemma4 (gemma4) embedding 0 ⚪
gemma4 (gemma4) vision 0 ⚪
gemma4_text model 0 ⚪
gpt2 model 0 ⚪
llama model 0 ⚪
llama (static-cache) model 0 ⚪
mamba (ssm-text-generation) model 0 ⚪
phi3 model 0 ⚪
phi3 (static-cache) model 0 ⚪
qwen model 0 ⚪
qwen (static-cache) model 0 ⚪
qwen2 model 0 ⚪
qwen2 (static-cache) model 0 ⚪
qwen2_moe model 0 ⚪
qwen2_moe (static-cache) model 0 ⚪
qwen3 model 0 ⚪
qwen3 (static-cache) model 0 ⚪
qwen3_5_moe (hybrid-text-generation) model 0 ⚪
qwen3_5_text (hybrid-text-generation) model 0 ⚪
qwen3_5_vl (hybrid-qwen-vl) decoder 0 ⚪
qwen3_5_vl (hybrid-qwen-vl) embedding 0 ⚪
qwen3_5_vl (hybrid-qwen-vl) vision 0 ⚪
qwen3_moe model 0 ⚪
qwen3_moe (static-cache) model 0 ⚪
qwen3_next (hybrid-text-generation) model 0 ⚪
t5 (seq2seq) decoder 0 ⚪
t5 (seq2seq) encoder 0 ⚪
whisper (speech-to-text) decoder 0 ⚪
whisper (speech-to-text) encoder 0 ⚪

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

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_mask as 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.

Comment thread src/mobius/components/_gemma4_audio.py Resolved
Comment thread src/mobius/components/_gemma4_audio.py Resolved
- 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]>
Comment thread src/mobius/tasks/_gemma4.py Resolved
Comment thread src/mobius/components/_gemma4_audio.py Resolved
@gramalingam

Copy link
Copy Markdown
Collaborator

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.

Comment thread src/mobius/components/_gemma4_audio.py Resolved
justinchuby and others added 10 commits April 25, 2026 01:01
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]>
#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]>
@justinchuby
justinchuby merged commit cd6155c into main Apr 25, 2026
20 of 23 checks passed
@justinchuby
justinchuby deleted the justinchu/audio-mask branch April 25, 2026 01:53
@justinchuby

Copy link
Copy Markdown
Member Author

Examples validated

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