Skip to content

4200: KV Cache Optimization

⏱ 4 min read·📅 Last updated: 2026-10-22

Module Overview

This module covers advanced techniques for optimizing the Key-Value cache in transformer models, enabling efficient handling of long contexts and faster inference.

Why This Matters:

  • KV cache dominates memory during generation (often >80% of memory)
  • Context window is a key differentiator for LLM applications
  • Efficient caching enables larger batch sizes and longer sequences
  • Speculative decoding can 2-3x generation speed
  • PagedAttention's block tables and prefix caching turn wasted reservation into schedulable memory

Learning Objectives

After completing this module, you will be able to:

  • KV Cache Fundamentals: Understand attention caching mechanics
  • Context Window Physics: Calculate memory requirements and optimize context length
  • Speculative Decoding: Implement draft-target models for faster generation
  • Cache Compression: Apply quantization and pruning to KV cache
  • Multi-Query Attention: Optimize cache with shared key/value projections
  • Cache Paging: Manage KV memory with block tables, prefix caching, and token budgets

Module Contents

4201: Context Window Physics

KV Cache Memory & OOM Prevention

  • KV cache memory calculation and analysis
  • KV cache quantization
  • Multi-round attention
  • Context window extension
  • OOM prevention and memory optimization strategies

Experiments:

  • Profile KV cache memory usage
  • Test context window limits
  • Benchmark long-context performance

4202: Speculative Decoding

Accelerating Generation with Draft Models

  • Speculative decoding theory
  • Draft model selection and training
  • Token-level verification
  • Multi-candidate speculation
  • Integration with sampling

Experiments:

  • Implement speculative decoding
  • Compare draft model strategies
  • Measure speedup vs quality
  • Optimize verification overhead

4204: KV-Cache Paging and Prefix Caching

The Page Manager: PagedAttention, Prefix Caching, and the Token Budget

  • The fragmentation ledger: contiguous versus paged reservation
  • Block tables: logical-to-physical mapping and the free list
  • The KV-head sharing ledger: MHA, GQA, MQA cache arithmetic
  • Automatic prefix caching and copy-on-write
  • Chunked prefill and the decode token budget

Experiments:

  • Implement PagedAttention
  • Trace a block table across appends into a fourth physical block
  • Price MHA-versus-GQA-versus-MQA cache at an 8k, batch-32 decode
  • Chunk a 2,048-token prefill under a 512-token iteration budget

4203: Context Window Optimization

Hands-On Context & Cache Implementations (Guide)

  • KV cache quantization (implementation)
  • Sliding window attention (implementation)
  • Multi-round context management (implementation)
  • Streaming with long context (implementation)
  • Context chunking strategy (implementation)

Guide: guides/4203-Context-Window-Optimization.md

Prerequisites

Before starting this module, ensure you have:

Review: PREREQUISITES.md for detailed requirements.

Assessment

Quiz

  • Format: 20 multiple-choice questions
  • Passing Score: 80% (16/20)
  • Topics: KV cache memory, cache quantization, context windows, speculative decoding
  • Location: 4200: KV Cache & Context Window - Quiz

Practice Exercises

  • Format: Advanced optimization projects
  • Duration: 3 hours
  • Topics:
    • Implement KV cache profiling
    • Build speculative decoding system
    • Optimize for long contexts
    • Compare cache strategies
  • Location: 4200: KV Cache Optimization - Practice

This module connects to:

Time Commitment

Activity Time
4201: Context Window Physics 4 hours
4202: Speculative Decoding 4 hours
4204: KV-Cache Paging and Prefix Caching 4 hours
Guide (4203) 6 hours
Quiz 30 minutes
Practice 3 hours
Total 21.5 hours

Resources

Essential Tools:

  • vLLM (PagedAttention)
  • TGI (Text Generation Inference)
  • llama.cpp (cache quantization)
  • TensorRT-LLM

Essential Papers:

  • "Accelerating Large Language Model Decoding with Speculative Sampling"
  • "Efficient Attention: Attention with Linear Complexities"
  • "PagedAttention: Efficient Attention for LLMs"

KV Cache Memory Analysis

For a model with:

  • Hidden size: 4096
  • Layers: 32
  • Context length: 8192

Memory calculation:

KV Cache Memory = 2 × layers × hidden × context × bytes_per_param
                = 2 × 32 × 4096 × 8192 × 2 (FP16)
                ≈ 4 GB per batch

Context Window Comparison

Model Context KV Cache (FP16) KV Cache (INT8)
LLaMA-7B 2K 1 GB 0.5 GB
LLaMA-7B 8K 4 GB 2 GB
LLaMA-7B 32K 16 GB 8 GB
LLaMA-70B 8K 40 GB 20 GB
LLaMA-70B 32K 160 GB 80 GB

Speculative Decoding Speedup

Draft Model Speculation Speedup Quality Impact
None 1x 1.0x None
Tiny (1B) 5-8 tokens 1.8-2.2x Minimal
Small (3B) 8-12 tokens 2.2-2.8x Minimal
Same size 12-16 tokens 2.5-3.2x Minimal

Tips for Success

  1. Profile first: Measure cache usage before optimizing
  2. Start with quantization: INT8 cache is easy and effective
  3. Consider your workload: Batch size matters for cache strategies
  4. Use PagedAttention: vLLM is production-ready
  5. Experiment with speculation: Draft models need tuning

Common Pitfalls

  1. Ignoring memory bandwidth: Cache is often bandwidth-bound
  2. Over-optimizing: Speculative decoding has overhead
  3. Wrong cache format: Some formats don't support all features
  4. Forgetting batch size: Per-token cache matters for batching
  5. Not measuring: Profile real workloads, not synthetic benchmarks

Cache Optimization Strategy

Start here:

  1. Profile current KV cache memory
  2. Apply INT8 quantization to cache
  3. Enable PagedAttention if using vLLM
  4. Consider MQA/GQA if training models

Advanced:

  1. Implement speculative decoding
  2. Add prefix caching for repeated prompts
  3. Experiment with cache eviction policies

Next Module: 4300: Quantization-Aware Training

Previous Module: 4100: Low-Bit Quantization

Questions? Review the PREREQUISITES.md or check Phase 4 documentation.