A TTS that fits in your CPU (and pocket)
AGENTS.md
This file provides guidance to AI agents when working with code in this repository.
Project Overview
pocket-tts is a CPU-based text-to-speech (TTS) model. The project uses a flow-based language model architecture with a neural audio codec (Mimi) for efficient speech synthesis.
Key Architecture Components:
- FlowLMModel: Transformer-based flow language model that generates latent representations from text using Lagrangian Self Distillation (LSD)
- MimiModel: Neural audio codec (from the
moshipackage) that compresses/decompresses audio to/from latent representations - Conditioners: Text processing via SentencePiece tokenizer and lookup table embeddings
- Streaming Architecture: The entire pipeline supports streaming generation via stateful modules
- Web API: FastAPI-based server for HTTP API access with web interface
Common Commands
Setup and Development
# Install pre-commit hooks
uvx pre-commit install
# Run tests (3 parallel workers)
uv run pytest -n 3 -v
# Run a single test
uv run pytest tests/test_python_api.py -v
# Run CLI locally (editable install)
uv run pocket-tts generate
uv run pocket-tts serve
Linting and Formatting
Pre-commit handles this automatically, but you can run manually:
# Ruff will run automatically on commit via pre-commit
# Includes: ruff-check, ruff-format (with --fix), and import sorting
Building (No Build Step)
This is a pure Python package with Rust extensions in training/rust_exts/audio_ds/ for training-time audio processing. The main package does not require building.
Development Workflow
Key Patterns
Streaming Generation: The model generates audio frame-by-frame (12.5 Hz frame rate, 80ms per frame). All modules inherit from
StatefulModuleto maintain internal state.Voice Cloning: Audio prompts are encoded via Mimi encoder to create "voice state" (latent representations + speaker embeddings). This state is cached via
lru_cacheon_cached_get_state_for_audio_prompt().Flow Matching: Uses Lagrangian Self Distillation (LSD) with configurable decode steps. Fewer steps = faster but lower quality.
EOS Detection: Model predicts end-of-speech via an EOS head. Generation continues for
frames_after_eosframes after EOS is detected.Config-Driven: Model architecture is defined in YAML configs. Weights are loaded from HuggingFace Hub via
safetensors.
Important Implementation Details
- Thread Safety: The code is NOT thread-safe. Server mode does not support concurrent requests.
- Batching: Batch size is always 1. No batching support currently.
- Device: Defaults to CPU. Whether GPU helps is hardware-dependent: on hardware with strong
single-thread CPU performance (e.g. Apple Silicon) we did not observe a speedup, but on a cloud
x86 VM with a Tesla T4,
.to("cuda")measured ~2.6x speedup over CPU (RTF ~2.3-2.5x CPU vs. ~6.28x GPU). There is nodeviceargument onTTSModel.load_model()(thegenerateCLI has a documented--device, though its own doc page's "no speedup" note predates this finding;serveand Docker have none and are CPU-only).generate_audio()returns a tensor on the model's device, so code moved to GPU must call.detach().cpu().numpy()instead of.numpy().quantize=Trueis CPU-only and raisesNotImplementedErroron CUDA; the optionaltorchaobackend declarestorch>=2.11, which a fresh install satisfies, but installing it on top of an older pinnedtorch(e.g. for GPU driver compatibility) can breakquantize=Trueeven on CPU. See README's "Running on GPU" section for details. - Torch Threads:
torch.set_num_threads(1)intts_model.pyfor optimal CPU performance - dtype: Models use float32 by default (configurable in YAML)
- Type checking:
tychecks the whole repo statically (uv run ty check, also run in CI). There is no runtime type checking.
Adding Features
When adding features, be aware of:
- The streaming architecture: any changes to model forward passes need to maintain state correctly
- The config system: new model parameters must be added to config classes in
utils/config.py - The public API: only
TTSModelis exported; keep implementation details internal - Ruff formatting: line length 100, LF line endings, skip magic trailing comma
Model Weights
Weights are downloaded from HuggingFace Hub on first use:
- Model weights:
hf://kyutai/pocket-tts/tts_b6369a24.safetensors - Tokenizer:
hf://kyutai/pocket-tts/tokenizer.model - Voice prompts:
hf://kyutai/tts-voices/<speaker>/<style>.wav
The download_if_necessary() utility handles hf:// URLs and caches locally.
Common Gotchas
- PyTorch Version: Requires PyTorch 2.5+. Version 2.4.0 produces incorrect audio.
- Python Version: Supports Python 3.10 through 3.14 (>= 3.10,<3.15).
- uv Python Preference: Set to "only-managed" in pyproject.toml because system Python may lack headers.
- CPU-Only PyTorch: Uses PyTorch CPU index from
download.pytorch.org/whl/cpuin uv config. - Web Dependencies: FastAPI and Uvicorn are included for server functionality.