Unit 3 of 11 · about 3 hours · derive

Weight and KV sharding and per-rank memory ownership

Aggregate memory is not admission capacity. One rank can run out of KV space while other ranks have free memory, and replicated state can erase an expected sharding gain.

You'll be able to

  • Derive each rank's weight and KV bytes from the actual layer, head and position ownership rather than dividing everything by device count.
  • Explain KV-head replication when TP exceeds the number of KV heads in a head-sharded GQA layout.
  • Reject a plan whose busiest rank exceeds memory after runtime buffers, page waste and reserve are included.

Covers

Model weights and KV shardingPer-rank memory and local tensor ownership
1

Read

In this order, only the parts listed

0 of 2 done

  1. Head sharding and the GQA ceilingLLM Inference: From scratch, one token at a time - Module 09

    Module 09, What TP degree actually does to your numbers - Weights per GPU and KV cache per GPU stops shrinking at TP-8

    Use the head-ownership argument only. Skip the hardware speed table and Code lab planner: evenly divided weights are an approximation, and its PP latency estimate does not sum the dependent stage service times.

  2. Layer and expert ownershipAI Systems Performance Engineering - Chapters 15 and 17, distributed and disaggregated inference

    Chapter 15, Tensor Parallelism, Pipeline Parallelism, Expert Parallelism and Data Parallelism

    Revisit only which weights each rank holds. Carry forward the runtime-memory categories from Weight memory by dtype and the runtime memory categories.

2

Focus on

  • Reuse Per-layer KV layouts and the cache consequences of attention variants. For ordinary full-attention K and V, a rank owning layers, KV heads and cached positions holds logical bytes, where is head dimension and is bytes per element.
  • PP assigns KV to the ranks that own its layers. Head-sharded TP assigns local KV heads; if there are more TP ranks than KV heads, a supported layout may replicate heads instead of shrinking each cache further.
  • EP partitions expert weights, not automatically attention weights or KV. Sum shared, expert, embedding and output weights according to the chosen layout; include replicated tensors explicitly.
  • Admission uses the maximum per-rank allocation, including page rounding, workspace, graphs, communication buffers and reserve. Distinguish logical unique KV from physical copies across ranks.
Common mistakes · 3
  • Dividing all memory by the product of TP, PP and EP sizes. Only the tensors actually partitioned along those axes get that divisor.
  • Treating a head-count ceiling as a universal limit on usable devices. Position sharding can reduce cache further, but requires its own layout and communication contract.
  • Checking total free memory instead of each rank's budget. The first exhausted rank can stop the whole serving group.
3

Try it yourself

Closed-book, before checking anything

Make a per-rank ledger for a synthetic full-attention model with eight layers, two KV heads, head dimension 16, two-byte KV elements and one 128-token sequence. Its unique KV is 131,072 bytes. Under two-way head-sharded TP each rank holds 65,536 bytes; under four-way TP with each KV head replicated twice, each rank still holds 65,536 bytes. Add two-way PP with four layers per stage and recompute local and aggregate physical bytes. Ignore page waste only for this logical-byte exercise.
4

Quiz yourself

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

0 got · 0 missed

  1. 1.In the four-way TP example, how many physical KV bytes exist across ranks?Answer

    262,144 bytes, twice the unique KV. Each of the two KV heads has two physical copies.

  2. 2.Does expert parallelism alone divide dense attention KV by expert count?Answer

    No. Experts belong to the MoE feed-forward path. Attention KV follows attention layer, head and position ownership.

  3. 3.How does two-way PP change each TP rank's KV in this equal-layer example?Answer

    It halves local KV to 32,768 bytes because each stage owns four of the eight layers. Aggregate physical KV stays unchanged for the same TP replication pattern.

  4. 4.Why is weight bytes divided by rank count insufficient to prove a plan fits?Answer

    Some weights are replicated or unevenly placed, and KV, temporary tensors, communication buffers and runtime reserve also consume local memory.

You're done when

Your ledger reconciles unique and physical KV bytes for TP, PP and their combination, and rejects a plan that fails any local memory budget.

Only when you can do this without notes.

Leads to

Notes

Resources · 2 for this unit, 11 more in the section

Also in this section