Repository navigation
Description
Summary
Follow-up to #436 (CLI Gemma 4 audio). The server (mlxcel-server / /v1/chat/completions) Gemma 4 audio path still places the <|audio|> block outside the user turn, the same placement defect #436 fixed for the CLI. #436 fixed the server resampling (Fix B, in prepare_gemma4_audio_embeddings) but deliberately deferred the server placement fix (Fix A) because it requires reworking the prefix-cache-sensitive prompt rendering path, which is out of scope for that bug fix.
Root cause
The server renders chat messages text-only. build_chat_messages_with_thinking / build_raw_json_messages_with_thinking (src/server/chat_request.rs) flatten each message to m.content.text() and never emit {"type":"audio"} (or image/video) content parts. So for an input_audio request the chat template never renders an <|audio|> marker into the user turn.
expand_gemma4_audio_tokens_for_server (src/multimodal/vlm_runtime.rs) then finds no placeholder and uses its "insert before the last token" fallback, which for the Gemma generation prompt (<bos><start_of_turn>user\n{text}<end_of_turn>\n<start_of_turn>model\n) lands the BOA + audio*N + EOA block inside the model turn. As shown empirically in #436, audio in the model turn forces an immediate EOS, so the server emits 0 tokens for Gemma 4 audio understanding.
The OpenAI content type on the wire is input_audio (src/server/types/request.rs), while the gemma-4 template expects a content item of type audio (chat_template.jinja: {%- elif item['type'] == 'audio' -%} {{- '<|audio|>' -}}), so even a multimodal rendering path must map input_audio to an audio content item.
Evidence (from #436)
- 24 kHz input on the CLI before the fix: encoder output
[1, 64, 1536]vs 43 placeholders -> 0 tokens, even with the audio token correctly placed (that was the resample defect, now fixed on both CLI and server). - Audio placed in the model turn (no
<|audio|>in the user turn) -> immediate EOS.BOA@index 17while the two<start_of_turn>markers are at[1, 11]; the audio block (17..61) sits after the model-turn marker. - The CLI fix (fix: Gemma 4 audio understanding (CLI --audio) produces no output (missing 16kHz resample + audio token placed outside the user turn) #436) renders
{"type":"audio"}into the user turn so the template emits<|audio|>andexpand_gemma4_audio_tokensexpands it in place. The server needs the analogous rendering.
Proposed fix
Render an audio content part into the user turn for server requests that carry input_audio, so the gemma-4 template emits <|audio|> and expand_gemma4_audio_tokens_for_server expands it in place instead of using the model-turn fallback. Options:
- Extend the server multimodal rendering to emit
{"type":"audio"}(and, for symmetry, image/video) content parts into the user turn when the model's template supports them (ChatTemplateProcessor::supports_audio_content()already exists after fix: Gemma 4 audio understanding (CLI --audio) produces no output (missing 16kHz resample + audio token placed outside the user turn) #436), mirroring the CLIapply_vlm_chat_template. Must preserve prompt-cache prefix stability (see thesrc/server/chat_request.rsmodule docs) and stay byte-identical for non-audio requests. - Or, as a narrower interim fix, change
expand_gemma4_audio_tokens_for_serverto insert before the final<end_of_turn>of the last user turn rather than before the generation prompt (requires threading the end-of-turn token id through).
Acceptance criteria
- A
/v1/chat/completionsrequest with aninput_audiopart on a Gemma 4 audio model (e.g.gemma-4-e2b-it-4bit) produces a coherent transcription (no 0-token EOS). - The
<|audio|>block lands inside the user turn (encoder frame count matches placeholder count, already true after the fix: Gemma 4 audio understanding (CLI --audio) produces no output (missing 16kHz resample + audio token placed outside the user turn) #436 resample fix). - Text-only and image requests on the server are unchanged (prompt-cache prefix stability preserved).
Notes
The server resampling half of #436 (Fix B) is already merged in the #436 PR; this issue tracks only the server placement (Fix A) half.
Activity
fix(server): resolve Gemma 4 end-of-turn token so audio lands in the …
test: add regression tests for resolve_end_of_turn_token_id
fix: place Gemma 4 server <|audio|> block inside the user turn (#440)
feat: wire Nemotron-H Nano Omni input_audio into server chat audio (#443
fix: place Gemma 4 server <|audio|> block inside the user turn (lablu…
feat: wire Nemotron-H Nano Omni input_audio into server chat audio (l…