Skip to content

feat: add optional MLX Audio backend with model adapters - #58

Merged
StuBehan merged 4 commits into
mainfrom
feat/mlx-backend
Oct 22, 2026
Merged

StuBehan merged 4 commits into
mainfrom
feat/mlx-backend

Conversation

@StuBehan

@StuBehan StuBehan commented Sep 24, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Adds an optional mlx backend. On Apple Silicon it runs any text-to-speech model that MLX Audio can load from a local folder, including models that support voice cloning and style instructions. Kokoro (ONNX) stays the default, and nothing changes unless you pass --backend mlx.

stackvox carries no model-specific code. Models that need special handling at load time (renamed weights, codec paths, tokenizer quirks) are handled by a model adapter: a small script the user writes and keeps locally. That keeps the core model-agnostic and keeps model licences out of stackvox. We never download, bundle or recommend a model.

Changes

  • Backend: stackvox/mlx_backend.py loads a local model through MLX Audio and returns the same (samples, sample_rate) result as Kokoro.
  • CLI:
    • speak and serve take --backend {kokoro,mlx}, --model-dir and --model-adapter.
    • speak, say and serve take --reference-audio, --reference-text, --instruct, --temperature, --top-p and --top-k.
    • Bash completion is updated.
  • Generation options are only sent when set. Each model's own defaults apply otherwise; we don't impose values.
  • Model adapters:
    • --model-adapter PATH (model_adapter= in Python) points at a script defining load(model_dir, load_model). It receives MLX Audio's loader and can patch, wrap or replace it.
    • swapped() is public, so adapters can patch a class attribute and have it restored afterwards, even on error.
    • Adapters only run when passed explicitly; nothing is picked up from a model folder.
  • Daemon: serve --backend mlx keeps the model loaded between requests. Generation options given to serve become the defaults, and each say request can override them.
  • Streaming: MLX output plays in chunks of about two sentences (up to ~170 characters), so first audio arrives after the first chunk rather than the whole text. Kokoro still streams one sentence at a time. Longer chunks made models skip or invent speech: at 400 characters, 25–35% of an 8-sentence passage went missing; at 170, all of it came through.
  • Threading: MLX binds GPU streams to threads, so MlxBackend does all MLX work (load, reference decoding, generation) on one dedicated thread. Without this, live speak and daemon playback failed with There is no Stream(gpu, 0) in current thread.
  • Errors:
    • Generation options without --backend mlx are rejected: speak and serve give a usage error, and a Kokoro daemon replies err: to say. Previously they would have been silently dropped.
    • Load failures raise ModelLoadError with a one-line summary. When no adapter was used, the message suggests --model-adapter.
  • Packaging: a new stackvox[mlx] extra, mlx-audio>=0.4.5,<0.5, Apple Silicon only. The cap is there because MLX Audio is pre-1.0; the floor is the version verified end to end.
  • Docs:
    • The README has a new section on local MLX models and adapters, including an adapter example.
    • README and NOTICE attribute mlx-audio, mlx, mlx-lm and transformers, and say that each model's own licence applies to its use.
    • *.m4a is added to .gitignore.

Testing

$ stackvox speak --backend mlx --model-dir <MLX Kokoro, Apache 2.0> --out a.wav "..."
Wrote a.wav                              # 5.7s of audio, 24 kHz, no adapter

$ stackvox speak --backend mlx --model-dir <model needing custom loading> --out b.wav "..."
[stackvox] MLX Audio could not load the model in … (ValueError: Missing 358 parameters:).
If this model needs custom loading, pass a model adapter with --model-adapter …

$ stackvox speak --backend mlx --model-dir <same> --model-adapter ~/…/adapter.py --out b.wav "..."
Wrote b.wav                              # 3.8s of audio, 44.1 kHz

$ stackvox speak --backend nope ...      # CLI validation
stackvox: error: argument --backend: invalid choice …
$ stackvox serve --instruct "Warmly."
stackvox: error: --model-adapter and generation options (…) require --backend mlx

331 tests pass, and ruff, ruff format and mypy are clean. MLX Audio is mocked where it's imported, so CI doesn't need MLX installed. The new tests cover:

  • choosing the backend and passing options through the CLI, daemon client and socket protocol
  • request overrides of the daemon's defaults
  • Kokoro rejecting generation options
  • chunked streaming
  • the adapter contract and its error cases, and swapped() restoring attributes
  • shortening of load-error messages
  • all MLX work staying on the backend's thread when called from several threads

Also run locally against a real model through an adapter:

  • live speak playback
  • a serve daemon handling plain and voice-cloned say requests, plus cancel
  • long-text completeness over repeated runs

Notes

  • Some models need their own extra packages; MLX Kokoro needs misaki. Some fetch assets by Hugging Face repo id even when loaded locally. The README mentions both, and these quirks are what adapters are for.
  • --voice and --lang still apply to Kokoro only. Mapping them onto each MLX model's own voice and language settings could be a follow-up.

Comment thread stackvox/engine.py Resolved
StuBehan and others added 4 commits October 22, 2026 00:25
MLX binds GPU streams to threads, so a model loaded on one thread failed
with "There is no Stream(gpu, 0) in current thread" when streaming
playback or the daemon worker generated on another. Live `speak` and all
daemon playback were broken; only `--out` worked. MlxBackend now owns a
single daemon thread for load, reference decoding and generation.

Also lower CHUNK_CHARS from 400 to 170: long prompts made autoregressive
models skip or invent speech, losing a quarter to a third of the text in
testing, while ~2-sentence chunks kept all of it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@StuBehan
StuBehan merged commit 80ec22a into main Oct 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant