Skip to content

Unsloth Studio: LFM3 spoken replies and read aloud on the transformers backend - #16972

Merged
danielhanchen merged 10 commits into
unslothai:mainfrom
LeoBorcherding:up/lfm3-spoken-replies
Nov 7, 2026
Merged

danielhanchen merged 10 commits into
unslothai:mainfrom
LeoBorcherding:up/lfm3-spoken-replies

Conversation

@LeoBorcherding

@LeoBorcherding LeoBorcherding commented Nov 6, 2026 •

Copy link
Copy Markdown
Collaborator

LFM3-Audio-2B and LFM3-Omni-2B can answer in speech. On MLX, Unsloth Studio already supports that: the status reports speaks, and a chat completion with audio_base64 plus modalities: ["text", "audio"] comes back as the reply text plus a WAV that the chat UI plays under the reply. On the transformers backend (CUDA) nothing set speaks, so the same request was refused and the chat UI got a text-only answer. This adds the same behaviour there, and lets read aloud use the LFM3 chat model's own voice instead of needing a separate TTS model.

What changed

  • Spoken replies. After load, an LFM3 model whose processor carries the speech codec reports speaks. A spoken request:

    • uses the model card's prompt (Respond with interleaved text and audio.);
    • lets each checkpoint's own generation_config pick the audio sampling (LFM3-Audio and LFM3-Omni recommend different settings);
    • decodes the speech through the codec, and returns the text and then one audio item.

    That matches what the MLX backend returns, so the worker, route and chat UI need no changes.

  • One clip. A spoken reply answers one audio clip. A request with extra clips gets a clear error instead of having them silently dropped.

  • Stop. LFM3's generate does not accept stopping criteria, so a small streamer ends generation once the request is cancelled. A stopped reply returns nothing.

  • Read aloud with the chat model. The read-aloud button used to need a separate TTS model. It now also works with an LFM3 chat model that speaks, using the model's TTS mode (Perform TTS in <language> using a <male/female> voice.). The language comes from the request and the voice from the voice option.

Results

All on a Colab L4 (NVIDIA L4, 22.5 GB, Linux), through Unsloth Studio's API on the VM.

  • Before is main at 692fcd6bd3. After is this PR at 6f44e370b6.
  • Both are the same VM and the same install, with only the backend checkout switched.
  • Both LFM3 models load through Unsloth Studio's transformers 5.22.2 sidecar, the same on main and here.
  • Every WAV was transcribed with faster-whisper small.en.

Before and after

main 692fcd6bd3 this PR 6f44e370b6
speaks after load (both models) false true
spoken question to /v1/chat/completions with modalities: ["text","audio"] 400, "The loaded model cannot answer in speech." 200, reply text plus a WAV
read aloud (/api/inference/audio/generate) with the LFM3 model loaded 400, "Active model is not an audio model." 200, a WAV in the model's own voice
chat UI, spoken question text reply, no audio player text reply with an audio player under it
chat UI, read aloud button "No Unsloth speech model is loaded" reads the reply with the LFM3 model

What the audio says

The spoken question is a 1.92 s clip of "What is the capital of France?" made with LFM3-Audio-2B's own read aloud. Each model ran 2 non-streamed and 2 streamed replies, plus 2 read-aloud calls. Colab L4, 6f44e370b6.

model read aloud (Whisper) spoken reply text spoken reply audio (Whisper)
LFM3-Audio-2B 1.92 s, "What is the capital of France?" (both runs) "The capital of France is Paris." (all 4) 2.48 to 3.44 s, "The capital of France is Paris." (all 4)
LFM3-Omni-2B 2.08 s and 2.24 s, "What is the capital of France?" "The capital of France is Paris." (all 4) 2.32 to 3.04 s, "The capital of France is Paris." (all 4)

Speed

Wall-clock time of each request, measured on the VM. Colab L4, 6f44e370b6. Two runs back to back; the second is the warm one.

