Skip to content

Commit 64baa6a

Browse files
Your Nameclaude
andcommitted
Apply repetition penalty to the silence codec token
Exempting silence (codec 4299) from the repetition penalty lets the main checkpoint stall in silence runs: no speech, silent tails, dropped sentences and runaways to the token cap. Only <speech_end> and [STOP] stay exempt now, matching the reference gradio demo's SpeechPolicy. vLLM reuses build_logits_processors, so it is fixed too. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent cd1a5f0 commit 64baa6a

4 files changed

Lines changed: 40 additions & 19 deletions

File tree

‎kittenml/kittentts2/logits.py‎

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,16 @@
2121
TopPLogitsWarper,
2222
)
2323

24-
# The S3 codec's silence token, as an offset within the audio range. Runs of it are
25-
# the model's most common stuck state, and it is the one token the repetition
26-
# penalty must leave alone (see RepetitionPenaltyExempt).
27-
SILENCE_TOKEN = 4299
28-
29-
3024
class RepetitionPenaltyExempt(LogitsProcessor):
3125
"""Repetition penalty that never penalises the sequence-terminating tokens.
3226
33-
`<speech_end>`, `[STOP]` and the silence codec token are exempt: penalising
34-
them suppresses the trailing silence the model needs before it can close a
35-
segment, which turns a clean ending into an over-generation.
27+
`<speech_end>` and `[STOP]` are exempt: penalising them would push the model
28+
away from closing the segment it has just finished.
29+
30+
The silence codec token is deliberately *not* exempt. Runs of it are the
31+
model's most common stuck state, and the windowed penalty is what pulls it out:
32+
with silence exempt, the main checkpoint drifts into seconds of silence (or
33+
silence until the token cap) and drops sentences from long chunks.
3634
3735
window=None scans the whole sequence. window=N restricts the scan to the last
3836
N *generated* positions, so codec tokens from a long in-context reference
@@ -99,13 +97,12 @@ def build_logits_processors(token_map, temperature, top_k, top_p, min_p,
9997
scores; that ordering is part of what the decode presets were tuned against.
10098
"""
10199
lo, hi = token_map.audio_id_base, token_map.audio_id_end
102-
silence_id = lo + SILENCE_TOKEN
103100
processors = LogitsProcessorList()
104101

105102
if token_run_penalty:
106103
processors.append(TokenRunPenalty(lo, hi, token_run_grace, token_run_penalty))
107104
if repetition_penalty != 1.0:
108-
exempt = {token_map.speech_end_id, token_map.stop_id, silence_id}
105+
exempt = {token_map.speech_end_id, token_map.stop_id}
109106
processors.append(RepetitionPenaltyExempt(
110107
repetition_penalty, exempt, window=repetition_window, prompt_length=prompt_length))
111108
if temperature != 1.0:

‎kittenml/kittentts2/mlx/sample.py‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414

1515
import mlx.core as mx
1616

17-
from ..logits import SILENCE_TOKEN
18-
1917
FILTER_VALUE = -float("inf")
2018

2119

@@ -43,9 +41,9 @@ def __call__(self, history, scores):
4341
class RepetitionPenaltyExempt:
4442
"""Repetition penalty that never penalises the sequence-terminating tokens.
4543
46-
See `logits.RepetitionPenaltyExempt`: `<speech_end>`, `[STOP]` and the silence
47-
codec token keep their original scores, and `window` restricts the scan to the
48-
last N generated positions.
44+
See `logits.RepetitionPenaltyExempt`: `<speech_end>` and `[STOP]` keep their
45+
original scores, and `window` restricts the scan to the last N generated
46+
positions.
4947
"""
5048

5149
def __init__(self, penalty, exempt_ids, window=None, prompt_length=0):
@@ -126,7 +124,7 @@ def build_logits_processors(token_map, temperature, top_k, top_p, min_p,
126124
if token_run_penalty:
127125
processors.append(TokenRunPenalty(lo, hi, token_run_grace, token_run_penalty))
128126
if repetition_penalty != 1.0:
129-
exempt = {token_map.speech_end_id, token_map.stop_id, lo + SILENCE_TOKEN}
127+
exempt = {token_map.speech_end_id, token_map.stop_id}
130128
processors.append(RepetitionPenaltyExempt(
131129
repetition_penalty, exempt, window=repetition_window,
132130
prompt_length=prompt_length))

‎tests/test_logits.py‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import pytest
2+
import torch
3+
4+
from kittenml.kittentts2.logits import build_logits_processors
5+
from kittenml.kittentts2.tokens import TokenMap
6+
7+
TOKEN_MAP = TokenMap(audio_id_base=100, num_audio_tokens=6561, speech_start_id=1,
8+
speech_end_id=2, text_start_id=3, text_end_id=4, start_id=5,
9+
stop_id=6)
10+
SILENCE = TOKEN_MAP.audio_id_base + 4299
11+
12+
13+
def test_repetition_penalty_covers_silence_but_not_terminators():
14+
"""Silence runs are the main checkpoint's stuck state; only the stop tokens are exempt."""
15+
processors = build_logits_processors(TOKEN_MAP, temperature=1.0, top_k=0, top_p=1.0,
16+
min_p=0.0, repetition_penalty=1.1,
17+
repetition_window=50, token_run_penalty=0,
18+
prompt_length=1)
19+
history = torch.tensor([[TOKEN_MAP.start_id, SILENCE, TOKEN_MAP.speech_end_id,
20+
TOKEN_MAP.stop_id, 150]])
21+
scores = torch.full((1, TOKEN_MAP.audio_id_end), 2.0)
22+
out = processors(history, scores.clone())
23+
assert out[0, SILENCE].item() == pytest.approx(2.0 / 1.1)
24+
assert out[0, 150].item() == pytest.approx(2.0 / 1.1)
25+
assert out[0, TOKEN_MAP.speech_end_id] == 2.0
26+
assert out[0, TOKEN_MAP.stop_id] == 2.0

‎tests/test_mlx.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717

1818
from kittenml.kittentts2.mlx import loader, sample # noqa: E402
1919

20-
# The real codec vocabulary size: the repetition penalty exempts the silence
21-
# token at offset 4299, so a toy audio range would not reach it.
20+
# The real codec vocabulary size, so codec offsets such as the silence token
21+
# (4299) fall inside the audio range as they do in a real checkpoint.
2222
TOKEN_MAP = TokenMap(audio_id_base=100, num_audio_tokens=6561, speech_start_id=1,
2323
speech_end_id=2, text_start_id=3, text_end_id=4, start_id=5,
2424
stop_id=6)

0 commit comments

Comments
 (0)