skip to content

On a Slurm cluster, when do you launch a 64-node PyTorch job as one torchrun per node versus one srun task per GPU?

level: middleimportance: should knowfreq 14%

answer

  1. who supervises the processes
  2. --ntasks-per-node=1 versus 8
  3. rendezvous host from the node list
  4. SLURM_PROCID and SLURM_LOCALID as ranks
  5. --gpus-per-task binds one GPU per task

basics

~20 s

Run one torchrun per node (srun --ntasks-per-node=1) when you want torchrun's c10d rendezvous and in-place restarts; run one srun task per GPU (--ntasks-per-node=8) when Slurm should own every process and you map SLURM_PROCID and SLURM_LOCALID to ranks yourself.

solid answer

~30 s

Both forms start the same 512 processes; they differ in who supervises them. With `srun --ntasks-per-node=1 torchrun --nnodes=64 --nproc-per-node=8 --rdzv-backend=c10d --rdzv-id=$SLURM_JOB_ID --rdzv-endpoint=$head:29400`, each torchrun agent spawns eight workers, sets `RANK`, `LOCAL_RANK` and `WORLD_SIZE` for them and can restart them under `--max-restarts`; `$head` is the first line of `scontrol show hostnames "$SLURM_JOB_NODELIST"`. With `srun --ntasks-per-node=8` running the script directly, Slurm is the launcher: you export `RANK=$SLURM_PROCID`, `LOCAL_RANK=$SLURM_LOCALID`, `WORLD_SIZE=$SLURM_NTASKS` plus `MASTER_ADDR`/`MASTER_PORT`, and nothing restarts a failed task in place. Watch the GPU binding: with `--gpus-per-task=1` each task sees only its own GPU, so the device index is 0, not `SLURM_LOCALID`.

code

bash · 17 lines
bash
#!/bin/bash
#SBATCH --job-name=pretrain-64n
#SBATCH --nodes=64
#SBATCH --ntasks-per-node=1
#SBATCH --gpus-per-node=8
#SBATCH --requeue

head=$(scontrol show hostnames "$SLURM_JOB_NODELIST" | head -n 1)

srun torchrun \
  --nnodes="$SLURM_JOB_NUM_NODES" \
  --nproc-per-node=8 \
  --rdzv-backend=c10d \
  --rdzv-id="$SLURM_JOB_ID" \
  --rdzv-endpoint="$head:29400" \
  --max-restarts=3 \
  train.py --checkpoint-dir /ckpt/pretrain-64n

go deeper

for a junior

Know that something must start one process per GPU and tell it its rank and world size, and that on Slurm this is either torchrun per node or srun per GPU.

for a middle

Write both launch lines from memory: tasks per node, where the rendezvous host comes from, which SLURM_* variables become RANK, LOCAL_RANK and WORLD_SIZE.

for a senior

Explain what each form does when a node dies, including Slurm's default of killing the allocation, --no-kill, --requeue keeping the job ID, and the per-task GPU-binding trap.

for a principal

Pick the form a platform standardises on from how failures should be handled: restarts inside the allocation, or ending the step and letting the scheduler requeue.

