4.1.14 · D1Transformer Architecture

Foundations — Flash attention and efficient attention

3,112 words14 min readBack to topic

Before you can understand why Flash Attention is clever, you have to be fluent in the symbols the parent note throws at you. This page builds every one of them from nothing — plain words, a picture, and the reason the topic needs it. Read top to bottom; each symbol is earned before it is used.


1. A sequence of tokens, and the number

Picture it. Line up the words of a sentence in boxes, left to right. If the sentence is "the cat sat", then .

Figure 1 — A sequence: tokens in order. (Alt text: three boxes labelled "the", "cat", "sat" for tokens 0, 1, 2, with a red double-headed arrow underneath marking the span as "N = 3 tokens".)

Figure — Flash attention and efficient attention

Why the topic needs it. Everything expensive in attention grows with . The parent note's whole complaint is that memory grows like — a table with one row and one column per token. So is the dial that turns a cheap problem into an impossible one.


2. A vector, and the dimension

Picture it. Think of a vector as an arrow, or more simply as a short strip of numbered slots. If , each token becomes 2 numbers, e.g. "cat" .

Why the topic needs it. The parent's memory math (, ) counts how many numbers move between memory chips. To count numbers you need to know both how many tokens () and how many numbers per token (). When (many tokens, few numbers each), the terms win — that is the entire motivation.


3. A matrix and its shape

Picture it. A spreadsheet: rows (one per token) and columns (one per feature). The whole sentence lives in one grid.

Figure 2 — Matrix : a spreadsheet, one row per token. (Alt text: a grid of numbers with 3 rows and 4 columns; one middle row is outlined in red and labelled "one token's vector (d numbers)"; black double arrows mark "N rows" down the side and "d columns" along the bottom.)

Figure — Flash attention and efficient attention

Why the topic needs it. The parent writes and . The shape tells you the memory cost at a glance: an grid holds numbers. Reading shapes is how you spot the memory explosion.


4. Matrix multiplication and the transpose

Picture it. Row of slides across every column of ; each landing spot gets one number. Row (length ) times column (length ) collapses to a single score.

Figure 3 — Dot product: a -long row and column collapse to ONE score. (Alt text: the row of Q and the column of K^T shown side by side; text "1·1 + 0·0 = 1"; a red arrow points down to a single red-outlined cell labelled "one entry S[i,j]".)

Figure — Flash attention and efficient attention

Why we need the transpose (why this tool, not another). is and is . You cannot multiply by — the inner sizes clash. Transposing to makes the inner sizes match ( and ), and the output comes out : exactly one score for every pair of tokens. That output is the scores matrix the parent worries about.


5. Query, Key, Value: , , and the weights

Picture it (library metaphor). Your query is the topic you want. Each book has a key on its spine (its subject). You compare your query to every spine; where they match, you take that book's value (its contents).

Why the topic needs it. compares queries to keys → scores. Then those scores weight the values to build the output. Flash Attention is a smarter way to do exactly this comparison-and-weighting, so you must know what each letter is before the algorithm makes sense.


6. The exponential

Picture it. A curve hugging zero on the left, passing through at , then shooting up on the right. Two key facts you will use:

  • (a score equal to the max becomes weight ).
  • (exponents split into products — this is the engine of the tiling trick).

Figure 4 — The exponential: always positive, big scores dominate. (Alt text: the curve in red on black axes, passing through the point ; annotations "shrinks toward 0" on the left and "shoots up fast" on the right.)

Figure — Flash attention and efficient attention

Why this tool and not another. We need a way to turn scores (which can be negative) into positive "weights" that never cancel out, and where bigger scores dominate smoothly. does exactly that: always positive, monotonically increasing. Its split rule is precisely what lets Flash Attention rescale block sums to a common baseline.


7. Softmax

Picture it. Take the scores, exponentiate each (all become positive, big ones stretch away from small ones), then divide by their total so they sum to . The result: "what fraction of attention goes to each token."

Why the topic needs it. The parent's incremental-softmax derivation is entirely about splitting this across blocks. If you don't know softmax is "exponentiate, then divide by the sum," none of that derivation is readable.


8. The max and the safe-softmax shift

