Skip to content

A TTS that fits in your CPU (and pocket)

Python

Refuse manifests without word alignments at startup (#318)

manu-lm0acce6b2f390
diff --git a/training/dataloader/loader.py b/training/dataloader/loader.py
index f49f103..e013741 100644
--- a/training/dataloader/loader.py
+++ b/training/dataloader/loader.py
@@ -31,11 +31,14 @@ logger = logging.getLogger(__name__)
def _prefetch(iterator: Iterator[Batch], depth: int = 4) -> Iterator[Batch]:
"""Run the (synchronous, IO-bound) loader in a background thread."""
- q: queue.Queue[Batch | None] = queue.Queue(maxsize=depth)
+ q: queue.Queue[Batch | BaseException | None] = queue.Queue(maxsize=depth)
def worker():
- for item in iterator:
- q.put(item)
+ try:
+ for item in iterator:
+ q.put(item)
+ except BaseException as exc: # noqa: BLE001 -- re-raised in the consumer
+ q.put(exc)
q.put(None)
threading.Thread(target=worker, daemon=True).start()
@@ -43,9 +46,15 @@ def _prefetch(iterator: Iterator[Batch], depth: int = 4) -> Iterator[Batch]:
item = q.get()
if item is None:
return
+ if isinstance(item, BaseException):
+ raise item
yield item
+class UnalignedEntry(ValueError):
+ """A manifest entry without word alignments (see training/scripts/align_data.py)."""
+
+
class DataLoader:
# Augmentation knobs; class defaults keep instances built without __init__ (tests) valid.
prompt_trim_max_sec: float = 0.0
@@ -132,7 +141,11 @@ class DataLoader:
# audio before the cut = voice conditioning, audio after = target,
# paired with the remaining words as text (see training/scripts/align_data.py).
if not entry.words:
- return None
+ # Without a boundary to cut at, the prompt would be a window of the target
+ # itself and the model would learn to copy it instead of reading the text.
+ raise UnalignedEntry(
+ f"{entry.path}: no word alignments; run training/scripts/align_data.py first"
+ )
cuts = []
for i in range(1, len(entry.words)):
prev, cur = entry.words[i - 1], entry.words[i]
@@ -306,6 +319,8 @@ class DataLoader:
def _sample_or_none(self, entry: Entry) -> tuple[Any, ...] | None:
try:
return self._sample(entry)
+ except UnalignedEntry:
+ raise
except Exception as exc: # noqa: BLE001 — skip unreadable samples, whatever the cause
self._failures += 1
if self._failures % 1000 == 1:
diff --git a/training/tests/test_dataloader.py b/training/tests/test_dataloader.py
index 6ab40ba..501fabf 100644
--- a/training/tests/test_dataloader.py
+++ b/training/tests/test_dataloader.py
@@ -6,6 +6,7 @@ from typing import Any
import numpy as np
import numpy.typing as npt
+import pytest
import sphn
from training.dataloader import DataLoader, load_entries
@@ -75,12 +76,12 @@ def test_target_audio_never_exceeds_max_duration(tmp_path: Path):
assert batch.audio.shape[-1] <= int(5.0 * SR) + 1
-def test_unaligned_manifest_still_yields_batches(tmp_path: Path):
- """Manifests without word alignments are a documented input: the loader
- falls back to a random window as the prompt instead of hanging."""
+def test_unaligned_manifest_is_refused(tmp_path: Path):
+ """Without word alignments the prompt would be a window of the target itself,
+ so the loader refuses the manifest instead of training a prompt-copier."""
loader = _loader(_manifest(tmp_path, n=8, words=False))
- batch = next(iter(loader))
- assert batch.audio.shape[0] == 2
+ with pytest.raises(ValueError, match="align_data"):
+ next(iter(loader))
def test_entry_start_offsets_into_a_shared_file(tmp_path: Path):
@@ -96,10 +97,22 @@ def test_entry_start_offsets_into_a_shared_file(tmp_path: Path):
manifest = tmp_path / "m.jsonl"
with open(manifest, "w") as f:
- f.write(json.dumps({"path": str(audio_path), "duration": 5.0, "transcript": "low"}) + "\n")
+ words = [{"word": "w", "start": 0.5, "end": 1.5}, {"word": "x", "start": 3.0, "end": 4.0}]
f.write(
json.dumps(
- {"path": str(audio_path), "duration": 5.0, "transcript": "high", "start": 5.0}
+ {"path": str(audio_path), "duration": 5.0, "transcript": "w x", "words": words}
+ )
+ + "\n"
+ )
+ f.write(
+ json.dumps(
+ {
+ "path": str(audio_path),
+ "duration": 5.0,
+ "transcript": "w x",
+ "start": 5.0,
+ "words": words,
+ }
)
+ "\n"
)