Skip to content

fix: Gemma 4 audio understanding (CLI --audio) produces no output (missing 16kHz resample + audio token placed outside the user turn) #436

Closed

Description

@inureyes

Summary

mlxcel generate -m <gemma-4-e2b/e4b-it> --audio <file.wav> -p "..." runs the Conformer audio encoder successfully but the model generates 0 tokens (immediate EOS), so Gemma 4 audio understanding is effectively non-functional from the CLI. Root-caused to two independent pre-existing defects; fixing both makes the model produce a coherent transcription. This is unrelated to PR #434 (verified: identical behavior before and after #434).

Reproduction

./target/release/mlxcel generate -m models/gemma-4-e2b-it-4bit --audio <24kHz wav> -p "Transcribe this audio." -n 120 -t 0

Output: encoder loads and runs ("Loaded Gemma4 audio encoder (12 Conformer layers)", "expanded audio into 43 soft tokens"), then [Generated 0 tokens]. Text-only and image inputs on the same model work fine.

Root cause A: the audio placeholder is never placed in the user turn

The CLI chat-template path (src/commands/generate.rs resolve_cli_prompt / apply_vlm_chat_template) only emits image and video content parts, never audio, and for an audio-only request it does not use the VLM template path at all (it calls apply_user_chat_template, plain text). The gemma-4 checkpoint chat template DOES support audio content (chat_template.jinja: {%- elif item['type'] == 'audio' -%} {{- '<|audio|>' -}}), but mlxcel never passes a {"type":"audio"} content item, so no <|audio|> (id 258881) marker is rendered into the user turn.

Consequently expand_gemma4_audio_tokens (src/commands/generate_vlm.rs) cannot find an <|audio|> placeholder and falls back to inserting BOA + audio*N + EOA "before the last token". After the Gemma generation prompt that tail is the model turn, so the audio block lands inside the model's own turn instead of the user turn. Evidence (instrumented prompt_tokens, default prompt): BOA@index 17, while the two <start_of_turn> (105) markers are at indices [1, 11] — the audio block (17..61) is after the model-turn marker (11). The model sees audio in its own turn with nothing to answer and emits EOS immediately.

The server path has the analogous helper expand_gemma4_audio_tokens_for_server (src/multimodal/vlm_runtime.rs) with the same "insert before last token" fallback; verify whether the server's input_audio content (src/server/types/request.rs) is rendered into the user turn as <|audio|> (the OpenAI content type is input_audio, the gemma-4 template expects audio), and fix if it has the same defect.

Root cause B: the waveform is not resampled to 16 kHz before mel extraction (more impactful)

audio::load_wav_file returns samples at the file's native sample rate (the test clips are 24000 Hz). The Gemma 4 audio path in compute_gemma4_audio_embeddings (src/commands/generate_vlm.rs) passes those native-rate samples straight into AudioFeatureExtractor::extract(waveform, max_length), which does NOT resample (its second arg is max_length, not a source rate) and assumes config.sampling_rate = 16000. So mel extraction runs on 24 kHz audio with a 16 kHz hop (160 samples = 6.67 ms instead of 10 ms), producing ~1.5x too many frames: the Conformer encoder outputs 64 frames for a 1.7 s clip instead of the expected 43. The placeholder count (43 = ceil(duration_ms / 40), correct) no longer matches the encoder output length (64), so masked_scatter fills the 43 audio-token positions with the first 43 of the 64 time-distorted frames. The model receives temporally-garbled audio embeddings and emits EOS even when the audio token is correctly placed.

Evidence:

  • 24 kHz input: instrumented encoder output shape [1, 64, 1536] (sane values, no NaN) vs 43 placeholders -> 0 tokens, even with the audio token correctly in the user turn.
  • The same clip resampled to 16 kHz: encoder output [1, 43, 1536] (matches the 43 placeholders) and the model engages ("Transcription...").
  • e4b + a 3.6 s clip resampled to 16 kHz + audio token in the user turn: a full coherent transcription is produced ("...I can make out: 'Well, honestly, I don't know...'").

A resample_to_16k(samples, src_rate) helper already exists in src/audio/whisper_mel.rs (used by the Whisper STT path); the Gemma 4 audio path should call it before mel extraction. The same resample is likely needed wherever the server builds Gemma 4 audio features.

Fix scope

  1. Resample the Gemma 4 audio waveform to 16 kHz before mel feature extraction (CLI compute_gemma4_audio_embeddings and the image+audio path compute_gemma4_multimodal_embeddings, plus the equivalent server feature-building path), reusing resample_to_16k. Keep the placeholder count computed from true duration (already correct).
  2. Render the <|audio|> marker into the USER turn for CLI audio: emit a {"type":"audio"} chat-template content part and route audio-bearing requests through the VLM chat template (resolve_cli_prompt currently only takes the VLM path when num_images>0 || num_videos>0), so expand_gemma4_audio_tokens finds and expands the placeholder in-place instead of the model-turn fallback. Verify/fix the server input_audio -> user-turn <|audio|> rendering analogously.
  3. Keep the "insert before last token" fallback only as a true last resort, or make it insert into the user turn (before the final <end_of_turn> of the user turn, not before the generation prompt).

Acceptance criteria

  • mlxcel generate -m models/gemma-4-e2b-it-4bit --audio <native-rate wav> -p "Transcribe this audio." -n 120 -t 0 (and the e4b model) produces a coherent transcription, with no need for the user to manually type <|audio|> and no external resampling. Validate on a real 24 kHz clip (e.g. references/mlx-vlm/computer_use/audio/ask.wav and a longer one).
  • The encoder output frame count matches the audio-token placeholder count for a representative clip after the fix.
  • Text-only and image inputs on the same model are unchanged (no regression).
  • If the server audio path shares either defect, it is fixed too (or a follow-up is filed with the same evidence if out of scope).

Notes

Found while regression-testing PR #434 (conv FFI fallibility). #434 is confirmed NOT the cause: the 0-token behavior is byte-identical before and after #434. These two defects are pre-existing.

Activity

  1. added
    type:bug
    priority:high
    area:core
    area:cli
    status:ready
    status:in-progress
    and removed
    status:ready
    on Jun 25, 2026
  2. inureyes commented on Jun 25, 2026

    @inureyes
    MemberAuthor

    Implemented Fix A (CLI <|audio|> placement into the user turn) and Fix B (16 kHz resample before mel extraction) on the CLI Conformer audio path, and Fix B (resample) on the server Conformer path (prepare_gemma4_audio_embeddings). The server placement half (Fix A for the server) requires reworking the prefix-cache-sensitive prompt rendering, which flattens messages to text-only today, so it is tracked as a focused follow-up in #437 with the evidence from this issue. The authoritative full release build and the real-model gemma-4 audio E2E run separately.

  3. added
    status:review
    status:done
    and removed
    status:in-progress
    status:review
    on Jun 25, 2026
  4. self-assigned this
    on Jun 25, 2026
  5. added a commit that references this issue on Jul 30, 2026
    d5cff84
  6. added 4 commits that reference this issue on Oct 27, 2026
    d475e39
    de778c3
    85d0a14
    5477468
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area:cliarea:corepriority:highstatus:donetype:bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions