Inference Optimization & Batching

Serving a model is limited by memory, not maths. Here is how to speed it up.

🔴 advanced5 min readinferenceserving
When a model writes text, its speed is usually limited by how fast the GPU can move data from memory, not by how fast it can calculate. Most speed-ups attack that. Continuous batching slots new requests in as soon as others finish. PagedAttention stores the model's working memory in small pages so less is wasted. Quantization stores weights in fewer bits, so there is less to move. FlashAttention avoids needless memory trips. Together they can serve five to twenty times more requests on the same hardware.

Compute-Bound vs Memory-Bound Regime

  PREFILL PHASE (Prompt Processing)                DECODE PHASE (Token Generation)
  - Processes full input prompt in parallel        - Generates 1 token at a time autoregressively
  - Matrix-Matrix Multiplication (GEMM)            - Matrix-Vector Multiplication (GEMV)
  - HIGH Arithmetic Intensity (FLOPs / Byte)       - LOW Arithmetic Intensity (~1 FLOP / Byte)
  - COMPUTE-BOUND (GPU Cores Saturation)           - MEMORY BANDWIDTH-BOUND (HBM Transfer Speed)

During the Decode Phase, GPU tensor cores sit idle waiting for weights to transfer from HBM to SRAM.

Key Inference Optimization Stack

                                  INFERENCE OPTIMIZATION STACK
┌──────────────────────────┬──────────────────────────┬──────────────────────────┬──────────────────────────┐
│  1. CONTINUOUS BATCHING  │    2. PAGEDATTENTION     │   3. FLASHATTENTION-2    │     4. QUANTIZATION      │
├──────────────────────────┼──────────────────────────┼──────────────────────────┼──────────────────────────┤
│ Iteration-level request  │ Virtual memory paging    │ SRAM tiling; avoids      │ FP16 ──► FP8 / INT4      │
│ scheduling; zero idle GPU│ for KV Cache; drops VRAM │ materializing N×N matrix │ 2x-4x lower VRAM & HBM   │
│ bubbles.                 │ fragmentation to < 1%.   │ in HBM.                  │ bandwidth transfer.      │
└──────────────────────────┴──────────────────────────┴──────────────────────────┴──────────────────────────┘

Continuous Batching vs Static Batching

  Static Batching (GPU idle waiting for Req 1 to finish 500 tokens):
  Req 1 (500 tokens)  ████████████████████████████████████████████████████
  Req 2 (50 tokens)   █████░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ (IDLE GPU VRAM!)

  Continuous Batching (vLLM schedules Req 3 as soon as Req 2 completes):
  Req 1 (500 tokens)  ████████████████████████████████████████████████████
  Req 2 (50 tokens)   █████
  Req 3 (New Ingest)       ███████████████████████████████████████████████ (100% GPU UTILIZATION!)

Say this out loud

"LLM token generation is memory bandwidth-bound because loading model weights for 1 token operation has low arithmetic intensity. We optimize serving throughput using Continuous Batching to schedule requests at the token iteration level, PagedAttention to eliminate KV cache memory fragmentation, and FP8/INT4 Quantization to shrink VRAM bandwidth load by 2-4x."

Follow-ups to expect

Check yourself

Question 1 of 3

Why is autoregressive LLM token generation memory bandwidth-bound rather than compute-bound?

More in LLMs & GenAI

See all →
Pretraining → SFT → RLHF5 minFine-Tune vs RAG vs Prompt: Choosing5 minRetrieval-Augmented Generation5 min