Basis Lab

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 WW with nn rows and mm columns. One position of the input is a list of nn numbers, and the layer turns it into a list of mm 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 bb positions, or bb requests travelling together, is stacked into a table XX with bb rows and nn columns, and the whole layer is one multiply.

FIG 1A layer, written as 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
Three sizes and a factor of two. The layer used throughout this course has n and m both 4,096, so its table holds 16,777,216 weights and one row of input costs 33,554,432 operations.

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 QQ and the keys into KK, each with LL rows of dd numbers. The scores are QKTQ K^{T}, an LL by LL table costing 2L2d2L^{2}d operations. After a small operation on each row of scores, the result is multiplied by the value table VV, which costs 2L2d2L^{2}d 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.

FIG 2One block of a pass, in the order it runs
Eight multiplies and a handful of small operations between them. The multiplies are where the arithmetic is, and lesson five is about what the boxes between them cost anyway.

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 ff bytes a number, the layer moves

$M=f (nm+bn+bm)M = f\,(n m + b n + b m)$

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.

FIG 3Four operations of one block, counted
stepoperationarithmeticbytes read and writtenoperations per bytewhat happened
1the layer on one position2 x 4,096 x 4,096 = 33,554,4322 x (16,777,216 + 4,096 + 4,096) = 33,570,8161.0The 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.
2the layer on 1,024 positions1,024 times as much: 34,359,738,3682 x (16,777,216 + 2 x 4,194,304) = 50,331,648683The arithmetic grew 1,024 times and the bytes by half, because the weights are read once however many rows use them.
3the scores, queries times keys2 x 1,024 x 1,024 x 4,096 = 8,589,934,5922 x (2 x 4,194,304 + 1,048,576) = 18,874,368455No weights at all. The two tables read are the size of the inputs and the table written has one cell per pair of positions.
4an element by element operation on the outputsone per number: 4,194,304two read and two written per number: 16,777,2160.25Each number is read, changed and written back. No reuse is possible, so this is as low as the ratio goes.
4 steps
The vendor's own performance guide lists a linear layer at a batch of one at 1 operation per byte and an activation at 0.25, the first and last rows here. The spread from a quarter to several hundred is inside one block of one model.

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.

FIG 4The arithmetic of one block, by kind
The small operations are four ten thousandths of the arithmetic. If time followed arithmetic they could be ignored, and the rest of this course is about why time does not follow arithmetic.

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

NextMultiplying in Tiles →

The rest of this course

  1. 01A Forward Pass Is a List of Matrix Multiplies, and You Can Count Each Oneyou are here
  2. 02A Multiply Is Cut Into Tiles So That Each Block of Workers Reuses What It Fetchedopening only
  3. 03A Unit That Only Multiplies Small Matrices Is Worth Eight Times, on the Multiplies That Can Feed Itopening only
  4. 04Halving the Size of a Number Is Worth Exactly Two to Memory and Whatever the Units Say to Arithmeticopening only
  5. 05Every Operation Has a Fixed Cost to Start, and the Small Ones Are Nearly All Startopening only
  6. 06A Run of Small Operations Joined Into One Pass Pays for One Start and One Trip Through Memoryopening only
  7. 07A Multiply Fills the Device Only Above a Certain Batch, and the Batch Can Be Computedopening only
  8. 08A Device Reported as Fully Busy May Be Using One Hundredth of Its Arithmeticopening only

Read alongside