Skip to content

A TTS that fits in your CPU (and pocket)

Python

Training: smaller loader defaults and an upfront manifest size check for small finetunes (#342)

manu-lmfbaf08a41711
diff --git a/training/args.py b/training/args.py
index 987b9af..3a44e92 100644
--- a/training/args.py
+++ b/training/args.py
@@ -33,10 +33,12 @@ class DataArgs:
shuffle: bool = True
# Loader subprocesses per rank. Each one is GIL-bound at ~90 samples/s from
# network storage (extra IO threads do not help), and a rank consumes
- # batch_size x steps/s: 6 keeps a small model at 35 it/s x 16 fed.
- loader_procs: int = 6
+ # batch_size x steps/s: raise it if training waits on data (6 keeps a small
+ # model at 35 it/s x 16 fed). Each process reads its own shard of the manifest.
+ loader_procs: int = 3
# Batches are drawn from a pool of this many batches sorted by row length; 1 = plain shuffled batches.
- num_bucket_batches: int = 20
+ # Every loader shard needs num_bucket_batches x batch_size entries before its first batch.
+ num_bucket_batches: int = 5
# Precompute Mimi latents for train_jsonl on first run and train from
# them (rank 0 encodes once; other ranks wait). False keeps the
# on-the-fly audio pipeline.
diff --git a/training/dataloader/loader.py b/training/dataloader/loader.py
index e013741..cb25dbf 100644
--- a/training/dataloader/loader.py
+++ b/training/dataloader/loader.py
@@ -339,11 +339,13 @@ class DataLoader:
# concurrently keeps the GPUs fed. sphn releases the GIL, so threads are
# enough. Without this a cold, wide corpus starves training (~2x).
pool = ThreadPoolExecutor(max_workers=self.io_workers)
- if len(self.entries) < self.batch_size:
+ need = max(1, self.num_bucket_batches) * self.batch_size
+ if len(self.entries) < need:
raise ValueError(
- f"{len(self.entries)} usable entries for this rank but batch_size="
- f"{self.batch_size}: a batch can never be filled. Lower batch_size, "
- "use fewer ranks, or point at a larger manifest."
+ f"{len(self.entries)} entries in this loader's shard of {self.jsonl}, but the "
+ f"first batch needs num_bucket_batches x batch_size = {need}. Lower "
+ "data.num_bucket_batches (1 disables length bucketing), data.loader_procs or "
+ "batch_size, use fewer ranks, or point at a larger manifest."
)
while True:
yielded = 0
diff --git a/training/tests/test_dataloader.py b/training/tests/test_dataloader.py
index 501fabf..1dfc889 100644
--- a/training/tests/test_dataloader.py
+++ b/training/tests/test_dataloader.py
@@ -132,3 +132,23 @@ def test_entry_start_offsets_into_a_shared_file(tmp_path: Path):
assert abs(dominant_freq(low_wav) - low_hz) < 2
assert abs(dominant_freq(high_wav) - high_hz) < 2
+
+
+def test_shard_smaller_than_the_bucket_pool_names_the_knobs(tmp_path: Path):
+ loader = _loader(_manifest(tmp_path, n=8), batch_size=2, num_bucket_batches=5)
+ with pytest.raises(ValueError, match="num_bucket_batches"):
+ next(iter(loader))
+
+
+def test_startup_check_rejects_manifests_too_small_for_the_loaders(tmp_path: Path):
+ from training.args import TrainArgs
+ from training.train_utils import check_manifest_sizes
+
+ args = TrainArgs()
+ args.batch_size = 2
+ args.data.train_jsonl = _manifest(tmp_path, n=12)
+ args.data.valid_jsonl = ""
+ args.data.loader_procs, args.data.num_bucket_batches = 3, 2
+ check_manifest_sizes(args, world_size=1) # 12 // 3 = 4 entries per shard, 2 x 2 needed
+ with pytest.raises(SystemExit, match="num_bucket_batches"):
+ check_manifest_sizes(args, world_size=2) # 12 // 6 = 2 < 4
diff --git a/training/train.py b/training/train.py
index ab6b40f..fa3529d 100644
--- a/training/train.py
+++ b/training/train.py
@@ -44,6 +44,7 @@ from training.train_utils import (
_compile_models,
add_file_logging,
build_optimizer,
+ check_manifest_sizes,
ensure_train_latents,
git_commit,
lr_at,
@@ -109,6 +110,7 @@ def setup(config_path: str) -> Run:
"Validation must encode audio directly so its metrics stay exact and "
"comparable; point valid_jsonl at the original manifest."
)
+ check_manifest_sizes(args, world_size)
if rank == 0:
save_args(args, run_dir / "args.yaml")
diff --git a/training/train_utils.py b/training/train_utils.py
index 54919c7..5a45c2f 100644
--- a/training/train_utils.py
+++ b/training/train_utils.py
@@ -17,6 +17,7 @@ from pocket_tts.models.mimi import MimiModel
from pocket_tts.modules.attention import StreamingMultiheadAttention
from pocket_tts.modules.stateful_module import init_states
from training.args import TrainArgs
+from training.dataloader.manifest import LazyEntries
from training.modules.builders import load_model_config
from training.modules.model import TrainableTTS
from training.modules.muon import MuonWithAuxAdam
@@ -306,3 +307,43 @@ def build_optimizer(
f"({n_split} fused weights orthogonalized per block), adamw on the rest"
)
return MuonWithAuxAdam(groups)
+
+
+def check_manifest_sizes(args: TrainArgs, world_size: int) -> None:
+ """Fail at startup, not at the first batch or validation step, when a loader shard is too
+ small to ever fill a batch: train_jsonl is split over world_size x loader_procs loaders that
+ each need num_bucket_batches x batch_size entries, valid_jsonl over world_size ranks that
+ each need batch_size."""
+ procs = max(1, args.data.loader_procs)
+ checks = [
+ (
+ "train_jsonl",
+ args.data.train_jsonl,
+ world_size * procs,
+ max(1, args.data.num_bucket_batches) * args.batch_size,
+ f"{world_size} rank(s) x {procs} data.loader_procs",
+ (
+ f"data.num_bucket_batches {args.data.num_bucket_batches} x batch_size "
+ f"{args.batch_size}"
+ ),
+ ),
+ (
+ "valid_jsonl",
+ args.data.valid_jsonl,
+ world_size,
+ args.batch_size,
+ f"{world_size} rank(s)",
+ "batch_size",
+ ),
+ ]
+ for name, path, shards, need, split, needs in checks:
+ if not path:
+ continue
+ n = len(LazyEntries(path, 0, 1))
+ if n // shards < need:
+ raise SystemExit(
+ f"{name} has {n} entries, split into {shards} shards ({split}) of about "
+ f"{n // shards}, but each shard needs {need} ({needs}) for its first batch.\n"
+ "Lower data.num_bucket_batches (1 disables length bucketing), data.loader_procs "
+ "or batch_size, or use a larger manifest."
+ )