Skip to content

Commit caa9496

Browse files
hans00claude
andcommitted
audio_lm: format and completion report min_frames
Hosts with a generic minimum-length setting (llama.rn's n_min_predict) need the model's minimum before the prompt is built. min_frames — audio frames before the generation may end, the request's when >= 0, else the model's — is in codec_audio_lm_format and _completion, and every family keeps to it: the grammar (NeuTTS, Soprano, Kitten, MOSS-TTSD), the end code masked in the codec-side head while below the request's minimum (Chatterbox, Qwen3-TTS / CSM, MOSS-TTS-Realtime, LFM2), or the model's own guard (CosyVoice, Pocket-TTS, BlueMagpie). OuteTTS's word grammar sets its length: 0. BlueMagpie counted min_len as frames although the stop head waits for min_len + 1; the request now maps to min_len = min_frames - 1 (default output unchanged). Codebook models report eos_min_step as a floor but do not mask it: CSM's end code 0 is also an audio code. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 27f951b commit caa9496

17 files changed

Lines changed: 84 additions & 16 deletions

‎common/audio_lm.cpp‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1403,7 +1403,7 @@ std::string outetts_build_grammar(outetts_version v, const std::string & text) {
14031403
}
14041404

14051405
std::string tts_auto_grammar(const audio_lm_prompt_info & pi,
1406-
const std::string & text) {
1406+
const std::string & text, int32_t min_frames) {
14071407
(void) text; // reserved for future prompt-dependent grammars
14081408

14091409
// MOSS-TTSD (and any merged-cb0 parallel-heads-delay model): constrain
@@ -1421,9 +1421,11 @@ std::string tts_auto_grammar(const audio_lm_prompt_info & pi,
14211421
// end-of-speech sentinel. The delay-pattern tail frames (cb0 forced
14221422
// to eos while cb1..N flush) are handled by the runtime, not the
14231423
// grammar, so `end` may appear once and then trailing eos repeats are
1424-
// allowed too (end+ ) to cover the flush window.
1424+
// allowed too (end+ ) to cover the flush window. The end waits for
1425+
// min_frames speech frames.
14251426
std::string g;
1426-
g += "root ::= speech* end+\n";
1427+
g += min_frames > 0 ? "root ::= speech{" + std::to_string(min_frames) + "} speech* end+\n"
1428+
: std::string("root ::= speech* end+\n");
14271429
g += "speech ::= \"<\" SPEECHID \">\"\n";
14281430
g += "end ::= \"<|end_of_speech|>\"\n";
14291431
g += "SPEECHID ::= " + num_rule + "\n";

‎common/audio_lm_c.cpp‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,7 @@ bool codec_audio_lm_tts_format(struct codec_audio_lm * ctx, const struct codec_a
321321
out->needs_prompt_hidden = d.needs_prompt_hidden;
322322
out->sample_first = d.sample_first;
323323
out->streamable = d.streamable;
324+
out->min_frames = d.min_frames;
324325
out->max_frames = d.max_frames;
325326
return true;
326327
}
@@ -420,6 +421,7 @@ bool codec_audio_lm_get_completion(struct codec_audio_lm * ctx, struct codec_aud
420421
out->needs_prompt_hidden = c.needs_prompt_hidden;
421422
out->sample_first = c.sample_first;
422423
out->streamable = c.streamable;
424+
out->min_frames = c.min_frames;
423425
out->max_frames = c.max_frames;
424426
ctx->head_groups.clear();
425427
for (const alm_head_group & g : c.head_groups) {

‎common/audio_lm_family.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ struct alm_completion {
6464
bool needs_prompt_hidden = false;
6565
bool sample_first = true;
6666
bool streamable = false;
67+
int32_t min_frames = 0; // frames before the end may come (codec_audio_lm_completion)
6768
int32_t max_frames = 0;
6869
std::vector<alm_head_group> head_groups; // codec-side heads (SAMPLE_HEAD)
6970
};

‎common/audio_lm_family_chatterbox.cpp‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "lm/lm_models.h"
1111
#include "tts_segment.h"
1212

13+
#include <cmath>
1314
#include <cstring>
1415

1516
namespace codec_common {
@@ -65,6 +66,7 @@ class chatterbox_family : public alm_family {
6566
d.needs_embeddings = true;
6667
d.sample_first = false;
6768
d.max_frames = 512;
69+
d.min_frames = std::max(0, req.min_frames); // the stop token is masked before it
6870
return true;
6971
}
7072

@@ -73,6 +75,7 @@ class chatterbox_family : public alm_family {
7375
hidden_ = audio_lm_hidden_dim(alm_);
7476
codes_.clear();
7577
frames_ = 0;
78+
min_frames_ = out->min_frames;
7679
stop_reason_ = "max_frames";
7780
cfg_ = guidance(req);
7881
if (!prompt.voice()) return fail("Chatterbox's prompt starts with <__voice__>");
@@ -159,6 +162,9 @@ class chatterbox_family : public alm_family {
159162
for (int32_t i = 0; i < n; ++i) {
160163
logits_[(size_t) i] = n_seq_ == 2 ? cond[(size_t) i] + cfg_ * (cond[(size_t) i] - uncond[(size_t) i]) : cond[(size_t) i];
161164
}
165+
if (frames_ < min_frames_ && ci_->stop_speech_token >= 0 && ci_->stop_speech_token < n) {
166+
logits_[(size_t) ci_->stop_speech_token] = -INFINITY;
167+
}
162168
*v = {CODEC_AUDIO_LM_SAMPLE_HEAD, false, true, false, -1, 0, n};
163169
return true;
164170
}
@@ -208,7 +214,7 @@ class chatterbox_family : public alm_family {
208214

209215
codec_lm * lm_;
210216
const codec_lm_chatterbox_info * ci_;
211-
int32_t hidden_ = 0, n_seq_ = 1, frames_ = 0;
217+
int32_t hidden_ = 0, n_seq_ = 1, frames_ = 0, min_frames_ = 0;
212218
float cfg_ = 0.0f;
213219
std::vector<int32_t> codes_;
214220
std::vector<float> next_, logits_;

‎common/audio_lm_family_codebook.cpp‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "audio_lm_family.h"
88

99
#include <algorithm>
10+
#include <cmath>
1011
#include <cstring>
1112

1213
namespace codec_common {
@@ -73,6 +74,11 @@ class codebook_family : public alm_family {
7374
alm_completion & d = out->desc;
7475
d.flow = CODEC_AUDIO_LM_FLOW_CODEC_HEAD;
7576
d.n_embd = hidden;
77+
// The end code (eos_code_c0) waits for the request's min_frames frames:
78+
// masked in the codebook-0 head, or by the grammar when codebook 0 is a
79+
// backbone token. The model's eos_min_step is a floor: before it the
80+
// codec takes the end code as an ordinary code (CSM's 0 is one).
81+
d.min_frames = std::max(pi.eos_code_c0 >= 0 ? pi.eos_min_step : 0, std::max(0, req.min_frames));
7682
// The host samples every codebook. Codebook 0, when a codec head, is
7783
// its own group with the penalties (Qwen3-TTS: the talker's
7884
// repetition_penalty over the frames' first codes); the depth codebooks
@@ -81,7 +87,7 @@ class codebook_family : public alm_family {
8187
else d.head_groups = {alm_head_group{true, {}, {}}, alm_head_group{false, {}, {}}};
8288
if (pi.cb0_from_backbone) {
8389
// Codebook 0 is a backbone token: keep it to the speech range + end.
84-
if (req.text_known) d.grammar = tts_auto_grammar(pi, req.text);
90+
if (req.text_known) d.grammar = tts_auto_grammar(pi, req.text, std::max(0, req.min_frames));
8591
if (d.grammar.empty() && pi.audio_tok_offset >= 0) {
8692
d.allowed = {{pi.audio_tok_offset, pi.audio_tok_offset + pi.audio_tok_count}};
8793
if (pi.audio_tok_eos >= 0) d.allowed.push_back({pi.audio_tok_eos, pi.audio_tok_eos + 1});
@@ -99,6 +105,7 @@ class codebook_family : public alm_family {
99105
hidden_ = audio_lm_hidden_dim(alm_);
100106
n_cb_ = audio_lm_n_codebook(alm_);
101107
frames_ = 0;
108+
mask_end_until_ = std::max(0, req.min_frames);
102109
stop_reason_ = "max_frames";
103110
talker_text_.clear();
104111
talker_step_ = 0;
@@ -219,6 +226,9 @@ class codebook_family : public alm_family {
219226
continue;
220227
}
221228
logits_.assign(lg, lg + n);
229+
if (cb_ == 0 && frames_ < mask_end_until_ && pi_.eos_code_c0 >= 0 && pi_.eos_code_c0 < n) {
230+
logits_[(size_t) pi_.eos_code_c0] = -INFINITY; // not the end yet (min_frames)
231+
}
222232
const int32_t group = (!pi_.cb0_from_backbone && cb_ > 0) ? 1 : 0; // head_groups above
223233
*v = {CODEC_AUDIO_LM_SAMPLE_HEAD, false, true, false, -1, group, n};
224234
return true;
@@ -334,7 +344,7 @@ class codebook_family : public alm_family {
334344
}
335345

336346
audio_lm_prompt_info pi_;
337-
int32_t hidden_ = 0, n_cb_ = 0, frames_ = 0;
347+
int32_t hidden_ = 0, n_cb_ = 0, frames_ = 0, mask_end_until_ = 0;
338348
std::vector<int32_t> talker_text_;
339349
int32_t talker_step_ = 0;
340350
std::vector<int32_t> icl_codes_; // reference frames [n, n_cb] of an ICL voice

‎common/audio_lm_family_continuous.cpp‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
#include "audio_lm_family.h"
77

8+
#include <algorithm>
9+
#include <cstdlib>
810
#include <random>
911

1012
namespace codec_common {
@@ -35,6 +37,10 @@ class continuous_family : public alm_family {
3537
d.sample_first = false;
3638
d.streamable = true;
3739
d.max_frames = 512;
40+
// The stop head counts from frame min_len + 1 on (VoxCPM: `i > min_len`,
41+
// the GGUF's codec.lm.min_len, else 2); a request asks for at least one frame.
42+
const char * m_min = audio_lm_meta_str(alm_, "codec.lm.min_len");
43+
d.min_frames = req.min_frames >= 0 ? std::max(1, req.min_frames) : (m_min ? std::atoi(m_min) : 2) + 1;
3844
return true;
3945
}
4046

@@ -50,7 +56,8 @@ class continuous_family : public alm_family {
5056
const char * m_steps = audio_lm_meta_str(alm_, "codec.lm.cfm_n_timesteps");
5157
const char * m_cfg = audio_lm_meta_str(alm_, "codec.lm.cfm_cfg_value");
5258
audio_lm_set_continuous_params(alm_, req.flow_guidance >= 0.0f ? req.flow_guidance : (m_cfg ? (float) std::atof(m_cfg) : 2.0f),
53-
req.flow_steps > 0 ? req.flow_steps : (m_steps ? std::atoi(m_steps) : 10), req.min_frames);
59+
req.flow_steps > 0 ? req.flow_steps : (m_steps ? std::atoi(m_steps) : 10),
60+
out->min_frames - 1);
5461
const std::vector<int32_t> toks = vocab.tokenize(text, pi_.add_bos, pi_.parse_special);
5562
if (toks.empty()) return fail("empty prompt after tokenize");
5663
codec_log(CODEC_LOG_LEVEL_INFO, "prompt: \"%s\" -> %zu tokens\n", text.c_str(), toks.size());

‎common/audio_lm_family_cosyvoice.cpp‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ class cosyvoice_family : public alm_family {
9494
d.needs_embeddings = true;
9595
d.sample_first = false;
9696
d.max_frames = (int32_t) (text_len * ci_->max_token_text_ratio) + 1; // the text-length bound stops first
97+
d.min_frames = req.min_frames >= 0 ? req.min_frames : (int32_t) (text_len * ci_->min_token_text_ratio);
9798
return true;
9899
}
99100

@@ -156,6 +157,7 @@ class cosyvoice_family : public alm_family {
156157
chunks.push_back(alm_chunk::of_embd(std::move(tail), n_tail));
157158
out->seqs = {std::move(chunks)};
158159
out->max_frames = max_len_ + 1; // the text-length bound stops first
160+
out->min_frames = min_len_;
159161
return true;
160162
}
161163

‎common/audio_lm_family_flow.cpp‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ class flow_family : public alm_family {
6767
d.needs_embeddings = true;
6868
d.sample_first = false;
6969
d.max_frames = max_frames_for(ids);
70+
d.min_frames = std::max(0, req.min_frames); // the EOS head counts from this frame on
7071
return true;
7172
}
7273

@@ -118,7 +119,7 @@ class flow_family : public alm_family {
118119
// draws; the FlowLM is very sensitive to the first frames' noise).
119120
set_seed(CODEC_AUDIO_LM_DEFAULT_SEED); // random unless the host sets one (codec_audio_lm_set_seed)
120121
flow_steps_ = req.flow_steps > 0 ? req.flow_steps : 0; // 0: the GGUF's lsd_decode_steps
121-
min_len_ = req.min_frames >= 0 ? req.min_frames : 0; // ignore the EOS head before this frame
122+
min_len_ = out->min_frames; // ignore the EOS head before this frame
122123
eos_step_ = -1;
123124
step_ = 0;
124125
latents_.clear();

‎common/audio_lm_family_kitten.cpp‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,11 @@ class kitten_family : public alm_family {
126126
// backbone from drifting into the Qwen3 text vocabulary.
127127
d.allowed = {{c.audio_base, c.audio_base + c.n_audio}, {c.speech_end, c.speech_end + 1}, {c.stop, c.stop + 1}};
128128
d.max_frames = budget(spoken);
129+
// A minimum length only on request: [STOP] waits in the grammar;
130+
// <|speech_end|> is the backbone's EOS, which llama.cpp's grammar
131+
// holds back until the grammar can end.
132+
d.min_frames = std::max(0, req.min_frames);
133+
d.grammar = alm_min_length_grammar(c.stop, d.min_frames);
129134
return true;
130135
}
131136

‎common/audio_lm_family_soprano.cpp‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ class soprano_family : public alm_family {
4343
d.needs_embeddings = true;
4444
d.sample_first = true;
4545
d.max_frames = 512;
46-
if (req.min_frames > 0) d.grammar = alm_min_length_grammar(eos, req.min_frames);
46+
d.min_frames = std::max(0, req.min_frames); // the model has no minimum of its own
47+
d.grammar = alm_min_length_grammar(eos, d.min_frames);
4748
return true;
4849
}
4950

0 commit comments

Comments
 (0)