Choosing Gloo or NCCL for Distributed PyTorch Training in PyTorchJob
PyTorch distributed training uses a backend to handle communication between training processes. The two most common backends are Gloo and NCCL.
As a rule of thumb:
Use Gloo for CPU-based distributed training.
Use NCCL for GPU-based distributed training with NVIDIA GPUs.
Use Gloo when NCCL is unavailable, unsupported, or when debugging distributed startup problems.
PyTorch documents gloo and nccl as supported distributed backends, with Gloo commonly used for CPU tensors and NCCL used for CUDA tensors. NCCL is available when PyTorch is built with CUDA support.
When to use Gloo
Use the Gloo backend when:
running simple distributed tests
debugging networking or rendezvous issues
the environment does not have NVIDIA GPUs
NCCL is not available in the PyTorch image
the cluster does not have working GPU-to-GPU communication
the cluster has different GPU cards
Example:
import os
import torch.distributed as dist
backend = os.getenv("BACKEND", "gloo")
dist.init_process_group(
backend=backend,
init_method="env://"
)
Gloo is usually easier to start with because it does not require CUDA, NVIDIA drivers, or NCCL libraries.
When to use NCCL
Use the NCCL backend when:
training on NVIDIA GPUs
using
DistributedDataParallelrunning multi-GPU or multi-node GPU training
performance matters
your containers have CUDA-enabled PyTorch
the Kubernetes nodes have NVIDIA GPU drivers and device plugin configured
Example:
import os
import torch
import torch.distributed as dist
backend = os.getenv("BACKEND", "nccl")
dist.init_process_group(
backend=backend,
init_method="env://"
)
local_rank = int(os.getenv("LOCAL_RANK", "0"))
torch.cuda.set_device(local_rank)
NCCL is normally the preferred backend for GPU training because it is optimized for NVIDIA GPU communication.
How to set the backend in a PyTorchJob
A MLOps Studio PyTorchJob runs distributed PyTorch workloads on Kubernetes. The backend is usually not set directly by the PyTorchJob controller. Instead, you pass it to your training code through an environment variable, for example BACKEND=gloo or BACKEND=nccl.
Example: CPU training with Gloo
apiVersion: kubeflow.org/v1
kind: PyTorchJob
metadata:
name: pytorch-gloo-example
spec:
pytorchReplicaSpecs:
Master:
replicas: 1
restartPolicy: OnFailure
template:
spec:
containers:
- name: pytorch
image: your-pytorch-image:latest
command:
- python
- /workspace/train.py
env:
- name: BACKEND
value: gloo
Worker:
replicas: 2
restartPolicy: OnFailure
template:
spec:
containers:
- name: pytorch
image: your-pytorch-image:latest
command:
- python
- /workspace/train.py
env:
- name: BACKEND
value: gloo
Use this when the job does not request GPUs.
Example: GPU training with NCCL
apiVersion: kubeflow.org/v1
kind: PyTorchJob
metadata:
name: pytorch-nccl-example
spec:
pytorchReplicaSpecs:
Master:
replicas: 1
restartPolicy: OnFailure
template:
spec:
containers:
- name: pytorch
image: your-cuda-pytorch-image:latest
command:
- python
- /workspace/train.py
env:
- name: BACKEND
value: nccl
- name: NCCL_DEBUG
value: INFO
resources:
limits:
nvidia.com/gpu: 1
Worker:
replicas: 2
restartPolicy: OnFailure
template:
spec:
containers:
- name: pytorch
image: your-cuda-pytorch-image:latest
command:
- python
- /workspace/train.py
env:
- name: BACKEND
value: nccl
- name: NCCL_DEBUG
value: INFO
resources:
limits:
nvidia.com/gpu: 1
Use this when each training pod has access to the same NVIDIA GPUs.
Training script example
The same training script can support both backends:
import os
import torch
import torch.distributed as dist
def setup_distributed():
backend = os.getenv("BACKEND", "nccl")
if backend == "nccl":
local_rank = int(os.getenv("LOCAL_RANK", "0"))
torch.cuda.set_device(local_rank)
dist.init_process_group(
backend=backend,
init_method="env://"
)
rank = dist.get_rank()
world_size = dist.get_world_size()
print(f"Distributed initialized: backend={backend}, rank={rank}, world_size={world_size}")
def cleanup_distributed():
if dist.is_initialized():
dist.destroy_process_group()
if __name__ == "__main__":
setup_distributed()
# training code here
cleanup_distributed()
Practical recommendation
For MLOps Studio PyTorchJob:
Scenario |
Backend |
|---|---|
CPU-only training |
|
Debugging distributed startup |
|
Single-node multi-GPU training |
|
Multi-node NVIDIA GPU training |
|
CUDA image without working NCCL/networking |
start with |
Production GPU training |
|
Common NCCL environment variables
For GPU jobs, these variables are often useful:
env:
- name: BACKEND
value: nccl
- name: NCCL_DEBUG
value: INFO
- name: NCCL_SOCKET_IFNAME
value: eth0
If your cluster uses a different network interface, change NCCL_SOCKET_IFNAME accordingly.
For example:
ip addr
Then set the correct interface name, such as:
- name: NCCL_SOCKET_IFNAME
value: ens192
Summary
Use Gloo when you are training on CPUs or troubleshooting distributed communication. Use NCCL when you are training on NVIDIA GPUs and need high performance. In a PyTorchJob, the backend is usually passed as an environment variable and consumed by the training script when calling torch.distributed.init_process_group().