LLM Architecture

Where 32K Prefill Memory Spikes Come From—and the I/O Tax of Chunked Prefill

Contents
  1. 1. Operator-Level Memory Breakdown for a 70B 32K Prefill
  2. 2. Transient Buffer Reuse and Eliminating $O(N^2)$ Attention Materialization
  3. 3. Chunked Prefill: Peak Flattening vs. Weight & History-KV I/O
  4. 3.1 Chunked I/O Bounds: Quadratic History KV vs. the 26× Weight Tax
  5. 3.2 Production Stacks: PagedAttention, Prefix Caching, and PD Disaggregation
  6. References

Even after sizing static weights (140 GB) and resident 32K KV Cache conservatively on an 8× H100 node, serving long prompts or concurrent Prefill requests (the prompt pass governing Time-to-First-Token, TTFT) still triggers unexpected OOMs once frameworks such as vLLM pre-allocate nearly all spare VRAM into the KV pool (via --gpu-memory-utilization), leaving thin headroom for transient activations. Shaving a 5.75 GiB single-layer SwiGLU spike down to ~360 MiB via Chunked Prefill, however, triggers more than 80.5 GB of quadratic history-KV rereads—it forces 2.10 TB of extra weight streaming across HBM, a 26× larger I/O tax.


1. Operator-Level Memory Breakdown for a 70B 32K Prefill#

To trace every byte cleanly, take layer $\ell$ of a standard 80-layer 70B GQA-8 model running in BF16 on a single sequence of length $N = 32{,}768$ tokens ($d = 8192$, $n_q = 64$ query heads, $n_{\text{kv}} = 8$ KV heads, $d_{\text{head}} = 128$, SwiGLU intermediate width $d_{\text{ffn}} = 28672$). Static weights (~140 GB in BF16) typically shard across Tensor Parallelism degree $T_p \in \{4, 8\}$; the table reports aggregate multi-GPU bytes alongside per-GPU scaling:

Operator StageGlobal ShapeLifetimeAggregate / Per-GPU
Input $X$ & $Q$ Projection$[N, 8192]$Transient (per layer)$512\text{ MiB}$ each · $Q \div T_p$
KV Write to Cache$2\times [N, 1024]$Persistent for session$128\text{ MiB/L}$ · $10\text{ GiB}$ / 80L $\div T_p$
FlashAttention (Tiled)SRAM $[B_r, B_c]$Avoids $[64, N, N]$ scores$512\text{ MiB}$ out · avoids $128\text{ GiB}$
SwiGLU Gate + Up$2\times [N, 28672]$Peak transient spikeAgg $5.75\text{ GiB}$ · 0.5 + 5.25/T_p ($1.81 / 1.16\text{ GiB/GPU}$)
LM Head Last Slice$[1, V]$Last position logits$250.5\text{ KiB}$ · $8\text{ GiB}$ BF16 un-sliced

Checking each number by hand confirms the arithmetic:


2. Transient Buffer Reuse and Eliminating $O(N^2)$ Attention Materialization#

A common mistake when auditing the table above is multiplying the ~5 GiB activation footprint by $L = 80$ layers (~400 GiB). That factor of $L$ applies only during training (without activation checkpointing), where forward activations must stay pinned until backpropagation finishes. During inference Prefill, no backward pass follows:

  1. Transient activation memory caps at the widest single layer, independent of $L$: Once Layer 1 finishes its MLP down-projection and adds the update back into the residual stream, the allocator immediately recycles that $4.5\text{–}5.75\text{ GiB}$ workspace for Layer 2. Total peak activation memory across all 80 layers therefore stays bounded by the widest single-layer temporary footprint, while only the persistent KV Cache grows linearly with $L$ to $10\text{ GiB}$.
  2. FlashAttention (Dao et al., 2022) removes $O(N^2)$ global HBM allocation: Without tiled online Softmax, materializing $[64, 32{,}768, 32{,}768]$ attention scores for 64 query heads consumes $128\text{ GiB}$ in BF16—or $256\text{ GiB}$ in FP32 if upcast for numerical stability—overflowing single-GPU memory inside the very first layer. FlashAttention streams $Q, K, V$ tiles into fast on-chip SRAM, maintains exact running normalization statistics via online Softmax rescaling, and writes only the final $512\text{ MiB}$ attention output back to HBM—accelerating GPT-2 attention by $7.6\times$ (~16.5 ms → ~2.2 ms) and end-to-end GPT-2 training by ~3×.
