Repository navigation
fix: Gemma 4 CLI audio resample to 16kHz and place <|audio|> in user turn - #438
Conversation
…turn
Gemma 4 audio understanding from the CLI (`mlxcel generate --audio`) ran the Conformer encoder but generated 0 tokens (immediate EOS) due to two independent pre-existing defects. Fixing both makes the model produce a coherent transcription.
Fix B (resampling): `audio::load_wav_file` returns samples at the file's native rate (the reference clips are 24 kHz), but `AudioFeatureExtractor::extract` assumes a 16 kHz waveform (160-sample hop = 10 ms). Passing native-rate samples inflated the encoder frame count (64 instead of 43 for a 1.7 s clip) and desynced it from the duration-based audio-token placeholder count, garbling the embeddings. The Gemma 4 Conformer paths now resample to 16 kHz via `whisper_mel::resample_to_16k` before mel extraction: CLI `compute_gemma4_audio_embeddings` and `compute_gemma4_multimodal_embeddings`, plus the server `prepare_gemma4_audio_embeddings`. Duration (and thus the placeholder count) is rate-invariant, so the count stays correct and now matches the encoder output length.
Fix A (placement): the CLI chat-template path only emitted image/video content parts and only took the VLM template path when num_images>0 || num_videos>0, so an audio-only request got a plain text user turn with no `<|audio|>` marker. `expand_gemma4_audio_tokens` then fell back to inserting the audio block before the last token, which lands inside the model turn and forces an immediate EOS. A `num_audios` count is now threaded through `load_cli_prompt` -> `resolve_cli_prompt` -> `apply_vlm_chat_template`, which emits `{"type":"audio"}` content parts gated on a new `ChatTemplateProcessor::supports_audio_content()`. The gemma-4 template renders these as `<|audio|>` in the user turn, so the expansion finds and expands the placeholder in place. Routing only switches to the VLM template when the template actually handles audio content, so every non-audio model keeps the byte-identical text-only path.
The server resampling defect is fixed here; the server placement defect (the prompt is rendered text-only, so `input_audio` never becomes a user-turn `<|audio|>`) requires reworking the prefix-cache-sensitive rendering path and is tracked as a focused follow-up.
Tests: in-place and fallback expansion for `expand_gemma4_audio_tokens`, `apply_vlm_chat_template` / `resolve_cli_prompt` audio rendering and routing, `supports_audio_content` detection, and a 24 kHz -> 16 kHz resample length check.
Closes #436
Implementation Review SummaryIntent
Findings AddressedNone requiring a fix. The implementation is correct, complete, and integrated. Verification
Detailed checks
Remaining Items
|
Document that Gemma 4 Unified audio understanding is available from the CLI via --audio, with automatic resampling to 16 kHz, in docs/supported-models.md. Apply rustfmt brace-placement fix in generate_tests.rs (the assert! over the pipeline-stage iterator).
PR Finalization CompleteSummaryTests: 8 tests added by the developer; all pass. No additional test was added (the server-path resample and
Documentation: Updated Lint/Format: Commit: |
… path The earlier note placed the audio-understanding sentence on the encoder-free Gemma 4 Unified bullet and credited a "Conformer encoder" that path does not use, and claimed server input_audio support that is not yet wired (tracked separately). Move the note to its own Gemma 4 VL bullet: the Conformer-tower checkpoints take CLI --audio and resample to 16 kHz before the encoder, and state that the server path is not yet wired through the chat prompt.
The prior commit formatted generate_tests.rs with standalone rustfmt, which defaults to an older edition and lays out the multi-line assert! differently than `cargo fmt --all` under the project's edition 2024. CI runs `cargo fmt --all -- --check`, so reformat with cargo fmt to match.
The initial v0.3.3 cut (4d5f4fb) was never tagged, and 16 more commits landed on main afterward. Fold those fixes into the v0.3.3 CHANGELOG and debian/changelog entries and set the release date to 2026-06-25: - N-gram loop detection (#433) and Nemotron-H Nano Omni audio input (#443) - Prefill masks sized from the live window under --max-kv-size trim (#418, #420, #422, #431) - Double-transpose crash on mlx-community conv checkpoints (#429) - conv1d/conv2d and nemotron audio-encoder convs fallible at the FFI boundary (#434, #439) - Gemma 4 audio placed in the user turn (#438, #440) - mistral4 MLA backbone routing and 2D MoE token flattening (#423, #425) - Bump actions/checkout from 6 to 7 (#395)
…turn (lablup#438) * fix: Gemma 4 CLI audio resample to 16kHz and place <|audio|> in user turn Gemma 4 audio understanding from the CLI (`mlxcel generate --audio`) ran the Conformer encoder but generated 0 tokens (immediate EOS) due to two independent pre-existing defects. Fixing both makes the model produce a coherent transcription. Fix B (resampling): `audio::load_wav_file` returns samples at the file's native rate (the reference clips are 24 kHz), but `AudioFeatureExtractor::extract` assumes a 16 kHz waveform (160-sample hop = 10 ms). Passing native-rate samples inflated the encoder frame count (64 instead of 43 for a 1.7 s clip) and desynced it from the duration-based audio-token placeholder count, garbling the embeddings. The Gemma 4 Conformer paths now resample to 16 kHz via `whisper_mel::resample_to_16k` before mel extraction: CLI `compute_gemma4_audio_embeddings` and `compute_gemma4_multimodal_embeddings`, plus the server `prepare_gemma4_audio_embeddings`. Duration (and thus the placeholder count) is rate-invariant, so the count stays correct and now matches the encoder output length. Fix A (placement): the CLI chat-template path only emitted image/video content parts and only took the VLM template path when num_images>0 || num_videos>0, so an audio-only request got a plain text user turn with no `<|audio|>` marker. `expand_gemma4_audio_tokens` then fell back to inserting the audio block before the last token, which lands inside the model turn and forces an immediate EOS. A `num_audios` count is now threaded through `load_cli_prompt` -> `resolve_cli_prompt` -> `apply_vlm_chat_template`, which emits `{"type":"audio"}` content parts gated on a new `ChatTemplateProcessor::supports_audio_content()`. The gemma-4 template renders these as `<|audio|>` in the user turn, so the expansion finds and expands the placeholder in place. Routing only switches to the VLM template when the template actually handles audio content, so every non-audio model keeps the byte-identical text-only path. The server resampling defect is fixed here; the server placement defect (the prompt is rendered text-only, so `input_audio` never becomes a user-turn `<|audio|>`) requires reworking the prefix-cache-sensitive rendering path and is tracked as a focused follow-up. Tests: in-place and fallback expansion for `expand_gemma4_audio_tokens`, `apply_vlm_chat_template` / `resolve_cli_prompt` audio rendering and routing, `supports_audio_content` detection, and a 24 kHz -> 16 kHz resample length check. Closes lablup#436 * chore: note Gemma 4 audio CLI support in docs and apply rustfmt Document that Gemma 4 Unified audio understanding is available from the CLI via --audio, with automatic resampling to 16 kHz, in docs/supported-models.md. Apply rustfmt brace-placement fix in generate_tests.rs (the assert! over the pipeline-stage iterator). * docs: attribute Gemma 4 audio CLI support to the Gemma 4 VL Conformer path The earlier note placed the audio-understanding sentence on the encoder-free Gemma 4 Unified bullet and credited a "Conformer encoder" that path does not use, and claimed server input_audio support that is not yet wired (tracked separately). Move the note to its own Gemma 4 VL bullet: the Conformer-tower checkpoints take CLI --audio and resample to 16 kHz before the encoder, and state that the server path is not yet wired through the chat prompt. * style: format assert with cargo fmt (edition 2024) to satisfy CI The prior commit formatted generate_tests.rs with standalone rustfmt, which defaults to an older edition and lays out the multi-line assert! differently than `cargo fmt --all` under the project's edition 2024. CI runs `cargo fmt --all -- --check`, so reformat with cargo fmt to match.
The initial v0.3.3 cut (4d5f4fb) was never tagged, and 16 more commits landed on main afterward. Fold those fixes into the v0.3.3 CHANGELOG and debian/changelog entries and set the release date to 2026-06-25: - N-gram loop detection (lablup#433) and Nemotron-H Nano Omni audio input (lablup#443) - Prefill masks sized from the live window under --max-kv-size trim (lablup#418, lablup#420, lablup#422, lablup#431) - Double-transpose crash on mlx-community conv checkpoints (lablup#429) - conv1d/conv2d and nemotron audio-encoder convs fallible at the FFI boundary (lablup#434, lablup#439) - Gemma 4 audio placed in the user turn (lablup#438, lablup#440) - mistral4 MLA backbone routing and 2D MoE token flattening (lablup#423, lablup#425) - Bump actions/checkout from 6 to 7 (lablup#395)
Summary
Gemma 4 audio understanding from the CLI (
mlxcel generate --audio) ran the Conformer encoder but generated 0 tokens (immediate EOS) because of two independent pre-existing defects. Fixing both makes the model produce a coherent transcription. Root-caused and reproduced in #436 (this PR is unrelated to #434; the 0-token behavior is byte-identical before and after #434).What changed
whisper_mel::resample_to_16kbefore mel extraction, becauseAudioFeatureExtractor::extractassumes 16 kHz (160-sample hop = 10 ms) whileload_wav_file/load_wav_from_bytesreturn native-rate samples (the reference clips are 24 kHz). Without it the encoder emitted ~1.5x too many frames (64 vs 43 for a 1.7 s clip) and desynced from the duration-based placeholder count, garbling the embeddings. Fixed in CLIcompute_gemma4_audio_embeddingsandcompute_gemma4_multimodal_embeddings(src/commands/generate_vlm.rs) and serverprepare_gemma4_audio_embeddings(src/server/model_worker.rs). Duration (and thusnum_audio_tokens) is rate-invariant, so the placeholder count stays correct and now matches the encoder output length.num_audioscount is threaded throughload_cli_prompt->resolve_cli_prompt->apply_vlm_chat_template(src/commands/generate.rs), which now emits{"type":"audio"}content parts into the user turn, gated on a newChatTemplateProcessor::supports_audio_content()(src/server/chat_template.rs). The gemma-4 template renders these as<|audio|>in the user turn soexpand_gemma4_audio_tokensfinds and expands the placeholder in place, instead of the old "insert before the last token" fallback that landed the audio block inside the model turn and forced an immediate EOS. Routing only switches to the VLM template when the template actually handles audio content, so non-audio models keep the byte-identical text-only path (no regression).build_chat_messages_with_thinkingflattens tocontent.text()), soinput_audionever becomes a user-turn<|audio|>. Fixing that requires reworking the prefix-cache-sensitive rendering path, so it is tracked as a focused follow-up in fix: Gemma 4 server audio (input_audio) places <|audio|> outside the user turn (0-token output) #437 with the evidence from fix: Gemma 4 audio understanding (CLI --audio) produces no output (missing 16kHz resample + audio token placed outside the user turn) #436. The server resampling half is fixed in this PR.expand_gemma4_audio_tokensin-place and fallback expansion;apply_vlm_chat_template/resolve_cli_promptaudio rendering and routing (and the no-regression text path for non-audio templates);supports_audio_contentdetection; a 24 kHz -> 16 kHz resample length check.Test plan
cargo check --bin mlxcel --features metal,accelerate(compiles)cargo fmt --checkandcargo clippy --bin mlxcel --features metal,accelerate(clean)cargo test --bin mlxcel -- gemma4_audio vlm_chat_template resolve_cli_prompt(10 passed)cargo test --lib -- resample_scales_length_from_24k supports_audio_content(passed)cargo build --release --features metal,accelerate(run by the orchestrator)mlxcel generate -m models/gemma-4-e2b-it-4bit --audio references/mlx-vlm/computer_use/audio/ask.wav -p "Transcribe this audio." -n 120 -t 0produces a coherent transcription with no manual<|audio|>and no external resampling (the 24 kHz clip is resampled internally), and the encoder frame count equals the placeholder count; gemma-4-e2b text and image inputs remain coherent (no regression).Closes #436