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

Stage-local parameter and optimizer ownershipLoss computation and final-stage semanticsTied embeddings across first and last stages
1

Read

In this order, only the parts listed

0 of 3 done

  1. Stage-local parameters and last-stage lossPicotron - Pipeline Parallelism

    PipelineParallel init owning embedding on the first stage and final_norm plus final_proj on the last, per-microbatch mean cross_entropy on the last stage only, and logging_loss averaged over grad_acc_steps; note the backward loss is not divided, so accumulated gradients sum microbatch means

  2. Split ownership and last-stage loss contractPyTorch Pipeline Parallelism Tutorial

    Step 1 manual split (page read 2026-10-14): tok_embeddings with stage 0 and norm plus output with stage 1; Step 2 tokenwise_loss_fn with the losses list filled on the last stage through step(target=y, losses=losses)

  3. Reference loss and gradient averagingTorchFeather - Pipeline Minimal Example and Runtime

    run_reference full-model loss versus summed per-microbatch losses each divided by num_microbatches, and average_gradients all-reducing over the dp_replicate group

2

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-microbatch losses list 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.
3

Try it yourself

Closed-book, before checking anything

Closed-book, write which rank owns parameters, loss and optimizer state for a 2-stage run, and the gradient-scale correction that makes accumulated microbatch-mean gradients equal the global-batch mean.
4

Quiz yourself

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

0 got · 0 missed

  1. 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. 2.How do TorchFeather and PyTorch scale_grads=True avoid that factor?Answer

    TorchFeather divides each microbatch loss by the microbatch count before summing. scale_grads=True divides accumulated gradients by n_microbatches, which matches an averaging loss function.

  3. 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. 4.Why must ModuleDict names survive the split?Answer

    Checkpoint keys and fully-qualified-name services such as FSDP or tensor parallelism resolve by name; renaming breaks save and load.

5

Practice

  • PyTorch pipelining tutorial: single-process output equivalence

    Compare the rank-1 merged output and losses against the same 8-layer model run on one process with dropout off (call eval() 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 losses

    Run 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.py with 8 Gloo processes on CPU (two stages by four replicas); it checks outputs and averaged gradients against a reference. Read its tagged isend and irecv calls, then distributed/pipeline_parallel.py, and compare the event order with Picotron.

  • PyTorch Pipeline Parallelism TutorialPyTorch · required

    Complete the tutorial and compare ScheduleGPipe and Schedule1F1B, 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