Skip to main content
In this section, you submit a training job to Slurm using the PyTorch 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 the torchrun 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.
  • torchrun spawns 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 the srun command runs once on each node. Replace [USERNAME] with your username.
The following annotated version of the script shows the same code:
Example script with annotations
Most of these parameters are similar to those used in the previous section, with a few notable exceptions:
  • #SBATCH --ntasks-per-node=1: This sbatch parameter is important when running multi-GPU PyTorch jobs with Slurm.
    • Slurm manages nodes, and torchrun manages what runs on them. If you omit this line, the default number of tasks per node is 1.
    • 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.
  • 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 1 explicitly.
    • 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.
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 the torch.distributed.elastic.rendezvous.RendezvousHandler interface.
  • --rdzv-endpoint: The endpoint where the rendezvous backend runs, usually in host:port format.
The preceding script implements these parameters as follows:
Rendezvous parameters
In this case, the --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-v2 and etcd (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 srun command specifies the container by targeting its squash file.
Replace [USERNAME] with your username.
Example srun using a container
You can load this container image from a shared filesystem or pull it from a registry.
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.
The following examples aren’t full Python code for TensorFlow and don’t run as presented. They are for example purposes only.
To launch this job, use the following Slurm script. Replace [USERNAME] with your username.
After you submit a job to Slurm, it’s visible through Grafana and from the command line. In the next section, you monitor running jobs using these methods.
Last modified on July 7, 2026