Tri Dao et al. (2022). FlashAttention: Fast and Memory-Efficient Exact Attention with IO-Awareness. arXiv:2205.14135
Amey Agrawal et al. (2024). Taming Throughput-Latency Tradeoff in LLM Inference with Sarathi-Serve (Chunked Prefill). arXiv:2403.02310
Woosuk Kwon et al. (2023). Efficient Memory Management for Large Language Model Serving with PagedAttention (vLLM). arXiv:2309.06180
Lianmin Zheng et al. (2024). SGLang: Efficient Execution of Structured Language Model Programs (RadixAttention). arXiv:2312.07104
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:
Checking each number by hand confirms the arithmetic:
Persistent KV accumulation: Each layer stores both $K$ and $V$ of shape $[32{,}768, 1024]$, occupying $2 \times 32{,}768 \times 1024 \times 2\text{ B} = 128\text{ MiB/layer}$. Across $80$ layers, aggregate KV capacity reaches $80 \times 128\text{ MiB} = 10{,}240\text{ MiB} = \mathbf{10\text{ GiB}}$, corresponding to $\kappa = \mathbf{320\text{ KiB/token}}$.
SwiGLU wide projections and non-uniform TP sharding: A single gate_proj or up_proj activation tensor of shape $[32{,}768, 28{,}672]$ in BF16 occupies $32{,}768 \times 28{,}672 \times 2\text{ B} = \mathbf{1.75\text{ GiB}}$. Without kernel fusion, the allocator concurrently holds the full-width replicated input residual $X$ (0.5 GiB, not sharded across T_p after AllReduce) alongside column-parallel Gate (1.75 GiB), Up (1.75 GiB), and the elementwise $\mathrm{SiLU}(\text{Gate})\odot\text{Up}$ buffer (1.75 GiB)—summing to $0.5 + 3 \times 1.75 = \mathbf{5.75\text{ GiB}}$ aggregate (or ~4.0 GiB when the product buffer is fused). Per GPU, SwiGLU scales as $0.5 + 5.25/T_p$: 1.81 GiB/GPU at TP=4 and 1.16 GiB/GPU at TP=8, rather than naive 5.75 / T_p.
MoE routing amplification: Replacing the dense FFN with a Top-$k$ Mixture-of-Experts block multiplies dispatch buffer rows by routing fan-out $k$: under Top-8 routing, active token rows entering expert GEMMs expand from $N$ to $8N = 262{,}144$, scaling transient dispatch workspaces accordingly.
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:
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}$.
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×.
⤢ Zoom
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.
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.
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}$).
⤢ Zoom
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:
Zero reduction in persistent KV capacity: Once all $M = 16$ chunks finish, the resident KV Cache still occupies the exact same $10\text{ GiB}$.
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:
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.
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^*$:
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:
Chunked Prefill + PagedAttention block writes: As each chunk finishes, its new KV states are written into non-contiguous physical GPU DRAM pages rather than monolithic contiguous buffers. In the PagedAttention diagram below, vLLM's PagedAttention (Kwon et al., 2023, arXiv:2309.06180) translates logical KV blocks (Block 0, 1, 2) for Request A via the Block Table into non-contiguous physical blocks (7, 1, 3), incrementing # filled dynamically across prefill chunks and subsequent decode tokens—cutting KV fragmentation waste from 60%–80% down to <4% and raising serving throughput by 2–4×.
Automatic Prefix Caching (APC / SGLang RadixAttention): In multi-turn chat, tool schemas, and repo-level agent loops, RadixAttention (Zheng et al., 2024, arXiv:2312.07104) indexes KV pages in a radix tree so requests sharing a prefix skip chunked prefill altogether—eliminating both the $M \cdot M_W$ weight rereads and quadratic history-KV rereads on the cached prefix.
PD Disaggregation: Routing long prefills onto dedicated compute-optimized nodes isolates decode ITL completely without Chunked Prefill's $M\times$ weight-read tax, at the cost of dual model weight replicas and cross-node RDMA KV transfer (~215 ms for 10 GiB over 400 Gbps InfiniBand).
⤢ Zoom
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.