Skip to content

A TTS that fits in your CPU (and pocket)

Python

Training: graceful stop via a STOP file; stopped runs exit cleanly (#414)

manu-lm2e117a1e7df0
diff --git a/training/distributed.py b/training/distributed.py
index 2d2185c..0f494d3 100644
--- a/training/distributed.py
+++ b/training/distributed.py
@@ -48,17 +48,6 @@ def init_distributed() -> torch.device:
return torch.device("cuda" if torch.cuda.is_available() else "cpu")
-def shutdown_distributed():
- """Hold every rank until all are done, then tear NCCL down.
-
- Without the barrier, multi-GPU training can crash at the end.
- """
- if not dist.is_initialized():
- return
- dist.barrier(device_ids=[torch.cuda.current_device()])
- dist.destroy_process_group()
-
-
def avg_across_ranks(value: float) -> float:
if not dist.is_initialized():
return value
@@ -75,3 +64,19 @@ def average_grads(params: list[torch.nn.Parameter]):
dist.all_reduce(flat, op=dist.ReduceOp.AVG)
for p, g in zip(params, flat.split([p.numel() for p in params]), strict=True):
p.grad = g.view_as(p)
+
+
+def exit_training():
+ """Exit the process now (NCCL is torn down with it).
+
+ The spawned loader processes and their queue feeder threads can keep the interpreter alive
+ after training; a hard exit releases the GPUs immediately. Kill the loaders first: as
+ orphans they would keep a piped stdout open and stall whatever runs after training.
+ """
+ import logging
+ import multiprocessing
+
+ for child in multiprocessing.active_children():
+ child.kill()
+ logging.shutdown()
+ os._exit(0)
diff --git a/training/train.py b/training/train.py
index 8fdd524..8c9481e 100644
--- a/training/train.py
+++ b/training/train.py
@@ -33,11 +33,11 @@ from training.dataloader.teacher_cache import cached_batches
from training.distributed import (
average_grads,
avg_across_ranks,
+ exit_training,
get_rank,
get_world_size,
init_distributed,
is_torchrun,
- shutdown_distributed,
)
from training.modules.builders import build_models
from training.modules.model import TrainableTTS
@@ -228,6 +228,13 @@ def main(config_path: str):
signal.signal(signal.SIGTERM, _request_stop)
signal.signal(signal.SIGUSR1, _request_stop)
+ # graceful stop with no 30 s deadline: `touch <run_dir>/STOP`, the run checkpoints after
+ # its current step and exits. A STOP left from the last stop is renamed so a resume runs.
+ _stop_file = os.path.join(args.run_dir, "STOP")
+ if rank == 0 and os.path.exists(_stop_file):
+ os.rename(_stop_file, f"{_stop_file}.used_{int(time.time())}")
+ if run.world_size > 1:
+ torch.distributed.barrier()
if rank == 0:
logger.info("starting training loop, first step can take a few minutes (compilation etc.)")
last_log = time.time()
@@ -298,7 +305,10 @@ def main(config_path: str):
if ema is not None:
ema.update(model)
- stop = torch.tensor(float(stop_requested), device=device)
+ stop = torch.tensor(
+ float(stop_requested or os.path.exists(os.path.join(args.run_dir, "STOP"))),
+ device=device,
+ )
if run.world_size > 1:
torch.distributed.all_reduce(stop, op=torch.distributed.ReduceOp.MAX)
if stop.item():
@@ -312,8 +322,7 @@ def main(config_path: str):
progress.log("checkpoint", step + 1)
if train_data is not None:
train_data.close()
- shutdown_distributed()
- return
+ exit_training()
steps_since_log += 1
if rank == 0 and step == start_step and args.compile:
@@ -365,7 +374,7 @@ def main(config_path: str):
logger.info("done")
if train_data is not None:
train_data.close()
- shutdown_distributed()
+ exit_training()
@torch.no_grad()