Skip to content

A TTS that fits in your CPU (and pocket)

Python

Generate every chunk from the voice prompt with copy_state=False (#443)

Caio Theodorobc4d640c925a
diff --git a/pocket_tts/models/tts_model.py b/pocket_tts/models/tts_model.py
index c8bf72f..3552b91 100644
--- a/pocket_tts/models/tts_model.py
+++ b/pocket_tts/models/tts_model.py
@@ -624,7 +624,9 @@ class TTSModel(nn.Module):
based on text length (1-3 frames).
copy_state: Whether to create a deep copy of the model state before
generation. If True, preserves the original state for reuse.
- If False, modifies the input state in-place. Defaults to True.
+ If False, modifies the input state in-place: a long text is generated in
+ chunks that each start from the voice prompt, so only the last chunk is
+ generated in the input state (earlier ones use a copy). Defaults to True.
max_tokens: Long texts are generated in chunks of at most this many tokens.
Defaults to 50, or 25 with `teacher_force_previous_chunk`.
teacher_force_previous_chunk: When True, each chunk after the first is generated
@@ -702,7 +704,9 @@ class TTSModel(nn.Module):
based on text length (1-3 frames). Defaults to None.
copy_state: Whether to create a deep copy of the model state before
generation. If True, preserves the original state for reuse.
- If False, modifies the input state in-place. Defaults to True.
+ If False, modifies the input state in-place: a long text is generated in
+ chunks that each start from the voice prompt, so only the last chunk is
+ generated in the input state (earlier ones use a copy). Defaults to True.
max_tokens: Long texts are generated in chunks of at most this many tokens.
Defaults to 50, or 25 with `teacher_force_previous_chunk`.
teacher_force_previous_chunk: When True, each chunk after the first is generated
@@ -769,7 +773,7 @@ class TTSModel(nn.Module):
prev_chunk: str | None = None
prev_latents: list[torch.Tensor] = []
- for chunk in chunks:
+ for i, chunk in enumerate(chunks):
if stop.is_set():
break
forced, text_chunk = None, chunk
@@ -802,7 +806,8 @@ class TTSModel(nn.Module):
model_state=model_state,
text_to_generate=text_to_generate,
frames_after_eos=effective_frames,
- copy_state=copy_state or teacher_force_previous_chunk,
+ # Every chunk starts from the voice state: only the last one may consume it.
+ copy_state=copy_state or teacher_force_previous_chunk or i < len(chunks) - 1,
stop=stop,
latents_out=prev_latents if teacher_force_previous_chunk else None,
forced_latents=forced,
diff --git a/tests/test_teacher_force_previous_chunk.py b/tests/test_teacher_force_previous_chunk.py
index 3c77d3d..a76c580 100644
--- a/tests/test_teacher_force_previous_chunk.py
+++ b/tests/test_teacher_force_previous_chunk.py
@@ -53,6 +53,20 @@ def test_flag_off_generates_every_chunk_afresh_from_the_voice(
assert torch.equal(audio, torch.cat(separate))
+def test_copy_state_false_generates_every_chunk_afresh_from_the_voice(
+ model: TTSModel, monkeypatch: pytest.MonkeyPatch
+):
+ monkeypatch.setattr(model, "max_decoder_frames_per_call", 1)
+ assert len(_chunks(model, 50)) > 1
+
+ torch.manual_seed(0)
+ expected = model.generate_audio(model.get_state_for_audio_prompt("alba"), TEXT)
+ torch.manual_seed(0)
+ audio = model.generate_audio(model.get_state_for_audio_prompt("alba"), TEXT, copy_state=False)
+
+ assert torch.equal(audio, expected)
+
+
def test_flag_on_continues_the_previous_chunk(model: TTSModel, monkeypatch: pytest.MonkeyPatch):
voice_state = model.get_state_for_audio_prompt("alba")
chunks = _chunks(model, 25)