Linear algebra for LLMsPart 2: Matrices and matrix multiplication

Matrix × matrix

8 min

Two matrices

=

AA is 2×32 \times 3 and BB is 3×23 \times 2. ABAB will have AA’s 2 rows and BB’s 2 columns. Each of its 4 entries is one dot product.

Write

A (2×3)A\,(2 \times 3), B (3×2)B\,(3 \times 2). ABAB is 2×22 \times 2.

Entry (1,1)(1, 1): row 1 ⋅\cdot column 1

=

Row 1 of AA dotted with column 1 of BB: 1⋅2+2⋅1+0⋅4=41 \cdot 2 + 2 \cdot 1 + 0 \cdot 4 = 4.

Entry (1,2)(1, 2): row 1 ⋅\cdot column 2

=

Same row, next column: 1⋅1+2⋅0+0⋅(−1)=11 \cdot 1 + 2 \cdot 0 + 0 \cdot (-1) = 1.

Row 2: two more dot products

=

Row 2 with column 1: 3⋅2+(−1)⋅1+1⋅4=93 \cdot 2 + (-1) \cdot 1 + 1 \cdot 4 = 9. Row 2 with column 2: 3⋅1+(−1)⋅0+1⋅(−1)=23 \cdot 1 + (-1) \cdot 0 + 1 \cdot (-1) = 2.

Write

(AB)ij=(row i of A)⋅(column j of B)(AB)_{ij} = (\text{row } i \text{ of } A) \cdot (\text{column } j \text{ of } B). AB=[4192]AB = \begin{bmatrix} 4 & 1 \\ 9 & 2 \end{bmatrix}

The shape rule

(m×k‾) (k‾×n)=(m×n)\displaystyle (m \times \underline{k})\,(\underline{k} \times n) = (m \times n)
(2×3)(3×2)=(2×2)\displaystyle (2 \times 3)(3 \times 2) = (2 \times 2)

The inner sizes must match, because each row of AA is dotted with each column of BB. They disappear in the result; the outer sizes stay. Check shapes first: it catches most bugs before any arithmetic.

Write

(m×k)(k×n)=(m×n)(m \times k)(k \times n) = (m \times n). Inner sizes match and vanish.

✏️ Your turn

Shapes, then a product

(4×3)(3×5)=  ?\displaystyle (4 \times 3)(3 \times 5) = \;?
(3×2)(3×2)=  ?\displaystyle (3 \times 2)(3 \times 2) = \;?
= ??

Write both shapes (or “doesn’t fit”), then compute PQPQ.

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

Answer

(4×3)(3×5)=(4×5)\displaystyle (4 \times 3)(3 \times 5) = (4 \times 5)
(3×2)(3×2):doesn’t fit\displaystyle (3 \times 2)(3 \times 2): \text{doesn't fit}
=

(3×2)(3×2)(3 \times 2)(3 \times 2) fails because the inner sizes are 2 and 3. For PQPQ, row 1: 1⋅3+2⋅1=51 \cdot 3 + 2 \cdot 1 = 5 and 1⋅0+2⋅2=41 \cdot 0 + 2 \cdot 2 = 4; row 2: 0⋅3+1⋅1=10 \cdot 3 + 1 \cdot 1 = 1 and 0⋅0+1⋅2=20 \cdot 0 + 1 \cdot 2 = 2.

Order matters: AB≠BAAB \ne BA

a different shape from ABAB (2 × 2) ≠

BABA is (3×2)(2×3)=3×3(3 \times 2)(2 \times 3) = 3 \times 3, not even the same shape as ABAB. Even square matrices usually give different answers in the other order. Matrix multiplication is not commutative.

Write

AB≠BAAB \ne BA in general.

Regrouping is fine: (AB)C=A(BC)(AB)C = A(BC)

(AB)C=A(BC)\displaystyle (AB)C = A(BC)

You may move the brackets but not the matrices. Multiplication is associative. The result is the same either way, but the cost can be very different, as you’ll see in What a matmul costs.

The transpose of a product flips the order

(AB)⊤=B⊤A⊤\displaystyle (AB)^\top = B^\top A^\top
= =

The shapes force the flip: A⊤A^\top is 3×23 \times 2 and B⊤B^\top is 2×32 \times 3, so only B⊤A⊤B^\top A^\top fits to give a 2×22 \times 2. It’s the same rule as taking off socks and shoes: the last one on comes off first.

Write

(AB)⊤=B⊤A⊤(AB)^\top = B^\top A^\top

In PyTorch and in a transformer

C = A @ B              # matrix product; torch.matmul(A, B) is the same
Q = X @ W_q            # every token's query at once

In a transformer

XX holds TT tokens as rows (T×dT \times d). One product XWQXW_Q, (T×d)(d×dk)=T×dk(T \times d)(d \times d_k) = T \times d_k, computes every token’s query at once: each row of the result is that token’s row of XX times WQW_Q. Batching rows into one matmul is what makes GPUs fast.

📌 Recap

Matrix × matrix in one card

  • (AB)ij=(row i of A)⋅(column j of B)(AB)_{ij} = (\text{row } i \text{ of } A) \cdot (\text{column } j \text{ of } B).
  • (m×k)(k×n)=(m×n)(m \times k)(k \times n) = (m \times n): check shapes first.
  • AB≠BAAB \ne BA, but (AB)C=A(BC)(AB)C = A(BC).
  • (AB)⊤=B⊤A⊤(AB)^\top = B^\top A^\top.
  • XWXW applies WW to every token (row) at once.

Write

The five lines above.