LLM Architecture

Memory Arithmetic for LLM Serving: TP/EP, KV Crossover, and Looped Decode

Contents
  1. 1. Decimal SI vs. Binary IEC Units and 2T MoE Footprint
  2. 2. TP/EP/DCP Sharding, Collectives, and Training Optimizer States
  3. 3. When KV Cache Overtakes Static Weights
  4. 3.1 KV Volume Formula and Three Deployment Crossovers
  5. 3.2 How MLA and Hybrid Linear Attention Shift the Crossover
  6. 4. The Looped Decode HBM Bandwidth Bottleneck
  7. References

Sizing GPU clusters by dividing total parameter bytes by vendor-advertised card memory routinely underestimates real deployment budgets. Three compounding systems effects separate naive capacity ratios from cluster reality: a nearly 10% unit divergence between decimal SI parameter/bandwidth specifications and binary IEC tensor allocators, multi-GPU sharding constraints and collective synchronization overheads across Tensor Parallelism (TP) and Expert Parallelism (EP), and the growing dominance of persistent KV Cache at long contexts and large batch sizes.

Following the back-of-the-envelope arithmetic framework of Kipply (2022) and Google DeepMind's How to Scale Your Model (Jacob Austin et al., 2025), the sections below derive each bound from hardware unit conventions up through looped-decode HBM traffic.


1. Decimal SI vs. Binary IEC Units and 2T MoE Footprint#

Memory accounting starts with reconciling unit conventions between decimal model/storage/network figures and binary allocator reports:

$$1\text{ GB} \approx \mathbf{0.9313\text{ GiB}}, \qquad 1\text{ TB} \approx \mathbf{0.9095\text{ TiB}}$$

One hardware detail is frequently inverted: NVIDIA H100 80 GB GPUs use binary-organized HBM stacks (five 16 GiB stacks) and report ~79.65 GiB usable in nvidia-smi (~637.2 GiB per 8-GPU node after ECC and firmware overhead)—not 74.5 GiB or 596 GiB. Instead, the $6.9\%\text{–}9.1\%$ SI-versus-IEC divergence bites when translating decimal-counted parameter footprints and decimal-rated NVMe SSD / network / HBM bandwidths (such as 3.35 TB/s) into PyTorch's binary GiB and TiB CUDA allocations:


2. TP/EP/DCP Sharding, Collectives, and Training Optimizer States#

Across multi-node clusters, Tensor Parallelism (TP degree $T_p$) partitions matrix columns and rows within high-bandwidth intra-node NVLink domains, while Expert Parallelism (EP degree $E_p$) spreads routed MoE experts across machines. When Data Parallelism (DP) and EP groups do not overlap for dense weights (so dense Attention blocks, embeddings, and router gates replicate across EP ranks), per-GPU static weight and KV capacity become:

$$M_{\text{weight/gpu}} = \frac{M_{\text{dense}}}{T_p} + \frac{M_{\text{MoE}}}{T_p \cdot E_p}, \qquad M_{\text{KV/gpu}} = \frac{M_{\text{KV, total}}}{T_p}$$

An important constraint hides in the second equation: dividing $M_{\text{KV, total}}$ cleanly by $T_p$ under GQA assumes $T_p \le n_{\text{kv}}$. Once tensor parallelism exceeds $n_{\text{kv}}$ (e.g., $T_p = 16 > n_{\text{kv}} = 8$), excess ranks must duplicate KV heads; similarly, MLA holds a single low-rank latent vector. Serving engines restore linear KV scaling via DCP (Decode Context Parallel)—partitioning KV along sequence length, computing local attention per shard, and merging with all-gather LSE scalars plus an all-reduce.

Following the distributed communication accounting in DeepMind's Scaling Book, multi-GPU partitioning also introduces three concrete network/interconnect terms:

  1. TP NVLink AllReduce: Each Transformer layer executes two AllReduce barriers (after Attention output projection and MLP down projection), each decomposing into ReduceScatter + AllGather. Across $T_p$ ranks over NVLink of unidirectional bandwidth $\mathrm{BW}_{\text{NVLink}}$, letting $b_a$ denote activation bytes per element, per-layer transfer time is:

