Repository navigation
fix(cli): render Gemma 4 audio user turn after the prompt text - #798
Merged
Merged
Conversation
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
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
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
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 byLIST_WITH_IMAGE_TYPE_TEXTforgemma4/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
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 feat: video input support for Gemma 4 Unified (gemma4_unified) #164/fix: Gemma 4 audio understanding (CLI --audio) produces no output (missing 16kHz resample + audio token placed outside the user turn) #436); text-only and image/video-only prompts are byte-identical.mlxcel run --audioand the server chat path now render the identical audio user turn.vlm_chat_template_renders_audio_content_part_in_user_turn,resolve_cli_prompt_routes_audio_through_vlm_template) to expect audio-after-text, and addgemma4_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 bug(gemma4): dense gemma-4 text modeling is catastrophically degraded (ppl ~12k vs gemma-3's 15 on the same corpus); likely root of the temp-0 pad-collapse #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, soenable_thinkingstays off and the generation prompt renders the closed thought scaffold exactly as transformers does withenable_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
cargo test --release --features cuda --bin mlxcel commands::generate::tests(40/40 pass, including the three Gemma4 12B Unified audio path answers the perceived content instead of transcribing (deterministic on FLEURS ko_kr test[3]; LiteRT-LM transcribes the same clip) #797 regressions and the unchanged video/image ordering tests)Closes #797