Module 4

Efficient attention and memory

Compute the same attention with less intermediate traffic; organize growing KV history without wasteful reservation.

From “The sky is” to memory engineering

Module 2 explained what attention computes. Module 3 explained how execution and memory movement affect cost. Now change the organization while preserving the attention result.

Use “The sky is” to remember prefill: three known query rows. Later, one newly processed token supplies one decode query while reading earlier cached K/V. The numerical examples here are explicitly separate invented fixtures; they do not change your Module 2 embeddings.

Before starting

Checks explain mistakes and never block navigation. Everything required runs on a Mac CPU in this browser; no CUDA, model download or paid machine.

Terms from Module 3 you need

Lessons 4.1–4.3 use six GPU terms. Read these one-line definitions now, or follow a link to the Module 3 lesson that teaches the term. Lessons 4.4 and 4.5 (paging, sharing) need little of it.

Global memory
The GPU’s large main memory (HBM on data-centre GPUs), reachable by every thread. It is the slow boundary: K/V, weights and any full score matrix normally live here. Module 3.2 →
Shared memory
Small, fast memory shared by the threads of one thread block; a kernel stages a tile of operands here on purpose so the block can reuse them. Module 3.2 →
Register
Per-thread working storage, the fastest and smallest level; running sums (accumulators) live here. Too many per thread can spill to slower memory. Module 3.2 →
Tile reuse
Load a tile of operands once into fast memory and use each value for many outputs, so fewer bytes cross the slow boundary. Module 3.3 →
Logical vs actual bytes
Logical bytes are what the algorithm asks to load in the model; actual bytes are what the hardware moves, which caches, padding and spills can change. Name the boundary you count. Module 3.4 →
Bandwidth
Bytes per second a memory path can deliver. When a step needs more bytes than the path supplies, memory, not arithmetic, sets its speed. Module 3.4 →

Five lessons, one mechanism at a time

Start with memory accounting, then see how normalization permits tiling. Finally separate storage allocation from arithmetic.

  1. 4.1 Where attention spends memory

    Label persistent KV and temporary attention workspace; calculate rectangular score storage; separate peak bytes from transferred bytes.

  2. 4.2 Softmax without keeping every score

    Calculate an online merge; explain both rescale corrections; compare streamed output against dense attention.

  3. 4.3 FlashAttention: tiles, fusion and IO

    Trace tile residency; account for transfers; explain larger-tile resource trade-offs without promising speedup.

  4. 4.4 PagedAttention: logical history, physical storage

    Resolve token positions through a block table; predict boundary allocations; calculate slack and explain fragmentation.

  5. 4.5 Sharing safely; choosing an optimization

    Follow ownership through sharing, copy-on-write and release; distinguish prefix caching policy; choose an optimization from workload evidence.

Optional motion companion: online merge, tile residency, sharing/COW. These use the same models as the lessons.

Three different kinds of block

The same word appears in computation, execution and storage. Keep their roles separate.

Attention tile
A patch of query/key pairs processed together.
CUDA thread block
A group of GPU threads assigned together to an SM.
KV block (page)
A storage unit holding K/V for a fixed number of processed token positions. Not a group of threads.
Materialize
Write an intermediate array to memory so a later operation can read it.
Workspace
Temporary storage used during computation.
Persistent cache
Stored K/V retained between generation steps.

Sources and scope

Original mechanisms come from papers and primary implementation documentation. The small values and allocation pools are teaching examples.

No GPU schedule, production configuration, learned model output or speedup is measured. Paper versions above identify stable sources; living documentation checked 23 October 2026 and must be rechecked for current engine work.