Unit 7 of 11 · about 4 hours · derive

Tensor-parallel memory and communication volume by layer

A parameter shard can shrink while activation or communication buffers still dominate the peak. A byte ledger makes the degree choice testable.

You'll be able to

  • Derive per-rank parameter, gradient, optimizer and activation bytes from actual TP/SP ownership and dtypes.
  • Count forward and backward collective payloads by layer, separating logical payload from per-rank ring traffic.
  • Explain memory-model residuals caused by saved inputs, re-gathers, partial outputs and temporary buffers.

Covers

Activation and parameter memory accountingTensor-parallel communication volume by layer
1

Read

In this order, only the parts listed

0 of 3 done

  1. Saved activations, not a universal peak formulaReducing Activation Recomputation in Large Transformer Models

    arXiv:2205.05198v1, Sections 4.1, 4.2.1 and 4.2.2, equations 1, 2 and 4; Section 5 and Table 2

    Keep the stated two-byte activations, one-byte dropout masks and GPT-style architecture assumptions. Re-derive the ledger for SwiGLU, GQA or memory-efficient attention instead of applying its coefficients unchanged.

  2. The transformer-block collective countMegatron-LM: Training Multi-Billion Parameter Language Models Using Model Parallelism

    arXiv:1909.08053v4, Section 3, Figure 4 and its four-operation caption

  3. Communication follows the sharded contractionHow to Scale Your Model - Chapters 3 and 5

    Chapter 5, Tensor Parallelism: forward/backward algorithm and compute/communication model

    Stop before Combining FSDP and Tensor Parallelism. Its boundaries shard hidden features, not the SP token layout. Retain the topology and overlap assumptions; its TPU constants are not measurements of your machine.

2

Focus on

  • For a bias-free SwiGLU with model width , expanded width and TP degree , sharded weights total elements per rank. Replicated norm gains add . Count gradients and each optimizer tensor at their own dtype, with no automatic master-weight assumption.
  • For batch , sequence and bytes per activation , a full residual has bytes. A ring all-reduce sends bytes per rank; a ring all-gather or reduce-scatter sends . Receive traffic is equal and is not included in these send counts.
  • A plain Megatron attention-plus-MLP block has two forward and two backward activation all-reduces. TP+SP replaces those with four all-gathers and four reduce-scatters across both passes; norm-gradient sums, loss traffic and optional saved-input re-gathers are additional.
  • Record unique storage and lifetimes, not just tensor names. Views can share storage; all-gathered inputs, full-shaped partial outputs, receive buffers and allocator reserve can make peak memory exceed saved-activation estimates.
Common mistakes · 3
  • Dividing the entire optimizer or activation footprint by TP degree. Replicated gains, temporaries and optional full-precision copies need separate terms.
  • Confusing all-reduce payload bytes with sent-plus-received traffic. State the counting convention before comparing formulas.
  • Treating a saved-activation equation as a GPU memory-peak prediction, especially after replacing materialized attention with a tiled kernel.
3

Try it yourself

Closed-book, before checking anything

Derive a byte ledger for TorchFeather's bias-free RMSNorm-SwiGLU at , , , , , with torch.float32 weights and activations. Each rank's three weight shards plus norm gain occupy 800 bytes; a replicated residual occupies 256 bytes and its sequence shard 128 bytes. Separately derive 1024 sent bytes per rank for the four ring activation all-reduces of a plain attention-plus-MLP training block at those residual dimensions. TP+SP's corresponding eight collectives have the same ideal send volume, excluding the extra operations listed in Focus. Check the storage counts against local tensors and label the ring traffic as analytical, not measured backend traffic.
4

Quiz yourself

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

0 got · 0 missed

  1. 1.Where do the 800 parameter bytes in Reproduce come from?Answer

    Three matrices of shape split over two ranks give 192 elements, plus eight replicated gains. At four bytes per element, 200 elements occupy 800 bytes.

  2. 2.For two ranks and a 256-byte payload, how much does one ideal ring all-reduce send per rank?Answer

    256 bytes: . It also receives 256 bytes, so sent-plus-received traffic is 512 bytes.

  3. 3.Can matching ideal ring bytes establish matching TP and TP+SP runtime?Answer

    No. Startup costs, backend algorithms, re-gathers, kernel shapes and overlap differ. The byte equality covers only the specified collective payloads.

5

Practice

  • How to Scale Your Model: Chapter 3, Question 4 [matmul strategies]

    Some Problems to Work: Question 4 [matmul strategies]

    Answer before opening the printed solution. Derive local FLOPs and communication for gather-before-matmul versus partial-matmul-then-reduce. Distinguish the book's bandwidth-dominated TPU model from finite-degree ring send counts and from serial dependency bounds. The section mini-project covers the layer ledger and collective records.

You're done when

Your ledger reconciles local parameter and boundary-activation storage, accounts for both passes and extra collectives, and labels payload, send traffic, saved memory and peak memory separately.

Only when you can do this without notes.

Leads to

Notes

Resources · 3 for this unit, 10 more in the section

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

  • 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.py and cp_communications.py. tests/test_tensor_parallel.py hardcodes NCCL and CUDA; port it to Gloo on CPU and compare with torch.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_parallel with 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, then tp_swiglu.py and tp_attention.py with 2 processes and ring_attention.py with 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-parallel as a practitioner second view. Its script needs one GPU per rank.

  • PyTorch Tensor Parallel TutorialPyTorch · required

    Use the tutorial to map ColwiseParallel, RowwiseParallel, SequenceParallel, PrepareModuleInput and loss parallelism to the ownership model you derived, with the DTensor reference at https://docs.pytorch.org/docs/2.14/distributed.tensor.html for Shard, Replicate, Partial and redistribute. Add assertions around every layout transition.

  • PyTorch Context Parallel TutorialPyTorch · support

    Read the context_parallel API, 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.

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