Why this tool (why subtract the max). overflows the computer for even moderately large (e.g. = infinity to a computer). Shifting by the max guarantees the biggest exponent is exactly and all others are — no overflow. This is the "safe" in safe softmax, and it is why Flash Attention must carry a running max, not just a running sum.


9. Scaling by

Why this tool. A dot product of two -long vectors is a sum of products; the more terms, the larger its typical size grows — roughly proportional to . If scores get huge, softmax becomes razor-sharp (all weight on one token) and gradients vanish. Dividing by cancels this growth, keeping scores at a stable scale regardless of how big is. That is exactly the parent's note "keeps variance stable as dimensions grow." Note it is specifically (the query/key size), not or , because it is the query–key dot product whose size we are taming.


10. The memory hierarchy: SRAM vs HBM, and bandwidth

Picture it. SRAM is the small desk right in front of you; HBM is the big filing cabinet across the room. Computing is instant once the paper is on your desk — the slow part is walking to the cabinet. Flash Attention keeps small tiles on the desk and avoids trips to the cabinet.

Why the topic needs it. The entire justification ("2–4× faster") comes from cutting round-trips to HBM, not from doing less arithmetic. Without this picture, "memory-bound" is just words.


11. Big-O notation: vs

Picture it. Two curves: a straight-ish line () and a steep upward bend (). At small they're close; at large the quadratic dwarfs the linear one.

Why the topic needs it. The headline claim is " memory memory." This notation is the language of that win. See 4.13-Scaling-laws-for-transformers for how these growth curves shape whole-model budgets.


Prerequisite map

The arrows read "is needed before". Follow any path top-to-bottom and every symbol on it is already defined by the time you reach the next.

In words (read this if the diagram below does not render):

  • Tokens & vectors & matrix shape matrix product the projectionsFlash Attention.
  • Separately, exponential softmaxmax-shift / safe softmaxFlash Attention.
  • Scaling by , SRAM vs HBM bandwidth, and big-O growth each feed Flash Attention directly.

Tokens and N

Vectors and dimension d

Matrix and shape N x d

Matrix product Q K transpose

Q K V and weight matrices

Exponential e to the x

Softmax

Max shift m and safe softmax

Attention scores S

Scale by sqrt of d_k

Flash Attention

SRAM vs HBM bandwidth

Big-O growth N vs N squared

This page feeds directly into the parent Flash Attention topic. The same machinery underlies 4.1.1-Self-attention-mechanism and 4.1.3-Multi-head-attention; the recomputation trick is a cousin of 5.27-Gradient-checkpointing; and these tokens gain order from 4.1.8-Positional-encoding and power 4.2.1-BERT-architecture and 4.2.3-GPT-architecture.


Equipment checklist

Cover the right side and answer before revealing.

What is ?
The number of tokens (chunks of text) in the sequence.
What is ?
The embedding dimension — how many numbers represent each token as it arrives.
Read the notation .
" is a grid of real numbers with rows and columns."
What are the shapes of and the resulting ?
and , giving and .
How do , and differ?
is the arriving embedding size; is the shared query/key working size; is the value working size. Often all equal, but the roles differ.
Where does the memory term come from?
Fetching two grids and , each holding about numbers, to start the calculation.
Why must we transpose before multiplying by it?
So inner sizes match ( with ); the output becomes , one score per token pair.
What does a single entry of mean?
A dot product measuring how aligned token 's query is with token 's key.
Name , , in plain words.
Query = what a token looks for; Key = what it offers; Value = the content passed on.
What two facts about power the tiling trick?
, and (exponents split into products).
What does softmax produce?
Positive weights that sum to 1 — a probability distribution over tokens.
What is (the normalizer)?
The sum of exponentials on the bottom of softmax; must be built incrementally in Flash Attention.
Why subtract the max before exponentiating?
To prevent overflow; the largest exponent becomes without changing the result.
Why divide scores by (and why , not )?
Query–key dot products grow like ; dividing keeps scores stable. It is because that is the size of the vectors being dot-producted.
What is the difference between SRAM and HBM?
SRAM is small and on-chip (fast); HBM is large GPU RAM (~12× slower bandwidth).
What does " memory" mean?
Memory grows like squared — double the tokens, roughly quadruple the memory.
Recall Self-test: the one-sentence summary

In your own words, why does the full attention matrix cost ? ::: It has one row and one column per token, so it holds numbers.