$$t_{\text{AR, layer}} = 2 \cdot \frac{T_p - 1}{T_p} \cdot \frac{B \cdot d \cdot b_a}{\mathrm{BW}_{\text{NVLink}}} \times 2$$

At $B=1$, byte volume is tiny (~0.009 ms of transfer across 80 layers of 70B over 450 GB/s unidirectional NVLink4), so the real bottleneck is collective launch latency across $2L = 160$ sequential synchronization barriers (typically adding several milliseconds).

  1. EP All-to-All dispatch/combine: With Top-$k$ routing across $E_p$ ranks, each MoE layer transfers roughly $2 \cdot \frac{E_p - 1}{E_p} \cdot k \cdot B \cdot d \cdot b_a$ bytes per rank across dispatch and combine, making multi-node EP sensitive to InfiniBand/RoCE bisection bandwidth.
  2. PD Disaggregation RDMA KV transfer: Splitting compute-bound Prefill nodes from bandwidth-bound Decode nodes eliminates ITL stalls, but requires full weight replicas on both pools plus RDMA KV shipping. Transferring 10 GiB (~10.74 GB = $85.9\text{ Gbit}$) of 70B 32K BF16 KV over a 400 Gbps (50 GB/s line rate) InfiniBand link takes at least $10.74 / 50 \approx \mathbf{215\text{ ms}}$ (~230–250 ms after NIC/protocol overhead) unless overlapped layer-by-layer or compressed via MLA/GQA.
Distributed Sharding Topology: Intra-Node TP (NVLink), Cross-Node EP (All-to-All), and DCP 1. Tensor Parallel (TP=4, NVLink) GPU 0W0 · KV0GPU 1W1 · KV1GPU 2W2 · KV2GPU 3W3 · KV3 2× AllReduce / layer (RS + AG) M_dense / T_p · KV scales if T_p ≤ n_kv B=1 transfers 0.009 ms; hurt by 160× latency 2. Expert Parallel (EP, Routed MoE) Top-k Router: Dispatch Tokens to Remote Experts GPU 0E0–15GPU 1E16–31GPU 2E32–47GPU 3E48–63 M_MoE / (T_p · E_p) · 2× All-to-All / layer Cross-node gated by IB/RoCE bisection & skew When T_p > n_kv or single-vector MLA: enable DCP to shard sequence KV + merge local LSE
Figure 1: Distributed sharding boundaries across intra-node TP (AllReduce), EP (All-to-All), and sequence-sharded DCP.

Combining these with per-rank CUDA Context ($0.5\text{–}1.5\text{ GiB}$), CUDA Graph multi-batch recording, NCCL Send/Recv buffers, 10%–20% dynamic headroom, and 8-GPU node alignment (~79.65 GiB usable on H100, ~140.4 GiB on H200, ~179 GiB on B200):

2T MoE PrecisionBinary SizeH100 (79.65 GiB) Floor / PracticalH200 (140.4 GiB) Floor / PracticalB200 (~179 GiB) Floor / Practical
BF16~3,725 GiB47 / 64 GPUs (8 nodes)27 / 40–48 GPUs (5–6 nodes)21 / 32 GPUs (4 nodes)
FP8~1,863 GiB24 / 32 GPUs (4 nodes)14 / 24 GPUs (3 nodes)11 / 16 GPUs (2 nodes)
INT4 / FP4~1,024 GiB13 / 16–24 GPUs (2–3 nodes)8 / 16 GPUs (2 nodes)6 / 8 GPUs (1 node)

