Module 4 · Lesson 1 of 5
Where attention spends memory
Separate persistent KV, temporary workspace and transferred bytes before choosing an optimization.
Terms from Module 3 you need
This lesson uses these GPU terms; each link opens the Module 3 lesson that teaches 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 →
- 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 →
What stays, what is temporary?
A stored key/value row survives into later decode steps. A score or probability buffer can be temporary workspace for one attention call.
What you will be able to explain
Label persistent KV and temporary attention workspace; calculate rectangular score storage; separate peak bytes from transferred bytes.
FlashAttention focuses on attention’s intermediate storage and IO. KV paging focuses on how persistent history occupies memory. Their benefits can coexist.
Predict: Remove a probability buffer. Have cached keys and values disappeared?
One query is not a new square matrix
Let Tq be query rows processed now and Tk be visible key positions. Scores have shape Tq × Tk, not always Tk × Tk.
A [Tq × Tk] · V [Tk × dv] → O [Tq × dv].
Tq/Tk count rows; dk is query/key width; dv is value/output width. Ordinary decode usually adds one query per active sequence. Prefill can process many supplied queries under causal visibility. Self-attention can include the newly processed position.
For 4,096 queries and keys, one two-byte dense score buffer is 4,096 × 4,096 × 2 bytes = 32 MiB, for one head, one layer and one sequence. A separate probability buffer adds another 32 MiB. A real model has many heads and layers, and whether their buffers are live at the same time depends on the implementation.
Full dense allocation may include masked entries. Causal allowed-pair count does not by itself imply triangular physical storage.
Footprint and traffic answer different questions
Footprint asks what is live together. Traffic asks how many bytes cross a named boundary over the whole operation.
- Compute scores and write them to global memory.
- Read scores, compute normalization, write probabilities.
- Read probabilities and values; write final attention output.
For equal two-byte score/probability arrays: two distinct buffers occupy 4 × Tq × Tk bytes together in this explicitly materialized baseline. Writing and reading each once transfers 8 × Tq × Tk bytes. Q/K/V input reads and output stores are additional. A different allocation schedule can have a different peak.
Do not add a cache’s capacity to a transfer counter. Do not call logical loads measured DRAM bytes: hardware caches, spills and padding can change actual traffic.
scores = Q @ K.T / sqrt(dk) # global score write
weights = softmax(scores + mask) # score read + probability write
output = weights @ V # probability read + output writeSource: FlashAttention §2.2, materialized baseline
Takeaway: Persistent K/V, temporary scores and transferred bytes are three different counts. Name which one you mean before judging an optimization.