## What a launcher does on Slurm Slurm allocates the nodes; something still has to start one Python process per GPU, tell each process its **rank** (its number among all processes), the **world size** (how many there are) and where to meet the others, and decide what happens when one of them dies. On a Slurm cluster that "something" is either **torchrun** (PyTorch's elastic launcher, one agent per node) or **srun itself** (one Slurm task per GPU). For a 64-node job with eight GPUs per node, both give 512 processes; the choice is about who supervises them. ## Form 1: one torchrun per node `srun --ntasks-per-node=1` starts exactly one task per node, and that task is `torchrun`: - `--nnodes=$SLURM_JOB_NUM_NODES` and `--nproc-per-node=8` give 64 x 8 = 512 workers. - `--rdzv-backend=c10d` is the rendezvous backend the PyTorch 2.14 docs recommend; it assigns node ranks itself, so no node needs to know its own number. - `--rdzv-endpoint=$head:29400` names any one node of the allocation as the rendezvous host; `head=$(scontrol show hostnames "$SLURM_JOB_NODELIST" | head -n 1)` turns Slurm's compressed node list (`node-[0-63]`) into one host name per line and takes the first. - `--rdzv-id=$SLURM_JOB_ID` gives every agent the same unique group id. - Each agent then sets `RANK`, `LOCAL_RANK`, `WORLD_SIZE`, `MASTER_ADDR` and `MASTER_PORT` for its eight workers and supervises them, which is what makes `--max-restarts` possible. If you keep torchrun's default `--rdzv-backend=static` instead, each node must be given `--node-rank=$SLURM_NODEID` and `--master-addr`, because nothing hands out node ranks dynamically. ## Form 2: one srun task per GPU `srun --ntasks-per-node=8 python train.py` makes Slurm the launcher. Each task exports what torchrun would have set: - `RANK=$SLURM_PROCID` (Slurm's global task id, 0 to 511) - `LOCAL_RANK=$SLURM_LOCALID` (the task's id on its node, 0 to 7) - `WORLD_SIZE=$SLURM_NTASKS` - `MASTER_ADDR` from the same `scontrol show hostnames` line, and a fixed `MASTER_PORT` There is no per-node agent, so nothing restarts a crashed task in place. With `-K1` / `--kill-on-bad-exit=1` Slurm ends the whole step as soon as one task exits non-zero; without the option the cluster's `KillOnBadExit` setting decides, and if it does not kill the step, the survivors wait in their next collective until the process group's own timeout ends them. ## The two forms side by side | | one torchrun per node | one srun task per GPU | |---|---|---| | srun tasks per node | 1 | 8 | | who sets `RANK` / `WORLD_SIZE` | torchrun | your script, from `SLURM_PROCID` / `SLURM_NTASKS` | | rendezvous | c10d on `$head:29400` | `MASTER_ADDR` / `MASTER_PORT` you export | | restart without resubmitting | yes, `--max-restarts` | no | | elastic node count | `--nnodes=MIN:MAX` | no | | one less layer to debug | no | yes | ## The GPU-binding trap Slurm sets `CUDA_VISIBLE_DEVICES` for every job step. If the step asks for `--gpus-per-task=1`, Slurm binds one GPU to each task, so each process sees exactly one device and that device is index 0. A script that calls `torch.cuda.set_device(int(os.environ["LOCAL_RANK"]))` then fails on every task but the first with an invalid device ordinal. Either request `--gpus-per-node=8` and index by `SLURM_LOCALID`, or keep per-task binding and use device 0. The opposite mistake is mixing the forms: `srun --ntasks-per-node=8 torchrun --nproc-per-node=8` starts eight agents per node and 64 workers on each node, all fighting over eight GPUs. ## What happens when a node dies at hour 40 1. By default Slurm terminates the **entire job allocation** if any of its nodes fails; `--no-kill` keeps the allocation for the surviving nodes, and the job then owns fault tolerance itself. 2. If the batch job was submitted with `--requeue` (and the cluster's `JobRequeue` policy allows it), Slurm can requeue it after a node failure; the batch script starts again **from its beginning with the same job ID**, so `--rdzv-id=$SLURM_JOB_ID` still matches across the new attempt. 3. Either way the training script must find its latest checkpoint and resume from it; neither launcher keeps any in-memory state. The practical rule: choose torchrun-per-node when you want restarts inside the allocation and an elastic node range; choose task-per-GPU when the site's tooling already expects one Slurm task per rank and a failure should simply end the step so the batch job can be requeued.

  • Why pass --rdzv-id=$SLURM_JOB_ID instead of a fixed string such as pretrain?
    The rendezvous id names the worker group every agent joins. A fixed string lets two jobs that happen to share a rendezvous host and port collide. `SLURM_JOB_ID` is unique per job and, because a requeued batch job keeps its job ID, every agent of the restarted attempt still agrees on it.
  • What changes if you keep torchrun's default static rendezvous backend on Slurm?
    With `--rdzv-backend=static` nothing assigns node ranks, so each node's torchrun needs `--node-rank=$SLURM_NODEID` plus `--master-addr` and `--master-port` pointing at node 0. The node count and node ranks are fixed up front, which gives up the dynamic membership the c10d backend provides.

  • srun --ntasks-per-node=8 combined with torchrun --nproc-per-node=8 still gives eight workers per node.
  • Slurm restarts a crashed task in place without any extra configuration.
  • SLURM_LOCALID is always the right CUDA device index, whatever GPU binding the step uses.
  • MASTER_ADDR can stay localhost on every node of a multi-node job.
  • When one node fails, Slurm by default keeps the other 63 nodes running the job.

Assumes PyTorch 2.14 (torchrun's default --rdzv-backend is static; c10d is the recommended backend) and Slurm 26.05 option names.