model what run 1 run 2 audio (run 2) realtime factor (run 2)
LFM3-Audio-2B read aloud 6.61 s 1.80 s 1.92 s 1.07x
LFM3-Audio-2B spoken reply, not streamed 5.21 s 2.64 s 2.48 s 0.94x
LFM3-Audio-2B spoken reply, streamed: first audio 3.01 s 2.64 s 2.48 s 0.94x
LFM3-Omni-2B read aloud 6.96 s 2.07 s 2.24 s 1.08x
LFM3-Omni-2B spoken reply, not streamed 3.79 s 2.51 s 2.32 s 0.92x
LFM3-Omni-2B spoken reply, streamed: first audio 2.69 s 3.10 s 3.04 s 0.98x

Run 1 is the first call after load, so read aloud's run 1 includes warm-up. The reply is generated whole, then sent, so in a streamed reply the text and the audio arrive together (2 ms apart): time to first audio is the time to the whole clip.

VRAM

nvidia-smi memory.used, sampled every 100 ms. Colab L4, 6f44e370b6.

LFM3-Audio-2B LFM3-Omni-2B
loaded, idle (same on main 692fcd6bd3) 4,032 MiB 4,406 MiB
peak during a spoken reply 4,268 MiB 4,624 MiB
peak during read aloud 4,260 MiB 4,614 MiB

The alternative to the chat model's own voice is a separate TTS model loaded next to it. With LFM3-Audio-2B loaded, Kokoro-82M-GGUF was loaded beside it (multi-model set to "When they fit"; with the default "Off", loading Kokoro unloads the chat model).

LFM3-Audio-2B read aloud VRAM time realtime factor
chat model speaks (this PR) 4,260 MiB 1.80 s 1.07x
chat model plus Kokoro-82M-GGUF 5,110 MiB 0.52 s 4.20x

Kokoro is faster, but costs a second model and 850 MiB. With this PR, read aloud works with only the chat model loaded.

The math

  • Realtime factor = audio seconds ÷ wall-clock seconds of the request. Example, LFM3-Audio-2B spoken reply run 2: 2.48 ÷ 2.64 = 0.94. Above 1 means the speech is produced faster than it plays.

  • VRAM saved = 5,110 − 4,260 = 850 MiB. nvidia-smi listed the audio.cpp server running Kokoro at 844 MiB, so the difference is that process.

  • Speech tokens per second. The codec (from audio_tokenizer/config.json) runs at 24,000 Hz with 1,920 samples per frame: 24,000 ÷ 1,920 = 12.5 frames per second of speech, with 12 codebooks per frame (150 codes per second). Each generation step emits either one text token or one 12-code audio frame. So one second of speech is 12.5 steps.

  • Check against the four non-streamed replies. The reported completion tokens minus 12.5 × audio seconds leaves the same 9 text tokens every time:

    model, run completion tokens 12.5 × audio seconds text tokens left
    LFM3-Audio-2B run 1 52 12.5 × 3.44 = 43 9
    LFM3-Audio-2B run 2 40 12.5 × 2.48 = 31 9
    LFM3-Omni-2B run 1 43 12.5 × 2.72 = 34 9
    LFM3-Omni-2B run 2 38 12.5 × 2.32 = 29 9

Screenshots

Chat UI on the same Colab L4, headless Chromium on the VM. The before shots are main 692fcd6bd3 with LFM3-Omni-2B loaded; the after shots are 6f44e370b6 with LFM3-Audio-2B loaded.

Before, a spoken question gets a text reply only:

before, spoken question

After, the spoken reply plays under the text:

after, spoken reply

After, light mode:

after, spoken reply, light mode

Before, read aloud is refused with only the chat model loaded:

before, read aloud

After, read aloud uses the LFM3 chat model:

after, read aloud

Charts

peak VRAM

time to first audio

realtime factor