Switching to mixed-precision training raises static residency before activations to 16–18 B/param unsharded: 2 B BF16 weights, 2 B BF16 gradients (4 B with FP32 master gradient accumulation), 4 B FP32 master weights, and 8 B for AdamW first/second moments—totaling $32\text{–}36\text{ TB}$ for a 2T model. Across $N_d$ data-parallel ranks, ZeRO-1 / 2 / 3 (FSDP) shards static residency down to $4 + 12/N_d$, $2 + 14/N_d$, or $16/N_d\text{ B/param}$, while FP8 training mainly compresses forward weights and gradients, leaving unsharded total residency near $14\text{–}15\text{ B/param}$.


3. When KV Cache Overtakes Static Weights#

3.1 KV Volume Formula and Three Deployment Crossovers#

In long-context and high-concurrency serving, dynamic KV Cache growth obeys a rule decoupled from parameter count: per-token KV memory $\kappa$ depends not on total model parameters $P$, but strictly on effective layer count $L$, KV state dimension, and cache bit-width $b_{\text{kv}}$:

$$\kappa_{\text{GQA}} = 2 L n_{\text{kv}} d_{\text{head}} b_{\text{kv}}, \qquad \kappa_{\text{MLA}} = L (d_c + d_R) b_{\text{kv}}$$

Setting total resident KV bytes equal to static model weight bytes ($B \cdot S \cdot \kappa = P \cdot b_w$) gives the exact crossover concurrency-length product $(B \cdot S)^*$ where KV Cache equals model weights:

$$(B \cdot S)^* = \frac{P \cdot b_w}{\kappa}$$

Calculation

Worked Verification (LLaMA-3 8.03B, Binary $\text{Ki}=1024$ vs. Decimal $\text{K}=1000$): Across $L=32$ layers with GQA-8 ($n_{\text{kv}}=8$, $d_{\text{head}}=128$), static BF16 weights occupy $16.06\text{ GB} = \mathbf{14.96\text{ GiB}}$ ($\sim\mathbf{4.0\text{ GiB}}$ at 4-bit incl. group metadata), while BF16 KV state costs $\kappa_{\text{GQA}} = 2\times 32\times 8\times 128\times 2 = \mathbf{128\text{ KiB/token}}$. Using binary $1\text{ Ki} = 1{,}024$ tokens:

  • BF16 weights: $(B\cdot S)^* = 14.96\text{ GiB} / 128\text{ KiB} = \mathbf{119.7\text{ Ki}}$ tokens ($122{,}528$ decimal tokens, roughly $120\text{ Ki}$).
  • 4-bit quantized weights: Shrinking weights nearly fourfold drops the crossover to $4.0\text{ GiB} / 128\text{ KiB} = \mathbf{32.0\text{ Ki}}$ tokens ($32{,}768$ tokens)—a single 32K prompt or four 8K requests pushes KV past weights.

Contrast this with weight-tied looping from Are Looped Transformers Worth It at Equal Compute? Huginn, Ouro, and SMELT (shrinking 8B weights by $4\times$ while materializing all 32 effective KV layers). Using exact 14.96 GiB BF16 weights, three deployment points trace the full shift:

Regime (LLaMA-3 8B)KV SizeWeight Share (BF16 / 4-bit)4× Loop Savings (BF16 / 4-bit)Dominant Bottleneck
A. Phone chat · 1×4K$0.5\text{ GiB}$$96.8\%$ / $88.9\%$−72.6% / −66.7%Weights dominate; looping saves huge VRAM
B. Single long · 1×128K$16.0\text{ GiB}$$48.3\%$ / $20.0\%$−36.2% / −15.0%KV already exceeds weights; near 50/50
C. Cloud batch · 64×16K$128.0\text{ GiB}$$10.5\%$ / $3.0\%$−7.8% / −2.3%KV dominates; unshared-KV loop saves little
Interactive Calculator · VRAM, SI/IEC Units & KV Crossover
Static Weights (IEC GiB) vs. Dynamic KV Cache (IEC GiB) Weights: 14.96 GiB (16.1 GB SI) · KV Cache: 16.0 GiB · Total: 31.0 GiB KV Crossover (B·S)* ≈ 120K binary tokens Loop Decode (K× core > 50 MB L2): reads 16.1 GB/tok total from HBM
Batch B 1 Seq Len S 128K Loops K 4 KV > Weights
Figure 2: Adjust model, precision (BF16/FP8), batch B, length S, and loop count K to inspect the exact binary KV crossover and HBM traffic.

