A TTS that fits in your CPU (and pocket)
Python
Keep the data loaders alive through SIGTERM so the trainer can checkpoint (#320)
manu-lm5660137005c4
diff --git a/training/dataloader/subproc.py b/training/dataloader/subproc.pyindex ea841ac..d57d07a 100644--- a/training/dataloader/subproc.py+++ b/training/dataloader/subproc.py@@ -9,6 +9,7 @@ ranks by line index.import loggingimport multiprocessing.queuesimport queue+import signalfrom collections.abc import Iteratorfrom typing import Any@@ -22,11 +23,24 @@ from .types import Batchlogger = logging.getLogger(__name__)+def _ignore_stop_signals():+ """scancel and Slurm's pre-timeout warning signal the whole job, loader processes included.++ The trainer turns SIGTERM/SIGUSR1 into "finish this step, checkpoint, exit", which needs the+ batches in flight: a loader (or the torch_shm_manager it spawns, which inherits this+ disposition) that dies on the signal crashes the trainer before it can save. The trainer+ stops the loaders itself (SubprocessDataLoader.close).+ """+ signal.signal(signal.SIGTERM, signal.SIG_IGN)+ signal.signal(signal.SIGUSR1, signal.SIG_IGN)++def _feed_queue(q: "multiprocessing.queues.Queue[Batch]", # not subscriptable at runtime on 3.10serialized_tokenizer: tuple[str, bytes],loader_kwargs: dict[str, Any],):+ _ignore_stop_signals()torch_mp.set_sharing_strategy("file_system")loader = DataLoader(tokenize=encoder_from_serialized(*serialized_tokenizer), **loader_kwargs)for batch in loader:@@ -83,6 +97,15 @@ class SubprocessDataLoader:proc.start()self._procs.append(proc)+ def close(self):+ """Stop the loader processes. They ignore SIGTERM, which multiprocessing's exit handler+ relies on for daemon children, so they must be killed explicitly."""+ for p in self._procs:+ if p.is_alive():+ p.kill()+ for p in self._procs:+ p.join(timeout=10)+def _check_procs(self):dead = [p for p in self._procs if not p.is_alive()]if dead:diff --git a/training/tests/test_loader_signals.py b/training/tests/test_loader_signals.pynew file mode 100644index 0000000..e664afe--- /dev/null+++ b/training/tests/test_loader_signals.py@@ -0,0 +1,35 @@+import os+import signal+import time+from multiprocessing.synchronize import Event++import torch.multiprocessing as torch_mp++from training.dataloader.subproc import _ignore_stop_signals+++def _child(ready: Event) -> None:+ _ignore_stop_signals()+ ready.set()+ time.sleep(60)+++def test_loader_processes_survive_stop_signals() -> None:+ # scancel signals every process of the job; the loaders must outlive the trainer's+ # final step so it can checkpoint before exiting.+ ctx = torch_mp.get_context("spawn")+ ready = ctx.Event()+ proc = ctx.Process(target=_child, args=(ready,), daemon=True)+ proc.start()+ try:+ assert ready.wait(timeout=60)+ pid = proc.pid+ assert pid is not None+ os.kill(pid, signal.SIGTERM)+ os.kill(pid, signal.SIGUSR1)+ time.sleep(0.5)+ assert proc.is_alive()+ finally:+ proc.kill()+ proc.join(timeout=10)+ assert not proc.is_alive()diff --git a/training/train.py b/training/train.pyindex 313bf6a..ab6b40f 100644--- a/training/train.py+++ b/training/train.py@@ -170,28 +170,27 @@ def main(config_path: str):sentence_piece = model.flow_lm.conditioner.tokenizertokenize = sentence_piece.encode- train_loader = iter(- SubprocessDataLoader(- args.data.train_jsonl,- sentence_piece,- args.batch_size,- mimi.sample_rate,- mimi.frame_rate,- args.data.max_duration_sec,- args.data.max_voice_prompt_sec,- rank,- run.world_size,- # Fold the resume step into the seed: the loader keeps no state- # across restarts, so a fixed seed would replay the same- # permutation from the top and bias coverage toward its head.- seed=args.seed + start_step,- shuffle=args.data.shuffle,- num_procs=args.data.loader_procs,- num_bucket_batches=args.data.num_bucket_batches,- prompt_trim_max_sec=args.data.prompt_trim_max_sec,- final_punct_dropout=args.data.final_punct_dropout,- )+ train_data = SubprocessDataLoader(+ args.data.train_jsonl,+ sentence_piece,+ args.batch_size,+ mimi.sample_rate,+ mimi.frame_rate,+ args.data.max_duration_sec,+ args.data.max_voice_prompt_sec,+ rank,+ run.world_size,+ # Fold the resume step into the seed: the loader keeps no state+ # across restarts, so a fixed seed would replay the same+ # permutation from the top and bias coverage toward its head.+ seed=args.seed + start_step,+ shuffle=args.data.shuffle,+ num_procs=args.data.loader_procs,+ num_bucket_batches=args.data.num_bucket_batches,+ prompt_trim_max_sec=args.data.prompt_trim_max_sec,+ final_punct_dropout=args.data.final_punct_dropout,)+ train_loader = iter(train_data)autocast = torch.autocast(device_type=device.type, dtype=torch.bfloat16, enabled=device.type == "cuda"@@ -261,6 +260,7 @@ def main(config_path: str):args.run_dir, step + 1, model, optimizer, ema, args.num_ckpt_keep, mimi)progress.log("checkpoint", step + 1)+ train_data.close()shutdown_distributed()return@@ -310,6 +310,7 @@ def main(config_path: str):if device.type == "cuda":logger.info(f"peak GPU memory {torch.cuda.max_memory_allocated() / 2**30:.1f} GiB")logger.info("done")+ train_data.close()shutdown_distributed()