Unit 5 of 13 · about 3 hours · implement
All-to-all semantics and token exchange
Expert and sequence-parallel designs move distinct data to distinct destinations. That pattern is all-to-all, and it stresses the network in the opposite way from an average.
You'll be able to
- Explain all-to-all from input and output ownership on each rank.
- Implement all-to-all on several CPU processes and check the per-rank mix against an oracle.
- Contrast token exchange with gradient averaging and state which later layers route tokens.
Covers
Read
In this order, only the parts listed
0 of 3 done
- All-to-allMachine Learning Engineering Open Book - Communication Patterns
All-to-all with the
dist.all_to_allcall and the Ulysses sequence-parallel and MoE expert-parallel uses - All-to-all contention and the MoE exampleMachine Learning Systems Volume II - Collective Communication
Choosing the Vehicle with The six core primitives: the all-to-all row with its contention note, plus the MoE message-size example
- All-to-all meaning and the two-step exchangeBuilding a Distributed Training Framework from First Principles
From 9:57:55 to about 10:00:00 (hours:minutes:seconds) for the all-to-all meaning, then https://www.youtube.com/watch?v=XoGvCBRnwLs&t=69724s from 19:22:04 to about 19:24:00 for why one exchange carries counts and a second carries data
Watch the semantics segments only. Skip the expert-parallel implementation around them; it belongs to the MoE section.
Focus on
- All-to-all takes a list of per-destination inputs on each rank. Rank receives chunk from every source, concatenated in source-rank order, so each rank ends with a different mix.
- All-reduce gives every rank the same aggregate. All-to-all gives each rank a different slice. Token routing needs the second pattern.
- All-to-all fan-out grows as logical pairs, so contention bounds it where per-node bytes bound all-reduce. Small routed messages are latency-bound.
- The gloo backend runs
dist.all_to_allon CPU in PyTorch 2.14, although the backends table marks it unsupported. Test it instead of routing around it.
Common mistakes · 3
- Assuming all-to-all concatenates like all-gather. All-gather gives every rank the same concatenation; all-to-all gives each rank its own chunk from each source.
- Passing single tensors instead of per-rank lists. Both the input and the output of
dist.all_to_allare lists with one entry per rank. - Passing uneven per-rank lists to
dist.all_to_all. On gloo that aborts the process with "invalid tensor size". Ragged routing exchanges counts first and then callsdist.all_to_all_singlewith input and output split sizes, or pads every chunk to one size.
Try it yourself
Closed-book, before checking anything
[[1, 2, 3, 4], [5, 6, 7, 8]]: rank 0 receives [1, 2, 5, 6] and rank 1 receives [3, 4, 7, 8]. Then run dist.all_to_all on 2 gloo CPU processes and match both ranks.Quiz yourself
Answer first, then mark it. Missed ones go to Review.
0 got · 0 missed
1.Two ranks hold [1, 2, 3, 4] and [5, 6, 7, 8]. What does each rank hold after all-to-all?Answer
Rank 0 holds [1, 2, 5, 6] and rank 1 holds [3, 4, 7, 8]: chunk 0 from each source lands on rank 0, chunk 1 on rank 1.
2.Why does expert routing use all-to-all rather than all-reduce?Answer
Each token must reach one specific expert rank, so destinations need distinct payloads. All-reduce would give every rank the same sum, which cannot route.
3.Why is all-to-all contention-sensitive while ring all-reduce keeps constant per-node bytes?Answer
All-to-all opens a logical pair per rank pair, so pairs grow quadratically while all-reduce only talks to ring neighbours with constant bytes per node.
4.Rank 0 sends 3 tokens to rank 0 and 1 to rank 1; rank 1 sends 2 to rank 0 and 2 to rank 1. What split sizes does each rank pass to
all_to_all_single, and why must counts move first?AnswerRank 0 passes input splits [3, 1] and output splits [3, 2]; rank 1 passes input splits [2, 2] and output splits [1, 2]. Output splits are the other ranks' input counts, so a first all-to-all of counts tells each rank how much to receive before the data exchange.
Practice
- TensorTonic: Simulate Distributed Collectives
The
all_to_allpublic case: chunk from each source concatenated in source-rank orderSolve the
all_gatherandreduce_scattercases in the duality unit first, then finish with this case.
You're done when
Your hand result, the TensorTonic case and the gloo run all agree on both ranks, and you can contrast the all-to-all mix with the all-reduce aggregate without notes.
Only when you can do this without notes.
Leads to
Notes
Resources · 3 for this unit, 12 more in the section
- Machine Learning Systems Volume II - Collective CommunicationML Systems Textbook · primary
Read From Parallelism to Communication Patterns, Network Performance Modeling, Collective Operation Primitives, AllReduce Algorithms, Hierarchical Communication and Communication-Computation Overlap. Use its latency and bandwidth model to derive one communication budget, and return to The Distributed Training Step in https://mlsysbook.ai/vol2/distributed_training/distributed_training.html during DT-3.
- Building a Distributed Training Framework from First PrinciplesUmar Jamil · required
Watch only 04:32:59 for autograd and distributed mathematics, 05:04:19 for distributed graphs and DDP, 09:48:52 for collectives, and 10:49:41 for DeviceMesh, DDP and FSDP. Pause after each segment to reproduce the rank and tensor transitions with CPU torchrun; the nearly twenty-hour full video is not required.
- Machine Learning Engineering Open Book - Communication PatternsStas Bekman · required
Read
network/comms.mdfrom point-to-point through ring all-reduce, thennetwork/README.mdandnetwork/debug/README.mdselectively. Derive bytes and steps before running anything;network/benchmarks/all_reduce_bench.pyneeds two or more GPUs and reports bus bandwidth.
Also in this section
- How to Scale Your Model - Sharded Matrices and GPU CollectivesGoogle DeepMind · required
Work the collective-cost problems in Part 3 (all-gather, reduce-scatter, all-reduce and all-to-all volumes, latency versus bandwidth) before opening each answer, then read How Do Collectives Work on GPUs in Part 12 at https://jax-ml.github.io/scaling-book/gpus. The worked answers use TPU link constants; redo them with your own bandwidth and latency.
- Ultra-Scale Playbook - A0: Parallel Programming Crash CourseHugging Face · required
Read A0 from processes and ranks through point-to-point communication, broadcast, reduce, all-reduce, all-gather, reduce-scatter, the ring all-reduce and asynchronous operations. Reproduce every small
torch.distributedexample on Gloo and annotate tensor ownership before moving to higher parallelism. - CS336 Spring 2025 - Lecture 8 Parallelism (executable lecture)Stanford University · support
Read
lecture_08.pyPart 1 (collective operations,torch.distributed, all-reduce and reduce-scatter benchmarks) and skim Part 2 (data, tensor and pipeline parallelism sketches). Run its CPU examples before the Playbook appendix. - Picotron - Process Groups and Communication FilesHugging Face · required
Trace picotron/process_group_manager.py plus tp_communications.py, pp_communications.py and cp_communications.py. Draw every group, rank coordinate and tensor transfer for one 2 x 2 layout, then add a failing collective-order test in your own project.
- Picotron Tutorial - Steps 1 to 3 and Part 1 VideoHugging Face · support
Follow
step2_process_group_managerandstep3_dataloaderwith their diffs and the Part 1 video (model, process group manager and dataloader). Rebuild the rank grid yourself before reading the solution. - Writing Distributed Applications with PyTorchPyTorch · required
Work through process-group setup, point-to-point communication and the collective operations, then adapt the examples to test broadcast, all-reduce, all-gather, reduce-scatter and all-to-all on Gloo with several CPU processes. Check backend support in the 2.14
torch.distributedreference; Gloo all-to-all runs on CPU in 2.14. - CS336 Assignment 2 (Spring 2026) - Sections 5.1 and 8.1Stanford University · required
Complete
distributed_communication_single_nodewith the Section 5.1.1 benchmarking practices on as many processes as you have, then derive the ring all-gather, reduce-scatter and all-reduce costs in Section 8.1 and solvealternate_ring_all_reduce. - TensorTonic - Collective Simulation ProblemsTensorTonic · support
Solve Simulate Distributed Collectives and Calculate Collective Effective Bandwidth. The bandwidth problem counts ring passes; reconcile it with your alpha-beta model and with bus bandwidth before comparing numbers.
- AI Systems Performance Engineering - Chapter 4 Distributed Networking CommunicationChris Fregly (O'Reilly) · support
Read NCCL for Distributed Multi-GPU Communication, Topology Awareness in NCCL, NCCL Communication Algorithms, NCCL Communicator Lifecycle and Environment Gotchas, and Profiling and Debugging NCCL. With two or more GPUs, run
code/ch04/nccl_benchmark.pyfrom the companion repository and label the exact topology. - nccl-tests and its performance notesNVIDIA · support
With two or more GPUs, run
all_reduce_perfover a message-size sweep and readdoc/PERFORMANCE.mdfor algorithm bandwidth versus bus bandwidth. Without them, use the document to interpret published numbers. - PyTorch 2.14 `torch.distributed` referencePyTorch · reference
Look up the backends table,
init_process_groupwith timeout anddevice_id,new_groupandnew_subgroups, theWorkandasync_opcontract,monitored_barrier,destroy_process_groupandTORCH_DISTRIBUTED_DEBUG. Use it for API lookup beside the tutorial first pass. - PyTorch 2.14 torchrun (elastic launch) referencePyTorch · reference
Read Single-node multi-worker through the elastic membership rows for
RANK,WORLD_SIZEandLOCAL_RANK, plus flags--standalone,--nnodes,--nproc-per-node,--rdzv-backendand--rdzv-endpoint. Launch every CPU run withtorchrun --standaloneand a distinct port per run.