Skip to content

A TTS that fits in your CPU (and pocket)

Python
2.6 KB
Raw Blame
import os

import torch
import torch.distributed as dist


def is_torchrun() -> bool:
    return "LOCAL_RANK" in os.environ


def get_rank() -> int:
    return dist.get_rank() if dist.is_initialized() else 0


def get_world_size() -> int:
    return dist.get_world_size() if dist.is_initialized() else 1


def _require_cuda():
    """Training on CPU is accidental (a mismatched torch build), not a use case."""
    if torch.cuda.is_available() or os.environ.get("POCKET_TTS_ALLOW_CPU") == "1":
        return
    if torch.version.cuda is None:
        hint = (
            "this is a CPU-only torch build: pyproject pins the CPU wheel index for "
            "inference, so installing the train extra replaces a CUDA torch. Reinstall "
            "torch afterwards, see training/README.md."
        )
    else:
        hint = (
            "torch is built against CUDA "
            f"{torch.version.cuda}; if the driver reports an older version, install a "
            "matching build, see training/README.md."
        )
    raise SystemExit(
        f"no CUDA device visible to torch (torch {torch.__version__}).\n{hint}\n"
        "Set POCKET_TTS_ALLOW_CPU=1 to run on CPU anyway."
    )


def init_distributed() -> torch.device:
    _require_cuda()
    if is_torchrun():
        local_rank = int(os.environ["LOCAL_RANK"])
        torch.cuda.set_device(local_rank)
        dist.init_process_group(backend="nccl")
        return torch.device("cuda", local_rank)
    return torch.device("cuda" if torch.cuda.is_available() else "cpu")


def avg_across_ranks(value: float) -> float:
    if not dist.is_initialized():
        return value
    t = torch.tensor(value, device="cuda")
    dist.all_reduce(t, op=dist.ReduceOp.AVG)
    return t.item()


def average_grads(params: list[torch.nn.Parameter]):
    """Mean of the gradients across ranks (missing gradients count as zeros), in one all_reduce."""
    flat = torch.cat(
        [(p.grad if p.grad is not None else torch.zeros_like(p)).flatten() for p in params]
    )
    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)