SLURM
Pytorch
Multi-node Training
GPU Training
Parallel Computing

How SLURM and Pytorch handle multi-node multi-gpu training together

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Scientific computing and deep learning tasks often require vast computational resources, especially when training complex models or handling large datasets. A combination of Slurm Workload Manager for job scheduling and PyTorch for deep learning can efficiently leverage multi-node, multi-GPU environments to speed up computations and training processes.

What is SLURM?

SLURM (Simple Linux Utility for Resource Management) is an open-source cluster management and job scheduling system designed for Linux clusters of any size. It provides three key capabilities:

  • Allocation of exclusive and/or non-exclusive access to resources (computer nodes) to users for some duration of time so they can perform work.
  • Providing a framework for starting, executing, and monitoring work (normally a parallel job) on the set of allocated nodes.
  • Arbitrating contention for resources by managing a queue of pending jobs.

What is PyTorch?

PyTorch is an open-source machine learning library based on the Torch library, used for applications such as computer vision and natural language processing, primarily developed by Facebook's AI Research lab (FAIR). PyTorch provides two high-level features:

  • Tensor computing (like NumPy) with strong GPU acceleration
  • Deep neural networks built on a tape-based autograd system

Multi-Node Multi-GPU Training in PyTorch

PyTorch supports multi-GPU training by using torch.nn.DataParallel or torch.nn.parallel.DistributedDataParallel. For multi-node training, DistributedDataParallel (DDP) is preferred as it supports each process running on a node and manages parallelism across the nodes.

Integration of SLURM and PyTorch

When dealing with multi-node and multi-GPU training tasks, integrating SLURM with PyTorch involves several steps:

  1. SLURM Setup for Job Scheduling: Define a SLURM batch job script that specifies job parameters, including the number of nodes, tasks per node, GPUs per task, and other resource requirements.
  2. Environment Configuration: On each node, the necessary environment variables must be set, particularly those that define GPU visibility and parallel execution settings, such as CUDA_VISIBLE_DEVICES.
  3. Launching PyTorch Processes: Typically, each SLURM task will correspond to one PyTorch process. Each process will operate on its node and GPU(s) as specified by the SLURM job configuration. The script often uses torch.distributed.launch to launch multiple worker processes for distributed training.
  4. Initialization of PyTorch Distributed Backend: Each PyTorch process needs to initialize the distributed backend (usually NCCL) with the respective world size (total number of processes), rank (process identifier), and communication protocol.

Example of SLURM Script for PyTorch Multi-Node Job

bash
1#!/bin/bash
2#SBATCH --job-name=pytorch_mnist    # Job name
3#SBATCH --nodes=2                  # Number of nodes
4#SBATCH --ntasks-per-node=4        # Number of tasks per node
5#SBATCH --gres=gpu:2               # Number of GPUs per node
6#SBATCH --time=02:00:00            # Time limit hrs:min:sec
7#SBATCH --output=result_%j.out     # Standard output and error log
8
9srun python -m torch.distributed.launch \
10    --nproc_per_node=2 \
11    --nnodes=2 \
12    --node_rank=$SLURM_NODEID \
13    --master_addr="192.168.1.1" \
14    --master_port=1234 \
15    your_training_script.py

Key Considerations and Best Practices

  • Ensure that all nodes have access to the necessary data sets and the PyTorch environment.
  • Tune the number of workers, batch sizes, and learning rate according to the increase in computing resources to ensure efficient scaling.
  • Adjust the master_addr and master_port in the PyTorch script for establishing communication among the nodes.

Summary Table

FeatureSLURMPyTorch
Primary FunctionResource management and job schedulingDeep learning model development & training
Best Used ForMulti-node, multi-job managementComputational graph construction, GPU acceleration
Requires Explicit Parallelism?Yes, users must define job parallelismYes, through DataParallel or DistributedDataParallel
GPU SupportThrough resource allocation tagsInherent in the library with CUDA integration

This combination harnesses the robust, manageable job scheduling capabilities of SLURM and the flexible, high-performance computing power of PyTorch, making it ideal for high-scale deep learning tasks.


Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.