From a Matrix Multiply to a Forward Pass
A Forward Pass Is a List of Matrix Multiplies, and You Can Count Each One
A layer and an attention step are both matrix multiplies. Counting the arithmetic and the bytes in each shows which ones can fill a GPU and which cannot.
Courses one to three of this track built the device: *Why a GPU Is Built This Way*, Memory Is the Limit on a GPU and How a Program Runs on a GPU. This course runs one thing on it, a single pass of a model, and accounts for where the time goes. The first step is to see that the pass is nearly all one operation.
A layer is a multiply
A layer holds a table of weights with rows and columns. One position of the input is a list of numbers, and the layer turns it into a list of numbers, each a running total of inputs times weights. That is a matrix multiply with one thin side.
Nobody runs it one position at a time. A prompt of positions, or requests travelling together, is stacked into a table with rows and columns, and the whole layer is one multiply.
- the inputs, b rows of n numbers, one row per position
- the weights, n rows of m numbers, the same for every row of X
- the outputs, b rows of m numbers
- the operations performed: every one of the b times m outputs is a total of n products, and each product costs one multiplication and one addition
Attention is two more
An attention step compares every position with every other and then mixes them accordingly. In the paper that introduced it the whole step is written as two products. Stack the queries into a table and the keys into , each with rows of numbers. The scores are , an by table costing operations. After a small operation on each row of scores, the result is multiplied by the value table , which costs again.
The difference from a layer is what gets read. Neither product touches a weight. Both tables are made from the inputs, so nothing is shared between requests, and the arithmetic grows with the square of the length.
Counting the arithmetic and the bytes
Arithmetic is half of the count. The other half is the bytes moved, because Memory Is the Limit on a GPU showed that a job runs at the lower of two limits and the ratio of arithmetic to bytes decides which. A multiply reads both of its tables and writes its result. With bytes a number, the layer moves
$$
bytes: the weights once, the inputs and the outputs. The arithmetic was a product of three sizes. The bytes are a sum of three tables. Dividing one by the other gives the operations per byte, and the trace does it for four operations at two bytes a number, with 1,024 positions of 4,096 numbers.
| step | operation | arithmetic | bytes read and written | operations per byte | what happened |
|---|---|---|---|---|---|
| 1 | the layer on one position | 2 x 4,096 x 4,096 = 33,554,432 | 2 x (16,777,216 + 4,096 + 4,096) = 33,570,816 | 1.0 | The weights are nearly all of the bytes, and each weight is used once. This is the case a model is in while it writes an answer one word at a time. |
| 2 | the layer on 1,024 positions | 1,024 times as much: 34,359,738,368 | 2 x (16,777,216 + 2 x 4,194,304) = 50,331,648 | 683 | The arithmetic grew 1,024 times and the bytes by half, because the weights are read once however many rows use them. |
| 3 | the scores, queries times keys | 2 x 1,024 x 1,024 x 4,096 = 8,589,934,592 | 2 x (2 x 4,194,304 + 1,048,576) = 18,874,368 | 455 | No weights at all. The two tables read are the size of the inputs and the table written has one cell per pair of positions. |
| 4 | an element by element operation on the outputs | one per number: 4,194,304 | two read and two written per number: 16,777,216 | 0.25 | Each number is read, changed and written back. No reuse is possible, so this is as low as the ratio goes. |
What It Costs to Run a Model takes a representative machine whose balance point is a hundred operations per byte. Against that, the first row is a hundred times short and the second is seven times over. Same weights, same code, opposite limits.
How much of a pass is multiplies
Add up the block for 1,024 positions. Four multiplies by a 4,096 square table come to 137.4 billion operations. The two wider ones in the second half of the block, with 16,384 numbers in the middle, come to 274.9 billion. The two attention multiplies add 17.2 billion. Allow the small operations five operations per number they touch, which is an illustration and a generous one: that is 0.17 billion.
So a forward pass is a list of matrix multiplies, to within a twentieth of a per cent of its arithmetic. Each one has a count of operations, a count of bytes and a ratio between them, and the ratio swings by a factor of a thousand with nothing changing except how many rows share the weights.
That makes the multiply the thing to understand first. The next lesson is how the device runs one: cut into tiles, so that each block of workers reuses what it fetched.
Recap
- A layer of a model is a matrix multiply of the inputs by a table of weights, and an attention step is two more matrix multiplies whose tables are the inputs themselves.
- A matrix multiply of an a by k table and a k by c table performs two times a times k times c operations, one multiplication and one addition for every pairing.
- The same layer carries about one operation per byte when one position passes through it and several hundred when a thousand positions do, because the weights are read once either way.
This is the reading half
Starting the course gives you your own copy of it. Every idea on every page has problems standing under it, marked with a reason rather than a tick, and any sentence you do not believe can be opened and argued with. None of that can happen on a page nobody owns.
The contents