Concepts / Calculating Model Memory Usage (Chapter 7)

Calculating Model Memory Usage (Chapter 7)

Nonlinear functions are needed to break the linearity of feedforward layers, and simpler functions that are faster to compute are preferred because sophisticated ones use too much training compute and memory.

  • Programming

Why Model Size Is Only the First Question

When people talk about large models, the first number they usually mention is the size of the model. Size matters, but the source passage behind this article asks a different set of questions about the design and cost of a neural network. Which mathematical ingredients make a network work at all? How can training be understood? How do hardware and memory constrain a design? What do compute costs look like? Together, these questions explain why architecture and scale are chosen with care rather than by simply making a network larger.

The source is a short passage, so this article stays close to what it actually establishes. Where it makes a claim, we explain that claim. Where it does not go further, we do not fill the gap with outside detail. The detailed method for calculating a model's memory usage is handled in Chapter 7 of the source, and this article only sets up the key idea behind it.

Why a Stack of Linear Layers Is Not Enough

The source explains that a feedforward layer by itself keeps the computation linear, and that the model needs a nonlinear function to break that linearity. The nonlinear step is what lets the network go beyond what a purely linear computation can express. The source does not require this function to be exotic. What matters is that the nonlinearity is present and that it is cheap enough to compute during training.

The reason linearity is a problem can be seen with a small generated example. Suppose one layer multiplies its input by 2, and a second layer multiplies its output by 3. Passing a number x through both layers gives 3 times (2 times x), which is 6 times x. The two layers together are no more powerful than one layer that multiplies by 6. Stacking more purely linear layers never escapes this pattern, which is why a nonlinear step is inserted between them.

Linear layerLinear layerLinear layerNonlinear functionOne linear functionstacking collapsesLinear layerBeyond one linearfunctionfeedscombines intofeedsfeedsrepresents
What happens to a stack of linear layers without a nonlinearity, and how inserting a nonlinear step changes what the network can represent?

Choosing a Nonlinear Function for Cost

Once you know that a nonlinear step is required, the next question is which one to use. The source states that simpler functions that are faster to compute are preferred, because sophisticated ones use too much training compute and memory. The source does not name specific functions, so the comparison below sticks to the trade-off it describes.

PropertySimpler nonlinear functionSophisticated nonlinear function
Breaks linearity of feedforward layersYesYes
Compute cost during trainingLower, faster to computeHigher
Memory cost during trainingLowerHigher
Preference stated in the sourcePreferredNot preferred for this reason

Trade-off described by the source for choosing a nonlinearity

Gradient Descent as a Search

Gradient descent is introduced in the source as a technique to train neural networks. The source then offers a less familiar way of thinking about it. Neural networks are good at simulating many computer programs, and gradient descent can be understood as a search algorithm. It searches through all the programs a neural network can simulate in order to find the best one for its target task.

Programs the networkcan simulateGradient descentthe searchBest program for thetarget taskis searched byoutputs
How does training move from the set of programs a network can simulate to the single program that best fits the target task?

Read this diagram from left to right. The set of programs a network can simulate is the space being searched, the search is carried out by gradient descent, and the output is the program that best fits the target task. The diagram shows the flow of this idea. It does not describe the individual update steps of training, which the source does not spell out.

Why New Architectures Are Hard to Beat

The source presents an argument, attributed to Ilya Sutskever, about why it is so hard to develop new neural network architectures that outperform existing ones. The argument rests on the search idea from the previous section. Because existing networks can simulate many programs, a new architecture can potentially be simulated by existing networks as well.

This changes how architecture work should be judged. A designer is not simply looking for a new shape of network. The real question is whether the new shape reaches a set of programs that the current shapes cannot reach. The source states that a new architecture must be able to simulate programs that existing architectures cannot in order to outperform them. For the full argument, the source points to a talk by Sutskever at the Simons Institute at Berkeley in 2023.

How Hardware Shapes Architecture

Architecture design is also influenced by the hardware that runs the model. The source notes that the transformer was originally designed by Google to run fast on Tensor Processing Units, usually called TPUs, and was only later optimized for Graphics Processing Units, or GPUs. The hardware a design targets therefore leaves a visible mark on the design itself.

The source links these pieces as a chain. The transformer connects to the TPU as its original target, and then to the GPU as a later optimization target. When you read an architecture, it helps to ask which hardware it was built to run on first, because that choice constrains what is fast, what is affordable, and what is practical to train.

Why Memory Use Exceeds Model Size

The source states that the actual memory needed for a model is higher than the model's size. The memory budget therefore has to be planned against more than the bare size of the model. The source does not list every component of that extra memory. It says only that Chapter 7 discusses how to calculate a model's memory usage, so the detailed method belongs there.

Model sizeActual memory neededhigher than model sizeis higher than
How does the memory a model needs relate to the model's size?

Reading Epochs, FLOP/s, and Compute Cost

An epoch is one full pass through the training data. The source notes that, as of its writing, large models are typically pre-trained on only one epoch of data. In other words, the common practice for large models is a single pass rather than many repeated passes over the same data. The source is careful about timing. The phrase as of this writing marks this as a snapshot of common practice at one point in time, not a permanent rule.