Original figure (Dao et al., 2022, Fig. 1): FlashAttention GPU SRAM-HBM tiling workflow computing exact attention on-chip without materializing the N x N score matrix in HBM (arXiv:2205.14135).
Figure 1: FlashAttention GPU SRAM-HBM tiling workflow computing exact attention on-chip without materializing the N x N score matrix in HBM (arXiv:2205.14135). (Source: Dao et al., 2022, Fig. 1)

Examining the three panels in Figure 1 clarifies how FlashAttention-1 and FlashAttention-2 tile across the memory hierarchy: the left pyramid summarizes A100 bandwidths (SRAM: 19 TB/s (20 MB), HBM: 1.5 TB/s (40 GB), CPU DRAM: 12.8 GB/s); the center diagram shows FlashAttention-1's Outer Loop: K,V / Inner Loop: Q tiling, which keeps $N \times N$ scores out of HBM (whereas FlashAttention-2 reverses the loop order to an outer $Q$ loop and inner $K,V$ loop to eliminate repeated $Q$ and output $O$ HBM writes); and the right-hand GPT-2 bar chart shows unfused PyTorch kernels (Matmul + Mask + Softmax + Dropout + Matmul at ~16.5 ms) collapsing into a single fused kernel at ~2.2 ms.

  1. Final-token slicing before lm_head: Autoregressive prefill needs next-token probabilities strictly for the last prompt token at index $N$. Slicing $[1, 8192]$ and computing $[1, 8192] \mathbin{@} [8192, V]$ to yield logits of shape $[1, V]$ across a LLaMA-3 vocabulary ($V = 128{,}256$) emits just $250.5\text{ KiB}$ ($128{,}256 \times 2\text{ B}$, or $256\text{ KiB}$ when padded to $2^{17} = 131{,}072$); projecting all $32{,}768$ prompt positions unnecessarily creates an $8\text{ GiB}$ BF16 / $16\text{ GiB}$ FP32 logits spike at the end of the forward pass.
70B (TP=T_p) Layer-ℓ 32K Prefill Lifecycle: Reusable Arena vs. Persistent KV Per-layer transient activation arena (reused across all L layers) Q, K, V Proj K,V: 128M/L (÷T_p) FlashAttention SRAM tile (no O(N²)) SwiGLU FFN Peak 0.5 + 5.25/T_p GiB/GPU Free & Reuse next layer LM Head Last Slice [1, 8192] × V_128K 250.5 KiB (vs. 8–16G full) Persistent HBM KV Pool: 80 × 128 MiB = 10 GiB total (10/T_p GiB/GPU)
Figure 2: Reusable per-layer activation arena versus persistent layer-accumulated KV Cache in 32K prefill.

3. Chunked Prefill: Peak Flattening vs. Weight & History-KV I/O#

Once concurrency climbs to eight 32K prompts (256K batched tokens) or single prompts scale to 256K–1M tokens, even a single layer's SwiGLU workspace inflates past $46\text{ GiB}$. At the same time, running one monolithic multi-hundred-millisecond prefill stalls inter-token latency (ITL) for all decode requests waiting in the scheduler.

