Unit 8 of 10 · about 4 hours · implement
Stage-local ownership, final-stage loss and tied embeddings
Ownership decides what each rank saves, optimizes and differentiates. Loss placement and gradient scaling decide whether pipeline gradients mean the same thing as single-process ones.
You'll be able to
- Assign parameters, loss computation and optimizer state to stages for embedding-first, output-last splits.
- Correct microbatch gradient scaling so pipeline updates match a single-process oracle.
- State the tied-weight contract across first and last stages.
Covers
Read
In this order, only the parts listed
0 of 3 done
- Stage-local parameters and last-stage lossPicotron - Pipeline Parallelism
PipelineParallelinit owningembeddingon the first stage andfinal_normplusfinal_projon the last, per-microbatch meancross_entropyon the last stage only, andlogging_lossaveraged overgrad_acc_steps; note the backward loss is not divided, so accumulated gradients sum microbatch means - Split ownership and last-stage loss contractPyTorch Pipeline Parallelism Tutorial
Step 1 manual split (page read 2026-10-14):
tok_embeddingswith stage 0 andnormplusoutputwith stage 1; Step 2tokenwise_loss_fnwith thelosseslist filled on the last stage throughstep(target=y, losses=losses) - Reference loss and gradient averagingTorchFeather - Pipeline Minimal Example and Runtime
run_referencefull-model loss versus summed per-microbatch losses each divided bynum_microbatches, andaverage_gradientsall-reducing over thedp_replicategroup
Focus on
- Token IDs enter as a batch of sequences and leave as vectors per the embeddings-and-positions unit; the first stage owns that lookup and the last stage owns the projection back to vocabulary.
- Only the last stage holds logits, so
loss_fn, targets and the per-microbatchlosseslist all belong there; other stages receive gradients, never targets. - Accumulated microbatch-mean gradients sum to times the global-batch mean: divide by or scale the learning rate before comparing with a single-process oracle.
- Tied weights live on two stages at once: Picotron sidesteps this by not tying, and any tied design must sync gradients across the first and last stage before the optimizer step.
Common mistakes · 3
- Comparing Picotron updates directly with a same-learning-rate single-process run. Per-microbatch mean losses sum to times the global mean, so divide by first or read a scaling difference as a pipeline bug.
- Splitting a tied embedding and output projection by copying the weight to two stages. The copies drift after step one unless gradients sync across both stages every step.
- Computing loss off the last stage. Only the last stage holds logits, so the loss function and the losses list belong there.
Try it yourself
Closed-book, before checking anything
Quiz yourself
Answer first, then mark it. Missed ones go to Review.
0 got · 0 missed
1.Four microbatches with per-microbatch mean losses accumulate gradients. What factor separates the sum from the global-batch mean gradient?Answer
4. Each backward adds one full mean gradient, so the accumulated sum is times the mean for equal microbatches.
2.How do TorchFeather and PyTorch
scale_grads=Trueavoid that factor?AnswerTorchFeather divides each microbatch loss by the microbatch count before summing.
scale_grads=Truedivides accumulated gradients byn_microbatches, which matches an averaging loss function.3.A tied embedding weight sits on stages 0 and 3 with independent optimizers. What breaks?Answer
Each stage updates its copy from its own gradient slice, so the copies diverge after the first step. Sync the gradients, or share the storage, before updating.
4.Why must
ModuleDictnames survive the split?AnswerCheckpoint keys and fully-qualified-name services such as FSDP or tensor parallelism resolve by name; renaming breaks save and load.
Practice
- PyTorch pipelining tutorial: single-process output equivalence
Compare the rank-1 merged output and
lossesagainst the same 8-layer model run on one process with dropout off (calleval()on both copies: the tutorial's decoder layers keep the default dropout of 0.1); gate on forward match within 1e-5 and equal per-microbatch lossesRun on CPU. In a measured example with 2 CPU processes on gloo and PyTorch 2.14.1, an equivalent probe matched forwards within 1e-5; your transformer numbers will differ, and the match is the gate, not the values.
You're done when
Pipeline outputs match the single-process run within 1e-5 with the scaling correction stated, and you can place parameters, loss and tied weights on a stage diagram from memory.
Only when you can do this without notes.
Leads to
Notes
Resources · 3 for this unit, 9 more in the section
- Picotron - Pipeline ParallelismHugging Face · required
Read pipeline_parallel/pipeline_parallel.py and pp_communications.py beside the pipeline branch in train.py. Build a schedule event simulator first, then reproduce GPipe and 1F1B with shape, tag and ordering assertions.
- TorchFeather - Pipeline Minimal Example and RuntimeTorchFeather · required
Run
minimal_examples/pp_gpipe.pywith 8 Gloo processes on CPU (two stages by four replicas); it checks outputs and averaged gradients against a reference. Read its taggedisendandirecvcalls, thendistributed/pipeline_parallel.py, and compare the event order with Picotron. - PyTorch Pipeline Parallelism TutorialPyTorch · required
Complete the tutorial and compare
ScheduleGPipeandSchedule1F1B, then read https://docs.pytorch.org/docs/2.14/distributed.pipelining.html for the interleaved, looped, zero-bubble and DualPipeV schedules the tutorial omits. Preserve the model split, microbatch and loss contracts before adapting it.
Also in this section
- Ultra-Scale Playbook - Pipeline ParallelismHugging Face · primary
Read Pipeline Parallelism through All forward, all backward, One forward, one backward, Interleaving stages and Zero bubble and DualPipe. Derive bubble fraction and peak activation count for your simulator before running any schedule.
- Picotron Tutorial - Steps 7 and 8Hugging Face · support
Read
step7_pipeline_parallel_afabandstep8_pipeline_parallel_1f1bwith their diffs; the video series stops before pipeline parallelism. - First-Principles Video - Pipeline Parallelism SegmentUmar Jamil · support
Watch 06:06:17 for pipeline parallelism and 06:26:52 for GPipe, 1F1B and zero-bubble schedules. Stop after GPipe and 1F1B to draw and test those event traces; zero-bubble implementation remains depth work.
- How to Scale Your Model - Pipeline Parallelism in Chapter 5Google DeepMind · support
Work the pipeline bubble and communication examples in Chapter 5. Use them to test your cost model under different stage counts, microbatch counts and imbalanced layers.
- GPipe: Efficient Training of Giant Neural Networks using Pipeline ParallelismHuang et al. (NeurIPS 2019) · support
Read the partitioning, microbatching and re-materialization design and the bubble overhead statement. Use it as the fill-drain specification your simulator reproduces.
- Memory-Efficient Pipeline-Parallel DNN TrainingNarayanan et al. (ICML 2021) · support
Read the PipeDream-Flush (1F1B) schedule and its activation-memory bound. Compare its stashed-activation count with GPipe in your simulator.
- Zero Bubble Pipeline ParallelismQi et al. (ICLR 2024) · support
Read the split of backward into input and weight gradients and the handcrafted schedules. Optional vocabulary for zero-bubble and DualPipe schedules.
- TensorTonic - Pipeline Bubble UtilizationTensorTonic · support
Solve the pipeline bubble utilization problem, then extend its model to 1F1B and interleaved stages in your simulator.
- Distributed Training Subsystem - Pipeline SchedulerRoadmap-Final · support
Complete the scheduler, event trace, activation-lifetime accounting and failure injection required in Milestone B. Keep the simulator runnable without GPUs.