Multi-GPU training
1
Create an Accelerate config
Run the interactive configuration wizard:Answer the questions for your multi-GPU or multi-node setup.
2
Launch distributed training
- Processes its own batch of data
- Computes loss and gradients for that batch
- Shares gradient updates across all GPUs
per_device_train_batch_size and gradient_accumulation_steps accordingly:
DeepSpeed ZeRO
DeepSpeed provides memory optimizations through the ZeRO (Zero Redundancy Optimizer) family of stages. TRL provides predefined accelerate configs you can use directly:
Pass the profile name via
--accelerate_config in the TRL CLI:
FSDP (Fully Sharded Data Parallel)
TRL also supports FSDP via predefined Accelerate config profiles:Sequence parallelism for long-context training
Sequence Parallelism (also called Context Parallelism) splits the sequence dimension across multiple GPUs, enabling training with sequences longer than what fits on a single GPU. TRL supports two implementations:Ring Attention (FSDP2)
Uses ring-based P2P communication. Best for extremely long sequences (1M+ tokens) and models with few attention heads. Requires Accelerate 1.11.0+ and FSDP2.
ALST/Ulysses (DeepSpeed)
Uses attention head parallelism. Best for high-bandwidth interconnects (NVLink, InfiniBand) and moderate sequence lengths (up to ~500k tokens). Requires DeepSpeed 0.18.1+ and Accelerate 1.12.0+.
Ring Attention (FSDP2)
Use the provided accelerate config (e.g.context_parallel_2gpu.yaml):
max_length refers to the global sequence length. The framework automatically splits it into micro-sequences per GPU based on cp_size. With max_length=8192 and cp_size=4, each GPU processes 2048 tokens.ALST/Ulysses (DeepSpeed)
Use the provided accelerate config (e.g.alst_ulysses_4gpu.yaml):
2D parallelism scaling reference
Ensure
dp_replicate_size × dp_shard_size × sp_size = num_processes.Multi-node training
When a single machine does not have enough GPUs, scale training across multiple machines (nodes).Accelerate config for multi-node
Create amulti_node.yaml config:
Replace
10.0.0.1 with the actual IP address of the rank 0 (main) node.Launching
- Manual launch
- SLURM (HPC)
Run on each node:
distributed_type: DEEPSPEED and adding a deepspeed_config block. See the DeepSpeed integration guide.