Skip to main content
Home › Categories › Artificial Intelligence & Computing › LLM Inference Optimization: FlashAttention, Paged KV-Cache, Continuous Batching, and Speculative Decoding

LLM Inference Optimization: FlashAttention, Paged KV-Cache, Continuous Batching, and Speculative Decoding

Author: ZUVDEX Team 4 min read
llm inference optimization flashattention kv cache prefill vs decode ttft tpot pagedattention vllm continuous batching grouped query attention gqa mla flashattention io aware sram tiling speculative decoding medusa eagle

Serving Large Language Models at production scale is governed by two distinct physical phases: compute-bound Prefill (processing all input prompt tokens in parallel to determine Time-To-First-Token, TTFT) and memory-bandwidth-bound Decode (generating output tokens sequentially one by one, governing Time-Per-Output-Token, TPOT). Modern inference engines—such as vLLM, TensorRT-LLM, and SGLang—achieve 10× to 25× higher throughput than naive PyTorch loops through IO-aware attention kernels, virtual-memory KV paging, continuous iteration batching, and lossless speculative decoding.

Key Highlights

  • FlashAttention fuses QKᵀ softmax V inside fast on-chip GPU SRAM tiles via online softmax rescaling, cutting attention HBM memory traffic from O(L²) to O(L) with zero mathematical approximation.
  • Grouped-Query Attention (GQA) and Multi-Head Latent Attention (MLA) shrink per-token KV-cache footprints by 4× to 16× compared with classical Multi-Head Attention.
  • PagedAttention eliminates 60%–80% KV-cache memory fragmentation by allocating non-contiguous fixed-size token blocks analogous to OS virtual memory.
  • Speculative Decoding drafts K candidate tokens cheaply with a tiny draft head/model and verifies all K tokens in parallel inside one target-model forward pass—accelerating generation 2×–3× with mathematically identical output distributions.

FlashAttention, KV-Cache Compression, and Paged Continuous Batching

Naive attention materializes an L × L score matrix in slow GPU High-Bandwidth Memory (HBM), exhausting memory at 16k tokens. Tri Dao’s FlashAttention tiles queries, keys, and values into tiny on-chip SRAM blocks (~20× faster than HBM) and tracks running maximums for exact softmax normalization without ever writing the L × L matrix to HBM.

During decoding, caching past Keys and Values avoids recomputing prompt tokens at every step, yet pre-allocating contiguous max-length buffers wastes most GPU RAM because completion lengths are unpredictable. Woosuk Kwon’s PagedAttention slices KV memory into small blocks mapped via page tables—unlocking huge batch concurrency alongside Continuous Batching (inserting new requests immediately as finished requests exit at each token iteration) and RadixAttention prefix sharing across shared system prompts.

Optimization Layer Bottleneck Solved Typical Gain Accuracy Impact
FlashAttention-2 / 3 Quadratic HBM read/write during attention 3×–5× faster long-prompt prefill 100% Exact (IEEE floating-point identical up to rounding)
Continuous Batching + Paged KV Idle GPU padding & fragmented KV buffers 8×–20× higher concurrent req/sec 100% Lossless
Prefix / Prompt Caching Re-prefilling identical system prompts & RAG docs Up to 80% lower TTFT & prefill cost 100% Lossless
Speculative Decoding (EAGLE / Draft) Idle GPU compute units during small-batch decode 1.8×–3.0× faster single-stream tokens/s 100% Lossless via rejection-sampling verification

Speculative Decoding and Disaggregated Prefill–Decode Clusters

When serving low-concurrency interactive coding assistants, GPUs sit 90% compute-idle waiting for HBM bytes during single-token decoding. Speculative decoding (and lightweight self-drafting heads like EAGLE-2 or Medusa) predicts 4 to 6 draft tokens ahead and checks all of them in one dense target forward pass—turning memory-bound vector-matrix products into high-utilization matrix-matrix operations while guaranteeing the exact target distribution via rejection sampling.

At hyperscale, mixing massive 64k-token prefills on the same GPU as live token decoding causes "generation stalls" (spikes in TPOT); chunked prefill and disaggregated Prefill–Decode GPU pools isolate TTFT compute bursts from smooth streaming generation.

  • Structure prompts with static instructions and shared documents at the exact front prefix so Radix/Prefix KV caching hits 95%+ reuse.
  • Quantize the KV cache to FP8 on Hopper/Blackwell GPUs to double effective batch concurrency on long contexts.
  • Set realistic max_tokens budgets and enable chunked prefill to guarantee p99 inter-token streaming smoothness.

FAQs

Does Speculative Decoding degrade output quality since a smaller draft model proposes tokens?

No—mathematical rejection sampling compares draft probabilities against exact target-model probabilities at every position, accepting matching tokens and resampling any discrepancy from the exact residual distribution so output quality is 100% lossless.

What is the difference between Multi-Query Attention (MQA), Grouped-Query Attention (GQA), and MLA?

MQA shares a single Key/Value head across all Query heads; GQA shares one KV head per group of 4–8 Query heads (preserving quality while shrinking KV cache 4×–8×); and Multi-Head Latent Attention (MLA) compresses Keys and Values into a low-rank joint latent vector for even tighter memory savings.

References & Sources

  • Dao, Tri et al. (2022) — FlashAttention: Fast and Memory-Efficient Exact Attention with IO-Awareness, NeurIPS.
  • Kwon, Woosuk et al. (2023) — Efficient Memory Management for Large Language Model Serving with PagedAttention (vLLM), ACM SOSP.
  • Leviathan, Yaniv; Kalman, Matan; Matias, Yossi (2023) — Fast Inference from Transformers via Speculative Decoding, ICML.