Skip to content

A TTS that fits in your CPU (and pocket)

Python

Length-bucketed batches by default (#309)

manu-lm108966e71eaf
diff --git a/training/README.md b/training/README.md
index 27fb0f7..6b1f914 100644
--- a/training/README.md
+++ b/training/README.md
@@ -170,12 +170,12 @@ Regarding timing: the first run precomputes Mimi latents for the train
manifest (once, using all GPUs; validation always encodes audio directly),
then trains from them. On 2000 h of HiFiTTS-2, effective batch size 64:
-| GPUs | per-GPU batch | precompute (once) | steps/s | peak VRAM/GPU | to 200k | to 400k |
-|---|---|---|---|---|---|---|
-| 1 x H100-80GB | 64 | ~50 min | 5.05 | 41.5 GiB | ~11 h | ~22 h |
-| 2 x H100-80GB | 32 | ~25 min | 7.97 | 24.9 GiB | ~7 h | ~14 h |
-| 4 x H100-80GB | 16 | ~15 min | 9.79 | 17.2 GiB | ~5.7 h | ~11.4 h |
-| 8 x H100-80GB | 8 | ~10 min | 10.60 | 15.5 GiB | ~5.2 h | ~10.5 h |
+| GPUs | per-GPU batch | precompute (once) | steps/s | steps/s, no bucketing | peak VRAM/GPU | to 200k | to 400k |
+|---|---|---|---|---|---|---|---|
+| 1 x H100-80GB | 64 | ~50 min | 6.3 | 5.1 | 54 GiB | ~8.8 h | ~17.5 h |
+| 2 x H100-80GB | 32 | ~25 min | 8.1 | 8.0 | 30 GiB | ~6.8 h | ~13.7 h |
+| 4 x H100-80GB | 16 | ~15 min | 9.8 | 10.2 | 18 GiB | ~5.7 h | ~11.3 h |
+| 8 x H100-80GB | 8 | ~10 min | 10.4 | 10.6 | 12 GiB | ~5.3 h | ~10.7 h |
Precompute stores ~6 GB of latents per 1000 h of audio next to the manifest.
`data.precompute: false` skips the precomputing, which will start training
@@ -184,10 +184,10 @@ immediately but at slower speeds.
And for the distillation step (`depth_distill.yaml`), here is the speed you
can expect:
-| GPUs | per-GPU batch | steps/s | peak VRAM/GPU | 200k steps |
-|---|---|---|---|---|
-| 1 x H100 | 64 | 7.61 | 9.7 GiB | ~7.3 h |
-| 8 x H100 | 8 | 23.40 | 5.7 GiB | ~2.4 h |
+| GPUs | per-GPU batch | steps/s | steps/s, no bucketing | peak VRAM/GPU | 200k steps |
+|---|---|---|---|---|---|
+| 1 x H100 | 64 | 10.5 | 7.5 | 9.8 GiB | ~5.3 h |
+| 8 x H100 | 8 | 21.3 | 21.4 | 3.8 GiB | ~2.6 h |
### Training format
diff --git a/training/args.py b/training/args.py
index 6474a4d..ddac7c3 100644
--- a/training/args.py
+++ b/training/args.py
@@ -28,6 +28,8 @@ class DataArgs:
# 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
+ # Batches are drawn from a pool of this many batches sorted by row length; 1 = plain shuffled batches.
+ num_bucket_batches: int = 20
# 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 505209c..af73560 100644
--- a/training/dataloader/loader.py
+++ b/training/dataloader/loader.py
@@ -60,8 +60,10 @@ class DataLoader:
seed: int = 0,
shuffle: bool = True,
io_workers: int = 16,
+ num_bucket_batches: int = 1,
):
self.jsonl = jsonl
+ self.num_bucket_batches = num_bucket_batches
self.entries = load_entries(jsonl, rank, world_size)
self.tokenize = tokenize
self.batch_size = batch_size
@@ -314,31 +316,50 @@ class DataLoader:
chunk_entries = [self.get_entry(i) for i in chunk]
got = [s for s in pool.map(self._sample_or_none, chunk_entries) if s is not None]
samples.extend(got)
- if len(samples) < self.batch_size:
+ # Sort a pool of batches by row length so rows in a batch have similar lengths.
+ pool_size = max(1, self.num_bucket_batches) * self.batch_size
+ if len(samples) < pool_size:
continue
- batch, samples = samples[: self.batch_size], samples[self.batch_size :]
- yielded += 1
- if self.stitch_frames:
- yield self._collate_latent(batch)
- continue
- wavs, tokens, prompts, prompt_lens = zip(*batch, strict=True)
- max_len = max(len(w) for w in wavs)
- audio = torch.zeros(len(wavs), 1, max_len)
- frames = torch.zeros(len(wavs), dtype=torch.long)
- for b, w in enumerate(wavs):
- audio[b, 0, : len(w)] = torch.from_numpy(w)
- frames[b] = max(1, int(len(w) * self.frame_rate / self.sample_rate))
- max_prompt = max(len(p) for p in prompts)
- voice = torch.zeros(len(prompts), 1, max_prompt)
- for b, pr in enumerate(prompts):
- voice[b, 0, : len(pr)] = torch.from_numpy(pr)
- num_voice_prompt_frames = torch.tensor(
- [max(1, int(n * self.frame_rate / self.sample_rate)) for n in prompt_lens],
- dtype=torch.long,
- )
- yield Batch(audio, frames, list(tokens), voice, num_voice_prompt_frames)
+ samples.sort(key=self._row_len)
+ n = len(samples) // self.batch_size
+ batches = [
+ samples[i * self.batch_size : (i + 1) * self.batch_size] for i in range(n)
+ ]
+ samples = samples[n * self.batch_size :]
+ if self.shuffle:
+ self.rng.shuffle(batches)
+ for batch in batches:
+ yielded += 1
+ yield self._collate(batch)
if not yielded:
raise ValueError(
f"no readable samples in {self.jsonl}: every entry failed to load "
f"({self._failures} failures). Check the paths in the manifest."
)
+
+ def _row_len(self, sample: tuple[Any, ...]) -> float:
+ """Voice prompt + text tokens + target audio frames: what a row costs after padding."""
+ if self.stitch_frames: # (stitch wav, tokens, prompt latents, tail latents, target frames)
+ return sample[2].shape[0] + len(sample[1]) + int(sample[4])
+ wav, tokens, _prompt, prompt_samples = sample
+ return (prompt_samples + len(wav)) * self.frame_rate / self.sample_rate + len(tokens)
+
+ def _collate(self, batch: list[tuple[Any, ...]]) -> Batch:
+ if self.stitch_frames:
+ return self._collate_latent(batch)
+ wavs, tokens, prompts, prompt_lens = zip(*batch, strict=True)
+ max_len = max(len(w) for w in wavs)
+ audio = torch.zeros(len(wavs), 1, max_len)
+ frames = torch.zeros(len(wavs), dtype=torch.long)
+ for b, w in enumerate(wavs):
+ audio[b, 0, : len(w)] = torch.from_numpy(w)
+ frames[b] = max(1, int(len(w) * self.frame_rate / self.sample_rate))
+ max_prompt = max(len(p) for p in prompts)
+ voice = torch.zeros(len(prompts), 1, max_prompt)
+ for b, pr in enumerate(prompts):
+ voice[b, 0, : len(pr)] = torch.from_numpy(pr)
+ num_voice_prompt_frames = torch.tensor(
+ [max(1, int(n * self.frame_rate / self.sample_rate)) for n in prompt_lens],
+ dtype=torch.long,
+ )
+ return Batch(audio, frames, list(tokens), voice, num_voice_prompt_frames)
diff --git a/training/dataloader/subproc.py b/training/dataloader/subproc.py
index 1525525..fc62af8 100644
--- a/training/dataloader/subproc.py
+++ b/training/dataloader/subproc.py
@@ -51,6 +51,7 @@ class SubprocessDataLoader:
io_workers: int = 16,
num_procs: int = 6,
depth: int = 8,
+ num_bucket_batches: int = 1,
):
ctx = torch_mp.get_context("spawn")
self._queue = ctx.Queue(maxsize=depth)
@@ -68,6 +69,7 @@ class SubprocessDataLoader:
"seed": seed + rank * num_procs + i,
"shuffle": shuffle,
"io_workers": io_workers,
+ "num_bucket_batches": num_bucket_batches,
}
proc = ctx.Process(
target=_feed_queue,
diff --git a/training/train.py b/training/train.py
index 7cb6d40..95390e3 100644
--- a/training/train.py
+++ b/training/train.py
@@ -189,6 +189,7 @@ def main(config_path: str):
seed=args.seed + start_step,
shuffle=args.data.shuffle,
num_procs=args.data.loader_procs,
+ num_bucket_batches=args.data.num_bucket_batches,
)
)
@@ -306,6 +307,8 @@ def main(config_path: str):
args.run_dir, args.max_steps, model, optimizer, ema, args.num_ckpt_keep, mimi
)
progress.log("checkpoint", args.max_steps)
+ if device.type == "cuda":
+ logger.info(f"peak GPU memory {torch.cuda.max_memory_allocated() / 2**30:.1f} GiB")
logger.info("done")
shutdown_distributed()