4.1.13 · D1Transformer Architecture

Foundations — Computational complexity of attention

1,961 words9 min readBack to topic

Before you can read the parent note Computational complexity of attention, you need to own every symbol it throws at you. We build them one at a time. Nothing is used before it is drawn.


1. What is a "token"? — the letter

A token is one small chunk of text the model reads: usually a word or piece of a word. If you feed the model the sentence "cats love naps", that is 3 tokens.

Look at the figure: each coloured box is one token. Lining them up in a row is literally what "a sequence of length " means. Everything expensive in attention grows as this row gets longer.


2. What is an "embedding"? — the letter

A computer cannot multiply the word "cats". So each token is turned into a list of numbers — like coordinates that place the word somewhere in a space of meaning. Similar words land near each other.

Look at the figure: each token box now has a column of numbers stacked under it. A vector is just that column — a list of numbers with a fixed length. We write "a vector lives in ", which is fancy for "it is a list of real (ordinary) numbers".


3. Stacking vectors into a grid — the matrix

If we take all token-vectors and stand them side by side, we get a rectangle of numbers: rows (one per token) and columns (one per feature).

So the input to attention is one matrix of shape . Row of is the vector for token .


4. What "multiplying two matrices" costs — the heart of complexity

This is the single most important mechanic in the whole topic, so we slow right down.

To multiply a matrix of shape by a matrix of shape :

  • The inner numbers must match (both are ). They "cancel", and the result has shape .
  • Each cell of the result is made by lining up one row (length ) with one column (length ), multiplying the pairs, and adding them up — that is multiplications per cell.
  • There are cells.

Look at the figure: one highlighted result cell is fed by one row (violet) and one column (orange), each of length . Count the arrows — that's multiplications for one cell. Repeat for all cells and you have the total.


5. The three players — , ,

Each token asks a question, offers a label, and carries content. So each token-vector is turned into three new vectors:

Here is the width of one query/key/value vector — often equal to , or to when we split into heads (Section 8). Full mechanics live in 4.1.1-Self-attention-mechanism.


6. Comparing every pair — the transpose and

To score how well token 's query matches token 's key, we multiply the query row by the key row. Doing this for all pairs at once needs the keys turned on their side — that is the transpose.

Now the score matrix:

Using the cost formula with :

Look at the figure: the output is a full square of side . Cell is the score "how much should token attend to token ". A square of side has cells — this is the birthplace of the quadratic cost the whole topic warns about.


7. Softmax, , and the aggregation

The scores are first divided by (square root of the key width). Why? Bigger vectors give bigger dot-product scores just because there are more terms to add; dividing by shrinks them back so softmax does not become razor-sharp. It answers the question "how do I keep scores at a sane scale regardless of ?"

Finally each token's output is a weighted blend of all value vectors:


8. Reading the growth rate — big- and heads

Look at the figure: the orange linear line () and the magenta quadratic curve () start close, but the quadratic soon rockets away. That crossover is exactly why long sequences hurt, and why 4.3.2-Sparse-attention-patterns and 4.5.1-Memory-optimization-techniques exist. Position information itself must also scale with — see 5.2.3-Positional-encoding-scaling.


Prerequisite map

Token = chunk of text

n = how many tokens

Embedding vector

d = numbers per token

Matrix X shape n by d

Matrix multiply cost a b c

Q K V projections

Scores S = Q Ktranspose gives n by n

Softmax weights A

Output = A V

Big-O growth reading

Quadratic complexity of attention


Equipment checklist

Cover the right side and answer out loud. If any stalls, reread that section.

What does count?
The number of tokens in the input sequence.
What does measure?
How many numbers are in each token's embedding vector.
What does mean?
A grid of numbers with rows (tokens) and columns (features).
Cost of multiplying an by a matrix?
operations.
Why must the inner dimensions match to multiply?
Each result cell pairs a row with a column of the same length to sum products.
What are , , in one word each?
Query = looking-for, Key = offered-label, Value = content.
What does the transpose do and why?
Flips rows and columns so becomes , lining up dimensions for .
What shape is and how many cells?
, so cells — one per token pair.
Why is that the source of quadratic cost?
Every token compares with every token, giving comparisons.
What does softmax produce from a row of scores?
Positive weights summing to — a probability spread over tokens.
Why divide scores by ?
To keep score magnitude stable so softmax isn't over-sharpened as grows.
Does using heads multiply attention cost by ?
No — heads split , so , same as one head.
What does tell you?
Cost grows like the square of ; doubling quadruples the work.