Concepts / Foundation Models and Finetuning

Foundation Models and Finetuning

The more trainable parameters a model has, the higher its memory footprint during finetuning. Reducing the number of parameters that are updated is therefore a direct route to lower finetuning memory. This reasoning is the motivation for PEFT.

  • Programming

Why Large Models Strain Memory

Foundation models are very large, and that size is where the trouble starts. Because of their scale, memory is a bottleneck for working with them. This is true in two situations: when you run a model to produce outputs, which is called inference, and when you adjust a model for a specific task, which is called finetuning.

Keep both situations in mind as you read. The goal of this article is to trace where memory goes, why finetuning asks for much more of it than inference, and which two techniques, parameter-efficient finetuning and quantization, are designed to reduce it.

Tracing Memory Through Inference and Finetuning

Definition

A model's memory footprint is the amount of memory it occupies.

Inference uses a model to produce outputs. Finetuning uses a model as a starting point and adjusts it for a specific task. The source is clear that finetuning typically needs much more memory than inference for the same model. So when you move from running a model to finetuning it, the memory question changes from whether the model fits to whether the extra work of adjusting it also fits.

This difference is the core of the memory bottleneck. The model itself is the same in both cases. What differs is the additional state that finetuning needs, and the next section names the three factors that drive the total.

The Three Contributors to the Footprint

During finetuning, the source identifies three key contributors to a model's memory footprint. The first is the number of parameters, which describes the model itself. The second is the number of trainable parameters, which describes how many of those parameters are adjusted during finetuning. The third is the numerical representations, which describe how many bits are used to store each value.

Memory footprintNumber of parametersdescribes the model itselfNumber of trainableparametersadjusted during finetuningNumericalrepresentationsbits used per valuefeeds intofeeds intofeeds into
Which three factors feed into the total memory footprint during finetuning, and what does each one describe?

The three contributors are not interchangeable, and that is what makes them useful to reason about. The number of parameters is a property of the model. The number of trainable parameters is a property of the finetuning process, because it counts how many parameters are adjusted. The numerical representations are a property of storage, because they set how many bits each value uses. Each one feeds into the total, so reducing any one of them can reduce the footprint.

Key point

The more trainable parameters a model has, the higher its memory footprint during finetuning. Reducing the number of parameters that are updated is therefore a direct route to lower finetuning memory. This reasoning is the motivation for PEFT.

How PEFT Cuts Trainable Parameters

Parameter-efficient finetuning, usually called PEFT, reduces the memory needed for finetuning by reducing the number of trainable parameters. It leaves the model's overall parameter count where it is. What it changes is how many of those parameters are adjusted when the model is finetuned.

Pretrained modelFixed parametersnot adjusted in finetuningFinetuning memorylower with fewer trainableparametersTrainable parametersreduced set adjusted infinetuningkeepsadjustssets
Under PEFT, which parameters of a pretrained model are adjusted during finetuning, and how does that split affect the memory needed?

Read the diagram as a split. Most of the pretrained model stays fixed, and only a reduced set of parameters is adjusted. Because the memory cost of finetuning grows with the number of trainable parameters, shrinking that set shrinks the cost. The model still has the same number of parameters, so PEFT is aimed at the second contributor, not the first.

How Quantization Shrinks Each Value

Quantization targets the third contributor, the numerical representations. It reduces memory by converting a model to fewer bits per value. The source gives 16 bits, 8 bits, and even 4 bits as examples of the lower widths used. Fewer bits per value means each stored number occupies less space, so the whole model takes up less memory without changing how many parameters it has.

32-bit values16-bit values13 billion parameters13 billion parameters52 GB26 GBsets sizesets size
When a 13 billion parameter model moves from a wider numerical format to a narrower one, what changes in the bits per value and in the total size?
Worked example

Halving the size of a 13 billion parameter model

A 13 billion parameter model occupies 52 GB when stored in its original numerical format. After quantization to fewer bits per value, it occupies 26 GB. Explain what the change tells you.

  1. 1
    Identify the starting size

    The model starts at 52 GB. This is the footprint before quantization.

  2. 2
    Identify the ending size

    After converting the model to fewer bits per value, the footprint is 26 GB.

  3. 3
    Compare the two sizes

    The footprint drops from 52 GB to 26 GB, which is half the original size.

  4. 4
    Connect the change to bits

    The number of parameters stays at 13 billion. Only the bits used per value change, so the reduction comes from the numerical representation, which is the third contributor.