Chunked Prefill—popularized by Sarathi-Serve (Agrawal et al., 2024)—slices an $N$-token prompt into $M = N / C$ sequential chunks of length $C$ (such as splitting 32,768 tokens into $M = 16$ chunks of $C = 2{,}048$), while piggybacking active decode tokens into every chunk's batch. Under strict P99 Time-Between-Tokens (TBT) latency SLOs, Sarathi-Serve boosts serving capacity by 2.6× on Mistral-7B (1× A100), up to 3.7× on Yi-34B (2× A100), and up to 5.6× on Falcon-180B (8× A100), while scaling per-layer SwiGLU activation peaks down by $1/M$ (from $\sim 5.75\text{ GiB}$ to $\sim 360\text{ MiB}$).

Original figure (Agrawal et al., 2024, Fig. 8): Sarathi-Serve chunked prefill and decode coalesced pipeline schedule eliminating generation stalls compared with Orca (arXiv:2403.02310).
Figure 3: Sarathi-Serve chunked prefill and decode coalesced pipeline schedule eliminating generation stalls compared with Orca (arXiv:2403.02310). (Source: Agrawal et al., 2024, Fig. 8)

3.1 Chunked I/O Bounds: Quadratic History KV vs. the 26× Weight Tax#

Tracking total HBM I/O across the chunk loop exposes three bandwidth tradeoffs:

  1. Zero reduction in persistent KV capacity: Once all $M = 16$ chunks finish, the resident KV Cache still occupies the exact same $10\text{ GiB}$.
  2. Quadratic history-KV rereads—and a $26\times$ larger weight-reread tax: Under causal masking, evaluating chunk $m$ must reload all prior KV blocks $1 \dots m-1$ from HBM and re-stream all 80 layers of static weights ($M_W = 140\text{ GB}$) across every chunk boundary:

$$\begin{aligned} \text{Extra KV Rereads} &= \sum_{m=1}^{M} (m-1)\, C\kappa = \frac{M(M-1)}{2}\, C\kappa \approx \frac{N^2}{2C}\,\kappa, \\ \text{Extra Weight Rereads} &= (M - 1)\, M_W \end{aligned}$$

With $N=32\text{K},\; M=16,\; C=2{,}048,\; \kappa=320\text{ KiB/tok}$: each chunk holds $C\kappa = \mathbf{0.625\text{ GiB}}$, so 16 chunks trigger $\frac{16\times 15}{2} = 120$ redundant chunk KV rereads ($\mathbf{75\text{ GiB}} \approx \mathbf{80.5\text{ GB}}$, 85 GiB total with current-chunk reads). Meanwhile, 15 extra full weight passes force $(16-1)\times 140\text{ GB} = \mathbf{2.10\text{ TB}}$ (2.24 TB total)—meaning weight rereads exceed extra KV rereads by ~26× at 32K! Setting both terms equal yields $N^* = \frac{2 M_W}{\kappa} \approx \mathbf{854\text{K tokens}}$, which strictly marks where cumulative KV rereads overtake extra weight streaming—not an automatic stall cliff.

Accounting for quadratic causal attention FLOPs completes the compute-overlap ledger: on TP=4 H100 (3,956 TFLOPS BF16, 13.4 TB/s HBM3), 70B 32K prefill executes ~4.59 PFLOPs of linear GEMMs plus 1.41 PFLOPs of causal $QK^\top/PV$ attention (~6.00 PFLOPs total, ~1.52 s compute floor) against ~174 ms raw HBM time (~157 ms extra weights + ~6 ms extra KV). Latency stalls appear when $C$ drops below the Roofline ridge ($C < B_{\text{crit}} \approx 295$) or when single-chunk history KV ($m C \kappa$) grows large enough at multi-hundred-K context to expose memory reads.

  1. Chunk length must clear the Roofline ridge point $C \ge B_{\text{crit}}$: As derived in Why Token Skipping Fails on GPUs: The Roofline Ridge, MoD, CDB, and Null Experts, falling below the H100 BF16 ridge point ($B_{\text{crit}} \approx 295$) puts chunks into the weight-bandwidth regime and inflates TTFT. Consequently, vLLM and Sarathi-Serve keep chunk sizes within $C \in [512, 2048]$.