3.2 How MLA and Hybrid Linear Attention Shift the Crossover#

Notice an important second-order synergy: Regime C assumes KV stays in full-size GQA. Once architectural compression—such as MLA, Gated DeltaNet (GDN), or Kimi Delta Attention (KDA)—slashes KV Cache by 80%–95% (or bounds linear-layer state to constant $O(1)$ size), the crossover $(B\cdot S)^*$ shifts dramatically back to the right, making weights dominate VRAM again and reviving the memory leverage of Looped weight tying even at long contexts.

Original figure (DeepSeek-AI, 2024, Fig. 2): DeepSeek-V3 basic architecture combining DeepSeekMoE (shared plus routed experts) and Multi-Head Latent Attention (MLA) with compressed inference KV cache (arXiv:2412.19437).
Figure 3: DeepSeek-V3 basic architecture combining DeepSeekMoE (shared plus routed experts) and Multi-Head Latent Attention (MLA) with compressed inference KV cache (arXiv:2412.19437). (Source: DeepSeek-AI, 2024, Fig. 2)

Walking through the DeepSeek-V3 architecture diagram above ties the equations directly to the tensor layout: in the bottom MLA panel, input hidden states $h_t$ down-project to a shared low-rank latent $c_t^{KV}$ ($d_c = 512$) alongside a decoupled RoPE key $k_t^R$ ($d_R = 64$). The hatched Cached During Inference circles mark exactly $c_t^{KV}$ and $k_t^R$, meaning each token stores only $d_c + d_R = 576$ dimensions (equivalent to $2.25$ 128-D K/V head pairs) instead of materializing expanded per-head keys $k_{t,i}^C$ and values $v_{t,i}^C$. The top DeepSeekMoE panel pairs green shared experts with blue Top-$K_r$ routed experts, illustrating the sparse FFN split from Section 2.

At binary $300\text{ Ki} = 307{,}200$ tokens ($B=1$, $d_{\text{head}}=128$), a single KV head in one layer costs $2\times 307{,}200\times 128\times 2 \approx \mathbf{0.1465\text{ GiB}}$ in BF16, yielding a fast mental-math rule:

$$\text{300K BF16 KV} \approx L \cdot n_{\text{kv}} \cdot 0.1465\text{ GiB}$$

Model ProfileLayers & KV LayoutEff. KV Heads300K BF16 KV300K FP8 KV
LLaMA-3 8B32L GQA-8$8$$37.5\text{ GiB}$$18.8\text{ GiB}$
70B Dense80L GQA-8$8$$93.8\text{ GiB}$ · >80G GPU$46.9\text{ GiB}$
2T MoE (GQA)100L GQA-8$8$$117.2\text{ GiB}$$58.6\text{ GiB}$
2T MoE (MLA)100L MLA · 576 latent$2.25$$33.0\text{ GiB}$$16.5\text{ GiB}$ · 7.1× less vs. GQA BF16
DeepSeek-V361L MLA · 576 latent$2.25$$20.1\text{ GiB}$$10.1\text{ GiB}$
Left: 16 B/param Training State Breakdown; Right: 300K Binary Context KV Cache (GiB) 1. Mixed-Precision AdamW (16 B/param) 2BBF16 W2BGrad4BFP32 Master4BAdam m4BAdam v Replacing 2 B grad with FP32 accum → 18 B/param (20 B if both) 2. B=1, S=300K KV Footprint (GiB) 2T 100L GQA-8117.2 GiB70B 80L GQA-893.8 GiB100L MLA (576)33.0 GiBDS-V3 61L MLA20.1 GiBDS-V3 FP810.1 GiB
Figure 4: 16 B/param AdamW training state breakdown and 300K binary context KV Cache footprints.