Result

Quantization reduced the footprint from 52 GB to 26 GB by storing each value in fewer bits, while the parameter count stayed the same.

Inference Precision Versus Training Precision

Quantization fits inference most naturally. The source explains that inference is typically done using as few bits as possible. Training is different. It is more sensitive to numerical precision, which makes it harder to train a model in lower precision. The table below sets the two side by side.

AspectInferenceTraining
Typical precisionAs few bits as possible, such as 16, 8, or 4 bitsMixed precision: some operations in higher precision such as 32-bit, others in 16-bit or 8-bit
Sensitivity to precisionLower, so quantization fits wellHigher, which makes lower precision harder to train with
Main memory leverStoring values in fewer bitsKeeping higher precision where needed while using fewer bits elsewhere

Numerical precision in inference and training, as described in the source

Training is typically done in mixed precision. Some operations run in higher precision, such as 32-bit, and others run in lower precision, such as 16-bit or 8-bit. The mix lets training use fewer bits in places while keeping higher precision where the source says it is needed for stability of training.

Predicting the Footprint After Quantization

What do you think happens?

A 13 billion parameter model takes 52 GB at its original precision. If it is quantized from 32-bit values to 16-bit values and the parameter count stays the same, what is its new footprint?

  • 13 GB

  • 26 GB

  • 52 GB

  • 104 GB

Reveal answer
Answer:

26 GB

Halving the bits per value halves the stored size, and the number of parameters does not change. The source gives this exact move from 52 GB to 26 GB for a 13 billion parameter model.

MEDIUM

A team finetunes a model using PEFT so that far fewer parameters are trainable, and separately quantizes the model to fewer bits per value. Name which contributor each technique targets, and say whether either one changes the total number of parameters in the model.

Hints
  • Recall the three contributors: number of parameters, number of trainable parameters, and numerical representations.
  • PEFT reduces the number of trainable parameters. Quantization converts the model to fewer bits per value.
  • The source says PEFT reduces memory by reducing trainable parameters, and it does not say it removes parameters from the model.

Mistakes Beginners Make With Memory and Precision

  • Common mistake

    Assuming finetuning needs about the same memory as inference

    Why it fails

    The source states that finetuning typically needs much more memory than inference for the same model, because finetuning adds the trainable parameters and their numerical handling to the footprint.

    Do this instead

    Estimate finetuning memory separately, and consider the number of trainable parameters as well as the size of the model.

  • Common mistake

    Thinking PEFT reduces the number of parameters in the model

    Why it fails

    PEFT reduces the number of trainable parameters, which is the second contributor. The model's overall parameter count is the first contributor, and PEFT targets the trainable subset.

    Do this instead

    Describe PEFT as reducing how many parameters are adjusted during finetuning.

  • Common mistake

    Treating quantization and PEFT as targeting the same contributor

    Why it fails

    Quantization changes the numerical representations, which is the third contributor. PEFT changes the number of trainable parameters, which is the second.

    Do this instead

    Match each technique to its contributor: PEFT to trainable parameters, quantization to bits per value.

  • Common mistake

    Assuming lower precision works equally well for training and inference

    Why it fails

    The source says inference is typically done with as few bits as possible, while training is more sensitive to numerical precision.

    Do this instead

    Use aggressive low-bit storage naturally for inference, and use mixed precision for training.

Key Takeaways

  • Memory is a bottleneck for foundation models during both inference and finetuning, and finetuning typically needs much more memory than inference.
  • During finetuning, a model's memory footprint is driven by its number of parameters, its number of trainable parameters, and its numerical representations.
  • PEFT reduces the memory needed for finetuning by reducing the number of trainable parameters, which is the motivation behind it.
  • Quantization reduces memory by converting a model to fewer bits per value, as shown by the move from 52 GB to 26 GB for a 13 billion parameter model.
  • Inference typically uses as few bits as possible, while training is more sensitive to precision and is typically done in mixed precision.

Related concepts

More concepts starting with F