Concepts / Numerical Representations of Model Values

Numerical Representations of Model Values

PTQ quantizes a fully trained model. It is the most common approach and the one most relevant to developers who do not train models.

  • Programming

A Model Is Mostly Numbers

A trained model is, in large part, a collection of numerical values. Each of those values has to be stored somewhere in memory, and the amount of memory those stored values occupy is called the model's memory footprint. Large models can have billions of parameters, so the way each value is stored has a large effect on how much memory the model needs.

The central relationship in this article is simple: the fewer bits needed to represent a model's values, the lower the model's memory footprint will be. The techniques covered here all act on that relationship. Before looking at them, it helps to test your intuition about how much the bit width matters.

What do you think happens?

A model has 10 billion parameters. Stored in a 32-bit format, its weights need 40 GB. If the same model is stored in a 16-bit format, how much memory would its weights need?

Reveal answer
Answer:

20 GB

Halving the bits per value from 32 to 16 halves the storage for the weights, from 40 GB down to 20 GB, because the number of parameters stays the same and each one takes half as many bits.

Where the Memory Goes

Storage cost is the number of values multiplied by the number of bits used for each value. Ten billion parameters at 32 bits each is 320 billion bits. Since 8 bits make one byte, that is 40 billion bytes, which is the 40 GB the source gives for the weights. The same ten billion parameters at 16 bits each is 160 billion bits, or 20 billion bytes, which is 20 GB.

Formula, step by step
  1. 1
    Start with 10 billion parameters, written as 10 times 10 to the power 9.
  2. 2
    Multiply by 32 bits per parameter to get the total number of bits, 320 billion.
  3. 3
    Divide by 8 bits per byte to convert to bytes, giving 40 billion bytes, or 40 GB.
  4. 4
    Repeat with 16 bits per parameter: the total bits are halved, so the weights need 20 GB.
32-bit weights40 GB16-bit weights20 GBhalf the bits per value
What changes in the weight storage when each parameter drops from 32 bits to 16 bits for a 10-billion-parameter model?
Worked example

Estimating weight memory for a different model size

A model has 7 billion parameters and its weights are stored in a 16-bit format. How much memory do the weights need?

  1. 1
    Count the parameters

    There are 7 billion values, written as 7 times 10 to the power 9.

  2. 2
    Multiply by bits per value

    At 16 bits each, the total is 7 times 10 to the power 9 times 16, which is 112 billion bits.

  3. 3
    Convert bits to bytes

    Divide by 8 to get 14 billion bytes.

  4. 4
    Express in GB

    That is 14 GB for the weights.

Result

The weights need about 14 GB at 16 bits. The same model at 32 bits would need 28 GB, so the reduction follows the same halving pattern as the 10-billion-parameter example.

Two Meanings of Quantization

A common assumption is that quantization always means converting a model's values into integers. Under that assumption, a technique that stores values in a floating-point format with fewer bits would not count as quantization at all. The belief sounds reasonable, because quantization is often associated with mapping values onto a discrete set of integer levels.

The belief is partly right. Strictly, quantization refers to converting values into an integer target format. In practice, however, the word is used more broadly, to cover all techniques that convert values to a lower-precision format. The source adopts this broader usage so that its terminology matches the wider literature, so when you read other material on this topic, expect the broad meaning unless the text specifically says integer.

Strict meaningEveryday meaningInteger target formatonly this countsAny lower-precisionformatbroad usagefloat32 to float16counts in everyday usageconverts tocoversexample
How does quantization in the strict sense differ from the looser usage that includes any reduction in precision, such as float32 to float16?
UsageWhat counts as quantizationExample
StrictConverting values to an integer target formatfloat32 to int8
Everyday (broad)Any technique that lowers the precision of the formatfloat32 to float16
Low precision generallyAny format that uses fewer bits than the standard FP32 formatfloat16

Two levels of meaning for the same word

Mapping Floats onto Integer Levels

To see what the strict meaning looks like in practice, consider mapping a continuous floating-point value onto a fixed set of integer levels. The source describes quantization in these terms: values are mapped onto a discrete set of integer levels. The example below uses a simple illustrative scheme, not one the source specifies. Values between -1 and 1 are scaled by 127 and rounded to the nearest integer, so each level is a whole number from -127 to 127.

Code walkthrough
values = [0.731, 0.734, -0.402]
levels = [round(v * 127) for v in values]
print(levels)
  1. 1
    The list holds three floating-point values. The first two differ slightly, at 0.731 and 0.734.
  2. 2
    Each value is multiplied by 127 and rounded. 0.731 times 127 is about 92.8, which rounds to 93.
  3. 3
    0.734 times 127 is about 93.2, which also rounds to 93. Two different inputs now share one level.
  4. 4
    -0.402 times 127 is about -51.1, which rounds to -51.
  • Each float is multiplied by 127 to stretch the range -1 to 1 onto integer scale.
  • round() snaps each scaled value to the nearest integer level.
