Unit 6 of 13 · about 4 hours · explain, implement

Barriers, collective ordering and shape and dtype agreement

Most distributed hangs are ordering faults, not network faults. The fix is a discipline about call order and metadata agreement, applied before any debugging tool.

You'll be able to

  • Explain why every member of a group must issue the same collective sequence.
  • Predict hang or corruption from conditional, reordered or shape-mismatched collectives.
  • Use barriers and short timeouts to localize an ordering fault to the guilty rank.

Covers

Barrier use, misuse and implicit synchronizationCollective ordering and shape or dtype agreement
1

Read

In this order, only the parts listed

0 of 4 done

  1. Ordering, barriers and debug entriesPyTorch 2.14 `torch.distributed` reference

    The batch_isend_irecv ordering rule, the barrier and monitored_barrier entries, and TORCH_DISTRIBUTED_DEBUG with OFF, INFO and DETAIL

  2. A0 Barrier sectionUltra-Scale Playbook - A0: Parallel Programming Crash Course

    A0 Parallel Programming Crash Course, the Barrier section with the sleep demonstration and dist.barrier

    Read the arrival semantics only. The sleep demo shows waiting, not data movement.

  3. Pipeline communicate orderingPicotron - Process Groups and Communication Files

    pipeline_communicate with recv_forward, send_forward, recv_backward and send_backward through batch_isend_irecv

    Read how operations pair sends with receives in a fixed order. Leave the stage schedule for the pipeline section.

  4. Barrier as a rendezvous pointBuilding a Distributed Training Framework from First Principles

    From 13:17:54 to about 13:18:30 (hours:minutes:seconds): all devices wait until every rank arrives at the barrier

2

Focus on

  • Every rank in a group must invoke the same collectives in the same order with agreeing shapes and dtypes. Any deviation hangs or corrupts, because there is no central scheduler to reconcile the calls.
  • dist.barrier blocks until all ranks in the group arrive. It synchronizes arrival only; it moves no tensors and fixes no data skew.
  • Conditional collectives, where only some ranks enter the call, are the classic hang. So are swapped orders, such as rank 0 calling all-reduce then broadcast while rank 1 calls broadcast then all-reduce.
  • dist.monitored_barrier with a short timeout plus TORCH_DISTRIBUTED_DEBUG=DETAIL names the rank that never arrived. The default 30-minute gloo timeout turns every hang into a long silence.
Common mistakes · 3
  • Using a barrier as if it synchronizes data. Ranks can arrive together and still hold different tensors.
  • Running hang drills with the default timeout. Set a timeout of seconds for drills; the 30-minute default is for production patience, not debugging.
  • Blaming the network for a shape mismatch hang. Agree shapes and dtypes first, then suspect links.
3

Try it yourself

Closed-book, before checking anything

Closed-book, write the order-swap drill on 2 gloo CPU processes with a 20-second timeout: rank 0 calls all-reduce then broadcast while rank 1 calls broadcast then all-reduce. Predict the timeout, then fix the order and show both calls pass.
4

Quiz yourself

Answer first, then mark it. Missed ones go to Review.

0 got · 0 missed

  1. 1.Rank 0 calls all-reduce then broadcast; rank 1 calls broadcast then all-reduce with matching shapes. What happens?Answer

    On gloo with the default debug level both block until the timeout: rank 0 sits in all-reduce and rank 1 in broadcast, so neither call finds its partner. With TORCH_DISTRIBUTED_DEBUG=DETAIL the swap fails at once with a collective mismatch report at sequence number 0.

  2. 2.All ranks call dist.barrier and proceed. Does that prove their tensors agree?Answer

    No. The barrier only proves every rank arrived. Tensor agreement needs the right collectives with matching data, not arrival.

  3. 3.A job hangs at a collective with the default gloo timeout. How long until it fails, and what finds the guilty rank faster?Answer

    About 30 minutes. A short explicit timeout plus monitored_barrier and DETAIL debug output names the rank that never arrived within seconds.

  4. 4.Only ranks with nonzero loss call all-reduce. Why is that unsafe even when the math looks conditional-safe?Answer

    Collectives are group operations, not local branches. Ranks that skip the call leave the others waiting, so the condition must be reduced to all ranks first or every rank must enter the call.

5

Practice

You're done when

The swapped order times out on a short fuse, the fixed order passes, and you can name the order, membership and metadata mismatch classes without notes.

Only when you can do this without notes.

Leads to

Notes

Resources · 4 for this unit, 11 more in the section
  • 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.distributed example on Gloo and annotate tensor ownership before moving to higher parallelism.

  • 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.

  • 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.

  • PyTorch 2.14 `torch.distributed` referencePyTorch · reference

    Look up the backends table, init_process_group with timeout and device_id, new_group and new_subgroups, the Work and async_op contract, monitored_barrier, destroy_process_group and TORCH_DISTRIBUTED_DEBUG. Use it for API lookup beside the tutorial first pass.

Also in this 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.

  • 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.

  • CS336 Spring 2025 - Lecture 8 Parallelism (executable lecture)Stanford University · support

    Read lecture_08.py Part 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 Tutorial - Steps 1 to 3 and Part 1 VideoHugging Face · support

    Follow step2_process_group_manager and step3_dataloader with their diffs and the Part 1 video (model, process group manager and dataloader). Rebuild the rank grid yourself before reading the solution.

  • Machine Learning Engineering Open Book - Communication PatternsStas Bekman · required

    Read network/comms.md from point-to-point through ring all-reduce, then network/README.md and network/debug/README.md selectively. Derive bytes and steps before running anything; network/benchmarks/all_reduce_bench.py needs two or more GPUs and reports bus bandwidth.

  • 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.distributed reference; 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_node with 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 solve alternate_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.py from the companion repository and label the exact topology.

  • nccl-tests and its performance notesNVIDIA · support

    With two or more GPUs, run all_reduce_perf over a message-size sweep and read doc/PERFORMANCE.md for algorithm bandwidth versus bus bandwidth. Without them, use the document to interpret published numbers.

  • PyTorch 2.14 torchrun (elastic launch) referencePyTorch · reference

    Read Single-node multi-worker through the elastic membership rows for RANK, WORLD_SIZE and LOCAL_RANK, plus flags --standalone, --nnodes, --nproc-per-node, --rdzv-backend and --rdzv-endpoint. Launch every CPU run with torchrun --standalone and a distinct port per run.