The 12-line script below verifies the non-uniform TP SwiGLU spike, 26× weight vs. KV traffic, 1.41 PF attention FLOPs, and $N^*$:

PYTHON
# 70B 32K SwiGLU TP spike & Chunked Prefill I/O + Attn FLOPs
N, C, L, d, d_ffn = 32768, 2048, 80, 8192, 28672
M, gib = N // C, 1024 ** 3
tp4_gib = (N * d * 2 + 3 * N * d_ffn * 2 / 4) / gib
tp8_gib = (N * d * 2 + 3 * N * d_ffn * 2 / 8) / gib
kappa = 2 * L * 8 * 128 * 2  # 320 KiB/token
kv_gb = (M * (M - 1) / 2) * C * kappa / 1e9
w_tb = (M - 1) * 140.0 / 1000.0
attn_pf = L * 64 * 2 * (N * N) * 128 / 1e15
print(f"SwiGLU TP4={tp4_gib:.2f}GiB, TP8={tp8_gib:.2f}GiB")
print(f"KV={kv_gb:.1f}GB vs W={w_tb:.2f}TB, Attn={attn_pf:.2f}PF")
Full vs. Chunked Prefill: Trading Triangular History KV Rereads for Flat Activation Peaks Full Prefill (1× weights) ~5 GiB Transient peak 1× KV · 1× weights Chunked Prefill (First 8 Chunks Ch1–Ch8) Chunk act (~360 MiB) 1× Base KV (0.625 GiB) Extra history reread Ch1Ch2Ch3Ch4Ch5Ch6Ch7Ch8 Ex: N=32K, C=2K (M=16) → 75 GiB KV + 2.10 TB weight rereads (26×)
Figure 4: Chunked Prefill trading flat per-chunk activation peaks for quadratic KV and 26× weight rereads.

3.2 Production Stacks: PagedAttention, Prefix Caching, and PD Disaggregation#

In production serving stacks, Chunked Prefill integrates tightly with PagedAttention block management and sits alongside two complementary baselines:

Original figure (Kwon et al., 2023, Fig. 6): vLLM PagedAttention block-table translation from logical KV blocks to non-contiguous physical GPU DRAM blocks (arXiv:2309.06180).
Figure 5: vLLM PagedAttention block-table translation from logical KV blocks to non-contiguous physical GPU DRAM blocks (arXiv:2309.06180). (Source: Kwon et al., 2023, Fig. 6)

For agent workloads with shared system or tool prefixes, turn on APC / RadixAttention first to skip redundant weight and history-KV reads entirely, then apply Chunked Prefill at $C \in [512, 2048]$ to cap activation spikes; only move to PD Disaggregation once extreme prompt lengths and strict P99 TBT SLOs justify duplicating full model weights across dedicated pools.


References#

  1. Tri Dao et al. (2022). FlashAttention: Fast and Memory-Efficient Exact Attention with IO-Awareness. arXiv:2205.14135
  2. Amey Agrawal et al. (2024). Taming Throughput-Latency Tradeoff in LLM Inference with Sarathi-Serve (Chunked Prefill). arXiv:2403.02310
  3. Woosuk Kwon et al. (2023). Efficient Memory Management for Large Language Model Serving with PagedAttention (vLLM). arXiv:2309.06180
  4. Lianmin Zheng et al. (2024). SGLang: Efficient Execution of Structured Language Model Programs (RadixAttention). arXiv:2312.07104
Cite this note
@misc{liu2026prefillactivations,
  title  = {{Where 32K Prefill Memory Spikes Come From—and the I/O Tax of Chunked Prefill}},
  author = {Feiyang Liu},
  year   = {2026},
  month  = {nov},
  url    = {https://zaychikliu.github.io/blog/2026/prefill-activations-and-chunked-io/}
}