torchrun utility. This step builds on the previous section by introducing a multi-node, multi-GPU job that more closely exemplifies a real-world distributed training scenario. By the end, you’ve submitted a PyTorch training job across multiple nodes and GPUs, and you understand how the equivalent workflow differs for TensorFlow.
This section helps you understand how to use Slurm with PyTorch or TensorFlow.
Submit a training job using PyTorch
Use thetorchrun launcher to schedule a multi-node, multi-GPU PyTorch job on Slurm. The torchrun launcher is a superset of the launch utility from the torch.distributed package, which provides PyTorch support and communication primitives for multi-process parallelism across multiple GPUs running on one or more nodes.
The directives for Slurm and the parameters for
torchrun can be confused with one another. To keep them straight, remember this distinction:- Slurm allocates the nodes to be used for PyTorch.
torchrunspawns the jobs within a single node.
Example using PyTorch
The following annotated example script shows a multi-node distributed training job using PyTorch. The goal is to define the allocation and ensure that thesrun command runs once on each node. Replace [USERNAME] with your username.
Example script with annotations
#SBATCH --ntasks-per-node=1: Thissbatchparameter is important when running multi-GPU PyTorch jobs with Slurm.- Slurm manages nodes, and
torchrunmanages what runs on them. If you omit this line, the default number of tasks per node is1. - However, setting this number higher might cause problems. As a best practice in this configuration, explicitly set the number of tasks to
1, as shown here. - For most other applications, set this number to the total number of desired tasks per node.
- Slurm manages nodes, and
export OMP_NUM_THREADS=1: An environment variable that sets the number of threads that run within an OpenMP job.- Here, it’s set to
1explicitly. - If you don’t set it, PyTorch relies on OpenMP’s default behavior, which may use as many threads as there are physical cores.
- Explicitly setting this parameter avoids accidentally launching more threads than there are processors on a node.
- Here, it’s set to
Invoking
python -m torch.distributed.run, as the preceding script does, is equivalent to invoking torchrun. Invoke torchrun using the same arguments on each node that participates in the training run, which the preceding script ensures.Rendezvous backend
The Rendezvous Backend in PyTorch coordinates how distributed workers find one another before beginning a distributed training job. As specified in the Rendezvous Backend documentation, multi-node training requires the following parameters:--rdzv-id: A unique job ID, shared by all nodes participating in the job.--rdzv-backend: An implementation of thetorch.distributed.elastic.rendezvous.RendezvousHandlerinterface.--rdzv-endpoint: The endpoint where the rendezvous backend runs, usually inhost:portformat.
Rendezvous parameters
--rdzv-backend is the c10d backend, the --rdzv-endpoint is set to the $RDZV_HOST:$RDZV_PORT combination generated earlier in the script, and the --rdzv-id is set to $SLURM_JOB_ID, which is a single unique identifier for the entire job and is the same on both nodes.
The following backends are supported without additional setup:
c10d(recommended)etcd-v2andetcd(legacy)
To use
etcd-v2 or etcd, set up an etcd server with the v2 API enabled using the --enable-v2 parameter.Launch the job in a container
To launch the same job inside a container, use a similar script with a few key differences:- Don’t use a virtual environment.
- A container already has the right components installed and configured, so the script doesn’t need to handle these installations.
- The
sruncommand specifies the container by targeting its squash file.
[USERNAME] with your username.
Example srun using a container
Inside an enroot or Pyxis container,
/tmp is backed by tmpfs (RAM), not the Node’s local NVMe. Writing large amounts of temporary data to /tmp can fill memory and cause ENOSPC errors even when disk metrics look clean. To keep temporary files on NVMe, set TMPDIR to an NVMe-backed path. For details, see Node-local storage and /tmp on Slurm nodes.For more information about developing containers, see Develop containers with Pyxis in the previous section.
TensorFlow launch utilities with Slurm
If you use TensorFlow instead of PyTorch, the launch workflow differs. With TensorFlow, some of the logic from the Slurm script moves into the Python code.[USERNAME] with your username.