Unit 3 of 12 · about 4 hours · derive, implement

Global batch across workers and accumulation, and `no_sync`

Global batch is the fixed semantic; microbatch, accumulation and worker count are interchangeable ways to reach it. no_sync is what makes accumulation cheaper than repeating full syncs.

You'll be able to

  • Derive the global batch in samples and tokens from microbatch, accumulation steps and DP degree.
  • Implement accumulation with no_sync so only the final micro-step synchronizes.
  • Explain the two failure modes: syncing every micro-step (correct but slow) and syncing none (stale grads).

Covers

Global batch under workers and gradient accumulationGradient accumulation with no_sync
1

Read

In this order, only the parts listed

0 of 3 done

  1. Global batch across workers and accumulationUltra-Scale Playbook - Data Parallelism

    Revisiting global batch size: the batch equation of microbatch, accumulation and DP degree plus the maximize-dp-then-accumulate recipe; Our journey up to now for the worked large-batch example pattern

  2. Microbatch accumulation on one GPUUltra-Scale Playbook - Data Parallelism

    Gradient accumulation: microbatch versus global batch before data parallelism enters

  3. Accumulation with no_syncDistributed Training Guide - Chapter 2 DDP

    Standard Implementation versus DataDistributedParalell Implementation: maybe_sync_grads with model.no_sync on non-final steps and nullcontext on the final step, with forward and backward inside the context

2

Focus on

  • Global batch is in samples, times sequence length in tokens. Example: , , gives 1024 samples, or 4,194,304 tokens at length 4096.
  • no_sync scopes forward plus backward on non-final micro-steps; grads accumulate locally and the first step outside the context syncs.
  • Under mean reduction each micro loss is scaled by one over the accumulation count before backward, so the accumulated grad equals the global mean grad. A 2-rank CPU run with two halved micro-steps matches the oracle. The single-process baseline is the one from Schedules, gradient accumulation and clipping.
  • The training guide's chapter 2 README (02-distributed-data-parallel/README.md) says optimizer.step() synchronizes gradients. For DDP that is wrong: the reducer all-reduces during backward and the step is local. During accumulation only the final backward syncs.
Common mistakes · 3
  • Wrapping only backward in no_sync. The DDP docs require the forward inside too, or grads still sync.
  • Skipping the one-over-K loss scaling under mean reduction. Accumulated grads come out K times too large, which looks like a learning-rate bug.
  • Reading the guide's chapter 2 sentence that optimizer.step() synchronizes as DDP behavior. With DDP the sync point is backward.
3

Try it yourself

Closed-book, before checking anything

Extend the unit-2 script: wrap the model in DistributedDataParallel on gloo (no_sync exists only on the wrapper), split each rank shard into two micro-batches with each micro loss halved, run the first forward and backward inside no_sync and the second outside it, then take one SGD step. Assert oracle match. Then move the first forward outside the context, count the all-reduces with a communication hook, and show the extra sync while results stay correct.
4

Quiz yourself

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

0 got · 0 missed

  1. 1.Microbatch 2, 4 accumulation steps, 128 workers, sequence length 4096. Global batch?Answer

    1024 samples; 4,194,304 tokens (about 4M).

  2. 2.Why scale each micro loss by one over K for K micro-steps under mean reduction?Answer

    The K micro-means sum to K times the global mean; dividing restores the oracle gradient.

  3. 3.Forward runs outside no_sync, backward inside. Effect?Answer

    Still correct, but DDP synchronizes on that step anyway, so the accumulation saving is lost.

  4. 4.Where does DDP synchronize: backward or optimizer step?Answer

    Backward, through bucket all-reduces on grad-ready hooks. The optimizer step is local on identical averaged grads.

You're done when

You derive the global batch both ways without notes, and your no_sync run matches the oracle while the mis-scoped variant stays correct but syncs.

Only when you can do this without notes.

Leads to

Notes

Resources · 2 for this unit, 9 more in the section
  • Distributed Training Guide - Chapter 2 DDPLambda Labs · support

    Apply Chapter 2 to a tiny causal-LM loop: torchrun, rank-aware logs, DistributedSampler, rank-zero side effects and the ZeRO memory subsection, plus the gradient-accumulation and effective-batch related topics. Its README says optimizer.step() synchronizes gradients; DDP averages them during backward, so correct that line in your notes.

  • Ultra-Scale Playbook - Data ParallelismHugging Face · required

    Read Data Parallelism through Revisiting global batch size and Our journey up to now (overlap, bucketing, accumulation and the global batch equation). Derive replicated-state memory, gradient communication and batch constraints for your tiny model before measuring it; ZeRO belongs to DT-4.

Also in this section

  • CS336 Assignment 2 (Spring 2026) - Section 5 Distributed Data Parallel TrainingStanford University · primary

    Complete naive_ddp, naive_ddp_benchmarking, minimal_ddp_flat_benchmarking, ddp_overlap_individual_parameters and its benchmarking problem with your own implementation. tests/test_ddp.py runs on CPU with Gloo; keep the benchmark method and written derivations even when multiple GPUs are unavailable. This year's handout has no bucketed problem: build buckets from Picotron.

  • Picotron - Data ParallelismHugging Face · required

    Read data_parallel/bucket.py and the DataParallelBucket path beside train.py, and rebuild bucketed overlap with an equality test against one process. Read DataParallelNaive as a trap: its register_post_accumulate_grad_hook hook receives the parameter, so it averages parameters and leaves gradients local; show the unequal gradients on two CPU ranks.

  • Picotron Tutorial - Steps 5 and 6 and Part 3 VideoHugging Face · support

    Follow step5_data_parallel_naive and step6_data_parallel_bucket with the Part 3 video. Step 5 carries the same parameter-hook trap as Picotron; use step 6 for the equivalence gate.

  • PyTorch DDP Tutorial, Design Note and Communication HooksPyTorch · support

    Use the tutorial for setup, DistributedSampler, no_sync, unused parameters and teardown, the design note at https://docs.pytorch.org/docs/2.14/notes/ddp.html for buckets and gradient-ready order, and https://docs.pytorch.org/docs/2.14/ddp_comm_hooks.html for communication hooks. Consult them after the from-scratch path so wrappers do not replace the mechanism model.

  • PyTorch Distributed: Experiences on Accelerating Data Parallel TrainingLi et al. (VLDB 2020) · support

    Read the design sections on gradient bucketing, overlap with backward, no_sync and unused-parameter detection. Use them to explain why buckets follow reverse registration order.

  • Deep Learning at Scale - Chapter 7 Data ParallelismSuneeta Mall (O'Reilly) · support

    Read Data Partitioning, Sampling Strategies, DDP and the data-pipeline sections, and use the small DDP exercises as extra practice. Its FSDP and ZeRO material predates FSDP2.

  • TensorTonic - Synchronize Data-Parallel GradientsTensorTonic · support

    Solve Synchronize Data-Parallel Gradients before the CS336 DDP tests.

  • AI Systems Performance Engineering - Chapter 4 Overlap and DDP StrategiesChris Fregly (O'Reilly) · support

    Read Overlapping Communication and Computation, Achieving Maximal Overlap in Practice and Distributed Data Parallel Strategies in Chapter 4, and the Chapter 13 streams section on overlapping communication. With two or more GPUs, compare code/ch04/ddp_no_overlap.py and ddp_overlap.py; they refuse one-rank runs.

  • PyTorch SyncBatchNorm APIPyTorch · reference

    Use convert_sync_batchnorm to convert BatchNorm layers before wrapping with DDP, and read the training-only sync plus single-GPU-per-process limits.