Skip to content

Open-source State-of-the-art TTS model which runs on a CPU 😻

Python

Apply repetition penalty to the silence codec token

Your Name64baa6a675dd
diff --git a/kittenml/kittentts2/logits.py b/kittenml/kittentts2/logits.py
index 025de1d..a83ab8d 100644
--- a/kittenml/kittentts2/logits.py
+++ b/kittenml/kittentts2/logits.py
@@ -21,18 +21,16 @@ from transformers import (
TopPLogitsWarper,
)
-# The S3 codec's silence token, as an offset within the audio range. Runs of it are
-# the model's most common stuck state, and it is the one token the repetition
-# penalty must leave alone (see RepetitionPenaltyExempt).
-SILENCE_TOKEN = 4299
-
-
class RepetitionPenaltyExempt(LogitsProcessor):
"""Repetition penalty that never penalises the sequence-terminating tokens.
- `<speech_end>`, `[STOP]` and the silence codec token are exempt: penalising
- them suppresses the trailing silence the model needs before it can close a
- segment, which turns a clean ending into an over-generation.
+ `<speech_end>` and `[STOP]` are exempt: penalising them would push the model
+ away from closing the segment it has just finished.
+
+ The silence codec token is deliberately *not* exempt. Runs of it are the
+ model's most common stuck state, and the windowed penalty is what pulls it out:
+ with silence exempt, the main checkpoint drifts into seconds of silence (or
+ silence until the token cap) and drops sentences from long chunks.
window=None scans the whole sequence. window=N restricts the scan to the last
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,
scores; that ordering is part of what the decode presets were tuned against.
"""
lo, hi = token_map.audio_id_base, token_map.audio_id_end
- silence_id = lo + SILENCE_TOKEN
processors = LogitsProcessorList()
if token_run_penalty:
processors.append(TokenRunPenalty(lo, hi, token_run_grace, token_run_penalty))
if repetition_penalty != 1.0:
- exempt = {token_map.speech_end_id, token_map.stop_id, silence_id}
+ exempt = {token_map.speech_end_id, token_map.stop_id}
processors.append(RepetitionPenaltyExempt(
repetition_penalty, exempt, window=repetition_window, prompt_length=prompt_length))
if temperature != 1.0:
diff --git a/kittenml/kittentts2/mlx/sample.py b/kittenml/kittentts2/mlx/sample.py
index 301ef0f..cd5f8d3 100644
--- a/kittenml/kittentts2/mlx/sample.py
+++ b/kittenml/kittentts2/mlx/sample.py
@@ -14,8 +14,6 @@ to be penalised.
import mlx.core as mx
-from ..logits import SILENCE_TOKEN
-
FILTER_VALUE = -float("inf")
@@ -43,9 +41,9 @@ class TokenRunPenalty:
class RepetitionPenaltyExempt:
"""Repetition penalty that never penalises the sequence-terminating tokens.
- See `logits.RepetitionPenaltyExempt`: `<speech_end>`, `[STOP]` and the silence
- codec token keep their original scores, and `window` restricts the scan to the
- last N generated positions.
+ See `logits.RepetitionPenaltyExempt`: `<speech_end>` and `[STOP]` keep their
+ original scores, and `window` restricts the scan to the last N generated
+ positions.
"""
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,
if token_run_penalty:
processors.append(TokenRunPenalty(lo, hi, token_run_grace, token_run_penalty))
if repetition_penalty != 1.0:
- exempt = {token_map.speech_end_id, token_map.stop_id, lo + SILENCE_TOKEN}
+ exempt = {token_map.speech_end_id, token_map.stop_id}
processors.append(RepetitionPenaltyExempt(
repetition_penalty, exempt, window=repetition_window,
prompt_length=prompt_length))
diff --git a/tests/test_logits.py b/tests/test_logits.py
new file mode 100644
index 0000000..98dd7d1
--- /dev/null
+++ b/tests/test_logits.py
@@ -0,0 +1,26 @@
+import pytest
+import torch
+
+from kittenml.kittentts2.logits import build_logits_processors
+from kittenml.kittentts2.tokens import TokenMap
+
+TOKEN_MAP = TokenMap(audio_id_base=100, num_audio_tokens=6561, speech_start_id=1,
+ speech_end_id=2, text_start_id=3, text_end_id=4, start_id=5,
+ stop_id=6)
+SILENCE = TOKEN_MAP.audio_id_base + 4299
+
+
+def test_repetition_penalty_covers_silence_but_not_terminators():
+ """Silence runs are the main checkpoint's stuck state; only the stop tokens are exempt."""
+ processors = build_logits_processors(TOKEN_MAP, temperature=1.0, top_k=0, top_p=1.0,
+ min_p=0.0, repetition_penalty=1.1,
+ repetition_window=50, token_run_penalty=0,
+ prompt_length=1)
+ history = torch.tensor([[TOKEN_MAP.start_id, SILENCE, TOKEN_MAP.speech_end_id,
+ TOKEN_MAP.stop_id, 150]])
+ scores = torch.full((1, TOKEN_MAP.audio_id_end), 2.0)
+ out = processors(history, scores.clone())
+ assert out[0, SILENCE].item() == pytest.approx(2.0 / 1.1)
+ assert out[0, 150].item() == pytest.approx(2.0 / 1.1)
+ assert out[0, TOKEN_MAP.speech_end_id] == 2.0
+ assert out[0, TOKEN_MAP.stop_id] == 2.0
diff --git a/tests/test_mlx.py b/tests/test_mlx.py
index df0bdc0..7ab9213 100644
--- a/tests/test_mlx.py
+++ b/tests/test_mlx.py
@@ -17,8 +17,8 @@ mx = pytest.importorskip("mlx.core", reason="the MLX backend is an optional extr
from kittenml.kittentts2.mlx import loader, sample # noqa: E402
-# The real codec vocabulary size: the repetition penalty exempts the silence
-# token at offset 4299, so a toy audio range would not reach it.
+# The real codec vocabulary size, so codec offsets such as the silence token
+# (4299) fall inside the audio range as they do in a real checkpoint.
TOKEN_MAP = TokenMap(audio_id_base=100, num_audio_tokens=6561, speech_start_id=1,
speech_end_id=2, text_start_id=3, text_end_id=4, start_id=5,
stop_id=6)