Skip to main content

Distributed Training

ONNX Runtime Training seamlessly integrates with popular distributed training frameworks to scale training across multiple GPUs and nodes. This guide covers setup and best practices for distributed training with ORTModule.

Supported Frameworks

ORTModule works with:
  • PyTorch DDP (DistributedDataParallel): Native PyTorch multi-GPU training
  • DeepSpeed: Memory-efficient training with ZeRO optimizer
  • DeepSpeed Pipeline Parallelism: Model parallelism for very large models
  • PyTorch FSDP: Fully Sharded Data Parallel
  • Horovod: Multi-framework distributed training

PyTorch DistributedDataParallel (DDP)

Basic Setup

Wrap your model with ORTModule before DDP:

Launch Script

Complete DDP Example

DeepSpeed Integration

DeepSpeed provides memory-efficient training through ZeRO optimizer stages.

Basic DeepSpeed Setup

DeepSpeed Configuration File

Launch with DeepSpeed

DeepSpeed Pipeline Parallelism

For models too large for single GPU, use pipeline parallelism:

Data Loading Best Practices

Use DistributedSampler

Ensure each process gets different data:

Load Balancing for Variable Length Sequences

For NLP and speech tasks with variable length inputs:
This helps avoid the “straggler problem” where some GPUs finish faster than others.

Environment Variables for Distributed Training

Essential Variables

ORTModule Distributed Settings

Checkpoint Saving and Loading

Save Checkpoints (Rank 0 only)

Testing and Debugging

Test Distributed Setup

Enable Detailed Logging

Performance Tips

  1. Wrap Order: Always wrap with ORTModule before DDP/DeepSpeed
  2. Batch Size: Use largest batch size that fits in memory
  3. Gradient Accumulation: Simulate larger batches with accumulation
  4. Mixed Precision: Enable FP16 training for faster computation
  5. Communication Backend: Use NCCL for GPU training, Gloo for CPU
  6. Pin Memory: Enable pin_memory=True in DataLoader
  7. Persistent Workers: Set persistent_workers=True to avoid respawning
  8. NCCL Tuning: Optimize NCCL settings for your network topology

Common Issues

Hanging on Initialization

Out of Memory

Gradient Synchronization Issues

Next Steps

ORTModule

Learn more about ORTModule features

Training Overview

Explore other training options