Skip to content

fix(cli): render Gemma 4 audio user turn after the prompt text - #798

Merged
inureyes merged 1 commit into
mainfrom
fix/issue-797-gemma4-unified-audio-turn-order
Jul 16, 2026
Merged

inureyes merged 1 commit into
mainfrom
fix/issue-797-gemma4-unified-audio-turn-order

Conversation

@inureyes

Copy link
Copy Markdown
Member

Summary

On the Gemma 4 12B Unified audio path, the CLI rendered the <|audio|> placeholder BEFORE the prompt text, an out-of-distribution turn frame that deterministically flipped the 12B checkpoint from transcription into answering the perceived content on acoustically hard clips (issue #797, FLEURS ko_kr test[3]), and diverged from mlxcel's own server audio frame. This reorders the audio placeholder to follow the prompt text so the CLI matches both the upstream reference and the server.

Root cause

apply_vlm_chat_template (src/commands/generate.rs) built the user-turn multimodal content list as [images, videos, audios, text], so the rendered user turn was <|audio|>{prompt} (audio before text). The reference and mlxcel's server both place audio AFTER the text: upstream mlx-vlm's Gemma 4 formatter _format_list_with_image_type (selected by LIST_WITH_IMAGE_TYPE_TEXT for gemma4/gemma3n) builds [image]*n + [text] + [audio]*n, and the server audio path splices the BOA/AUDIO/EOA run right before the user turn's closing <end_of_turn> (expand_gemma4_audio_tokens_for_server, src/multimodal/vlm_runtime.rs). Feeding the 12B an audio-before-text frame is out of distribution; on unambiguous speech it still transcribes (4/5 FLEURS clips), but on the acoustically hard test[3] it flips into answering the content it misheard. Perception parity with LiteRT-LM already holds (both mishear the opening phrase near-identically), which is why this is a task-adherence/turn-framing bug rather than a DSP/injection-numerics bug; the scatter/injection math is untouched.

What changed

Notes

The thinking default is not implicated: wants_thinking_default_off() matches this template, so enable_thinking stays off and the generation prompt renders the closed thought scaffold exactly as transformers does with enable_thinking=False. The new regression test pins that. The reorder is a no-op for the other audio families (the Nemotron omni and Qwen3-Omni server expanders already place audio after the text).

Test plan

Closes #797

On the Gemma 4 12B Unified audio path the CLI built the user-turn multimodal content list as [images, videos, audios, text], placing the `<|audio|>` placeholder BEFORE the prompt text. Upstream mlx-vlm's Gemma 4 formatter (`_format_list_with_image_type`, selected by `LIST_WITH_IMAGE_TYPE_TEXT` for `gemma4`/`gemma3n`) builds `[image]*n + [text] + [audio]*n`, i.e. audio AFTER the text, and mlxcel's own server audio path already lands the block there (it splices the BOA/AUDIO/EOA run right before the user turn's closing `<end_of_turn>`, see `expand_gemma4_audio_tokens_for_server`). The CLI therefore fed the 12B checkpoint an out-of-distribution audio-before-text frame that deterministically flipped it from transcription into answering the perceived content on acoustically hard clips (issue #797, FLEURS ko_kr test[3]), and diverged from the server frame.

Reorder the content parts so the audio placeholder follows the prompt text: `[images, videos, text, audios]`. Images and video keep their pre-text placement (unchanged, no regression to #164/#436), and text-only and image/video-only prompts are byte-identical. This makes `mlxcel run --audio` and the server chat path render the identical audio user turn.

Tests: update the two existing audio-ordering regressions to expect audio-after-text, and add `gemma4_unified_audio_prompt_matches_reference_framing`, which pins the full rendered CLI prompt using the real Gemma 4 template shape (BOS, system-block guard, content-parts loop, closed thinking-channel scaffold), asserts the audio marker follows the text, and checks CLI/server frame parity.

Runtime validation of the FLEURS ko_kr test[3] clip is pending: this machine has no ffmpeg/datasets and cannot run the MLX/Metal path. The issue reporter offered to run test branches on Apple Silicon.

Refs #797
@inureyes inureyes added type:bug Bug fixes, error corrections, or issue resolutions priority:medium Medium priority status:review Under review status:done Completed and removed status:review Under review labels Jul 16, 2026
@inureyes
inureyes merged commit b20ea30 into main Jul 16, 2026
4 checks passed
@inureyes
inureyes deleted the fix/issue-797-gemma4-unified-audio-turn-order branch July 16, 2026 09:55
@inureyes inureyes self-assigned this Aug 31, 2026
be-free-snowboy pushed a commit to freestylejs/snowbrain-mlxcel that referenced this pull request Oct 27, 2026
…p#798)

## Summary

On the Gemma 4 12B Unified audio path, the CLI rendered the `<|audio|>` placeholder BEFORE the prompt text, an out-of-distribution turn frame that deterministically flipped the 12B checkpoint from transcription into answering the perceived content on acoustically hard clips (issue lablup#797, FLEURS ko_kr test[3]), and diverged from mlxcel's own server audio frame. This reorders the audio placeholder to follow the prompt text so the CLI matches both the upstream reference and the server.

## Root cause

`apply_vlm_chat_template` (src/commands/generate.rs) built the user-turn multimodal content list as `[images, videos, audios, text]`, so the rendered user turn was `<|audio|>{prompt}` (audio before text). The reference and mlxcel's server both place audio AFTER the text: upstream mlx-vlm's Gemma 4 formatter `_format_list_with_image_type` (selected by `LIST_WITH_IMAGE_TYPE_TEXT` for `gemma4`/`gemma3n`) builds `[image]*n + [text] + [audio]*n`, and the server audio path splices the BOA/AUDIO/EOA run right before the user turn's closing `<end_of_turn>` (`expand_gemma4_audio_tokens_for_server`, src/multimodal/vlm_runtime.rs). Feeding the 12B an audio-before-text frame is out of distribution; on unambiguous speech it still transcribes (4/5 FLEURS clips), but on the acoustically hard test[3] it flips into answering the content it misheard. Perception parity with LiteRT-LM already holds (both mishear the opening phrase near-identically), which is why this is a task-adherence/turn-framing bug rather than a DSP/injection-numerics bug; the scatter/injection math is untouched.

## What changed

- src/commands/generate.rs: in `apply_vlm_chat_template`, emit the text content part before the audio parts, producing `[images, videos, text, audios]`. Images and video keep their pre-text placement (no regression to lablup#164/lablup#436); text-only and image/video-only prompts are byte-identical. `mlxcel run --audio` and the server chat path now render the identical audio user turn.
- src/commands/generate_tests.rs: update the two existing audio-ordering regressions (`vlm_chat_template_renders_audio_content_part_in_user_turn`, `resolve_cli_prompt_routes_audio_through_vlm_template`) to expect audio-after-text, and add `gemma4_unified_audio_prompt_matches_reference_framing`, which pins the full rendered CLI prompt using the real Gemma 4 template shape (BOS, system-block guard, content-parts loop, and the closed `<|channel>thought\n<channel|>` scaffold that renders when thinking defaults OFF per lablup#686), asserts the `<|audio|>` marker follows the prompt text and no system turn is emitted, and checks CLI/server frame parity.

## Notes

The thinking default is not implicated: `wants_thinking_default_off()` matches this template, so `enable_thinking` stays off and the generation prompt renders the closed thought scaffold exactly as transformers does with `enable_thinking=False`. The new regression test pins that. The reorder is a no-op for the other audio families (the Nemotron omni and Qwen3-Omni server expanders already place audio after the text).

## Test plan

- [x] `cargo test --release --features cuda --bin mlxcel commands::generate::tests` (40/40 pass, including the three lablup#797 regressions and the unchanged video/image ordering tests)
- [ ] Runtime FLEURS ko_kr test[3] check on Apple Silicon is PENDING: this machine has no ffmpeg or datasets library and cannot run the MLX/Metal path. The issue reporter offered to run test branches on Apple Silicon, which is the intended runtime gate for the transcription-vs-answer behavior and CER of the other four clips.

Closes lablup#797
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

priority:medium Medium priority status:done Completed type:bug Bug fixes, error corrections, or issue resolutions

Projects

None yet

1 participant