4.2.10 · D2Tokenization & Language Modeling

Visual walkthrough — Context window and sequence length

2,142 words10 min readBack to topic

Before any symbol appears, here is the vocabulary we will earn along the way:

Nothing else is assumed. Let's go.


Step 1 — Lay the tokens on a line

WHAT. Draw the input as a row of boxes, one per token. If the sentence is "the cat sat", that's three boxes. We label their positions .

WHY. Before we can count "comparisons," we need a clear picture of the things being compared. The row of boxes is that picture. The number written under the last box is exactly — this is the only quantity that will drive the whole derivation.

PICTURE. Below: five token-boxes in a line. The number at the end (in red) is the star of the whole page — it is the one knob you turn when you make the context window bigger.

Figure — Context window and sequence length

Step 2 — One token looks at all the others

WHAT. Pick a single box — say box (here just names which box we picked; think "box number "). To build its meaning, the model draws an arrow from box to every box on the line, including back to itself.

WHY. This is the defining rule of self-attention: a token's new representation is a blend of all tokens, so it needs a "how much do I care about you?" number pointing at each one. No box is allowed to be skipped — otherwise its information could never reach box . That "no skipping" is the seed of the quadratic cost.

PICTURE. One red box on the left fires arrows at all boxes. Count the arrows: exactly of them, because there are targets.

Figure — Context window and sequence length

Step 3 — Every token does this — build the grid

WHAT. Step 2 was for one box. But every box must look at every other box. If we let box run over all rows and box (the target) run over all columns, the arrows fill a full square grid of cells. Cell = "how much box attends to box ."

WHY. We switched from "one row of arrows" to "a grid" because it lets us count everything at once. A grid with rows and columns has an obvious cell count, and counting cells is easier than counting tangled arrows.

PICTURE. An square lights up. Row = who is looking (the "query"), column = who is being looked at (the "key"). One red cell marks a single pair so you can see that each cell is exactly one arrow from Step 2.

Figure — Context window and sequence length

Step 4 — What lives inside one cell: the dot product

WHAT. Zoom into one cell . To get its number, the model takes the vector for box and the vector for box — each a list of numbers (call the model width, i.e. how long each token's number-list is) — and does a dot product: multiply matching entries, then add them all up.

WHY a dot product, and not something else? We need one single score that says "how aligned are these two tokens?" The dot product is the cheapest, most natural "alignment meter": when two lists point the same way, their products are large and the sum is big; when they clash, terms cancel and the sum is small. So it answers exactly the question a cell asks. It costs about multiply-add operations, because there are entries to multiply and add.

PICTURE. Two vertical stacks of numbers, matching entries joined by red multiply-links, all funnelling into one output number — the value of the cell.

Figure — Context window and sequence length

Step 5 — Multiply: cost of the whole grid (COMPUTE)

WHAT. Now combine Step 3 (there are cells) with Step 4 (each cell costs work). The total compute is cells times cost-per-cell.

WHY. "Compute" means "number of arithmetic operations the machine must actually perform." Total work is always (how many jobs) (work per job). Here jobs cells, work per job .

PICTURE. The grid, with a little "" tag on the corner cell reminding you each cell hides a -long dot product. The whole thing is stamped .

Figure — Context window and sequence length

Step 6 — Memory is , not — a crucial subtlety

WHAT. After each cell's dot product finishes, what gets stored is a single number (one scalar weight) per cell, so the softmax (see Self-Attention) can normalise them. The numbers that went into the dot product are not kept in the attention matrix — they collapse into that one output. So the stored grid is scalars.

WHY the distinction matters. It is the most common mistake around context cost (the parent note steel-mans it). Compute carries the because you must do multiplications per cell. Memory drops the because after the multiplication you keep only the sum — one number.

PICTURE. Left: the fat -deep dot product (Step 4) — that depth is compute. Right: the flat grid of single red dots — that flatness is memory. The depth vanishes once the sum is taken.

Figure — Context window and sequence length

Step 7 — Edge cases: read the square at its extremes

A derivation is only trustworthy if it survives the corners. Let's push to its limits.

WHAT & WHY — the degenerate inputs:

  • (empty input): a grid has cells. Cost . The formula agrees — nothing to attend to, nothing to compute. ✅
  • (single token): a grid — one cell: the token attending to itself. Cost , essentially free. The quadratic hasn't "kicked in" yet because . ✅
  • (the ceiling): the biggest legal square. This is the worst case the model was trained for; go one box further and there is no Positional Encoding slot for it — the picture would need a column with no address. ✅
  • Growth, not a jump: because area grows smoothly and steeply with side length, there is no sudden cliff in cost — but there is a sudden cliff in quality at , for the position reason above.

PICTURE. Three little squares in a row: a dot (), a small grid (), a big grid (). The red highlights that going (double the side) turns a -cell square into a -cell square — four times the area, not twice.

Figure — Context window and sequence length

The one-picture summary

WHAT. One figure that fuses the whole chain: a line of tokens (Step 1) → arrows from one token (Step 2) → the full grid (Step 3) → the -deep dot product hiding in a cell (Step 4) → the two stamps (compute) and (memory) (Steps 5–6).

Figure — Context window and sequence length
Recall Feynman retelling — say it to a friend with no math

Imagine every word in a sentence has to shake hands with every other word to understand it. If there are words, the number of handshakes is a full square: people down the side, across the top, so handshakes fill the grid — that's the . Each handshake isn't instant either: comparing two words means checking a list of little features and adding up the matches, so each handshake costs about units of effort. Multiply the handshakes by the effort each and you get total work. But remembering the handshakes is cheaper: you only jot down a single "how firm was it?" number per handshake, so you store just numbers, no . And the punchline: if you double the number of people in the room, you don't double the handshakes — you quadruple them, because a square twice as wide has four times the area. That's the entire reason context windows can't just grow forever.

Recall Rebuild the whole page from four numbers

comes from where? ::: The area of the token-vs-token square — rows (who looks) times columns (who is looked at). Why does compute carry an extra but memory does not? ::: Each cell does a -long dot product ( multiply-adds → compute), but only stores the one number that comes out ( scalar → memory). What happens to cost when you double ? ::: It quadruples, because . What is the smallest and largest legal square? ::: Smallest legal non-trivial is (, one token attending to itself); largest is , the trained ceiling.


Related: Self-Attention · Positional Encoding · Transformer Architecture · Truncation and Chunking · KV Cache · Tokenization