Repository navigation
Unsloth Studio: LFM3 spoken replies and read aloud on the transformers backend - #16972
Conversation
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.
…nce backend, and format it
…t backend fixture now needs
|
Codex review on the fork mirror (LeoBorcherding#782) found no major issues at |
…ch has no TTS mode [skip ci]
[skip ci]
|
@codex review |
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 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".
| supported_audio_types = _TRANSFORMERS_TTS_AUDIO_TYPES | ( | ||
| {"audio_vlm"} if chat_speaks else set() | ||
| ) |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
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.
…ead of dropping them [skip ci]
|
Codex Review: Didn't find any major issues. You're on a roll. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
…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.
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 withaudio_base64plusmodalities: ["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 setspeaks, 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:Respond with interleaved text and audio.);generation_configpick the audio sampling (LFM3-Audio and LFM3-Omni recommend different settings);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
generatedoes 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.
692fcd6bd3. After is this PR at6f44e370b6.Before and after
692fcd6bd36f44e370b6speaksafter load (both models)/v1/chat/completionswithmodalities: ["text","audio"]/api/inference/audio/generate) with the LFM3 model loadedWhat 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.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.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.692fcd6bd3)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).
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:
Screenshots
Chat UI on the same Colab L4, headless Chromium on the VM. The before shots are main
692fcd6bd3with LFM3-Omni-2B loaded; the after shots are6f44e370b6with LFM3-Audio-2B loaded.Before, a spoken question gets a text reply only:
After, the spoken reply plays under the text:
After, light mode:
Before, read aloud is refused with only the chat model loaded:
After, read aloud uses the LFM3 chat model:
Charts
Tests (macOS, CPU, stub models, at
6f44e370b6)tests/test_lfm3_spoken_reply.py: 14 passed (new file).test_audio_chat_load,test_audio_tts_cancellation,test_chat_input_audio_part,test_audio_runtime_stopped): 95 passed on692fcd6bd3, 96 here (one new case intest_chat_input_audio_part).speaks, TTS or chat modalities (60 files): 5521 passed, 5 failed. The same 5 fail on692fcd6bd3(diffusion clip rotation and an sd.cpp extract test).Not covered