Basis Lab

What It Costs to Run a Model

The Two Kinds of Work

A chip that runs a model multiplies numbers and moves numbers, and the second is hundreds of times slower, which quietly decides almost everything else.

A chip that runs a trained model does two separable things, and almost every badly wrong estimate of what serving costs comes from collapsing them into one. The first is arithmetic: multiplying two numbers and adding the result to a running total, which is very nearly the only operation a model performs. The second is movement: fetching those numbers out of the memory they are stored in and delivering them to the small fast region where the multiplying happens.

Both have a top speed. Both speeds are published. And the two are nothing like each other.

The two numbers on the box

Take a representative accelerator of the current generation. It will advertise something near three hundred trillion multiply-and-add operations per second in a half-size number format, and something near three terabytes per second of memory bandwidth. The first is how fast it can calculate, the second how fast it can be fed.

Divide one by the other and you get a number that describes the machine rather than any job: three hundred trillion over three trillion is one hundred. The chip can perform about a hundred operations in the time it takes to fetch a byte. Any job carrying more arithmetic than that per byte will be held up by the arithmetic. Any job carrying less will be held up by the fetching, and the expensive multiplying units will sit idle.

FIG 1The machine's balance point
the operations the chip can perform each second, as advertised
the bytes it can read out of its own memory each second, also as advertised
operations per byte, meaning how much arithmetic a job has to carry for every byte it reads if the multiplying units are to be kept busy
Three hundred trillion operations a second against three terabytes a second gives a hundred. Work carrying less arithmetic than that per byte cannot fill the chip, whatever else is done to it.

That number is not a quirk of one product. It has drifted upward for thirty years, because the multipliers that can be packed onto a chip have grown much faster than the rate at which numbers can be delivered to them. A machine from a decade ago sat nearer ten. The direction of travel is the part worth remembering: as hardware improves, movement becomes relatively more expensive, not less.

Operations per byte of a real piece of work

Now the job. A layer of a model is a large table of weights, and pushing one list of numbers through it means multiplying the list by the table: for every weight, one multiplication and one addition. A table of a million weights therefore costs two million operations and requires every weight to be read once. At two bytes a weight, that is two million bytes moved to do two million operations. Exactly one operation per byte.

One against a hundred. Writing a single word of an answer, the model reaches about one per cent of what the chip could calculate. The same sum points at the way out: push not one list through the table but many at once, and the table is read once and used for all of them.

FIG 2Operations per byte of one layer, for the table of a million weights
stephow many lists go through the table togetheroperations, one multiply and one add per weight per listbytes read, the table once at two bytes a weightoperations per bytewhat happened
1one list, which is writing one word of one answer1,000,000 x 2 x 1 = 2,000,0001,000,000 x 2 = 2,000,0001The case worked out in the paragraph above, and the case a serving system spends most of its time in. One operation per byte against a machine that wants a hundred.
2sixteen lists1,000,000 x 2 x 16 = 32,000,0002,000,000, unchanged16The table is read once and used sixteen times. Nothing about the model changed and nothing about the arithmetic per list changed.
3a hundred lists1,000,000 x 2 x 100 = 200,000,0002,000,000, unchanged100The machine's balance point, reached by waiting for a hundred requests rather than by buying anything.
4a two thousand word prompt, all at once1,000,000 x 2 x 2,000 = 4,000,000,0002,000,000, unchanged2,000Every word of a prompt goes through the table in the same pass, which is why reading one is twenty times past the point where the multipliers are the limit.
4 steps
The third column never moves, because the table is read once however many lists are waiting, and the second column is the only one that grows. Make the table bigger and those two grow together in step, so the last column does not shift at all: what a layer carries per byte is just how many requests are travelling through it at the same moment, and almost nothing to do with how big the model is.

That cancellation is easy to miss. A small model and a huge one, each serving one request at a time, waste the same ninety-nine per cent. The huge one is slower in absolute terms, having more bytes to read, but the inefficiency is about how many requests are in flight at one moment, and a later lesson is given over to it.

The shape of the limit

The two limits apply at the same time rather than in sequence, so a piece of work takes the larger of the two times it implies, not their sum. Plot the rate you can achieve against the arithmetic the work carries per byte and the picture has a characteristic shape: a straight rise, where you are getting everything memory can deliver, then a flat ceiling, where you are getting everything the multipliers can do. The corner sits at the machine's balance point.

FIG 3What the machine will actually give you
0.00200.00400.00600.00800.000.050.0100.0150.0200.0operations the work carries per byte it reads
held back by movement: everything memory can deliverheld back by arithmetic: everything the multipliers can do
The lower of the two lines is what you get. Left of the corner, extra arithmetic is free and only moving fewer bytes helps. Right of it, moving fewer bytes buys nothing.

This picture is old and not specific to models. It was named the roofline and proposed for any program on any multicore machine, long before a language model existed. Its value is that it refuses to let you call a program fast or slow without saying which wall it is standing against, because the remedy differs completely. Against the ceiling you need a better algorithm or more hardware. Against the slope, extra arithmetic is free.

FIG 4Four pieces of work on the same machine
operations carried per byte readper cent of the arithmetic reached
writing one word for one request11
writing one word for sixteen requests at once1616
writing one word for a hundred requests at once100100
reading a two thousand word prompt2000100
The marked cell is the first row that fills the machine. Reading a prompt is comfortable because every word in it goes through the table at once. Writing the answer is not, because the words come out one at a time, each depending on the one before.

A first pass over the rest of the course

Those four rows are most of what a serving system does, and they differ by three orders of magnitude. Reading a prompt is the comfortable case: the whole prompt goes through each table at once, so the table is read once and used thousands of times. Writing the answer is the opposite case, and it is also the case that takes the time, since a reply of five hundred words is five hundred separate passes through the entire model.

Everything in production serving that looks like a trick is an attack on that first row. Shrinking the number format halves the bytes without touching the operations. Grouping requests multiplies what the work carries per byte by the size of the group. Letting a small model guess several words so the large one can check them in a single pass converts a run of single words into one wider piece of work. Different mechanisms, one target.

FIG 5The two hops, and which one is published
Only the first arrow is the number on the box. The art of a fast implementation is arranging for data that has made that journey once to be used many times before it is discarded.

Where the bytes actually travel

That is the frame for everything that follows. A job has two costs, a machine has two speeds, dividing the speeds gives one threshold, and making a model cheap to run is largely the business of taking work that sits at one operation per byte and getting it up somewhere near a hundred.

Recap

  • Every piece of work has an arithmetic cost and a movement cost, and the time it takes is set by whichever of the two is larger, never by the two of them added together.
  • A machine's two published speeds divide to give one number, the operations it can perform per byte it can fetch, and that number is a line which any given job either clears or does not.
  • A model answering one request a word at a time carries about one operation per byte it reads, roughly a hundred times short of the line, so it is a movement problem wearing the costume of a computing problem.

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

NextHow Many Bits a Number Needs →

The rest of this course

  1. 01The Two Kinds of Workyou are here
  2. 02Paying for Precisionopening only
  3. 03The Ceiling You Cannot Code Aroundopening only
  4. 04What the Model Holds On Toopening only
  5. 05The Length at Which Everything Changesopening only
  6. 06The One Trick That Actually Worksopening only
  7. 07Reading and Writing Are Different Jobsopening only
  8. 08What a Million Words Actually Costsopening only

Read alongside