From f3e3e5d84d004d05f519dbb6a1f3bf36ea00c8b5 Mon Sep 17 00:00:00 2001 From: Hans Date: Sat, 14 Nov 2026 19:08:21 +0800 Subject: [PATCH 1/3] fix(tts-runner): micro-batch 512 unless the model needs more codec_audio_lm_backbone_req::n_ubatch == 0 made the runner use n_ctx as the micro-batch, so the context reserved a compute buffer for the whole context at once: 2.5 GB for Qwen3-TTS 0.6B (n_ctx 4096) on every load, 172 MB for Soprano. A model whose prompt needs one physical batch still says so (OuteTTS: 4096); otherwise the runner now uses llama.cpp's default of 512 and llama.cpp splits longer prompts. tts-cli / tts-server take --ubatch N (tts_runner_params::n_ubatch). Verified on CPU (8 threads): - Qwen3-TTS 0.6B Base, ICL clone: compute buffer 2502 -> 313 MiB, output bit-identical to --ubatch 4096, same timing. - Soprano, prompt shorter than 512 tokens: bit-identical; a ~500-token prompt (split in two): first 6 s at 35-46 dB SNR (summation order), then a greedy step flips and the generations diverge, as any float change does. Peak RSS 932 -> 872 MB, compute buffer 172 -> 22 MiB. --- README.md | 1 + common/tts_runner.cpp | 6 +++++- common/tts_runner.h | 5 +++++ docs/codec_common_api.md | 2 +- include/codec_audio_lm.h | 3 ++- tools/cli/main.cpp | 5 ++++- tools/server/main.cpp | 5 ++++- 7 files changed, 22 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 7e55f2a..eb4f6a6 100644 --- a/README.md +++ b/README.md @@ -88,6 +88,7 @@ tts-cli synthesize --backbone MODEL.gguf --model CODEC.gguf \ | `--seed N` | repeatable output | | `--temp F`, `--top-k N`, `--top-p F`, `--min-p F`, `--repeat-penalty F`, ... | llama.cpp's sampling flags; unset ones come from the backbone file (`general.sampling.*`, the model card's), then llama.cpp's defaults | | `--max-frames N` | cap the output length | +| `--ubatch N` | backbone micro-batch (default 512, as llama.cpp; a model that needs more sets it itself). Larger only reserves more memory | | `--gpu` | run on the GPU backend the build has (CUDA / Vulkan / Metal) | | `--n-threads N` | CPU threads | | `--override-kv KEY=TYPE:VALUE` | change a setting stored in the codec file (keys are listed in [docs/models](docs/models)) | diff --git a/common/tts_runner.cpp b/common/tts_runner.cpp index ad79f4f..b1b5258 100644 --- a/common/tts_runner.cpp +++ b/common/tts_runner.cpp @@ -566,7 +566,11 @@ tts_runner_session * tts_runner_load(const tts_runner_params & a, std::string * llama_context_params cp = llama_context_default_params(); cp.n_ctx = (uint32_t) req.n_ctx; cp.n_batch = cp.n_ctx; - cp.n_ubatch = req.n_ubatch > 0 ? (uint32_t) req.n_ubatch : cp.n_ctx; + // A model that needs its prompt in one physical batch says so (req.n_ubatch); otherwise llama.cpp splits the + // batch, and a micro-batch of the whole context would only reserve a compute buffer that size (2.5 GB for + // Qwen3-TTS 0.6B at n_ctx 4096, 0.3 GB at 512). + const uint32_t ub = a.n_ubatch > 0 ? (uint32_t) a.n_ubatch : TTS_RUNNER_DEFAULT_UBATCH; + cp.n_ubatch = req.n_ubatch > 0 ? (uint32_t) req.n_ubatch : std::min(cp.n_ctx, ub); cp.n_seq_max = (uint32_t) std::max(1, req.n_seq_max); cp.embeddings = req.embeddings; cp.pooling_type = LLAMA_POOLING_TYPE_NONE; diff --git a/common/tts_runner.h b/common/tts_runner.h index 4c8d0b8..51084d8 100644 --- a/common/tts_runner.h +++ b/common/tts_runner.h @@ -25,6 +25,8 @@ namespace codec_common { +constexpr int32_t TTS_RUNNER_DEFAULT_UBATCH = 512; + // ───────────────────────────────────────────────────────────────────── // Params — everything the reference loop needs. Sampling is llama.cpp's // (its defaults; the model card recommends values); decoder graph settings @@ -95,6 +97,9 @@ struct tts_runner_params { bool use_gpu = false; int32_t n_parallel = 1; // tts_runner_load: requests a session runs at once int32_t max_frames = 0; // 0 → per-model default + // Backbone micro-batch when the model sets no requirement (codec_audio_lm_backbone_req::n_ubatch == 0): + // 0 → TTS_RUNNER_DEFAULT_UBATCH (llama.cpp's default); a model's own requirement always wins. + int32_t n_ubatch = 0; int32_t min_frames = -1; // -1 → the model's own minimum // llama.cpp sampling (common_params_sampling defaults). Segment i of a diff --git a/docs/codec_common_api.md b/docs/codec_common_api.md index a70a897..4d6c35d 100644 --- a/docs/codec_common_api.md +++ b/docs/codec_common_api.md @@ -29,7 +29,7 @@ codec_audio_lm * alm = codec_audio_lm_init(¶ms, err, sizeof err); // Backbone: load the model, then size its context as the model asks. codec_audio_lm_backbone_req req; codec_audio_lm_backbone_requirements(alm, &vocab, max_frames, &req); -// llama_context_params: n_ctx = req.n_ctx, n_ubatch = req.n_ubatch ?: n_ctx, +// llama_context_params: n_ctx = req.n_ctx, n_ubatch = req.n_ubatch ?: min(n_ctx, 512), // n_seq_max = req.n_seq_max, embeddings = req.embeddings (pooling none), // flash_attn_type = req.flash_attn ? auto : disabled diff --git a/include/codec_audio_lm.h b/include/codec_audio_lm.h index 5fbde3b..9f13917 100644 --- a/include/codec_audio_lm.h +++ b/include/codec_audio_lm.h @@ -165,7 +165,8 @@ struct codec_audio_lm_vocab { // embeddings (pooling none) / flash_attn_type. struct codec_audio_lm_backbone_req { int32_t n_ctx; // minimum context length - int32_t n_ubatch; // physical batch (0 = n_ctx) + int32_t n_ubatch; // physical batch the prompt needs in one piece (0 = no requirement: the host + // picks; codec_common::tts_runner uses 512 as llama.cpp does) int32_t n_seq_max; // sequences decoded side by side (2: guidance pair) bool embeddings; // the context returns hidden states (no pooling) bool flash_attn; // false: the model needs flash attention disabled diff --git a/tools/cli/main.cpp b/tools/cli/main.cpp index 3ddc2f3..1dea10f 100644 --- a/tools/cli/main.cpp +++ b/tools/cli/main.cpp @@ -135,7 +135,7 @@ void print_usage(const char * prog) { " synthesize --model CODEC.gguf [--backbone LLAMA.gguf] --text STRING\n" " --output PATH.wav [--ref-audio PATH.wav] [--ref-text STR]\n" " [--voice NAME] [--speaker-file PATH.gguf] [--save-speaker PATH.gguf]\n" - " [--instruct STR] [--max-frames N] [--min-len N] [--segment-bytes N]\n" + " [--instruct STR] [--max-frames N] [--ubatch N] [--min-len N] [--segment-bytes N]\n" " [--seed N] [--temp F] [--top-k N] [--top-p F] [--min-p F]\n" " [--repeat-last-n N] [--repeat-penalty F] [--presence-penalty F]\n" " [--frequency-penalty F] [--dry-multiplier F] [--dry-base F]\n" @@ -230,6 +230,7 @@ struct args { int32_t n_threads = 0; bool use_gpu = false; int32_t max_frames = 0; + int32_t n_ubatch = 0; int32_t segment_bytes = 0; int32_t min_len = -1; @@ -275,6 +276,7 @@ bool parse_args(int argc, char ** argv, args * out) { else if (a == "--n-threads") { const char * v = need(); if (!v) return false; if (!parse_i32(v, &out->n_threads)) return false; i++; } else if (a == "--gpu") { out->use_gpu = true; } else if (a == "--max-frames") { const char * v = need(); if (!v) return false; if (!parse_i32(v, &out->max_frames)) return false; i++; } + else if (a == "--ubatch") { const char * v = need(); if (!v) return false; if (!parse_i32(v, &out->n_ubatch)) return false; i++; } else if (a == "--segment-bytes") { const char * v = need(); if (!v) return false; if (!parse_i32(v, &out->segment_bytes)) return false; i++; } else if (a == "--seed") { const char * v = need(); if (!v) return false; int32_t s; if (!parse_i32(v, &s)) return false; out->sampling.seed = (uint32_t) s; i++; } else if (a == "--min-len") { const char * v = need(); if (!v) return false; if (!parse_i32(v, &out->min_len)) return false; i++; } @@ -393,6 +395,7 @@ codec_common::tts_runner_params make_runner_params(const args & a) { rp.n_threads = a.n_threads; rp.use_gpu = a.use_gpu; rp.max_frames = a.max_frames; + rp.n_ubatch = a.n_ubatch; rp.min_frames = a.min_len; rp.segment_bytes = a.segment_bytes; rp.sampling = a.sampling; diff --git a/tools/server/main.cpp b/tools/server/main.cpp index 528bad1..dea651a 100644 --- a/tools/server/main.cpp +++ b/tools/server/main.cpp @@ -77,6 +77,7 @@ struct args { int32_t n_parallel = 1; bool use_gpu = false; int32_t max_frames = 0; + int32_t n_ubatch = 0; int32_t segment_bytes = 160; // ~10 s of speech per segment int32_t stream_chunk_ms = 1000; std::vector kv_overrides; @@ -91,7 +92,7 @@ void print_usage(const char * argv0) { "usage: %s --model CODEC.gguf [--backbone LLAMA.gguf] [--speaker-file SPK.gguf]\n" " [--voice-dir DIR [--allow-voice-create]]\n" " [--host 127.0.0.1] [--port 8080] [--api-key KEY] [--model-id NAME]\n" - " [--gpu] [--n-threads N] [--parallel N (1)] [--max-frames N] [--override-kv KEY=TYPE:VALUE]\n" + " [--gpu] [--n-threads N] [--parallel N (1)] [--max-frames N] [--ubatch N] [--override-kv KEY=TYPE:VALUE]\n" " [--segment-bytes N (160; 0 = whole input)] [--stream-chunk-ms N (1000; 0 = per segment)]\n" " [llama.cpp sampling: --seed N --temp F --top-k N --top-p F --min-p F --repeat-last-n N\n" " --repeat-penalty F --presence-penalty F --frequency-penalty F --dry-multiplier F\n" @@ -126,6 +127,7 @@ bool parse_args(int argc, char ** argv, args * out) { else if (a == "--n-threads") { if (!(v = need())) return false; out->n_threads = std::atoi(v); } else if (a == "--parallel" || a == "-np") { if (!(v = need())) return false; out->n_parallel = std::atoi(v); } else if (a == "--max-frames") { if (!(v = need())) return false; out->max_frames = std::atoi(v); } + else if (a == "--ubatch") { if (!(v = need())) return false; out->n_ubatch = std::atoi(v); } else if (a == "--segment-bytes") { if (!(v = need())) return false; out->segment_bytes = std::atoi(v); } else if (a == "--stream-chunk-ms") { if (!(v = need())) return false; out->stream_chunk_ms = std::atoi(v); } else if (a == "--gpu") { out->use_gpu = true; } @@ -324,6 +326,7 @@ int main(int argc, char ** argv) { lp.n_parallel = a.n_parallel; lp.use_gpu = a.use_gpu; lp.max_frames = a.max_frames; + lp.n_ubatch = a.n_ubatch; lp.segment_bytes = a.segment_bytes; lp.stream_chunk_ms = a.stream_chunk_ms; lp.sampling = a.sampling; From bfe476fadc080d54d100ff23e35ba4f77312eb58 Mon Sep 17 00:00:00 2001 From: Hans Date: Sat, 14 Nov 2026 21:19:45 +0800 Subject: [PATCH 2/3] Revert "fix(tts-runner): micro-batch 512 unless the model needs more" This reverts commit f3e3e5d84d004d05f519dbb6a1f3bf36ea00c8b5. --- README.md | 1 - common/tts_runner.cpp | 6 +----- common/tts_runner.h | 5 ----- docs/codec_common_api.md | 2 +- include/codec_audio_lm.h | 3 +-- tools/cli/main.cpp | 5 +---- tools/server/main.cpp | 5 +---- 7 files changed, 5 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index eb4f6a6..7e55f2a 100644 --- a/README.md +++ b/README.md @@ -88,7 +88,6 @@ tts-cli synthesize --backbone MODEL.gguf --model CODEC.gguf \ | `--seed N` | repeatable output | | `--temp F`, `--top-k N`, `--top-p F`, `--min-p F`, `--repeat-penalty F`, ... | llama.cpp's sampling flags; unset ones come from the backbone file (`general.sampling.*`, the model card's), then llama.cpp's defaults | | `--max-frames N` | cap the output length | -| `--ubatch N` | backbone micro-batch (default 512, as llama.cpp; a model that needs more sets it itself). Larger only reserves more memory | | `--gpu` | run on the GPU backend the build has (CUDA / Vulkan / Metal) | | `--n-threads N` | CPU threads | | `--override-kv KEY=TYPE:VALUE` | change a setting stored in the codec file (keys are listed in [docs/models](docs/models)) | diff --git a/common/tts_runner.cpp b/common/tts_runner.cpp index b1b5258..ad79f4f 100644 --- a/common/tts_runner.cpp +++ b/common/tts_runner.cpp @@ -566,11 +566,7 @@ tts_runner_session * tts_runner_load(const tts_runner_params & a, std::string * llama_context_params cp = llama_context_default_params(); cp.n_ctx = (uint32_t) req.n_ctx; cp.n_batch = cp.n_ctx; - // A model that needs its prompt in one physical batch says so (req.n_ubatch); otherwise llama.cpp splits the - // batch, and a micro-batch of the whole context would only reserve a compute buffer that size (2.5 GB for - // Qwen3-TTS 0.6B at n_ctx 4096, 0.3 GB at 512). - const uint32_t ub = a.n_ubatch > 0 ? (uint32_t) a.n_ubatch : TTS_RUNNER_DEFAULT_UBATCH; - cp.n_ubatch = req.n_ubatch > 0 ? (uint32_t) req.n_ubatch : std::min(cp.n_ctx, ub); + cp.n_ubatch = req.n_ubatch > 0 ? (uint32_t) req.n_ubatch : cp.n_ctx; cp.n_seq_max = (uint32_t) std::max(1, req.n_seq_max); cp.embeddings = req.embeddings; cp.pooling_type = LLAMA_POOLING_TYPE_NONE; diff --git a/common/tts_runner.h b/common/tts_runner.h index 51084d8..4c8d0b8 100644 --- a/common/tts_runner.h +++ b/common/tts_runner.h @@ -25,8 +25,6 @@ namespace codec_common { -constexpr int32_t TTS_RUNNER_DEFAULT_UBATCH = 512; - // ───────────────────────────────────────────────────────────────────── // Params — everything the reference loop needs. Sampling is llama.cpp's // (its defaults; the model card recommends values); decoder graph settings @@ -97,9 +95,6 @@ struct tts_runner_params { bool use_gpu = false; int32_t n_parallel = 1; // tts_runner_load: requests a session runs at once int32_t max_frames = 0; // 0 → per-model default - // Backbone micro-batch when the model sets no requirement (codec_audio_lm_backbone_req::n_ubatch == 0): - // 0 → TTS_RUNNER_DEFAULT_UBATCH (llama.cpp's default); a model's own requirement always wins. - int32_t n_ubatch = 0; int32_t min_frames = -1; // -1 → the model's own minimum // llama.cpp sampling (common_params_sampling defaults). Segment i of a diff --git a/docs/codec_common_api.md b/docs/codec_common_api.md index 4d6c35d..a70a897 100644 --- a/docs/codec_common_api.md +++ b/docs/codec_common_api.md @@ -29,7 +29,7 @@ codec_audio_lm * alm = codec_audio_lm_init(¶ms, err, sizeof err); // Backbone: load the model, then size its context as the model asks. codec_audio_lm_backbone_req req; codec_audio_lm_backbone_requirements(alm, &vocab, max_frames, &req); -// llama_context_params: n_ctx = req.n_ctx, n_ubatch = req.n_ubatch ?: min(n_ctx, 512), +// llama_context_params: n_ctx = req.n_ctx, n_ubatch = req.n_ubatch ?: n_ctx, // n_seq_max = req.n_seq_max, embeddings = req.embeddings (pooling none), // flash_attn_type = req.flash_attn ? auto : disabled diff --git a/include/codec_audio_lm.h b/include/codec_audio_lm.h index 9f13917..5fbde3b 100644 --- a/include/codec_audio_lm.h +++ b/include/codec_audio_lm.h @@ -165,8 +165,7 @@ struct codec_audio_lm_vocab { // embeddings (pooling none) / flash_attn_type. struct codec_audio_lm_backbone_req { int32_t n_ctx; // minimum context length - int32_t n_ubatch; // physical batch the prompt needs in one piece (0 = no requirement: the host - // picks; codec_common::tts_runner uses 512 as llama.cpp does) + int32_t n_ubatch; // physical batch (0 = n_ctx) int32_t n_seq_max; // sequences decoded side by side (2: guidance pair) bool embeddings; // the context returns hidden states (no pooling) bool flash_attn; // false: the model needs flash attention disabled diff --git a/tools/cli/main.cpp b/tools/cli/main.cpp index 1dea10f..3ddc2f3 100644 --- a/tools/cli/main.cpp +++ b/tools/cli/main.cpp @@ -135,7 +135,7 @@ void print_usage(const char * prog) { " synthesize --model CODEC.gguf [--backbone LLAMA.gguf] --text STRING\n" " --output PATH.wav [--ref-audio PATH.wav] [--ref-text STR]\n" " [--voice NAME] [--speaker-file PATH.gguf] [--save-speaker PATH.gguf]\n" - " [--instruct STR] [--max-frames N] [--ubatch N] [--min-len N] [--segment-bytes N]\n" + " [--instruct STR] [--max-frames N] [--min-len N] [--segment-bytes N]\n" " [--seed N] [--temp F] [--top-k N] [--top-p F] [--min-p F]\n" " [--repeat-last-n N] [--repeat-penalty F] [--presence-penalty F]\n" " [--frequency-penalty F] [--dry-multiplier F] [--dry-base F]\n" @@ -230,7 +230,6 @@ struct args { int32_t n_threads = 0; bool use_gpu = false; int32_t max_frames = 0; - int32_t n_ubatch = 0; int32_t segment_bytes = 0; int32_t min_len = -1; @@ -276,7 +275,6 @@ bool parse_args(int argc, char ** argv, args * out) { else if (a == "--n-threads") { const char * v = need(); if (!v) return false; if (!parse_i32(v, &out->n_threads)) return false; i++; } else if (a == "--gpu") { out->use_gpu = true; } else if (a == "--max-frames") { const char * v = need(); if (!v) return false; if (!parse_i32(v, &out->max_frames)) return false; i++; } - else if (a == "--ubatch") { const char * v = need(); if (!v) return false; if (!parse_i32(v, &out->n_ubatch)) return false; i++; } else if (a == "--segment-bytes") { const char * v = need(); if (!v) return false; if (!parse_i32(v, &out->segment_bytes)) return false; i++; } else if (a == "--seed") { const char * v = need(); if (!v) return false; int32_t s; if (!parse_i32(v, &s)) return false; out->sampling.seed = (uint32_t) s; i++; } else if (a == "--min-len") { const char * v = need(); if (!v) return false; if (!parse_i32(v, &out->min_len)) return false; i++; } @@ -395,7 +393,6 @@ codec_common::tts_runner_params make_runner_params(const args & a) { rp.n_threads = a.n_threads; rp.use_gpu = a.use_gpu; rp.max_frames = a.max_frames; - rp.n_ubatch = a.n_ubatch; rp.min_frames = a.min_len; rp.segment_bytes = a.segment_bytes; rp.sampling = a.sampling; diff --git a/tools/server/main.cpp b/tools/server/main.cpp index dea651a..528bad1 100644 --- a/tools/server/main.cpp +++ b/tools/server/main.cpp @@ -77,7 +77,6 @@ struct args { int32_t n_parallel = 1; bool use_gpu = false; int32_t max_frames = 0; - int32_t n_ubatch = 0; int32_t segment_bytes = 160; // ~10 s of speech per segment int32_t stream_chunk_ms = 1000; std::vector kv_overrides; @@ -92,7 +91,7 @@ void print_usage(const char * argv0) { "usage: %s --model CODEC.gguf [--backbone LLAMA.gguf] [--speaker-file SPK.gguf]\n" " [--voice-dir DIR [--allow-voice-create]]\n" " [--host 127.0.0.1] [--port 8080] [--api-key KEY] [--model-id NAME]\n" - " [--gpu] [--n-threads N] [--parallel N (1)] [--max-frames N] [--ubatch N] [--override-kv KEY=TYPE:VALUE]\n" + " [--gpu] [--n-threads N] [--parallel N (1)] [--max-frames N] [--override-kv KEY=TYPE:VALUE]\n" " [--segment-bytes N (160; 0 = whole input)] [--stream-chunk-ms N (1000; 0 = per segment)]\n" " [llama.cpp sampling: --seed N --temp F --top-k N --top-p F --min-p F --repeat-last-n N\n" " --repeat-penalty F --presence-penalty F --frequency-penalty F --dry-multiplier F\n" @@ -127,7 +126,6 @@ bool parse_args(int argc, char ** argv, args * out) { else if (a == "--n-threads") { if (!(v = need())) return false; out->n_threads = std::atoi(v); } else if (a == "--parallel" || a == "-np") { if (!(v = need())) return false; out->n_parallel = std::atoi(v); } else if (a == "--max-frames") { if (!(v = need())) return false; out->max_frames = std::atoi(v); } - else if (a == "--ubatch") { if (!(v = need())) return false; out->n_ubatch = std::atoi(v); } else if (a == "--segment-bytes") { if (!(v = need())) return false; out->segment_bytes = std::atoi(v); } else if (a == "--stream-chunk-ms") { if (!(v = need())) return false; out->stream_chunk_ms = std::atoi(v); } else if (a == "--gpu") { out->use_gpu = true; } @@ -326,7 +324,6 @@ int main(int argc, char ** argv) { lp.n_parallel = a.n_parallel; lp.use_gpu = a.use_gpu; lp.max_frames = a.max_frames; - lp.n_ubatch = a.n_ubatch; lp.segment_bytes = a.segment_bytes; lp.stream_chunk_ms = a.stream_chunk_ms; lp.sampling = a.sampling; From d1fc2d5af417e42187f4abd46f44c4d6b414d568 Mon Sep 17 00:00:00 2001 From: Hans Date: Sat, 14 Nov 2026 21:19:45 +0800 Subject: [PATCH 3/3] docs(proposals): backbone micro-batch follows llama.cpp (host n_ubatch, default 512) Replaces the reverted implementation: both parts touch public headers (codec_audio_lm.h n_ubatch meaning, tts_runner.h host field), so they come as a proposal with the evidence for where the model-specified value came from. Co-Authored-By: Claude Opus 5.5 --- docs/proposals/backbone-ubatch.md | 58 +++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 docs/proposals/backbone-ubatch.md diff --git a/docs/proposals/backbone-ubatch.md b/docs/proposals/backbone-ubatch.md new file mode 100644 index 0000000..c0b229c --- /dev/null +++ b/docs/proposals/backbone-ubatch.md @@ -0,0 +1,58 @@ +# Proposal: the backbone micro-batch follows llama.cpp (host n_ubatch, default 512) + +Status: proposal for review (no implementation on this branch). Public headers touched if accepted: +`include/codec_audio_lm.h` (`codec_audio_lm_backbone_req::n_ubatch` meaning) and `common/tts_runner.h` +(`tts_runner_params::n_ubatch`), plus `python/codec_cpp/_ffi.py` if a field changes. + +## Motivation + +`tts_runner` creates the backbone context with `n_ubatch = req.n_ubatch ?: n_ctx` +(`common/tts_runner.cpp:569`). A micro-batch of the whole context makes llama.cpp reserve its compute buffer for +`n_ctx` tokens at once: **2502 MiB for Qwen3-TTS 0.6B** (n_ctx 4096), 172 MiB for Soprano, on every load, for +prompts that are a few hundred tokens. On a phone that is the difference between fitting next to an ASR model and +not (measured in Live Translate on a Pixel 7 Pro: 2.5 GB reserved vs 0.31 GB at 512). + +## Where the model-specified value comes from (evidence) + +- `examples/tts.py` (7afe2ab, the first Python host) set `cp.n_ubatch = max(n_ctx, 64)` with no stated reason; + `tts-cli` copied it, and `3ac2156` moved it into `codec_tts_runner`. +- `f10a3d5` (OuteTTS, 8192-token context) set `cp.n_ubatch = 4096` next to the larger context, as a cap, not a + requirement; `8ce8eb8` moved both into `codec_audio_lm_backbone_requirements` (`audio_lm_family_token.cpp:269`). + The base family returns `n_ubatch = 0` (`audio_lm_family.cpp:161`). +- llama.cpp needs `n_ubatch >= n_tokens` only for **non-causal attention** (`src/llama-context.cpp:1750`, + `GGML_ASSERT(cparams.causal_attn || cparams.n_ubatch >= n_tokens_all)`) and for encoders (`:1405`). Every + codec.cpp backbone is a causal LM run embd-in / hidden-out; no family calls `llama_set_causal_attn` and no patch + under `patches/` makes attention non-causal. So no model has a real requirement today. +- Splitting a long prompt only changes float summation order: Soprano with a ~500-token prompt (two micro-batches) + matches a single micro-batch at 35-46 dB for the first 6 s, then a greedy step flips (as any float change can); + Qwen3-TTS prompts below 512 tokens are bit-identical. + +## Proposal + +1. Hosts set the micro-batch like llama.cpp: `tts_runner_params::n_ubatch` (default 512 = llama.cpp's + `n_ubatch` default), `tts-cli` / `tts-server` `-ub` / `--ubatch-size N` (llama.cpp's flag names). + `cp.n_ubatch = max(params.n_ubatch, req.n_ubatch)`, never "0 means something". +2. `codec_audio_lm_backbone_req::n_ubatch` becomes a **minimum**: "the prompt must be decoded in one micro-batch of + at least this many tokens (non-causal backbones); 0 = no requirement". Every family returns 0 today + (OuteTTS's 4096 goes away). Same field, same type, so the struct layout and the Python mirror do not change; + only the documented meaning does. + +## Alternatives considered + +- **A bool** `needs_single_ubatch` (the host then uses `n_ubatch = n_ctx` for that model). Clearer intent, but a + struct layout change (ABI break for compiled hosts, `_ffi.py` update), and a minimum covers the same case. +- **Keep the field, change only `tts_runner`'s default** (the reverted first version of this PR): leaves the public + meaning "0 = n_ctx" contradicted by the reference host. +- **Each family sets 512 explicitly**: keeps a model-specified value nobody needs. + +## Compatibility + +- Hosts that follow today's doc (0 = n_ctx) keep working; they only keep reserving the large buffer. +- Hosts that use `tts_runner` get llama.cpp's default and can override it. +- No model needs a larger micro-batch, so outputs only move by float summation order on prompts > 512 tokens. + +## Models that benefit + +All backbone models. Compute buffer (CPU, host): Qwen3-TTS 0.6B 2502 → 313 MiB; Soprano 172 → 22 MiB; OuteTTS +(n_ctx 8192) from a 4096-token micro-batch to 512. Peak RSS drops only when the reservation is touched (long +prompts): Soprano ~500-token prompt 932 → 872 MB.