Tests (macOS, CPU, stub models, at 6f44e370b6)

  • tests/test_lfm3_spoken_reply.py: 14 passed (new file).
  • Adjacent audio suites (test_audio_chat_load, test_audio_tts_cancellation, test_chat_input_audio_part, test_audio_runtime_stopped): 95 passed on 692fcd6bd3, 96 here (one new case in test_chat_input_audio_part).
  • Every backend test file that touches speaks, TTS or chat modalities (60 files): 5521 passed, 5 failed. The same 5 fail on 692fcd6bd3 (diffusion clip rotation and an sd.cpp extract test).

Not covered

  • Stop in the middle of a spoken reply on a GPU. It's covered by the stub test only.
  • GPUs other than the Colab L4, and AMD or Windows.
  • Reading aloud a reply that already has an audio player under it. The chat UI sends the player's markup (with the WAV inside) as the text to read, so the request is refused as too long. That text comes from the frontend's speech-text conversion, which this PR doesn't touch, and the same reply format comes from the MLX backend. Read aloud on a normal text reply works, as shown above.

LFM3-Audio and LFM3-Omni speak on MLX (PR 15889) but not on transformers, where Studio loads them on CUDA. The transformers backend now follows the same contract: a loaded LFM3 model reports speaks, and a chat completion with audio_base64 and modalities [text, audio] answers through the model's interleaved mode, as the reply text then one {speech, sample_rate} item that the route already turns into message.audio / delta.audio. Stop ends the generation through a streamer, since LFM3's generate refuses stopping_criteria.

The same model also reads text aloud in its TTS mode, so chat's read aloud and /audio/generate work with only the LFM3 chat model loaded: the TTS route accepts an audio_vlm model that speaks, and the backend prompts 'Perform TTS in <locale> using a <gender> voice.' from the request's language and audio_options.voice.
…poken reply

LFM3-Omni recommends different audio sampling than LFM3-Audio, so the shared settings are dropped and each checkpoint's generation_config applies. A spoken reply with extra_audio_base64 was refused only on MLX; the route now returns 400 for every backend instead of a worker error.
@LeoBorcherding

Copy link
Copy Markdown
Collaborator Author

Codex review on the fork mirror (LeoBorcherding#782) found no major issues at 6f44e370b6, this PR's head: result.

@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 7, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-07T12:59:58.954914Z 7084239 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 756f80f5ff

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +22921 to +22923
supported_audio_types = _TRANSFORMERS_TTS_AUDIO_TYPES | (
{"audio_vlm"} if chat_speaks else set()
)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Recognize speaking audio-VLMs during TTS switch preflight

With auto-switch enabled, /v1/audio/speech still rejects a downloaded, code-approved LFM3 model unless it is already loaded. _generate_tts_wav calls _maybe_auto_switch_model(require_speech=True) before reaching this new allowance, and its _target_speech_audio_type probe accepts only the global _TRANSFORMERS_TTS_AUDIO_TYPES, which excludes audio_vlm. Consequently, the request returns “The requested model is not a text-to-speech model” instead of loading the newly supported model. Extend the switch preflight to recognize speech-capable LFM3 checkpoints as well as allowing them after load.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not changing this here: a loaded LFM3 returns early via _already_serving before the probe, and switching to an unloaded one keeps the clear 400 it had before this PR. Auto-switch also never grants trust_remote_code, which LFM3 needs, so loading it for speech is a separate feature.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. You're on a roll.

Reviewed commit: 70842395c9

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 7, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 7, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 7, 2026
@danielhanchen
danielhanchen merged commit 605a6f1 into unslothai:main Nov 7, 2026
LeoBorcherding added a commit to LeoBorcherding/unsloth that referenced this pull request Nov 8, 2026
…o voice mode any-audio

Conflicts were the LFM3 backend and tests, where this branch held older fork copies
of what main now has (unslothai#16242, unslothai#16243, unslothai#16972): C's versions are kept, and this
branch's spoken-reply streaming is re-applied on top, with main's unset speech budget
moved into the shared _speech_inputs. chat-page keeps C's releaseBeforeBorrow, which
already releases an owned voice when the chat model's voice is picked, so the
duplicate voiceReplacedByServing is dropped.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants