Skip to main content
Module 12 · Lesson 2 of 446/48 in the course~20 min
Module lessons (2/4)

PagedAttention & Virtual Memory KV-Cache

Lesson preview3 hands-on exercises readyAI Engineering & Production RAG · Module 12 — Local Serving, Quantization & Speculative Decoding

Eliminating memory fragmentation in GPU inference by allocating non-contiguous 16-token physical blocks.

What you'll learn in this lesson

  • Understand how Key-Value (KV) cache memory scaling impacts multi-user LLM serving concurrency.
  • Identify the root causes of memory fragmentation in traditional contiguous memory allocation.
  • Master PagedAttention virtual memory block tables inspired by operating system paging.
  • Calculate KV-cache memory footprints and fragmentation savings in Python.

Overview

In autoregressive Transformer generation, generating each new token requires attending to all prior tokens in the sequence. To avoid recomputing attention states from scratch on every step, models store the Key (K) and Value (V) activation vectors for every past token in GPU vRAM: the KV-Cache.

The memory size of a single token in the KV-cache is given by:

Sections covered in this lesson

  1. 1The KV-Cache Memory Crisis
  2. 2The Memory Fragmentation Disaster
  3. 3PagedAttention: OS Virtual Memory for LLMs
  4. 4Check Your Understanding
  5. 5Practice: KV-Cache Calculation & Block Mapping in Python
Pro Lesson · Module 12 — Local Serving, Quantization & Speculative Decoding

Unlock "PagedAttention & Virtual Memory KV-Cache" and the full AI Engineering & Production RAG curriculum

Eliminating memory fragmentation in GPU inference by allocating non-contiguous 16-token physical blocks.

Unlock all 3 hands-on exercises in this lesson plus every advanced WebAssembly module with automated checks.

  • Instant access to all advanced modules in AI Engineering & Production RAG plus 17 more courses
  • Hands-on browser exercises with automated test suites and solutions
  • 14-day money-back guarantee processed via Paddle.com

14-day guarantee · Cancel anytimeModule 1, all 18 interactive games, and cheatsheets are always free.