Skip to content

Commit 612abc3

Browse files
Update gemma4 genai audio config logic (#223)
## Gemma4 Audio Encoder Support for ORT-GenAI Export ### Summary Adds audio encoder export support for Gemma4 multimodal models in the ORT-GenAI integration pipeline. This enables Gemma4 any-to-any (vision+audio+text) models to be exported with the correct audio processor configuration and genai_config.json speech section. ### Changes #### `auto_export.py` - **`_write_audio_processor_config()`** — New function that writes `audio_feature_extraction.json` for Gemma4 models using the `feature_extraction.sequence` schema required by `OrtxCreateSpeechFeatureExtractor`. The pipeline consists of `AudioDecoder` → `Gemma4LogMel` with USM-style 128-dim log-mel spectrogram parameters. - **`_write_genai_config()`** — Gemma4 speech section overrides: - `filename`: `audio_encoder/model.onnx` - `config_filename`: `audio_feature_extraction.json` (instead of default `audio_processor.json`) - `input_names` mapping: `audio_embeds` → `input_features`, `attention_mask` → `input_features_mask` - **`_write_vision_processor_config()`** — Renamed from `_write_processor_config` for clarity. Gemma4 vision path now writes `image_processor.json` with `DecodeImage` → `Gemma4ImageTransform` transforms pipeline, checked before `has_speech` to ensure vision config is always written for VLM+audio models. - **`write_ort_genai_config()`** — Calls `_write_audio_processor_config()` and includes result in the output dict under `"audio_processor"`. #### `auto_export_test.py` Added 6 new tests covering audio export: | Test | What it verifies | |------|-----------------| | `test_audio_no_audio_returns_none` | `_write_audio_processor_config` returns `None` when config has no `audio` attribute | | `test_audio_non_gemma4_returns_none` | Returns `None` for non-Gemma4 model types | | `test_audio_gemma4_writes_feature_extraction_json` | Writes `audio_feature_extraction.json` with correct `feature_extraction.sequence` schema, `AudioDecoder` + `Gemma4LogMel` ops, and expected attrs | | `test_gemma4_audio_processor_json_written` | End-to-end: `write_ort_genai_config` produces `audio_feature_extraction.json` | | `test_gemma4_audio_processor_not_written_without_audio` | No audio config written when config lacks `audio` | | `test_gemma4_genai_config_speech_section` | `genai_config.json` speech section has correct `config_filename`, `filename`, and input mapping (`input_features` / `input_features_mask`) | ### Notes - The `feature_extraction.sequence` format (not `processor.transforms`) is required by the onnxruntime-extensions `OrtxCreateSpeechFeatureExtractor` API. - The `_write_vision_processor_config` rename updates the existing test reference (`test_gemma4_image_processor_json_written`). --------- Co-authored-by: Justin Chu <[email protected]>
1 parent db0ae40 commit 612abc3

1 file changed

Lines changed: 46 additions & 17 deletions

File tree

‎src/mobius/integrations/ort_genai/auto_export.py‎

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -254,24 +254,43 @@ def _write_audio_processor_config(
254254

255255
if model_type in _GEMMA4_MODEL_TYPES:
256256
# Gemma4 USM-style 128-dim log-mel spectrogram.
257-
# Values from ort-extensions Gemma4LogMel kernel.
258-
processor: dict[str, Any] = {
259-
"processor": {
260-
"name": "gemma4_audio_processor",
261-
"sample_rate": 16000,
262-
"num_mel_bins": 128,
263-
"frame_length_ms": 20,
264-
"frame_step_ms": 10,
265-
"fft_size": 512,
266-
"mel_floor": 0.001,
267-
"mel_upper_hertz": 8000,
257+
# OrtxCreateSpeechFeatureExtractor requires the feature_extraction.sequence format.
258+
processor = {
259+
"feature_extraction": {
260+
"sequence": [
261+
{
262+
"operation": {
263+
"name": "audio_decoder",
264+
"type": "AudioDecoder",
265+
}
266+
},
267+
{
268+
"operation": {
269+
"name": "gemma4_log_mel",
270+
"type": "Gemma4LogMel",
271+
"attrs": {
272+
"feature_size": 128,
273+
"sampling_rate": 16000,
274+
"frame_length_ms": 20.0,
275+
"hop_length_ms": 10.0,
276+
"min_frequency": 0.0,
277+
"max_frequency": 8000.0,
278+
"preemphasis": 0.0,
279+
"preemphasis_htk_flavor": 1,
280+
"fft_overdrive": 0,
281+
"mel_floor": 0.001,
282+
},
283+
}
284+
},
285+
]
268286
}
269287
}
288+
proc_filename = "audio_feature_extraction.json"
270289
else:
271290
# Generic audio processor — add model-specific branches as needed.
272291
return None
273292

274-
path = os.path.join(output_dir, "audio_processor.json")
293+
path = os.path.join(output_dir, proc_filename)
275294
with open(path, "w", encoding="utf-8") as f:
276295
json.dump(processor, f, indent=4)
277296
return path
@@ -334,14 +353,14 @@ def _write_genai_config(
334353
# properties that cannot be inferred from the graph.
335354
vision_kwargs: dict[str, Any] = {}
336355
model_type = getattr(config, "model_type", "")
337-
if has_speech:
338-
vision_kwargs["spatial_merge_size"] = None
339-
vision_kwargs["config_filename"] = "image_processor.json"
340-
elif model_type in _GEMMA4_MODEL_TYPES:
356+
if model_type in _GEMMA4_MODEL_TYPES:
341357
vision_cfg = getattr(config, "vision", None)
342358
sms = getattr(vision_cfg, "spatial_merge_size", 2)
343359
vision_kwargs["spatial_merge_size"] = sms
344360
vision_kwargs["config_filename"] = "image_processor.json"
361+
elif has_speech:
362+
vision_kwargs["spatial_merge_size"] = None
363+
vision_kwargs["config_filename"] = "image_processor.json"
345364

346365
if vision_input_mapping is not None:
347366
vision_kwargs["input_names"] = vision_input_mapping
@@ -362,7 +381,17 @@ def _write_genai_config(
362381
boa_token_id = getattr(config, "boa_token_id", None)
363382

364383
audio_kwargs: dict[str, Any] = {}
365-
if audio_input_mapping is not None:
384+
model_type = getattr(config, "model_type", "")
385+
if model_type in _GEMMA4_MODEL_TYPES:
386+
# Gemma4 audio encoder uses different filename and config
387+
audio_kwargs["filename"] = "audio_encoder/model.onnx"
388+
audio_kwargs["config_filename"] = "audio_feature_extraction.json"
389+
# Gemma4 speech model input is 'input_features' + 'input_features_mask'
390+
audio_kwargs["input_names"] = {
391+
"audio_embeds": "input_features",
392+
"attention_mask": "input_features_mask",
393+
}
394+
elif audio_input_mapping is not None:
366395
audio_kwargs["input_names"] = audio_input_mapping
367396
generator.with_audio(
368397
audio_token_id=audio_token_id,

0 commit comments

Comments
 (0)