Skip to content
E
Educatum

What are the primary bottlenecks in auto-regressive LLM inference, and how do standard optimizations mitigate them?

Curated by Educatum AI teamLast updated:
HardwareInference
Direct Answer

Autoregressive LLM inference splits into a compute-bound prefill phase that processes all prompt tokens in parallel, and a memory-bandwidth-bound decoding phase that generates one token per step while repeatedly loading model weights and the KV cache from HBM. Because decoding has very low arithmetic intensity, Tensor Cores sit underutilized waiting on memory transfers. Standard engines mitigate these bottlenecks with KV caching to avoid O(L²) recomputation, weight quantization to cut HBM bytes transferred, and continuous batching with PagedAttention to raise concurrency and eliminate KV memory fragmentation.

In auto-regressive Large Language Model (LLM) generation, execution is fundamentally split into two phases: a compute-heavy prefill phase and a memory-bandwidth-bound decoding phase. During the prefill phase, the entire prompt is processed in parallel with self-attention operating at O(L²) complexity, resulting in high arithmetic intensity and making this stage primarily compute-bound.

During decoding, the model generates one token at a time, requiring the inference engine to repeatedly read massive weight matrices from High Bandwidth Memory (HBM). Because the number of FLOPs performed per byte transferred is exceedingly low, the GPU cannot fully utilize its Tensor Cores, making the decoding stage strictly memory-bandwidth-bound. To mitigate this memory wall and improve hardware utilization, standard inference engines employ techniques that either reduce memory traffic or increase the useful work done per memory fetch:

  • KV Caching: Trades VRAM capacity for compute efficiency by storing the Key and Value vectors of past tokens in memory. During decoding, the model only computes the Query, Key, and Value for the newly generated token and reuses cached representations, avoiding redundant O(L²) recomputation.
  • Weight Quantization: Reduces the numerical precision of model weights (e.g., from 16-bit to 8-bit or 4-bit integers), lowering the number of bytes that must be fetched per parameter and thereby improving effective memory bandwidth utilization.
  • Batching: Processes multiple user requests simultaneously. By loading model weights once and using them to compute outputs across multiple sequence states, batching increases arithmetic intensity.

⚠️ Limitations & Caveats

  • KV caching consumes VRAM linearly with sequence length and batch size, frequently becoming the hard hardware limit on the number of concurrent requests a server can support.
  • Naive post-training quantization often struggles with activation outliers, where a small number of channels contain massive magnitudes that degrade accuracy. Specialized methods such as AWQ (Lin et al., 2023, arXiv:2306.00978) or GPTQ are required to mitigate this.

🧪 Core Insights & Mathematical Foundations

[Arithmetic Intensity]: I=Total FLOPsMemory Bytes Transferred[KV Cache Size per Token]: Stoken=2×nlayers×nkv_heads×dhead×precision bytes(nkv_heads=nheads in MHA; <nheads in GQA/MQA)[Total KV Cache Size]: Stotal=B×L×Stoken(B=Batch, L=Seq Len)[Symmetric Quantization]: Xint=clip⁡ ⁣(round⁡ ⁣(Xfloats), −(2b−1−1), 2b−1−1),s=max⁡(∣Xfloat∣)2b−1−1[Dequantization]: X~float=Xint×s\begin{aligned} & \text{[Arithmetic Intensity]: } I = \frac{\text{Total FLOPs}}{\text{Memory Bytes Transferred}} \\ & \text{[KV Cache Size per Token]: } S_{\text{token}} = 2 \times n_{\text{layers}} \times n_{\text{kv\_heads}} \times d_{\text{head}} \times \text{precision bytes} \quad (n_{\text{kv\_heads}} = n_{\text{heads}} \text{ in MHA; } < n_{\text{heads}} \text{ in GQA/MQA}) \\ & \text{[Total KV Cache Size]: } S_{\text{total}} = B \times L \times S_{\text{token}} \quad (B = \text{Batch},\, L = \text{Seq Len}) \\ & \text{[Symmetric Quantization]: } X_{\text{int}} = \operatorname{clip}\!\left(\operatorname{round}\!\left(\frac{X_{\text{float}}}{s}\right), \, -(2^{b-1}-1), \, 2^{b-1}-1\right), \quad s = \frac{\max(|X_{\text{float}}|)}{2^{b-1}-1} \\ & \text{[Dequantization]: } \tilde{X}_{\text{float}} = X_{\text{int}} \times s \end{aligned}

Follow-up 1: Explain the mechanics of the KV Cache and why it introduces memory fragmentation.

The KV Cache stores the Key and Value tensors produced by each self-attention layer for previously generated tokens. When the model predicts the token at position tt, it computes the Query, Key, and Value only for that specific position. The new Query is then compared against the cached Keys and Values from positions 11 to t−1t-1 to compute attention scores, reducing per-step attention compute from O(t2)O(t^2) to O(t)O(t) (and total LL-token generation from O(L3)O(L^3) to O(L2)O(L^2)).

In early inference implementations, memory for the KV cache was allocated as large contiguous blocks sized for the maximum possible sequence length. Because real user requests have highly variable and unpredictable lengths, this static allocation strategy leads to significant memory fragmentation.

This fragmentation manifests in two forms: Internal fragmentation occurs when a sequence terminates long before reaching the reserved maximum length, leaving unused memory trapped within the allocated block. External fragmentation occurs when free VRAM becomes divided into many small non-contiguous regions that cannot satisfy a large contiguous allocation request for a newly arriving sequence.

⚠️ Limitations & Caveats

  • Dynamically managing KV cache allocations introduces additional software scheduling complexity and CPU overhead in the inference engine.
  • If the aggregate KV cache exceeds available VRAM, the system may need to preemptively evict requests or offload KV states to CPU memory, which causes massive latency spikes and reduces tokens-per-second throughput.

Follow-up 2: How does Continuous Batching with PagedAttention solve the inefficiencies of standard static batching?

Standard static batching processes multiple sequences synchronously. Because all sequences must advance in lockstep, shorter requests idle while waiting for the longest sequence in the batch to finish, creating GPU utilization bubbles. Continuous batching (iteration-level scheduling) improves utilization by dynamically inserting new requests into the batch the moment another request finishes generation.

To handle the highly dynamic memory allocation requirements of continuous batching, frameworks like vLLM (Kwon et al., 2023, arXiv:2309.06180) address the issue using PagedAttention. PagedAttention divides the KV cache into fixed-size memory blocks (pages) that do not need to be physically contiguous in VRAM. Each sequence maintains a logical mapping to its blocks via a block table, analogous to how operating systems manage virtual memory.

Dimension Standard Static Batching Continuous Batching + PagedAttention
Computational Cost Low scheduling overhead Higher scheduling overhead
Data Requirements Efficient only when prompt lengths are uniform Efficient across highly diverse prompt lengths
Memory Efficiency High waste due to contiguous pre-allocation Near-zero waste (<4% internal fragmentation)
Known Failure Modes GPU idle bubbles, frequent OOM crashes Block table lookup overhead, hardware lock-in

⚠️ Limitations & Caveats

  • PagedAttention introduces a small but non-zero memory access overhead from block table lookups during every attention computation.
  • The technique relies on highly optimized custom CUDA kernels; adapting this architecture to non-NVIDIA accelerators (e.g., AMD ROCm, Google TPUs) requires significant low-level engineering effort.
View all →

Try keywords like RoPE, FlashAttention, DPO, or Blind 75.