Linear algebra for LLMsPart 2: Matrices and matrix multiplication

Matrix × vector

7 min

A matrix times a vector

=

AA is 2×32 \times 3 and xx is a column of 3 numbers. The result has one entry per row of AA: 2 entries.

Write

A (2×3)A\,(2 \times 3) times x (3×1)x\,(3 \times 1) gives y (2×1)y\,(2 \times 1).

Row view: entry 1

=

Entry 1 of yy is row 1 of AA dotted with xx: 1⋅2+2⋅1+0⋅4=41 \cdot 2 + 2 \cdot 1 + 0 \cdot 4 = 4.

Row view: entry 2

=

Entry 2 is row 2 dotted with xx: 3⋅2+(−1)⋅1+1⋅4=93 \cdot 2 + (-1) \cdot 1 + 1 \cdot 4 = 9. One dot product per row.

Write

Row view: yi=(row i of A)⋅xy_i = (\text{row } i \text{ of } A) \cdot x. y=[4,9]y = [4, 9]

The shape rule

(2×3‾) (3‾×1)=(2×1)\displaystyle (2 \times \underline{3})\,(\underline{3} \times 1) = (2 \times 1)

Each row of AA must be as long as xx, so the inner sizes have to match. The outer sizes are what’s left: one output per row of AA.

Column view: a mix of AA's columns

22+11+44=

Now read it by columns. xx’s entries say how much of each column of AA to take: 2 of column 1, 1 of column 2, 4 of column 3. [2,6]+[2,−1]+[0,4]=[4,9][2, 6] + [2, -1] + [0, 4] = [4, 9]. The same answer.

Write

Column view: Ax=x1(col 1)+x2(col 2)+x3(col 3)Ax = x_1 (\text{col } 1) + x_2 (\text{col } 2) + x_3 (\text{col } 3)

Two views, one product

View AxAx is Good for
Rows a list of dot products, one per row computing by hand; “each output scores the input”
Columns a mix of AA’s columns, weighted by xx seeing what outputs are possible at all

Every output AxAx is some combination of AA’s columns. Those combinations are AA’s column space (Part 3).

✏️ Your turn

Multiply

Write the product, using the row view. Then check it with the column view.

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

Answer

= 11 + 22 =

Rows: 2⋅1+0⋅2=22 \cdot 1 + 0 \cdot 2 = 2, 1⋅1+1⋅2=31 \cdot 1 + 1 \cdot 2 = 3, 0⋅1+3⋅2=60 \cdot 1 + 3 \cdot 2 = 6. Columns: 1 of the first column plus 2 of the second. A 3×23 \times 2 matrix turns 2 numbers into 3.

The code layout: a row vector times a matrix

= 22 + 11

With the vector on the left as a row, everything flips: xWxW is a mix of WW’s rows, weighted by xx’s entries. 2 [1,0,2]+1 [3,1,−1]=[5,1,3]2\,[1, 0, 2] + 1\,[3, 1, -1] = [5, 1, 3]. Entry jj is also xx dotted with column jj of WW.

Write

Row vector: xW=x1(row 1 of W)+x2(row 2 of W)+…xW = x_1 (\text{row } 1 \text{ of } W) + x_2 (\text{row } 2 \text{ of } W) + \dots

What a linear layer does to one token

In a transformer

For one token x (1×din)x\,(1 \times d_{\text{in}}) and a weight W (din×dout)W\,(d_{\text{in}} \times d_{\text{out}}), the layer outputs xW (1×dout)xW\,(1 \times d_{\text{out}}). Each output feature jj is x⋅W:,jx \cdot W_{:,j}: column jj of WW acts as a detector, scoring how much xx points its way. One head’s slice of WQW_Q is 4096×1284096 \times 128 in a Llama 3 8B-sized model.

y = x @ W        # x: (d_in,), W: (d_in, d_out) → y: (d_out,)

📌 Recap

Matrix × vector in one card

  • Shapes: (m×n)(n×1)=(m×1)(m \times n)(n \times 1) = (m \times 1). Inner sizes match.
  • Row view: each output is a row of AA dotted with xx.
  • Column view: AxAx mixes AA’s columns, weighted by xx.
  • Code layout: xWxW mixes WW’s rows; output jj = x⋅W:,jx \cdot W_{:,j}.

Write

The four lines above.