Linear algebra for LLMsPart 1: Vectors and embedding space

High dimensions

6 min

In 2D, a random angle can be anything

abc

Pick two random directions in the plane and the angle between them is anywhere from 0° to 180°. Their cosine is often close to 11 or −1-1. In thousands of dimensions, something very different happens.

In high dimensions, random vectors are nearly orthogonal

dd typical cos⁡θ\cos\theta of two random vectors
2 ±0.71\pm 0.71
64 ±0.13\pm 0.13
768 ±0.036\pm 0.036
4,096 ±0.016\pm 0.016

The cosine of two random directions has an average of 00 and a spread (standard deviation) of 1/d1/\sqrt{d}. At d=4096d = 4096 two random embeddings are almost exactly at 90°.

Write

Random vectors in Rd\mathbb{R}^d: cos⁡θ≈0±1/d\cos\theta \approx 0 \pm 1/\sqrt{d}. Nearly orthogonal.

Why: the products cancel

a⋅b=a1b1+a2b2+⋯+adbd\displaystyle a \cdot b = a_1 b_1 + a_2 b_2 + \dots + a_d b_d

Each product aibia_i b_i is as likely to be negative as positive, so most of them cancel. What’s left grows only like d\sqrt{d}, while the lengths ∣a∣|a| and ∣b∣|b| each grow like d\sqrt{d} too. Divide and you get about d/d=1/d\sqrt{d} / d = 1/\sqrt{d}.

The size of a random dot product

qi,ki ⁣: mean 0, variance 1\displaystyle q_i, k_i\!: \text{ mean } 0, \text{ variance } 1
Var⁡(q⋅k)=d\displaystyle \operatorname{Var}(q \cdot k) = d
typical ∣q⋅k∣≈d\displaystyle \text{typical } |q \cdot k| \approx \sqrt{d}

A sum of dd independent terms, each with variance 1, has variance dd. Its typical size, the standard deviation, is d\sqrt{d}: 8 for d=64d = 64, about 11.3 for d=128d = 128.

Write

Random q,kq, k with unit-variance entries: Var⁡(q⋅k)=d\operatorname{Var}(q \cdot k) = d, size ≈d\approx \sqrt{d}.

✏️ Your turn

How big?

dk=100\displaystyle d_k = 100

Query and key entries are random with mean 0 and variance 1. Roughly how big is a typical q⋅kq \cdot k?

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

Answer: about 10

100=10\displaystyle \sqrt{100} = 10

Scores of ±10\pm 10 are a problem, as the next card shows.

Big scores make softmax pick one winner

softmax⁡\operatorname{softmax} = softmax⁡\operatorname{softmax} =

Softmax (Part 5) turns scores into weights that sum to 1. With scores around ±10\pm 10, almost all the weight lands on one key, and the gradients for the others nearly vanish. Divided by 10, the same scores give a useful spread.

In a transformer

This is why attention computes QK⊤/dkQK^\top / \sqrt{d_k}: dividing by dk\sqrt{d_k} brings random scores back to a typical size of about 1, whatever the head width.

Write

Attention divides scores by dk\sqrt{d_k} so they stay ≈1\approx 1 in size.

Room for many more directions than dimensions

Directions at… How many fit in Rd\mathbb{R}^d
exactly 90° at most dd
nearly 90° exponentially many in dd

Rd\mathbb{R}^d has room for only dd directions at exactly 90° to each other. But if “nearly 90°” is enough (say ∣cos⁡θ∣<0.1|\cos\theta| < 0.1 for every pair), the number of directions you can fit grows exponentially with dd.

In a transformer

One explanation of how models store more concepts than they have dimensions, called superposition, relies on this: features get nearly orthogonal directions, so they interfere only a little (Anthropic, Toy Models of Superposition, 2022).

Try it in PyTorch

a, b = torch.randn(2, 4096)          # two random vectors, entries ~ N(0, 1)
F.cosine_similarity(a, b, dim=0)     # typically within ±0.03
(a @ b) / 4096 ** 0.5                # typically between −2 and 2

Run it a few times: the cosine hugs 0, and the scaled dot product behaves like a draw from N(0,1)\mathcal{N}(0, 1).

📌 Recap

High dimensions in one card

  • Two random vectors in Rd\mathbb{R}^d are nearly orthogonal: cos⁡θ≈0±1/d\cos\theta \approx 0 \pm 1/\sqrt{d}.
  • With unit-variance entries, q⋅kq \cdot k has variance dd and typical size d\sqrt{d}.
  • Big scores make softmax put nearly all the weight on one key, so attention divides by dk\sqrt{d_k}.
  • Exactly orthogonal: dd directions. Nearly orthogonal: exponentially many.

Write

The four lines above.