Multi-node training (SLURM / Elastic)¶
HydraDDP runs single-node multi-GPU out of the box. For multi-node, delegate
process launching to a cluster scheduler; Hydra's
hydra-submitit-launcher +
Lightning's auto-detected SLURMEnvironment. Under an external launcher,
HydraDDP steps aside and Lightning uses the scheduler-launched ranks directly.
This guide is about a cell trained with DDP, a task per GPU, on one node or
several. For a sweep whose cells each run in one process as an ordinary SLURM
job (one task, with any GPUs it uses itself), build the launcher config with
mushin.slurm instead (the
SLURM guide walks through it): a job of several tasks runs the cell
once per task.
The one contract: one SLURM task per GPU¶
For DDP there must be exactly one process (rank) per GPU:
Get this wrong and you hang at rendezvous (too few tasks), OOM (too many), or
silently run single-GPU. mushin.submitit_slurm_config derives tasks_per_node
from gpus_per_node so they cannot desync, and HydraDDP fails fast with a
clear error if the launched world size doesn't match num_nodes x devices.
Building the launcher config¶
from mushin import submitit_slurm_config
slurm = submitit_slurm_config(
nodes=2,
gpus_per_node=4, # -> tasks_per_node=4, one rank per GPU
cpus_per_task=8,
partition="gpu",
timeout_min=120,
mem_gb=64,
)
Hand the dict straight to run() (install the plugin first:
pip install "mushin-py[slurm]"): no hand-rolled hydra.launcher.*
overrides:
(Outside the workflow API, the equivalent raw overrides are
hydra/launcher=submitit_slurm plus hydra.launcher.<key>=<value> per field.)
The Trainer must use a matching devices/num_nodes:
import pytorch_lightning as pl
from hydra_zen import builds
from mushin import HydraDDP
TrainerConfig = builds(
pl.Trainer,
accelerator="gpu",
devices=4, # == gpus_per_node
num_nodes=2, # == nodes
strategy=builds(HydraDDP),
populate_full_signature=True,
)
Reproducibility¶
Seed each rank deterministically so a 64-GPU run reproduces a 1-GPU run:
from mushin import seed_everything_per_rank
seed_everything_per_rank(1234) # each rank: 1234 + global_rank
Metrics¶
MetricsCallback writes its output (fit_metrics.pt / test_metrics.pt, one per
stage) only on global rank 0, so the N ranks don't clobber the file on a shared
filesystem; load_experiment reads it back as usual.
Preemption & resume¶
When SLURM preempts (or times out) a cell's job, the sweep driver observes a
failed job: under the default on_error="raise" the sweep raises that error
once the jobs launched with it have ended; under on_error="nan" it records
the failure and finishes the rest. At a time limit, submitit usually raises
an error inside the task, which fails the cell as any task error does. A job
that ends without returning (an out-of-memory kill, a failed node,
scancel, a time limit that ends the process before Python can act, or an
error outside the task, such as the job failing to import it) fails its cell
the same way, and under on_error="nan" the cell's mushin_error.txt then
names the job's log. A job of one task that ends so after its cell has
completed (its result reached the driver too late, say) can keep the cell's
result: mushin reads it from the cell's directory. Otherwise (a job of
several tasks, or a result submitit could not save) the cell fails, its
record says so too, and a resume runs it again.
(mushin runs launcher="submitit_slurm" through its own subclass of the
plugin's launcher, which waits on every job, so a cell's .hydra/hydra.yaml
names mushin_submitit_slurm.)
Either way, recovery is one command: re-run the same sweep with
resume=True (same working_dir): completed cells are reused from their
sidecars, and only the preempted/missing cells re-run. On mushin's submitit
launchers, a resume submits jobs only for those cells (as sample= does only
for the cells it keeps, and a cache_dir hit none); the others are settled
before anything is queued. A cell is still submitted, and its job decides as
before, when the driver cannot judge it as the job would: its config reads
the environment (${oc.env:...}), or the sweep has Hydra callbacks, which
run in each job. Inside a long cell, use
the mushin_resume checkpoint contract so the re-run
continues from last.ckpt instead of epoch 0.
To have SLURM requeue preempted jobs automatically, pass the scheduler knobs
through submitit_slurm_config(**extra), e.g.
signal_delay_s=120 (submitit's grace signal before the kill, time to write a
checkpoint) and additional_parameters={"requeue": True}.
Two multi-rank cautions:
max_total_secondsis disabled for cells that run under a multi-rank launch, or as one of several tasks of a job (tasks_per_node > 1,submitit_localincluded). Each rank or task would check the deadline at its own moment: a rank that stops while its siblings train would hang DDP at rendezvous, and a late task could record the cell skipped. A warning is emitted in the job's log, and byrun()before it submits anything whenlauncher_configgives each cell several tasks. Bound multi-rank jobs with the scheduler's owntimeout_mininstead. Single-rank jobs, one per cell, share the sweep's deadline; one that SLURM preempts and requeues after the deadline still runs, since it started before it.- Every rank of a cell runs the task function, so per-cell files
(status/metrics/provenance sidecars) are written once per rank; the writes
are atomic;
MetricsCallback's.ptfiles are rank-0-only by design. The cell's result is rank 0's, but the cell fails when the task raises on any rank, or a rank dies, under eitheron_error.
Runbook (the merge gate)¶
To validate a real multi-node run on your cluster:
pip install "mushin-py[slurm]"on the cluster.- Build the launcher config with
submitit_slurm_config(nodes=2, gpus_per_node=<G>, partition=<P>, account=<A>). - Configure the Trainer with
devices=<G>,num_nodes=2,strategy=builds(HydraDDP). - Launch your hydra-zen workflow with
hydra/launcher=submitit_slurmand--multirun. - Confirm: the job completes,
fit_metrics.ptexists exactly once per job dir,load_experimentaggregates the results, and a mismatchedtasks_per_noderaises the fail-fast world-size error rather than hanging.
This is the condition for merging the multi-node branch.