Foundations

Matrix multiplication as transformation

Matrix multiplication composes learned changes of coordinates; one real example is Qwen3.8-27B's feed-forward up-projection from 5120 inputs to 17408 outputs, about 89.1 million multiply-adds for a single token.

Updated

01 · Concept

Concept

You have a token’s hidden state: 5120 numbers, as lesson 0.4 established. Inside every layer of Qwen3.8-27B, that state gets pushed through a feed-forward block that first widens it to 17408 dimensions and then narrows it back. Both of those numbers come straight out of the model’s configuration. The question this lesson answers is what that widening actually does, and what it costs, because the answer explains most of where a 27-billion-parameter model’s parameters and arithmetic actually go.

Matrix multiplication is easiest to understand as a transformation rather than as a grid-filling ritual. A matrix WW takes coordinates in one space and produces coordinates in another. Each output component of WxWx is a dot product between one row of WW and the input xx. The rows are therefore independent learned questions asked about the same input, and the number of rows is how many questions get asked.

A small case makes the geometry visible. Take

W=[2001/2].W=\begin{bmatrix}2&0\\0&1/2\end{bmatrix}.

Applied to (x1,x2)(x_1,x_2) it yields (2x1,x2/2)(2x_1, x_2/2): a square becomes a rectangle, stretched horizontally and squashed vertically. Other matrices rotate, reflect, shear, or project onto a subspace. At 5120 dimensions the picture disappears, but the operation is unchanged — a learned change of representation, nothing more exotic.

Now the real one. With the convention used here, WxWx, Qwen3.8-27B’s up-projection has shape 17408×512017408 \times 5120: one row for each output and one column for each input. To transform a single token, the machine must produce 17408 output coordinates, and each one is a dot product over 5120 inputs. Count it directly:

5120×17408=89,128,96089.1 million multiply-adds.5120 \times 17408 = 89{,}128{,}960 \approx 89.1\ \text{million multiply-adds}.

That is one matrix, one layer, one token. The model has 64 layers, and the feed-forward block of each layer contains more than one matrix of comparable size; lesson 4.9 does the full accounting and shows that the feed-forward blocks hold the large majority of the model’s parameters. But hold the single number first, because it recalibrates intuition. Generating a hundred-token reply is not a hundred small lookups. Every token pushed through every layer triggers tens of millions of multiply-adds in this matrix alone, and the hardware questions of track 9 exist because of it.

The rank of a matrix measures how many independent directions actually pass through it. A low-rank map forces its outputs into a smaller subspace than the shape suggests. This is not merely a caveat: it is the mechanism behind parameter-efficient fine-tuning in lesson 6.3, which adapts a huge matrix by adding a deliberately low-rank correction rather than retraining all 89 million numbers.

Matrix-matrix multiplication does many of these transformations at once. Entry (i,j)(i,j) of ABAB is the dot product of row ii of AA with column jj of BB. More important than the recipe is the meaning: ABAB represents composition, with BB acting first and AA second. Multiplication is not commutative. ABAB and BABA can differ, and one can be defined while the other is not. Shape is what records which compositions make sense.

Neural layers usually compute y=Wx+by = Wx + b. The matrix supplies the linear transformation and the bias translates every result, making the map affine rather than strictly linear: zero can now map to bb instead of to zero. That freedom lets a decision boundary sit somewhere other than through the origin.

Here is the classic wrong turn, and it is the reason activation functions exist at all. Suppose you stack two linear layers to get depth: W2(W1x)W_2(W_1 x). By associativity that equals (W2W1)x(W_2 W_1)x, and W2W1W_2 W_1 is just another matrix. Two layers have exactly the expressive power of one. Add biases and the same collapse happens with a single combined affine map. Stack fifty of them and you still have one matrix. Depth in a purely linear network is an illusion, purchased with parameters and paid for in arithmetic, delivering nothing. Something nonlinear must interrupt the composition, which in Qwen’s feed-forward block is a gated activation covered in lesson 2.3.

Matrix multiplication dominates neural computation partly because it is expressive and largely because hardware is exceptionally good at it. A batch of token vectors multiplied by one weight matrix reuses the same parameters across every position, so the arithmetic-to-memory ratio is favorable. Accelerators split large products into tiles, stream them through a memory hierarchy, and accumulate partial sums. The algebra is simple; whether it runs fast depends on shapes, precision, data movement, and fusion, which is the subject of tracks 8 and 9.

The reading habit to take forward is a question asked at every matrix in a model: which space enters, which space leaves, how many independent directions can survive, and what does this cost per token? Applied to Qwen’s projection from 5120 inputs to 17408 outputs, the answers are a narrow state in, a wide state out, at most 5120 independent directions, and 89.1 million multiply-adds. That is what a transformer layer feels like from the inside.

02 · Analogy

Analogy

A theater lighting desk maps a handful of control sliders to hundreds of lamps: one matrix says how each slider drives red, green, and blue across the stage. A second matrix maps the resulting stage colors to what the broadcast camera records. Multiplying the two matrices builds a single cue that goes straight from slider to camera. The order cannot be swapped, because lighting a room and then filming it is not the same operation as trying to light a finished recording.

03 · Teach it back

Teach it back

Describe matrix multiplication as composition, explain why a stack of purely linear layers collapses, and compute the multiply-add cost of Qwen3.8-27B's feed-forward up-projection for one token.

Minimum: 80 characters and 15 words. Your writing stays only in this browser.

Waiting for your explanation.

Compare with a model answer

A matrix maps input coordinates to output coordinates by taking a dot product of each row with the input, so applying A then B is the single transformation BA, read right to left; AB generally differs and may not even be shape-valid. Because the product of two matrices is another matrix, stacking linear layers with nothing between them is equivalent to one layer, which is why nonlinear activations must interrupt the stack. Qwen3.8-27B has hidden size 5120 and feed-forward intermediate size 17408, so with WxWx and output units stored as rows, its up-projection is a 17408 by 5120 matrix; producing all 17408 outputs for one token requires 17408 dot products of length 5120, which is 5120 times 17408, about 89.1 million multiply-add operations, for that one matrix at that one layer for that one token.

04 · Check your understanding

Check your understanding

01Qwen3.8-27B's feed-forward up-projection maps 5120 dimensions to 17408. How many multiply-add operations does it take to transform one token?
Answer and explanation

About 89.1 million — Each of the 17408 output coordinates is a dot product over 5120 inputs, so the cost is 5120 times 17408, which is 89,128,960.

02If A acts on x and B acts on the result, what single matrix performs both?
Answer and explanation

BA, since composition reads right to left — B(Ax) = (BA)x. The rightmost matrix touches the input first, which is why the notation reverses the order of the steps.

03Lesson 0.4 defined the dot product as a sum over matching components. Where does that operation appear inside a matrix-vector product?
Answer and explanation

Each output coordinate is the dot product of one matrix row with the input vector — A matrix-vector product is exactly a batch of dot products, one per row, which is why widening the output dimension multiplies the work linearly.

Complete the teach-back and answer the quiz correctly to finish this lesson.

◎ · Evidence marker

Sources

  1. Ian Goodfellow, Yoshua Bengio, and Aaron Courville (2016). Deep Learning.
  2. PyTorch Contributors (2026). PyTorch Linear.
  3. Qwen Team (2026). Qwen3.8-27B Model Card.