Skip to main content
TRL trainers use Accelerate to enable distributed training across multiple GPUs or nodes.

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

This automatically distributes the workload across all available GPUs.
You can also use the example config files provided in the TRL examples folder:
Under the hood, Accelerate creates one model replica per GPU. Each process:
  • Processes its own batch of data
  • Computes loss and gradients for that batch
  • Shares gradient updates across all GPUs
The effective batch size is:
To maintain a consistent effective batch size when scaling to more GPUs, adjust per_device_train_batch_size and gradient_accumulation_steps accordingly:
Having one model per GPU can cause high memory usage for large models. Use DeepSpeed for model sharding, ZeRO Redundancy Optimizer, and CPU/NVMe offloading.

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:
Or pass a path to a custom Accelerate YAML config:
For a full DeepSpeed integration guide, see DeepSpeed Integration.

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):
With the corresponding training configuration:
Launch with:
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):
With the corresponding training configuration:
Launch a complete example with 4 GPUs:

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 a multi_node.yaml config:
Replace 10.0.0.1 with the actual IP address of the rank 0 (main) node.

Launching

Run on each node:
You can combine multi-node training with DeepSpeed by setting distributed_type: DEEPSPEED and adding a deepspeed_config block. See the DeepSpeed integration guide.