A TTS that fits in your CPU (and pocket)
Python
Split tts_model into text chunking and model-state I/O (#259)
manu-lmd4228ee76e87
diff --git a/pocket_tts/__init__.py b/pocket_tts/__init__.pyindex dc959fa..05d1d9f 100644--- a/pocket_tts/__init__.py+++ b/pocket_tts/__init__.py@@ -8,11 +8,14 @@ if os.environ.get("POCKET_TTS_NO_BEARTYPE") != "1":from beartype import BeartypeConffrom beartype.claw import beartype_this_package- beartype_this_package(conf=BeartypeConf(is_color=False))+ # is_pep484_tower: accept int where float is annotated, as PEP 484 says.+ # Without it an `int | float` hint is not redundant and ruff's PYI041+ # advice to collapse it would break callers passing ints.+ beartype_this_package(conf=BeartypeConf(is_color=False, is_pep484_tower=True))+from pocket_tts.models.model_state import export_model_statefrom pocket_tts.models.tts_model import ( # noqa: E402TTSModel,- export_model_state,)# Public methods:diff --git a/pocket_tts/data/audio_utils.py b/pocket_tts/data/audio_utils.pyindex bc83435..a01839d 100644--- a/pocket_tts/data/audio_utils.py+++ b/pocket_tts/data/audio_utils.py@@ -6,7 +6,7 @@ from scipy.signal import resample_polydef convert_audio(- wav: torch.Tensor, from_rate: int | float, to_rate: int | float, to_channels: int+ wav: torch.Tensor, from_rate: float, to_rate: float, to_channels: int) -> torch.Tensor:"""Convert audio to new sample rate and number of audio channels."""if from_rate != to_rate:diff --git a/pocket_tts/main.py b/pocket_tts/main.pyindex 3874a25..64eb297 100644--- a/pocket_tts/main.py+++ b/pocket_tts/main.py@@ -24,7 +24,8 @@ from pocket_tts.default_parameters import (get_default_text_for_language,get_default_voice_for_language,)-from pocket_tts.models.tts_model import TTSModel, export_model_state+from pocket_tts.models.model_state import export_model_state+from pocket_tts.models.tts_model import TTSModelfrom pocket_tts.utils.logging_utils import enable_loggingfrom pocket_tts.utils.utils import _ORIGINS_OF_PREDEFINED_VOICESdiff --git a/pocket_tts/models/model_state.py b/pocket_tts/models/model_state.pynew file mode 100644index 0000000..d255e5a--- /dev/null+++ b/pocket_tts/models/model_state.py@@ -0,0 +1,49 @@+"""Reading and writing the voice-conditioning state as safetensors.++A voice prompt is expensive to encode, so `pocket-tts export-voice` saves the+resulting per-module streaming state and generation reloads it.+"""++from pathlib import Path+from urllib.parse import urlsplit++import safetensors+import safetensors.torch+import torch+++def export_model_state(model_state: dict[str, dict[str, torch.Tensor]], dest: str | Path):+ dict_to_store = {}+ for module_name, module_state in model_state.items():+ for key, tensor_value in module_state.items():+ dict_to_store[f"{module_name}/{key}"] = tensor_value+ safetensors.torch.save_file(dict_to_store, dest)+++def _is_safetensors_source(source: str | Path) -> bool:+ source_text = str(source)+ if source_text.startswith(("http://", "https://")):+ source_text = urlsplit(source_text).path+ elif source_text.startswith("hf://"):+ source_text = source_text.rsplit("@", 1)[0]+ return source_text.endswith(".safetensors")+++def _import_model_state(+ source: str | Path, device: torch.device+) -> dict[str, dict[str, torch.Tensor]]:+ result = {}+ with safetensors.safe_open(source, framework="pt") as f:+ for key in f.keys():+ module_name, tensor_key = key.split("/")+ result.setdefault(module_name, {})+ if tensor_key == "current_end":+ # we used the shape[0] as step index before for torch.compile() compatibility,+ # but it's not needed anymore+ tensor = f.get_tensor(key)+ result[module_name]["offset"] = torch.full(+ (1,), fill_value=tensor.shape[0], dtype=torch.long, device=device+ )+ else:+ result[module_name][tensor_key] = f.get_tensor(key).to(device)+ return resultdiff --git a/pocket_tts/models/text_chunking.py b/pocket_tts/models/text_chunking.pynew file mode 100644index 0000000..bc28255--- /dev/null+++ b/pocket_tts/models/text_chunking.py@@ -0,0 +1,173 @@+"""Turning a text prompt into the chunks the model generates one at a time.++The model is trained on single sentences, so long inputs are split on sentence+boundaries and regrouped into chunks that fit `max_tokens`.+"""++import logging++logger = logging.getLogger(__name__)+++def prepare_text_prompt(+ text: str, pad_with_spaces_for_short_inputs: bool, remove_semicolons: bool+) -> tuple[str, int]:+ text = text.strip()+ if text == "":+ raise ValueError("Text prompt cannot be empty")+ text = text.replace("\n", " ").replace("\r", " ").replace(" ", " ")+ if remove_semicolons:+ text = text.replace(";", ",")+ number_of_words = len(text.split())+ if number_of_words <= 4:+ frames_after_eos_guess = 3+ else:+ frames_after_eos_guess = 1++ # Make sure it starts with an uppercase letter+ if not text[0].isupper():+ text = text[0].upper() + text[1:]++ # Let's make sure it ends with some kind of punctuation+ # If it ends with a letter or digit, we add a period.+ if text[-1].isalnum():+ text = text + "."++ # The model does not perform well when there are very few tokens, so+ # we can add empty spaces at the beginning to increase the token count.+ if pad_with_spaces_for_short_inputs and len(text.split()) < 5:+ text = " " * 8 + text++ return text, frames_after_eos_guess+++def _is_decimal_period_boundary(+ list_of_tokens: list[int], segment_start_idx: int, tokenizer+) -> bool:+ """Return True when segment_start_idx begins right after a decimal period."""+ prefix = tokenizer.sp.decode(list_of_tokens[:segment_start_idx])+ suffix = tokenizer.sp.decode(list_of_tokens[segment_start_idx:])+ return (+ len(prefix) >= 2+ and prefix[-1] == "."+ and prefix[-2].isdigit()+ and bool(suffix)+ and suffix[0].isdigit()+ )+++def _find_boundary_indices(+ list_of_tokens: list[int],+ boundary_tokens: list[int],+ tokenizer=None,+ skip_decimal_periods: bool = False,+) -> list[int]:+ """Find token indices where text should be split based on boundary tokens.++ Returns a list of boundary positions used to slice segments. Each consecutive+ pair (indices[i], indices[i+1]) defines one segment. The first element is+ always 0 and the last is always len(list_of_tokens).+ """+ boundary_set = set(boundary_tokens)+ indices = [0]+ previous_was_boundary = False+ for idx, token in enumerate(list_of_tokens):+ if token in boundary_set:+ previous_was_boundary = True+ else:+ if previous_was_boundary:+ if (+ skip_decimal_periods+ and tokenizer is not None+ and _is_decimal_period_boundary(list_of_tokens, idx, tokenizer)+ ):+ previous_was_boundary = False+ continue+ indices.append(idx)+ previous_was_boundary = False+ indices.append(len(list_of_tokens))+ return indices+++def _segments_from_boundaries(+ list_of_tokens: list[int], boundary_indices: list[int], tokenizer+) -> list[tuple[int, str]]:+ """Decode token segments between boundary indices into (token_count, text) pairs."""+ segments = []+ for i in range(len(boundary_indices) - 1):+ start = boundary_indices[i]+ end = boundary_indices[i + 1]+ text = tokenizer.sp.decode(list_of_tokens[start:end])+ segments.append((end - start, text))+ return segments+++def split_into_best_sentences(+ tokenizer,+ text_to_generate: str,+ max_tokens: int,+ pad_with_spaces_for_short_inputs: bool,+ remove_semicolons: bool,+) -> list[str]:+ text_to_generate, _ = prepare_text_prompt(+ text_to_generate, pad_with_spaces_for_short_inputs, remove_semicolons+ )+ text_to_generate = text_to_generate.strip()+ tokens = tokenizer(text_to_generate)+ list_of_tokens = tokens.tokens[0].tolist()++ _, *end_of_sentence_tokens = tokenizer(".!...?").tokens[0].tolist()+ sentence_boundaries = _find_boundary_indices(+ list_of_tokens, end_of_sentence_tokens, tokenizer, skip_decimal_periods=True+ )+ nb_tokens_and_sentences = _segments_from_boundaries(+ list_of_tokens, sentence_boundaries, tokenizer+ )++ # Sub-split oversized sentences on commas, semicolons, and colons to prevent skipped words+ _, *fallback_tokens = tokenizer(",;:").tokens[0].tolist()+ refined_segments = []+ for nb_tokens, text in nb_tokens_and_sentences:+ if nb_tokens <= max_tokens:+ refined_segments.append((nb_tokens, text))+ else:+ sub_tokens = tokenizer(text.strip()).tokens[0].tolist()+ sub_boundaries = _find_boundary_indices(sub_tokens, fallback_tokens)+ sub_segments = _segments_from_boundaries(sub_tokens, sub_boundaries, tokenizer)+ if len(sub_segments) > 1:+ refined_segments.extend(sub_segments)+ else:+ refined_segments.append((nb_tokens, text))++ max_nb_tokens_in_a_chunk = max_tokens+ chunks = []+ current_chunk = ""+ current_nb_of_tokens_in_chunk = 0+ for nb_tokens, sentence in refined_segments:+ if current_chunk == "":+ current_chunk = sentence+ current_nb_of_tokens_in_chunk = nb_tokens+ continue++ if current_nb_of_tokens_in_chunk + nb_tokens > max_nb_tokens_in_a_chunk:+ chunks.append(current_chunk.strip())+ current_chunk = sentence+ current_nb_of_tokens_in_chunk = nb_tokens+ else:+ current_chunk += " " + sentence+ current_nb_of_tokens_in_chunk += nb_tokens++ if current_chunk != "":+ chunks.append(current_chunk.strip())++ for chunk in chunks:+ chunk_tokens = tokenizer(chunk.strip()).tokens[0].tolist()+ if len(chunk_tokens) > max_tokens:+ logger.warning(+ "Chunk has %d tokens (max %d), generation may skip words: '%.50s...'",+ len(chunk_tokens),+ max_tokens,+ chunk,+ )++ return chunksdiff --git a/pocket_tts/models/tts_model.py b/pocket_tts/models/tts_model.pyindex e4ff9c8..347a0d0 100644--- a/pocket_tts/models/tts_model.py+++ b/pocket_tts/models/tts_model.py@@ -8,7 +8,6 @@ import threadingimport timefrom functools import lru_cachefrom pathlib import Path-from urllib.parse import urlsplitimport safetensorsimport safetensors.torch@@ -30,6 +29,8 @@ from pocket_tts.default_parameters import ()from pocket_tts.models.flow_lm import FlowLMModelfrom pocket_tts.models.mimi import build_mimi+from pocket_tts.models.model_state import _import_model_state, _is_safetensors_source+from pocket_tts.models.text_chunking import prepare_text_prompt, split_into_best_sentencesfrom pocket_tts.modules.stateful_module import StatefulModule, increment_steps, init_statesfrom pocket_tts.quantization import RECOMMENDED_CONFIG, apply_dynamic_int8from pocket_tts.utils.config import CONFIGS_DIR, Config, load_config@@ -256,9 +257,9 @@ class TTSModel(nn.Module):cls,language: str | None = None,config: str | Path | None = None,- temp: float | int | None = None,+ temp: float | None = None,sampler_decode_steps: int = DEFAULT_SAMPLER_DECODE_STEPS,- noise_clamp: float | int | None = DEFAULT_NOISE_CLAMP,+ noise_clamp: float | None = DEFAULT_NOISE_CLAMP,eos_threshold: float = DEFAULT_EOS_THRESHOLD,quantize: bool = False,checkpoint: str | Path | None = None,@@ -957,204 +958,3 @@ class TTSModel(nn.Module):gen_len_sec = token_count / self._TOKENS_PER_SECOND_ESTIMATE + self._GEN_SECONDS_PADDINGframe_rate = self.config.mimi.frame_ratereturn math.ceil(gen_len_sec * frame_rate)---def prepare_text_prompt(- text: str, pad_with_spaces_for_short_inputs: bool, remove_semicolons: bool-) -> tuple[str, int]:- text = text.strip()- if text == "":- raise ValueError("Text prompt cannot be empty")- text = text.replace("\n", " ").replace("\r", " ").replace(" ", " ")- if remove_semicolons:- text = text.replace(";", ",")- number_of_words = len(text.split())- if number_of_words <= 4:- frames_after_eos_guess = 3- else:- frames_after_eos_guess = 1-- # Make sure it starts with an uppercase letter- if not text[0].isupper():- text = text[0].upper() + text[1:]-- # Let's make sure it ends with some kind of punctuation- # If it ends with a letter or digit, we add a period.- if text[-1].isalnum():- text = text + "."-- # The model does not perform well when there are very few tokens, so- # we can add empty spaces at the beginning to increase the token count.- if pad_with_spaces_for_short_inputs and len(text.split()) < 5:- text = " " * 8 + text-- return text, frames_after_eos_guess---def _is_decimal_period_boundary(- list_of_tokens: list[int], segment_start_idx: int, tokenizer-) -> bool:- """Return True when segment_start_idx begins right after a decimal period."""- prefix = tokenizer.sp.decode(list_of_tokens[:segment_start_idx])- suffix = tokenizer.sp.decode(list_of_tokens[segment_start_idx:])- return (- len(prefix) >= 2- and prefix[-1] == "."- and prefix[-2].isdigit()- and bool(suffix)- and suffix[0].isdigit()- )---def _find_boundary_indices(- list_of_tokens: list[int],- boundary_tokens: list[int],- tokenizer=None,- skip_decimal_periods: bool = False,-) -> list[int]:- """Find token indices where text should be split based on boundary tokens.-- Returns a list of boundary positions used to slice segments. Each consecutive- pair (indices[i], indices[i+1]) defines one segment. The first element is- always 0 and the last is always len(list_of_tokens).- """- boundary_set = set(boundary_tokens)- indices = [0]- previous_was_boundary = False- for idx, token in enumerate(list_of_tokens):- if token in boundary_set:- previous_was_boundary = True- else:- if previous_was_boundary:- if (- skip_decimal_periods- and tokenizer is not None- and _is_decimal_period_boundary(list_of_tokens, idx, tokenizer)- ):- previous_was_boundary = False- continue- indices.append(idx)- previous_was_boundary = False- indices.append(len(list_of_tokens))- return indices---def _segments_from_boundaries(- list_of_tokens: list[int], boundary_indices: list[int], tokenizer-) -> list[tuple[int, str]]:- """Decode token segments between boundary indices into (token_count, text) pairs."""- segments = []- for i in range(len(boundary_indices) - 1):- start = boundary_indices[i]- end = boundary_indices[i + 1]- text = tokenizer.sp.decode(list_of_tokens[start:end])- segments.append((end - start, text))- return segments---def split_into_best_sentences(- tokenizer,- text_to_generate: str,- max_tokens: int,- pad_with_spaces_for_short_inputs: bool,- remove_semicolons: bool,-) -> list[str]:- text_to_generate, _ = prepare_text_prompt(- text_to_generate, pad_with_spaces_for_short_inputs, remove_semicolons- )- text_to_generate = text_to_generate.strip()- tokens = tokenizer(text_to_generate)- list_of_tokens = tokens.tokens[0].tolist()-- _, *end_of_sentence_tokens = tokenizer(".!...?").tokens[0].tolist()- sentence_boundaries = _find_boundary_indices(- list_of_tokens, end_of_sentence_tokens, tokenizer, skip_decimal_periods=True- )- nb_tokens_and_sentences = _segments_from_boundaries(- list_of_tokens, sentence_boundaries, tokenizer- )-- # Sub-split oversized sentences on commas, semicolons, and colons to prevent skipped words- _, *fallback_tokens = tokenizer(",;:").tokens[0].tolist()- refined_segments = []- for nb_tokens, text in nb_tokens_and_sentences:- if nb_tokens <= max_tokens:- refined_segments.append((nb_tokens, text))- else:- sub_tokens = tokenizer(text.strip()).tokens[0].tolist()- sub_boundaries = _find_boundary_indices(sub_tokens, fallback_tokens)- sub_segments = _segments_from_boundaries(sub_tokens, sub_boundaries, tokenizer)- if len(sub_segments) > 1:- refined_segments.extend(sub_segments)- else:- refined_segments.append((nb_tokens, text))-- max_nb_tokens_in_a_chunk = max_tokens- chunks = []- current_chunk = ""- current_nb_of_tokens_in_chunk = 0- for nb_tokens, sentence in refined_segments:- if current_chunk == "":- current_chunk = sentence- current_nb_of_tokens_in_chunk = nb_tokens- continue-- if current_nb_of_tokens_in_chunk + nb_tokens > max_nb_tokens_in_a_chunk:- chunks.append(current_chunk.strip())- current_chunk = sentence- current_nb_of_tokens_in_chunk = nb_tokens- else:- current_chunk += " " + sentence- current_nb_of_tokens_in_chunk += nb_tokens-- if current_chunk != "":- chunks.append(current_chunk.strip())-- for chunk in chunks:- chunk_tokens = tokenizer(chunk.strip()).tokens[0].tolist()- if len(chunk_tokens) > max_tokens:- logger.warning(- "Chunk has %d tokens (max %d), generation may skip words: '%.50s...'",- len(chunk_tokens),- max_tokens,- chunk,- )-- return chunks---def export_model_state(model_state: dict[str, dict[str, torch.Tensor]], dest: str | Path):- dict_to_store = {}- for module_name, module_state in model_state.items():- for key, tensor_value in module_state.items():- dict_to_store[f"{module_name}/{key}"] = tensor_value- safetensors.torch.save_file(dict_to_store, dest)---def _is_safetensors_source(source: str | Path) -> bool:- source_text = str(source)- if source_text.startswith(("http://", "https://")):- source_text = urlsplit(source_text).path- elif source_text.startswith("hf://"):- source_text = source_text.rsplit("@", 1)[0]- return source_text.endswith(".safetensors")---def _import_model_state(- source: str | Path, device: torch.device-) -> dict[str, dict[str, torch.Tensor]]:- result = {}- with safetensors.safe_open(source, framework="pt") as f:- for key in f.keys():- module_name, tensor_key = key.split("/")- result.setdefault(module_name, {})- if tensor_key == "current_end":- # we used the shape[0] as step index before for torch.compile() compatibility,- # but it's not needed anymore- tensor = f.get_tensor(key)- result[module_name]["offset"] = torch.full(- (1,), fill_value=tensor.shape[0], dtype=torch.long, device=device- )- else:- result[module_name][tensor_key] = f.get_tensor(key).to(device)- return resultdiff --git a/pocket_tts/modules/rope.py b/pocket_tts/modules/rope.pyindex 7013af1..2c15575 100644--- a/pocket_tts/modules/rope.py+++ b/pocket_tts/modules/rope.py@@ -5,10 +5,7 @@ from torch import nndef apply_rope(- q: torch.Tensor,- k: torch.Tensor,- offset: int | torch.Tensor = 0,- max_period: int | float = 10_000,+ q: torch.Tensor, k: torch.Tensor, offset: int | torch.Tensor = 0, max_period: float = 10_000):"""Args:@@ -65,7 +62,7 @@ class RotaryEmbedding(nn.Module):max_period (float): Maximum period of the rotation frequencies."""- def __init__(self, max_period: float | int = 10000.0):+ def __init__(self, max_period: float = 10000.0):super().__init__()self.max_period = max_period