Linear algebra for LLMsPart 5: Attention, one operation at a time

Tokens to vectors

5 min

Token ids

“the cat sat”→

A tokenizer turns text into integer ids, positions in a vocabulary of size VV. Our toy vocabulary from Part 1 has V=5V = 5: the, cat, sat, on, mat.

A token as a one-hot vector

ecat=e_{\text{cat}} =

A one-hot vector has a 1 in the token’s position and 0 everywhere else: length VV. It’s a way to write a token as a vector without giving it any meaning yet.

Write

One-hot: 1 at the token’s id, 0 elsewhere. Length VV.

One-hot times E picks a row

=

Row view of xExE (Part 2): a mix of EE’s rows weighted by xx. Only one weight is 1, so you get exactly row 1, cat’s embedding. An embedding lookup is a matrix product.

Write

eid⊤E=Eid,:e_{\text{id}}^\top E = E_{\text{id},:}: one-hot × table = that row.

A whole sequence at once

=

Stack the one-hot rows into a T×VT \times V matrix and multiply: (T×V)(V×d)=T×d(T \times V)(V \times d) = T \times d. That’s XX, the input to the first layer.

Code indexes instead

X = E[ids]                 # what the code does: pick rows
X = F.one_hot(ids, V).float() @ E   # the same numbers, the slow way

In a transformer

The product would cost 2TVd2TVd FLOPs and multiply almost entirely by zeros, so frameworks just index. But thinking of it as a product explains two things: the gradient reaches only the rows that were used, and the output layer can reuse EE transposed (weight tying, Part 6).

Where does position come in?

Attention compares every token with every other, and nothing in it knows which token came first: shuffle the input rows and the outputs shuffle the same way. Order has to be added.

  • GPT-2 adds a learned position vector to each embedding: X=E[ids]+P[:T]X = E[\text{ids}] + P[{:}T].
  • Llama and most current LLMs add nothing to XX. They rotate the queries and keys instead (RoPE, Part 6).

Write

Attention alone ignores order. GPT-2: add PP. Llama: rotate q,kq, k (RoPE).

✏️ Your turn

Look up “sat”

Write the product.

Work it out on paper, then go to the next card to check.

Answer

Row 2 of EE: [2,1,0,1][2, 1, 0, 1].

📌 Recap

Tokens to vectors in one card

  • A token id becomes a one-hot vector of length VV.
  • One-hot × EE = the token’s row: a lookup is a matrix product.
  • A sequence: (T×V)(V×d)=X (T×d)(T \times V)(V \times d) = X\,(T \times d).
  • Position comes from added vectors (GPT-2) or rotations (RoPE).

Write

The four lines above.