At 300K context, 70B Dense BF16 KV alone (93.8 GiB) exceeds a single 80G (~79.65 GiB) GPU. Switching a 100-layer 2T MoE from BF16 GQA-8 (117.2 GiB) to FP8 MLA (16.5 GiB) reduces KV capacity by 85.9% and cuts per-token Decode KV read traffic by ~7× (7.1×).

Original figure (Ainslie et al., 2023, Fig. 6): Inference time per sample on GQA-XXL across group counts, comparing MHA, GQA, and MQA KV-read bandwidth savings (arXiv:2305.13245).
Figure 5: Inference time per sample on GQA-XXL across group counts, comparing MHA, GQA, and MQA KV-read bandwidth savings (arXiv:2305.13245). (Source: Ainslie et al., 2023, Fig. 6)

The GQA latency curve above visualizes how KV-read bandwidth governs decode latency: between groups = 1 and 8 (MQA to GQA-8), blue GQA latency hugs the orange MQA floor near ~0.6 s, before climbing sharply at 32 and 64 groups to meet the dashed MHA ceiling near ~2.4 s.


4. The Looped Decode HBM Bandwidth Bottleneck#

During low-batch or single-request ($B = 1$) autoregressive decoding, GPU tensor cores sit mostly idle, waiting on HBM transfers. Latency per generated token is bounded by the total bytes streamed out of HBM—both model weights and the resident $B \cdot S \cdot \kappa$ bytes of historical KV Cache.

It is tempting to assume that compressing a model of effective-depth weight size $M_W$ by $K\times$ via a $K$-pass shared core of size $M_W / K$ cuts Decode weight transfers proportionally. That assumption fails on standard GPUs because even a $K\times$ compressed shared core of several gigabytes vastly exceeds GPU on-chip L2 cache (just $50\text{ MB}$ across an entire NVIDIA H100). Weights streamed at the start of pass 1 are evicted from L2 long before pass 1 finishes, forcing the memory controller to re-stream the shared core from HBM on passes $2 \dots K$. Including historical KV reads, total decode HBM traffic per token is:

$$\text{Decode HBM Traffic} = K \cdot \frac{M_W}{K} + B \cdot S \cdot \kappa = M_W + B \cdot S \cdot \kappa$$

This directly connects back to Regime B in Section 3: at short context (1 × 4K, 0.5 GiB KV), $M_W$ dominates; at long context (1 × 128K on LLaMA-3 8B), reading 16.0 GiB of KV exceeds the 14.96 GiB weight read itself. Meanwhile, looped decoding still transfers the full $M_W$ weight bytes over HBM while adding inter-pass kernel launch and normalization overhead.

Left: BF16 (120K) & 4-Bit (32K) Crossover; Right: Loop Decode HBM Traffic Bound 1. Static Weight vs. KV Crossover (LLaMA-3 8B) BF16 (14.96 GiB) 4-bit (4.0 GiB) KV 32K 120K Total Context (B · S) 2. Loop Decode Weight Traffic (Batch=1) HBM Core Weight M_W/K (>1 GB) × K Every pass misses L2 and streams HBM 50 MB L2 (Miss) Shared core M_W/K ≫ 50 MB H100 L2 cache Total read = K · (M_W/K) = M_W (no savings)
Figure 6: BF16 (120K) vs. 4-bit (32K) KV crossover curves and Loop Decode HBM traffic bound (following Kipply, 2022 and DeepMind Scaling Book).

Sizing a 70B BF16 checkpoint (140 GB ≈ 130.4 GiB) against real hardware also highlights why single-card H100 estimates fail: 140 GB exceeds a single 80 GB (79.65 GiB) H100's VRAM. Two physically valid deployments bound real latency:

