Basis Lab

Memory Is the Limit on a GPU

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.

FIG 1What the job holds at its fullest
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 job runs when M is no more than the device's memory. Only the second term can be traded: a smaller batch shrinks it and leaves F exactly where it was.

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.

FIG 2Sizing each thing the job holds
stepwhat it isvaluesbytes eachsizewhat happened
1the weights7,000,000,000214 GBFixed. Read on every pass, never changed while the model is only being run.
2one layer's output for one item1,000 x 4,000 = 4,000,00028 MBWorking. One of these exists per item for each layer whose output is still needed.
3a correction for every weight, when training7,000,000,000214 GBFixed, and present only when training: one number per weight saying which way to move it.
3 steps
Three multiplications and no measurement. The sizes that surprise people are the ones nobody multiplied out, and the second row is the usual one, because eight megabytes looks harmless until it is multiplied by layers and by items.

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 w=2×8=16w = 2 \times 8 = 16 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: w=48×8=384w = 48 \times 8 = 384 megabytes for each item, twenty-four times as much. The fixed part doubles too, to 28 GB, because of the corrections.

FIG 3One training step on a batch of a hundred items
stepmomentfixed part, GBlayer outputs aliveworking part, GBheld, GBwhat happened
1before the pass forward280028Weights and room for their corrections. This much is held before a single item arrives.
2forward, after layer 1228129.637.6A hundred items times twelve outputs of eight megabytes each.
3forward, after layer 48: the turn284838.466.4The largest moment. Every output of every layer for every item is alive.
4back, with 12 layers still to do28129.637.6Each output is dropped once the pass back has used it.
5step finished280028Back where it started, ready for the next batch.
5 steps
Averaged over the step, this job holds about 47 GB. The device has to hold 66.4 GB, because that is what is alive at the turn, and a tool that samples memory now and then will usually miss it.

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 DD in place of MM, the largest batch is what is left after the fixed part, divided by what one item needs at the largest moment, rounded down:

$n=⌊D−Fw⌋n = \left\lfloor \frac{D - F}{w} \right\rfloor$

Running the model: 80−14=6680 - 14 = 66 GB left, and 66/0.016=4,12566 / 0.016 = 4{,}125 items. Training it: 80−28=5280 - 28 = 52 GB left, and 52/0.384=135.452 / 0.384 = 135.4, so 135 items.

FIG 4The same model on the same 80 GB device
fixed part, GBworking part per item, MBleft for the batch, GBlargest batch
running the model1416664125
training the model2838452135
Nothing about the model changed between the rows. The fixed part doubled and the working part per item grew twenty-four times, so the largest batch fell from 4,125 to 135, a factor of about thirty.

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

NextThe Rate Bytes Arrive →

The rest of this course

  1. 01Add Up the Largest Moment, Not the Averageyou are here
  2. 02The Fastest a Job Can Run Is Its Bytes Divided by the Bandwidthopening only
  3. 03One Division Says Whether Faster Arithmetic Would Helpopening only
  4. 04Where a Byte Is Kept Decides What It Costs to Readopening only
  5. 05A Group That Reads Next-Door Addresses Wastes Nothingopening only
  6. 06Store Each Field in Its Own Array and the Group Reads Next-Door Bytesopening only
  7. 07Use a Value Many Times While It Is Near and the Memory Stops Being the Limitopening only
  8. 08Four Ways to Fit, and What Each One Costsopening only

Read alongside