Output
[93, 93, -51]
0.731float input93integer level0.734float input-0.402float input-51integer levelrounds torounds torounds to
How does a range of float values get mapped onto a fixed set of integer levels, and what is lost at each step?
Edge case

Two distinct inputs, 0.731 and 0.734, collapse to the same level 93. The stored value no longer records the difference between them. This loss of detail is the basic trade-off of mapping continuous values onto a finite set of levels: fewer bits mean fewer distinguishable levels, so each stored value is an approximation of the original.

Choosing What to Quantize

Quantization is applied to specific parts of a model, so the next question is which parts to target. The source identifies the major contributors to a model's memory footprint during inference as the model's weights and its activations. The ideal target is whatever consumes the most memory, but the choice also depends on what can be quantized without hurting performance too much.

Memory footprintduring inferenceWeightsfixed after trainingUsual first targetmore common, more stableActivationschange with each inputQuantized less oftennot ruled outincludesincludesleads toleads to
Which components of a model get quantized, and why are static weights easier to quantize than activations that change with each input?

Weight quantization is more common than activation quantization. The source gives the reason: weight quantization tends to have a more stable impact on performance, with less accuracy loss. Activation quantization is not ruled out, but weights are the usual first target when the goal is to reduce memory. Intuitively, a weight is a fixed value stored with the model, so one quantization decision applies to every input. An activation is computed fresh for each input, so its range and behavior can vary from one input to the next.

When Quantization Happens and PTQ

The other decision is timing. The source states that quantization can happen during training or after training. Posttraining quantization, abbreviated PTQ, means quantizing a model after it has been fully trained. The model's training is already complete, and quantization is applied to its finished values.

PTQ is by far the most common approach. It is also the approach most relevant to AI application developers, who usually do not train models themselves. A developer who takes a trained model and reduces its precision for deployment is working with PTQ, so the technique fits the situation of someone who consumes models rather than builds them from scratch.

Key point

PTQ quantizes a fully trained model. It is the most common approach and the one most relevant to developers who do not train models.

Mistakes Beginners Make

  • Common mistake

    Assuming quantization always means converting to integers

    What it looks like

    Dismissing a float32-to-float16 reduction as not quantization because the target is still floating point.

    Why it fails

    The strict definition requires an integer target, but in practice the term covers all techniques that reduce precision.

    Do this instead

    Check whether the source means the strict integer case or the broad usage before drawing conclusions.

  • Common mistake

    Thinking fewer bits always mean the same accuracy

    What it looks like

    Expecting a 16-bit model to behave identically to its 32-bit version.

    Why it fails

    Fewer bits mean fewer distinguishable levels, so stored values are approximations. Two different inputs can end up at the same level.

    Do this instead

    Treat the memory saving as the main benefit and remember that some detail is lost in each mapped value.

  • Common mistake

    Quantizing activations first because they use more memory

    What it looks like

    Targeting the per-input activations before the weights when reducing memory.

    Why it fails

    Weights are the usual first target because weight quantization is more common and has a more stable effect on performance with less accuracy loss.

    Do this instead

    Start with weights, and treat activation quantization as a separate, less common option.

  • Common mistake

    Confusing PTQ with training-time decisions

    What it looks like

    Assuming a PTQ step must be built into the training loop.

    Why it fails

    PTQ quantizes a model after it has been fully trained, which is why it suits developers who do not train models.

    Do this instead

    Associate PTQ with the post-training stage of the workflow.

Practice Estimating Memory

EASY

A model has 7 billion parameters. Estimate the weight memory at 16 bits per value, then at 8 bits per value. Explain how the result changes when the bit width is halved.

Hints
  • Multiply the number of parameters by the bits per value, then divide by 8 to get bytes.
  • At 16 bits the weights need 14 GB, as in the worked example earlier in this article.
  • Halving the bits per value should halve the storage, so check whether your 8-bit result is half of the 16-bit result.

Key Takeaways

  • Reducing the number of bits per value lowers a model's memory footprint, so a 10-billion-parameter model needs 40 GB for its weights at 32 bits and 20 GB at 16 bits.
  • Strictly, quantization means converting values to an integer format, but in practice the term covers any technique that reduces precision.
  • Weights are usually the first target for quantization because weight quantization is more common and tends to have a more stable effect on performance than activation quantization.
  • Posttraining quantization (PTQ) quantizes a fully trained model, is the most common approach, and is most relevant to application developers who do not train models.

More concepts starting with N