The 14-line script below puts 1× H200 and TP=4 H100 on equal footing with 16K KV, alongside the 8B crossover and NVLink AllReduce byte transfer:

PYTHON
# VRAM, KV crossover, TP AllReduce & Loop decode check
gb_to_gib = 1e9 / (1024 ** 3)
mw_gib = 8.03 * 2 * gb_to_gib  # 8.03B BF16 -> 14.96 GiB
kappa = 2 * 32 * 8 * 128 * 2   # 128 KiB/token (GQA-8)
bs_star = mw_gib * (1024 ** 3) / kappa
kv_128k = 131072 * kappa / (1024 ** 3)

# 70B BF16 (140 GB) + 16K KV (5.37 GB): 1x H200 vs 4x H100
t_h200 = (140.0 + 5.37) / 4.8       # 141 GB, 4.8 TB/s
t_tp4 = (140.0 + 5.37) / (4 * 3.35) # TP=4, 13.4 TB/s
ar_b = 2 * (3 / 4) * (8192 * 2) * 2 * 80  # 80L x 2 AR
ar_ms = ar_b / 450e9 * 1000    # 450 GB/s unidir NVLink4
print(f"8B cross={bs_star/1024:.1f}Ki, 128K={kv_128k:.1f}GiB")
print(f"H200={t_h200:.1f}ms, TP4={t_tp4:.1f}ms + AR={ar_ms:.3f}ms")

Breaking the HBM weight-bandwidth wall requires one of two hardware or scheduling shifts:

  1. Pinning weights in on-chip SRAM: Specialized architectures such as Cerebras WSE (tens of GB of wafer-scale SRAM) or Groq LPUs avoid off-chip HBM reads altogether. However, SRAM capacity density is low: a single Groq LPU carries only ~230 MB of on-chip SRAM, so serving a 70B FP8 model (~70 GB) requires ~300+ interconnected LPUs (70 GB / 0.23 GB ≈ 304, plus KV/workspace) across racks.
  2. Cross-iteration GPU batching: Scheduling techniques such as Parallel Loop Transformer (PLT)—analyzed in Sharing KV Cache Across Depth and Loops: SWA, YOCO, PLT, and FlashLoop—batch different loop passes across consecutive sequence positions so one HBM read of size $M_W / K$ advances $K$ iterations simultaneously.

Taken together, the capacity–bandwidth ledger yields three practical rules: (1) compute $(B\cdot S)^*$ before optimizing—cloud high-concurrency needs MLA or linear attention to shrink KV before weight sharing helps, while edge short-context benefits from weight tying or W4A16 directly; (2) weight quantization beats unconditioned looping for latency—dropping BF16 to W4A16 cuts both weight bytes and the crossover fourfold; and (3) never expect loop weight tying to cut single-GPU latency on its own once the shared core exceeds 50 MB L2.


References#

  1. Kipply (2022). Transformer Inference Arithmetic. kipp.ly/transformer-inference-arithmetic
  2. Jacob Austin, Sholto Douglas, Roy Frostig, Anselm Levskaya, Charlie Chen, Sharad Vikram, Federico Lebron, Peter Choy, Vinay Ramasesh, Albert Webson & Reiner Pope (2025). How to Scale Your Model: A Systems View of LLMs on TPUs. jax-ml.github.io/scaling-book
  3. Joshua Ainslie et al. (2023). GQA: Training Generalized Multi-Query Transformer Models from Multi-Head Checkpoints. arXiv:2305.13245
  4. DeepSeek-AI (2024). DeepSeek-V3 Technical Report. arXiv:2412.19437
Cite this note
@misc{liu2026llmmemoryarithmeti,
  title  = {{Memory Arithmetic for LLM Serving: TP/EP, KV Crossover, and Looped Decode}},
  author = {Feiyang Liu},
  year   = {2026},
  month  = {nov},
  url    = {https://zaychikliu.github.io/blog/2026/llm-memory-arithmetic-and-loop-bandwidth/}
}