Compute is often described in FLOP/s, a count of floating-point operations per second. The source states that the FLOP/s count is measured in FP32, which is one floating point format. A FLOP/s figure should therefore always be read together with the format it was measured in. The source says floating point formats are discussed in Chapter 7, and it does not describe them here, so this article does not go into the individual formats. The practical point is that a FLOP/s number is tied to a stated format.

The source also gives a concrete price point for compute. As of its writing, cloud providers offer H100 GPUs for around 2 to 5 dollars per hour, and the source adds that compute is getting rapidly cheaper, so this number is expected to fall considerably. The price is a dated figure and should be read as a snapshot, not as a current quote.

Worked example

Turning an hourly price into a job cost

Using the source's snapshot price of 2 to 5 dollars per hour for an H100 GPU, what would 10 hours of GPU time cost at the low and high ends of that range?

  1. 1
    Identify the rate range

    The source gives a range of 2 to 5 dollars per hour as of its writing.

  2. 2
    Apply the low rate

    10 hours at 2 dollars per hour is 20 dollars.

  3. 3
    Apply the high rate

    10 hours at 5 dollars per hour is 50 dollars.

  4. 4
    Read the result with its date

    The answer is only as current as the source's snapshot, and the source says compute is getting cheaper.

Result

At the source's snapshot prices, 10 hours of H100 time would cost between 20 and 50 dollars. This is an illustration of the arithmetic, not a current price.

Mistakes When Reading the Source's Claims

  • Common mistake

    Treating a nonlinear function as something that must be sophisticated

    Why it fails

    The source says the function does not have to be exotic, and that simpler, faster functions are preferred.

    Do this instead

    Require that the nonlinearity is present and cheap enough to compute during training.

  • Common mistake

    Assuming that stacking more linear layers adds representational power

    What it looks like

    Two layers that multiply by 2 and then by 3 act like one layer that multiplies by 6.

    Why it fails

    Without a nonlinear step, the computation remains linear.

    Do this instead

    Insert a nonlinear function between layers to break linearity.

  • Common mistake

    Reading gradient descent as a single step that directly builds a model

    Why it fails

    The source frames gradient descent as a search through the programs a network can simulate. It does not describe the individual update steps.

    Do this instead

    Think of training as searching a space of possible programs for the best one for the target task.

  • Common mistake

    Judging a new architecture by how different its shape looks

    Why it fails

    The source says a new architecture must be able to simulate programs that existing architectures cannot in order to outperform them.

    Do this instead

    Ask whether the new design reaches programs that current designs cannot reach.

  • Common mistake

    Equating memory needed with model size

    Why it fails

    The source states that the actual memory needed is higher than the model's size.

    Do this instead

    Plan the memory budget against more than the bare size of the model, and consult Chapter 7 for the calculation method.

  • Common mistake

    Quoting a FLOP/s figure without its format

    Why it fails

    The source states its FLOP/s count is measured in FP32.

    Do this instead

    Always report the floating point format alongside the FLOP/s number.

  • Common mistake

    Treating the one-epoch practice or the GPU price as permanent facts

    Why it fails

    The source attaches both to the time of its writing and says compute is getting cheaper.

    Do this instead

    Read them as dated snapshots of practice and price.

Predicting the Effect of a Missing Nonlinearity

What do you think happens?

A designer stacks two linear layers with no nonlinear step between them. Does the stack represent anything that one linear layer could not?

Reveal answer
Answer:

No. Two linear layers with nothing between them compute a single linear function, so the stack represents nothing one linear layer could not.

This follows from the source's point that a feedforward layer keeps the computation linear, and that a nonlinear function is needed to break that linearity. The generated example of multiplying by 2 and then by 3 shows the same collapse.

MEDIUM

In your own words, explain why a new architecture must be able to simulate programs that existing architectures cannot in order to outperform them, using the search idea from gradient descent.

Hints
  • Start from the idea that gradient descent searches the programs a network can simulate.
  • Ask what it would mean if a new design could only simulate programs that existing networks already simulate.
  • Remember that the source attributes this argument to Ilya Sutskever.

Key Takeaways

  • A feedforward layer keeps the computation linear, so a nonlinear function is needed to break that linearity. It does not have to be exotic, but it must be cheap enough to compute during training, because sophisticated functions use too much compute and memory.
  • Gradient descent can be understood as a search algorithm over the programs a neural network can simulate, looking for the best one for a target task. A new architecture must simulate programs that existing architectures cannot in order to outperform them.
  • Hardware shapes design: the transformer was originally designed by Google for TPUs and later optimized for GPUs. The actual memory needed for a model is higher than the model's size, so memory must be planned beyond size alone.
  • The source's figures on compute are dated snapshots: one epoch of pre-training as common practice, FLOP/s measured in FP32, and H100 GPUs at around 2 to 5 dollars per hour, with compute getting cheaper.

Related concepts

More concepts starting with C