Linear algebra for LLMsPart 2: Matrices and matrix multiplication

What a matmul costs

6 min

One entry: kk multiplies and kk adds

Cij=ai1b1j+⋯+aikbkj\displaystyle C_{ij} = a_{i1}b_{1j} + \dots + a_{ik}b_{kj}

One entry of C=ABC = AB, with A (m×k)A\,(m \times k) and B (k×n)B\,(k \times n), takes kk multiplications and k−1k - 1 additions. The standard count rounds this to 2k2k floating-point operations (FLOPs): GPUs do a multiply and an add together as one fused multiply-add, which counts as 2 FLOPs.

Write

One entry ≈2k\approx 2k FLOPs (kk multiply-adds).

The whole product: 2mkn2mkn

FLOPs(AB)=2 m k n\displaystyle \text{FLOPs}(AB) = 2\,m\,k\,n
(2×3)(3×2):  2⋅2⋅3⋅2=24\displaystyle (2 \times 3)(3 \times 2): \; 2 \cdot 2 \cdot 3 \cdot 2 = 24

There are m×nm \times n entries and each costs 2k2k. Memorize this one; every cost estimate for a transformer is built from it.

Write

(m×k)(k×n)(m \times k)(k \times n) costs 2mkn2mkn FLOPs.

A transformer-sized product

(2048×4096)(4096×4096)\displaystyle (2048 \times 4096)(4096 \times 4096)
2⋅2048⋅4096⋅4096≈6.9×1010\displaystyle 2 \cdot 2048 \cdot 4096 \cdot 4096 \approx 6.9 \times 10^{10}

Projecting 2,048 tokens of width 4,096 through one 4096×40964096 \times 4096 weight takes 68,719,476,736 FLOPs, about 69 billion. That’s a single weight in a single layer.

✏️ Your turn

One token through one weight

(1×4096)(4096×4096)\displaystyle (1 \times 4096)(4096 \times 4096)

How many FLOPs? And how many parameters does the weight have?

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

Answer: 2 FLOPs per parameter

FLOPs=2⋅40962≈3.4×107\displaystyle \text{FLOPs} = 2 \cdot 4096^2 \approx 3.4 \times 10^{7}
params=40962≈1.7×107\displaystyle \text{params} = 4096^2 \approx 1.7 \times 10^{7}

33,554,432 FLOPs and 16,777,216 parameters: exactly 2 FLOPs per parameter per token, one multiply and one add for each weight.

In a transformer

Add this up over every weight in the model and you get the rule of thumb forward pass ≈2N\approx 2N FLOPs per token for NN parameters (Part 6).

Write

One token through a weight: 2 FLOPs per parameter. Forward pass ≈2N\approx 2N FLOPs per token.

Order matters for cost: (AB)x(AB)x vs A(Bx)A(Bx)

AA and BB are n×nn \times n, xx is a vector, n=4096n = 4096.

Order Work FLOPs
(AB)x(AB)x a matrix product, then matrix × vector 2n3+2n2≈1.37×10112n^3 + 2n^2 \approx 1.37 \times 10^{11}
A(Bx)A(Bx) two matrix × vector products 4n2≈6.71×1074n^2 \approx 6.71 \times 10^{7}

Same answer, about (n+1)/2≈2000(n + 1)/2 \approx 2000 times the work. Associativity lets you choose the cheap order.

Write

Same result, different cost: multiply by the vector first.

Where cheap ordering shows up

In a transformer

LoRA stores a weight update as two thin matrices instead of one d×dd \times d matrix: AA maps dd features down to rr, and BB maps rr back up to dd, with rr small (say 8). Passing a token through AA, then BB, costs 2dr+2rd=4dr2dr + 2rd = 4dr FLOPs. Multiplying the two matrices together first would build a full d×dd \times d matrix: 2d2r2d^2 r FLOPs to make, then 2d22d^2 per token. For d=4096d = 4096 and r=8r = 8 that’s about 131 thousand FLOPs per token against 34 million. (At inference you can instead merge the update into the weight once; Part 7.)

📌 Recap

What a matmul costs in one card

  • (m×k)(k×n)(m \times k)(k \times n) costs 2mkn2mkn FLOPs: a multiply-add is 2 FLOPs.
  • One token through a weight: 2 FLOPs per parameter, so a forward pass ≈2N\approx 2N FLOPs per token.
  • (AB)x=A(Bx)(AB)x = A(Bx), but A(Bx)A(Bx) is far cheaper: choose the order.

Write

The three lines above.