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 takes coordinates in one space and produces coordinates in another. Each output component of is a dot product between one row of and the input . 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
Applied to it yields : 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, , Qwen3.8-27B’s up-projection has shape : 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:
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 of is the dot product of row of with column of . More important than the recipe is the meaning: represents composition, with acting first and second. Multiplication is not commutative. and can differ, and one can be defined while the other is not. Shape is what records which compositions make sense.
Neural layers usually compute . 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 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: . By associativity that equals , and 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.
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 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
Complete the teach-back and answer the quiz correctly to finish this lesson.
◎ · Evidence marker
Sources
- Ian Goodfellow, Yoshua Bengio, and Aaron Courville (2016). Deep Learning.
- PyTorch Contributors (2026). PyTorch Linear.
- Qwen Team (2026). Qwen3.8-27B Model Card.