Collectives
Print
Use your browser's Print command: navigation, header and table of contents are left out, and this page fits about one sheet. Distilled from chapter 13; FSDP and TorchTitan in chapter 14.
Placements and the collective each move costs
| From |
To |
Collective |
Shard(0) |
Replicate() |
all-gather |
Replicate() |
Shard(0) |
none (keep your slice) |
Partial() |
Shard(0) |
reduce-scatter |
Partial() |
Replicate() |
all-reduce |
Shard(0) |
Shard(1) |
all-to-all |
Time on a ring
\(V\) = largest buffer one rank passes, \(n\) = ranks on the ring, \(W\) = one ICI link, one direction: 45 GB/s on v4 and v5e, 90 GB/s on v5p, v6e and v7x.
| Collective |
Ring with wraparound |
One direction (no wraparound) |
| all-gather, reduce-scatter |
\(V(n-1)/(2nW)\) |
\(V(n-1)/(nW)\) |
| all-reduce |
\(2V(n-1)/(2nW)\) |
\(2V(n-1)/(nW)\) |
| all-to-all |
\(V\lfloor n^2/4 \rfloor/(2nW)\) |
\(V(n-1)/(2W)\) |
Below about 1 MB the per-hop latency (about 1 µs) dominates. No wraparound: v5e and v6e below 16 chips, v7x below a full cube. Over DCN, use the chip's DCN egress instead of \(W\).
Which strategy uses which
| Strategy |
Collectives |
On the critical path? |
Put it on |
| DP |
gradient all-reduce, once per step |
no, overlaps backward |
outer ICI axis or DCN |
| FSDP |
weight all-gather, gradient reduce-scatter, every layer |
gather can run one layer ahead |
ICI within a slice |
| TP |
activation all-gather and reduce-scatter, every layer |
yes |
fastest local ICI ring |
| CP, SP |
all-to-all or ring transfers in attention |
yes |
local ICI |
| EP |
all-to-all, ragged_all_to_all per MoE layer |
yes |
local ICI |
Order mesh dimensions from least to most communication-intensive. Across slices the outermost dimension crosses DCN: make it data parallel (HSDP: shard within, replicate across).
TorchTPU specifics
- Backend:
dist.init_process_group(backend="tpu_dist"). Collectives become StableHLO ops in the TPU program; no NCCL.
- Mesh:
ranks = torch.tpu.distributed.topology_aware_mesh(mesh_shape) (after init_process_group), then DeviceMesh("tpu", mesh=ranks, ...).
- Launch, one host:
eval $(python3 -m torch.tpu.distributed | sed 's/^/export /') then torchrun --nproc_per_node=$WORLD_SIZE train.py. Several hosts: add --nnodes and --node_rank. Or call torch.tpu.distributed.set_tpu_launch_env() in the parent before spawning.
- Every rank must run the same collectives with the same shapes in the same order, or the job hangs.
| Fusion mode |
Turn on |
Use for |
| skinny collectives (default) |
nothing |
correctness first; rank-dependent code |
@torch.tpu.distributed.spmd_safe |
decorate the step |
a step you can prove runs identically on every rank |
| handshake |
TORCH_TPU_SPMD_HANDSHAKE_STAGE=COMPILE_STAGE or DISPATCH_STAGE |
torch.compile with a guard; DISPATCH_STAGE tolerates one rank recompiling |
Failure signatures
| Symptom |
Fix |
| hang at a collective |
collective inside if rank == 0:, uneven shapes or shards: remove divergence, pad, or skinny collectives |
| TP or FSDP 10 to 100 times slower on multi-slice |
heavy axis crossing DCN: reorder mesh_shape or pass dcn_mesh_shape |
| collectives twice the model on a small slice |
no wraparound: model with one direction |
| workers see a one-device world |
topology variables not set before workers start |
DTensor.from_local warns about uneven shards |
pass global shape and stride; make sharded dims divisible |
| collectives never overlap compute |
default skinny modules: compile the step, then spmd_safe or the handshake |
all_to_all_single with uneven splits fails |
use dist.all_to_all with a list of tensors |