All courses
Courses › How LLMs really work · foundation

Matrix multiplication

Multiply matrix by vector and matrix by matrix, predict shapes like (T×d)(d×k) → T×k.

Builds on: Dot product and cosine similarity, Matrices and shapes

▶ Watch this lesson Start the whole course

What the animation says

Sentences as tables

Inside a language model, every word piece of a sentence is held as a row of numbers. Stack a sentence of six pieces and you get a table: six rows, each four numbers wide. Over and over, the model must turn every row into a new row, often of a different length. It uses one shared table of numbers for all the rows. That step is matrix multiplication.

One row, every column

Start small. Take one row: one, two. The shared table W has two rows, matching that length, and three columns. Dot the row with column one: one times three, plus two times one. Five. Column two: one times zero, plus two times two. Four. Column three holds one and minus one. What comes out?

Every row, on its own

Minus one. So the row one, two becomes five, four, minus one. Two numbers in, three out: one number per column of the table. A second row, zero, one, walks across the same three columns. It gives one, two, minus one, and never looks at the first row. Each output row comes only from its own input row. Your turn: which row and which column made the circled number?

The shape rule

That's the whole rule: every entry is one left row dotted with one right column. Row i, column j of the result: row i of the left, dotted with column j of the right. Now the shapes. Two by two, times two by three, gave two by three. The inner sizes must match, because a row's length must equal a column's length. Then they vanish. The outer sizes stay: rows from the left, columns from the right. So: six by four, times four by eight. What shape comes out?

Order matters

Six by eight. The inner fours cancel; six rows and eight columns stay. Now flip the order: four by eight, times six by four. The inner sizes are eight and six. They don't match, so that product isn't even defined. Even when both orders work, the results usually differ. Order matters. Every left row, dotted with every right column. This one operation is most of the arithmetic a language model does.

Reshaping many rows in one step

Inside a language model, each word (or word piece) of a sentence is held as a row of numbers, so a sentence of $T$ pieces is a $T\times d$ matrix. Over and over, the model must turn **every** one of those rows into a new row, often of a different length, using one shared table of numbers. Matrix multiplication is that operation. How does it work, and what shape comes out?

Many dot products at once

Take a row vector $x$ with $n$ numbers and a matrix $W$ with $n$ rows and $k$ columns. Each **column** of $W$ is also a vector of $n$ numbers. Dot $x$ with column 1, then column 2, … up to column $k$. That gives $k$ numbers: a new row. For a matrix $X$, do this for each row of $X$ separately. Each output row depends only on its own input row.

The rule and the shape check

$$(XW)_{ij}=\sum_{m=1}^{n} X_{im}\,W_{mj}$$ The entry in row $i$, column $j$ of the result is the dot product of row $i$ of $X$ with column $j$ of $W$. Shapes: $(T\times n)(n\times k)\to T\times k$. The inner sizes must match and disappear; the outer sizes stay. Order matters: $WX$ is usually different, or not even defined.

Worked example: (2×2)(2×3)

$$X=\begin{bmatrix}1&2\\0&1\end{bmatrix},\ W=\begin{bmatrix}3&0&1\\1&2&-1\end{bmatrix}$$ Shape: $(2\times2)(2\times3)\to2\times3$. Row 1 $=[1,2]$ dotted with each column: $[1\cdot3+2\cdot1,\ 1\cdot0+2\cdot2,\ 1\cdot1+2\cdot(-1)]=[5,4,-1]$ Row 2 $=[0,1]$: $[0\cdot3+1\cdot1,\ 0\cdot0+1\cdot2,\ 0\cdot1+1\cdot(-1)]=[1,2,-1]$ $$XW=\begin{bmatrix}5&4&-1\\1&2&-1\end{bmatrix}$$

Remember this

Matrix multiplication = dot product of every left row with every right column. $(T\times n)(n\times k)\to T\times k$: inner sizes must match and vanish, and each output row comes only from its own input row. This one operation is most of the arithmetic a language model does.

On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.