Unit 4 of 11 · about 3 hours · derive, implement
Sequence-parallel normalization and residual layouts
Plain TP leaves the residual stream replicated. Sequence parallelism reduces its saved activation footprint while preserving full feature vectors for each local token.
You'll be able to
- Derive the all-gather/reduce-scatter boundaries between sequence-parallel norms and tensor-parallel projections in both passes.
- Keep residual additions on matching token shards and sum replicated norm-parameter gradients.
- Explain which saved activations shrink and which full-shaped temporary tensors remain.
Covers
Read
In this order, only the parts listed
0 of 3 done
- Sequence-parallel regions and norm gradientsUltra-Scale Playbook - Tensor, Sequence and Context Parallelism
Sequence parallelism: region-transition diagram, TP versus TP+SP layout table and closing LayerNorm-gradient note
Skip benchmark plots. Do not adopt the claim that every maximum activation is divided by TP degree: the all-gathered input and full-shaped partial output remain live temporaries.
- Sequence-parallel forward and backward derivationReducing Activation Recomputation in Large Transformer Models
arXiv:2205.05198v1, Section 4.2.2, Figures 5 and 6, equations 3 and 4, including the saved-input/re-gather paragraph
Stop before Pipeline Parallelism. The equal ring-byte argument is not a guarantee of equal runtime or zero overhead; extra saved-input re-gathers must be counted when enabled.
- Sequence-parallel normalization and residual contractPyTorch Tensor Parallel Tutorial
Apply Sequence Parallel to LayerNorm/RMSNorm layers: block forward and adjusted parallelization plan (page read 2026-10-14)
Check
SequenceParallelat https://docs.pytorch.org/docs/2.14/distributed.tensor.parallel.html#torch.distributed.tensor.parallel.SequenceParallel for sequence-axis defaults and the custom-initialization warning.
Focus on
- For activations, each SP rank keeps . Normalization reduces over features within each token, so it needs no cross-token statistics.
- All-gather the normalized token shards before the column projection. Reduce-scatter the row projection's partial output along sequence before residual addition. Both operands then own the same tokens.
- In backward, the forward all-gather has a reduce-scatter adjoint under this logical tensor contract; the forward reduce-scatter has an all-gather adjoint. Replicated norm weights receive different token contributions that must be summed.
- The paper's activation formula counts saved tensors under its storage/recomputation choices. A transient replicated input does not disappear merely because the block's boundary layout is sharded.
Common mistakes · 3
- Normalizing across the sharded sequence axis instead of the complete feature axis. That changes the model and may leak future tokens.
- Adding two equal-shaped local tensors that own different token ranges. Shape equality is not layout equality.
- Leaving custom-initialized norm gains different across ranks.
SequenceParallelassumes the usual unit initialization; synchronize custom gains explicitly.
Try it yourself
Closed-book, before checking anything
tp_swiglu.py from the previous unit. Without notes, trace x, normalized, replicated, hidden, down_partial, down_sequence and the residual output through both passes. With its two-rank settings, the residual shards are , the gathered input and partial down output are , and the intermediate features are logically , held as local shards. Confirm these layouts and the norm-gradient oracle on CPU.Quiz yourself
Answer first, then mark it. Missed ones go to Review.
0 got · 0 missed
1.Why can SP normalization be local while feature-sharded normalization may not be?Answer
An SP shard contains every feature for its tokens. A feature shard lacks part of each token's normalization statistics.
2.Why do norm weights need gradient reduction under SP but not necessarily under replicated plain TP?Answer
SP ranks process different tokens, so each gradient is a contribution to the shared weight. Plain TP replicated norms can compute identical complete gradients under identical input and loss semantics.
3.Does replacing an all-reduce with an all-gather plus reduce-scatter guarantee equal step time?Answer
No. Ideal ring payloads can match, but launch count, ordering, buffers, kernel shapes and extra re-gathers can differ.
You're done when
Your forward/backward layout table matches the CPU example, the residual and norm gradients match the unsharded oracle, and the table includes gathered and partial temporaries.
Only when you can do this without notes.
Leads to
Notes
Resources · 3 for this unit, 10 more in the section
- Ultra-Scale Playbook - Tensor, Sequence and Context ParallelismHugging Face · primary
Read Tensor Parallelism, Tensor parallelism in a transformer block, Sequence parallelism, Context Parallelism, Ring Attention and Zig-Zag Ring Attention in order. Redraw every tensor shape and collective for a concrete batch, sequence, hidden and head configuration; the context-parallel section gives no byte formula, so derive one yourself.
- Reducing Activation Recomputation in Large Transformer ModelsKorthikanti et al. (MLSys 2023) · support
Read the sequence-parallelism and selective-recomputation sections and the activation-memory equations. Check them against your own per-layer memory derivation.
- PyTorch Tensor Parallel TutorialPyTorch · required
Use the tutorial to map
ColwiseParallel,RowwiseParallel,SequenceParallel,PrepareModuleInputand loss parallelism to the ownership model you derived, with the DTensor reference at https://docs.pytorch.org/docs/2.14/distributed.tensor.html forShard,Replicate,Partialandredistribute. Add assertions around every layout transition.
Also in this section
- Megatron-LM: Training Multi-Billion Parameter Language Models Using Model ParallelismShoeybi et al. (2019) · required
Read the model-parallel section: the and operators, MLP and self-attention partitioning, and the parallel embedding and cross-entropy. Derive each forward and backward collective before reading the code.
- Ring Attention with Blockwise Transformers for Near-Infinite ContextLiu et al. (ICLR 2024) · support
Read the ring algorithm and its overlap condition, then compare it with all-gather context parallelism and DeepSpeed-Ulysses (arXiv 2309.14509) when choosing a context-parallel scheme.
- Picotron - Tensor and Context ParallelismHugging Face · required
Read
tensor_parallel/tensor_parallel.py,tp_communications.py,context_parallel/context_parallel.pyandcp_communications.py.tests/test_tensor_parallel.pyhardcodes NCCL and CUDA; port it to Gloo on CPU and compare withtorch.testing.assert_close, because its exact equality check fails when sharded and full matmuls round differently. - Picotron Tutorial - Step 4 and Part 2 VideoHugging Face · support
Follow
step4_tensor_parallelwith its diff and the Part 2 video. Write your own column and row layers first. - TorchFeather - TP and Ring Attention Minimal ExamplesTorchFeather · required
Run
minimal_examples/block_matrix_multiply.py, thentp_swiglu.pyandtp_attention.pywith 2 processes andring_attention.pywith 4, all on Gloo on CPU; each checks its output and gradients against ordinary tensors. Change one placement or mask and watch the check fail. - Distributed Training Guide - Chapter 6 Tensor ParallelismLambda Labs · support
Read colwise and rowwise linear layers, embeddings, norms, loss parallelism and throughput accounting in
06-tensor-parallelas a practitioner second view. Its script needs one GPU per rank. - PyTorch Context Parallel TutorialPyTorch · support
Read the
context_parallelAPI, buffer sequence dimensions, rotation methods and the rotary-frequency trap. It needs CUDA GPUs and NCCL; on CPU, keep to the TorchFeather ring-attention example. - How to Scale Your Model - Chapters 3 and 5Google DeepMind · support
Solve the sharded-matrix multiplication layouts in Chapter 3 and the Megatron sharding section of Chapter 5. Translate the results into PyTorch DTensor placements and collective counts.
- TensorTonic - Reconstruct a Column-Parallel MLP LayerTensorTonic · support
Solve Reconstruct a Column-Parallel MLP Layer, including its uneven-shard cases.
- PyTorch 2.14 SDPA - explicit masks and dropoutPyTorch · reference
Use the equivalent attention implementation and
attn_mask/dropout_pcontract to build a CPU packed-document oracle before translating masks to context-parallel blocks. Do not assume the experimental CP wrapper supports every SDPA mask.