Linear algebra for LLMsPart 3: Matrices as transformations

Span, basis and rank

8 min

Span: everything you can mix

ab

The span of some vectors is every linear combination of them. a=[1,2]a = [1, 2] and b=[2,−1]b = [2, -1] point different ways, so c1a+c2bc_1 a + c_2 b reaches every point of the plane.

Write

Span = all combinations c1v1+c2v2+…c_1 v_1 + c_2 v_2 + \dots

Parallel vectors span only a line

ac

c=−1.5 ac = -1.5\,a, so mixing aa and cc never leaves the dashed line. The second vector added nothing new.

Independence and basis

Vectors are linearly independent when none of them is a mix of the others. A basis is an independent set that spans the whole space. Every basis of Rd\mathbb{R}^d has exactly dd vectors; that number is the dimension.

In R2\mathbb{R}^2:

  • [1,2][1, 2] and [2,−1][2, -1]: independent, span the plane. A basis.
  • [1,2][1, 2] and [−1.5,−3][-1.5, -3]: dependent, span a line.
  • [1,0][1, 0], [0,1][0, 1] and [1,1][1, 1]: dependent (three vectors in 2D), span the plane.

Write

Independent: none is a mix of the others. Basis: independent and spanning. Dimension = basis size.

Column space and rank

col⁡(A)={ Ax }\displaystyle \operatorname{col}(A) = \{\,Ax\,\}
=span⁡(columns of A)\displaystyle = \operatorname{span}(\text{columns of } A)
rank⁡(A)=dim⁡col⁡(A)\displaystyle \operatorname{rank}(A) = \dim \operatorname{col}(A)

Every output AxAx is a mix of AA’s columns, so the outputs form the span of the columns: the column space. Its dimension is the rank: the number of independent columns. (It always equals the number of independent rows too.)

Write

rank⁡(A)\operatorname{rank}(A) = number of independent columns = number of independent rows.

A rank-1 matrix

=

Row 2 is twice row 1, and every column is a multiple of [1,2][1, 2]. Three columns, but only one direction: rank 1. Every rank-1 matrix is an outer product uv⊤uv^\top, and every output AxAx lands on the line through [1,2][1, 2].

Write

Rank 1 ⇔ A=uv⊤A = uv^\top. Outputs live on one line.

Limits on rank

rank⁡(A)≤min⁡(m,n)\displaystyle \operatorname{rank}(A) \le \min(m, n)
rank⁡(AB)≤min⁡(rank⁡A, rank⁡B)\displaystyle \operatorname{rank}(AB) \le \min(\operatorname{rank} A,\ \operatorname{rank} B)

An m×nm \times n matrix can’t have more independent columns than it has columns, or rows. And a product can’t have more rank than either factor: whatever passes through a narrow middle can’t widen again. A product (m×k)(k×n)(m \times k)(k \times n) has rank at most kk.

Write

rank⁡(AB)≤min⁡(rank⁡A,rank⁡B)\operatorname{rank}(AB) \le \min(\operatorname{rank} A, \operatorname{rank} B). Inner size kk caps the rank at kk.

✏️ Your turn

Find the rank

What is rank⁡(B)\operatorname{rank}(B)? Look for a row that’s a mix of the others.

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

Answer: rank 2

Row 3 = row 1 + row 2, so only two rows are independent. Rows 1 and 2 aren’t multiples of each other, so the rank is exactly 2. The same holds for columns: column 3 = column 1 + column 2.

An attention head is low-rank

q⋅k=(xWQ)(yWK)⊤\displaystyle q \cdot k = (xW_Q)(yW_K)^\top
=x (WQWK⊤) y⊤\displaystyle = x\,(W_Q W_K^\top)\,y^\top

In a transformer

WQW_Q and WKW_K are d×dhd \times d_h, so WQWK⊤W_Q W_K^\top is a d×dd \times d matrix of rank at most dhd_h. With d=4096d = 4096 and dh=128d_h = 128, each head compares tokens inside a 128-dimensional slice of the 4,096-dimensional space. LoRA (Part 7) uses the same fact on purpose: a rank-rr update is cheap.

Write

Each head: rank⁡(WQWK⊤)≤dh\operatorname{rank}(W_Q W_K^\top) \le d_h.

In PyTorch

torch.linalg.matrix_rank(A)      # rank, from the singular values
torch.outer(u, v)                # a rank-1 matrix u vᵀ

Real weight matrices are almost never exactly rank-deficient; what matters in practice is how much of them is captured by a few directions. Part 7 measures that with the SVD.

📌 Recap

Span, basis and rank in one card

  • Span: all combinations. Independent: none is a combination of the others. Basis: both.
  • Column space = all outputs AxAx; rank = its dimension.
  • Rank 1 ⇔ outer product uv⊤uv^\top.
  • rank⁡(AB)≤min⁡(rank⁡A,rank⁡B)\operatorname{rank}(AB) \le \min(\operatorname{rank} A, \operatorname{rank} B): one head’s WQWK⊤W_Q W_K^\top has rank ≤dh\le d_h.

Write

The four lines above.