Linear algebra for LLMsPart 2: Matrices and matrix multiplication

Four ways to see AB

8 min

One product, four readings

=

This is the product from the last deck. The numbers won’t change in this deck; only how you read them. Each reading explains a different part of a transformer.

We’ll write Ai,:A_{i,:} for row ii of AA and A:,jA_{:,j} for column jj, the same slicing as code: A[i, :] and A[:, j].

Write

C=ABC = AB. Row ii: Ai,:A_{i,:}. Column jj: A:,jA_{:,j}.

1. Every entry is a dot product

=

Cij=Ai,:⋅B:,jC_{ij} = A_{i,:} \cdot B_{:,j}: row ii of AA dotted with column jj of BB. This is the view you compute with.

Write

Cij=Ai,:⋅B:,jC_{ij} = A_{i,:} \cdot B_{:,j}

Dot products of every pair: XX⊤XX^\top

=

With B=X⊤B = X^\top, the columns of BB are the rows of XX, so entry (i,j)(i, j) of XX⊤XX^\top is xi⋅xjx_i \cdot x_j: every pair of rows, dotted. It’s called a Gram matrix, and it’s symmetric because xi⋅xj=xj⋅xix_i \cdot x_j = x_j \cdot x_i.

In a transformer

QK⊤QK^\top is the same idea with two different matrices: every query dotted with every key.

Write

(XX⊤)ij=xi⋅xj(XX^\top)_{ij} = x_i \cdot x_j. Symmetric.

2. Each column of CC mixes AA's columns

22+11+44=

Column jj of ABAB is AA times column jj of BB. Column 1 of BB is [2,1,4][2, 1, 4], so column 1 of CC is 2A:,1+A:,2+4A:,3=[4,9]2A_{:,1} + A_{:,2} + 4A_{:,3} = [4, 9]. It’s matrix × vector, once per column.

Write

C:,j=A B:,jC_{:,j} = A\,B_{:,j}: a mix of AA’s columns.

3. Each row of CC mixes BB's rows

11+22+00=

Row ii of ABAB is row ii of AA times BB. Row 1 of AA is [1,2,0][1, 2, 0], so row 1 of CC is 1 [2,1]+2 [1,0]+0 [4,−1]=[4,1]1\,[2, 1] + 2\,[1, 0] + 0\,[4, -1] = [4, 1].

In a transformer

This is the token view. In XWXW, each output row depends only on the same row of XX: a linear layer treats every token independently, with the same weights.

Write

Ci,:=Ai,: BC_{i,:} = A_{i,:}\,B: a mix of BB’s rows.

4. A sum of outer products: the first piece

=

A column times a row is an outer product uv⊤uv^\top: every entry of uu times every entry of vv. Here column 1 of AA times row 1 of BB. Every row of the result is a multiple of [2,1][2, 1].

Write

Outer product: (uv⊤)ij=uivj(uv^\top)_{ij} = u_i v_j

The second and third pieces

= =

Column 2 of AA with row 2 of BB, column 3 of AA with row 3 of BB. There’s one piece for each value of the inner index kk.

Add the pieces

++=

AB=∑kA:,k Bk,:AB = \sum_k A_{:,k}\,B_{k,:}: column kk of AA times row kk of BB, added up over kk. The same [4192]\begin{bmatrix} 4 & 1 \\ 9 & 2 \end{bmatrix}.

Write

AB=∑kA:,k Bk,:AB = \sum_k A_{:,k}\,B_{k,:}: one outer product per value of kk.

Why the outer-product view matters

Every outer product has rank 1: all its rows are multiples of one vector. So a product with inner size kk is a sum of kk rank-1 pieces, and has rank at most kk.

In a transformer

LoRA (Part 7) adds BABA to a weight, with a small inner size rr: a sum of rr rank-1 pieces, cheap to train. And an attention head’s output AVAV (Part 5) is one outer product per position: AV=∑jA:,j Vj,:AV = \sum_j A_{:,j}\,V_{j,:}.

✏️ Your turn

Outer product

Write the outer product and its shape.

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

Answer

=

(2×1)(1×3)=2×3(2 \times 1)(1 \times 3) = 2 \times 3. Row 2 is twice row 1: rank 1.

📌 Recap

Four ways to see ABAB in one card

View C=ABC = AB Shows up as
Dot products Cij=Ai,:⋅B:,jC_{ij} = A_{i,:} \cdot B_{:,j} QK⊤QK^\top, Gram matrices
Columns C:,j=A B:,jC_{:,j} = A\,B_{:,j} the column space (Part 3)
Rows Ci,:=Ai,: BC_{i,:} = A_{i,:}\,B XWXW: every token on its own
Outer products C=∑kA:,k Bk,:C = \sum_k A_{:,k}\,B_{k,:} rank, LoRA (Part 7)

Write

The four views in the table.