Add Up the Largest Moment, Not the Average
Whether a job runs on a GPU at all is decided by the bytes it holds at its fullest moment, and that total can be worked out from the shapes before anything is launched.
A GPU has its own memory, separate from the computer's. The vendor's programming guide calls it global memory; this course calls it the device's memory. Everything a job reads or writes while it runs has to be in it, and it has a fixed size. The device used as the example throughout this course is the NVIDIA A100 80GB, announced in November 2020, whose product page lists 80 GB. Take a gigabyte as a billion bytes.
A job that needs 81 GB at any instant does not run slowly on that device. It stops. So the first question about any job is whether it fits, and the answer can be worked out on paper.
Two kinds of bytes
Sort what the job holds into two piles.
The fixed part is there whatever the size of the batch. For a model it is the table of weights. The working part is produced along the way: the values each layer hands to the next. It exists once for every item in the batch, and each piece lives only until nothing needs it any more.
- bytes held at the largest moment
- the fixed part, the same for any batch
- the number of items in the batch
- working bytes one item needs at the largest moment
The split matters because the two piles answer to different remedies. If the weights alone exceed the device, no batch is small enough. If the working part is what overflows, the batch is the first thing to change.
Bytes from shapes
Every term comes from one multiplication: the count of values times the bytes each value takes. Take a model with seven billion weights stored at two bytes each, forty-eight layers deep, where each layer hands the next a thousand positions of four thousand values for every item.
| step | what it is | values | bytes each | size | what happened |
|---|---|---|---|---|---|
| 1 | the weights | 7,000,000,000 | 2 | 14 GB | Fixed. Read on every pass, never changed while the model is only being run. |
| 2 | one layer's output for one item | 1,000 x 4,000 = 4,000,000 | 2 | 8 MB | Working. One of these exists per item for each layer whose output is still needed. |
| 3 | a correction for every weight, when training | 7,000,000,000 | 2 | 14 GB | Fixed, and present only when training: one number per weight saying which way to move it. |
The largest moment
How many of those eight megabyte outputs are alive at once depends on what the job is doing, and this is where an average misleads.
Running the model, a layer's input can be dropped as soon as its output exists. At most two are alive for an item: the one being read and the one being written. So megabytes.
Training is different. The pass back through the layers needs the output of every layer from the pass forward, so none can be dropped until the pass back reaches it. At the turn all forty-eight are alive: megabytes for each item, twenty-four times as much. The fixed part doubles too, to 28 GB, because of the corrections.
| step | moment | fixed part, GB | layer outputs alive | working part, GB | held, GB | what happened |
|---|---|---|---|---|---|---|
| 1 | before the pass forward | 28 | 0 | 0 | 28 | Weights and room for their corrections. This much is held before a single item arrives. |
| 2 | forward, after layer 12 | 28 | 12 | 9.6 | 37.6 | A hundred items times twelve outputs of eight megabytes each. |
| 3 | forward, after layer 48: the turn | 28 | 48 | 38.4 | 66.4 | The largest moment. Every output of every layer for every item is alive. |
| 4 | back, with 12 layers still to do | 28 | 12 | 9.6 | 37.6 | Each output is dropped once the pass back has used it. |
| 5 | step finished | 28 | 0 | 0 | 28 | Back where it started, ready for the next batch. |
A job is safe only if its largest moment fits. A reading taken at any other moment is a lower number and proves nothing.
How many items fit
Turn the sum around. With the device's memory in place of , the largest batch is what is left after the fixed part, divided by what one item needs at the largest moment, rounded down:
$$
Running the model: GB left, and items. Training it: GB left, and , so 135 items.
| fixed part, GB | working part per item, MB | left for the batch, GB | largest batch | |
|---|---|---|---|---|
| running the model | 14 | 16 | 66 | 4125 |
| training the model | 28 | 384 | 52 | 135 |
Two cautions keep the estimate honest. The division assumes the free memory is one piece, and a long run can leave it in several, so plan to stay a few per cent under. And the count must include everything the job keeps: a model that remembers something about each conversation, as *What It Costs to Run a Model* works out, has a second working part that grows with the length of the conversation as well as with the number of them.
Fitting only says the job can start. How fast it could then run is set by a second number on the same product page, the rate at which the device's memory delivers bytes, and that is the next lesson.
Recap
- A job fits on a GPU only if the bytes it holds at its fullest moment are no more than the device's memory, and the average over the run tells you nothing about that.
- The bytes a job holds split into a fixed part that is there whatever the batch and a working part that grows with every item, and only the working part can be traded against the batch size.
- Training a model needs far more memory than running it, because every layer's output is kept until the pass back, where running it keeps only the two layers it is between.
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