Memory & Efficiency
Quantization Math & QAT
AdvancedQuantization & Precision covers the formats and when to use them. This page is about the arithmetic underneath. You'll see how a real number becomes a small integer and comes back, where accuracy gets lost along the way, and how quantization-aware training teaches a model to live with rounding.
Fewer bits save energy as well as memory
A smaller model needs fewer chips. The bigger win is energy. An 8-bit integer multiply uses about 18 times less energy than a 32-bit float multiply, and fetching data from DRAM costs hundreds of times more than either. With fewer bits per value you move fewer bytes and do cheaper math on each one. New to the formats themselves? Start with Quantization & Precision.
| Operation (45 nm) | Energy |
|---|---|
| 8-bit integer add | 0.03 pJ |
| 8-bit integer multiply | 0.2 pJ |
| 32-bit integer multiply | 3.1 pJ |
| FP16 multiply | 1.1 pJ |
| FP32 multiply | 3.7 pJ |
| 64-bit read from a 1 MB cache | 100 pJ |
| 64-bit read from DRAM | 1.3-2.6 nJ |
Rough energy per operation at 45 nm and 0.9 V (Horowitz, ISSCC 2014). Today's chips spend less on everything, but the ratios still hold: memory costs far more than math, and small integers far less than floats.
Scale and zero point
To store a real number as an integer you need two values. The scale S is what one integer step is worth. The zero point Z is the integer that stands for 0.0. Inside the range, rounding is never off by more than half a step. Values outside it get clipped to the edge, and that error can be far bigger.
Scale and zero point: real values onto integers
Pick a range to cover and a bit width. S is one integer step in real units, and Z is the integer for real 0. Drag r along the axis or use the slider, and try it outside the range too. Values are illustrative.
Bits
Scheme
scale: S = (r_max - r_min) / (q_max - q_min) = (1.20 - 0.00) / (15 - 0) = 0.0800
zero point: Z = round(q_min - r_min / S) = round(0 - 0.00 / 0.0800) = 0
encode: q = clamp(round(r / S) + Z, 0, 15) = clamp(round(0.53 / 0.0800) + 0) = clamp(7) = 7
decode: r̂ = S(q - Z) = 0.0800 x (7 - 0) = 0.5600
error: r - r̂ = 0.53 - 0.5600 = -0.0300 rounding error (at most S/2 = 0.0400)
Scale S
0.0800
real units per step
Zero point Z
0
integer for real 0
|Error|
0.0300
rounding, at most S/2
Levels in use
16 / 16
inside the data range
Asymmetric shifts the grid with Z so its 16 levels run from r_min to r_max, a good fit for one-sided values like ReLU outputs. Real 0 still lands exactly on an integer (Z), so zero padding stays exact. Past the edges of the grid (0.00 to 1.20) a value clamps to the end level, and the error grows the farther out it sits.
Doing the math in integers
Smaller storage is half the benefit. The other half comes when the multiplications themselves run on integers. If one scale covers a whole tensor, you can pull it out of the dot product. What's left is a loop of INT8 multiplies summed into a 32-bit register, then one rescale at the end, which hardware does with an integer multiply and a bit shift. Whether a scale can be pulled out like this is what separates fast quantization schemes from slow ones.
From float dot product to an all-integer layer
One output of a linear layer, step by step. The weights and activations are illustrative. The recipe follows the integer-only scheme of Jacob et al. (2018).
| i | 1 | 2 | 3 | 4 |
|---|---|---|---|---|
| w (real) | 0.42 | -0.81 | 0.17 | 0.63 |
| x (real) | 1.30 | 0.25 | 2.10 | 0.70 |
| w x | 0.546 | -0.203 | 0.357 | 0.441 |
y = Σ wi xi = 1.1415
Four floating-point multiplies and adds. A real layer does thousands of these per output, and a float multiplier costs more silicon and energy than an integer one.
How many scales: per tensor, channel or group
A scale has to stretch to the largest value it covers, so a single outlier coarsens the grid for everything that shares it. Give each output channel, or each small group of weights, its own scale and the damage stays local. You pay in storage, a fraction of a bit per weight, which is what block formats like budget for. How much it matters depends on the network: on MobileNet it was the difference between 0.1% and 70.4% accuracy, on ResNet-50 almost nothing.
Which weights share a scale?
A small weight matrix (illustrative values): 4 output channels (rows) by 8 inputs. Row 2 is an outlier row and row 3 hides one large weight. Each scale stretches to the largest value it covers, so watch which small weights collapse to code 0.
Granularity
Bits (symmetric INT)
Cells show
row 1
row 2outlier row
row 3one outlier weight
row 4
Scales stored
1
for 32 weights
Collapsed to 0
21 / 32
nonzero weights lost
RMS error
0.113
all weights
Small-weight error
97%
relative, |w| < 0.3
Per-tensor: the 2.8 in row 2 sets one scale for the whole matrix, including the other three rows. At INT4 most ordinary weights are smaller than half a step and round to 0.
What finer scales cost: effective bits per weight
Each group stores its own scale, and the group's elements split those bits: effective bits = element bits + scale bits / group size.
Scale bits
Group size
4 + 16 / 128 = 4 + 0.125 = 4.125 bits
Effective bits
4.125
per weight
Scale overhead
+0.125
16 bits / 128
Extra memory
+3.1%
vs elements alone
70B weights
36.1 GB
in this format
INT4, FP16 scale per 128: a common LLM weight-only recipe. A 16-bit scale per 128 weights adds only 1/8 bit. Per 4 weights it would add 4 bits and double the size of a 4-bit model. MXFP4 and NVFP4 use small blocks, so they shrink the scale to 8 bits to keep the cost down. NVFP4's extra FP32 per-tensor scale is spread over millions of weights and rounds to zero bits.
Measured: one scale per tensor can wreck a model
ImageNet top-1 accuracy after quantizing only the weights to 8 bits, with no retraining (Krishnamoorthi 2018, Table 2). MobileNets use depthwise convolutions whose channels have very different ranges.
MobileNet-v1 1.0 224
MobileNet-v2 1.0 224
ResNet-v1 50
Per-tensor 8-bit drops MobileNet-v1 from 70.9% to 0.1%, which is chance level for 1,000 classes. Per-channel scales bring it back to 70.4%. The larger ResNet-50 barely notices.
Choosing the range: clip or round
It's tempting to set the range wide enough to cover every weight. Usually that's a mistake. Clip the range and a handful of rare values take a big hit, while every ordinary value gets a finer grid. In the demo below the best clipping point sits far under the largest weight. Activations change with every input, so their ranges come from calibration: you run sample data through the model and pick each range with a rule like this one. That step is most of what is.
Clip or round? Finding the best range
About 1 million weights from a heavy-tailed distribution (illustrative): 98% sit within ±0.1, and 6 outliers reach 3.5. As you lower the clip threshold c, the step S = c / 7 shrinks and rounding error falls. Everything beyond c gets clipped.
Bits
Step S
0.500
c / 7
Rounded to 0
100.0%
of all weights
Clipped
0
of ~1 million weights
Total error
23.5x
vs the minimum
At INT4 the total error is lowest at c ≈ 0.120, which is 29x below the max. There it's 23.5x lower than clipping at the max. At the minimum you clip 0.8% of weights, the rare large ones, and everything else gets much finer steps.
Calibration: picking ranges for activations
Weights are known ahead of time. Activations change with every input, so post-training quantization runs a small calibration set through the model, records each activation tensor and picks its range with one of these rules.
Running max / moving average
Track the min and max of each activation tensor over calibration batches (or a moving average of them) and use that as the range.
Simple and cheap. One rare spike stretches the range for everything, which puts you at the right end of the chart above.
Jacob et al. 2018; Krishnamoorthi 2018
Minimum MSE
Pick the clip value that minimizes the mean squared error between original and quantized values, found by search or worked out analytically for a known distribution shape.
Minimizes the curve above directly. A few outliers lose accuracy so everything else gets much finer steps.
Banner et al. 2019 (ACIQ)
Percentile
Clip at a high percentile of the absolute values seen in calibration, such as 99.99%, so the largest 0.01% get clipped.
Easy to reason about. The best percentile varies by network, so you often need a small sweep.
Wu et al. 2020
Entropy (KL divergence)
Histogram each activation, then choose the threshold whose quantized histogram has the smallest KL divergence from the original, meaning the least information is lost.
The default calibration method in TensorRT's INT8 workflow. Like MSE and percentile, it clips outliers to get finer steps for the rest.
Migacz, GTC 2017; Wu et al. 2020
Rounding to nearest isn't optimal
Rounding every weight to its nearest grid point sounds like the best you can do. But a layer adds up many rounded weights, so what counts is the total error. If most errors lean the same way they pile up; round a few of them the other way and they cancel. On a 4-bit ResNet-18, choosing up or down per weight took accuracy from 24% to 68.7%, with no retraining.
Rounding that cancels out
Four weights, all multiplied by the same input (x = 1), on a coarse grid with steps of 0.25. Round-to-nearest gives each weight its smallest error, but the output only sees the sum. Tap a weight to round it the other way. Illustrative values.
Output y = Σ w·x
exact 1.47 · rounded 1.25
Output error -0.22
Sum of error²
0.053
what nearest minimizes
|Output error|
0.22
what the output sees
Nearest leaves the output off by -0.22. Round one of the 0.36 weights up instead and you get +0.03, even though that weight is now further off.
The same effect in a real network
ResNet-18 on ImageNet with every weight rounded to 4 bits after training (activations kept in full precision), top-1 accuracy. AdaRound (Nagel et al., 2020) learns each weight's up/down choice one layer at a time from 1,024 unlabeled images. That takes about 10 minutes on one GPU.
Rounding only the first layer to 4 bits shows the same pattern: nearest 52.29%, all up or all down 0.10%, and 48 of 100 random up/down choices beat nearest (the best reached 63.06%).
Inside quantization-aware training
Everything so far leaves the weights alone. keeps training the model with rounding switched on, so it learns to work around it. Rounding has no useful gradient, so the backward pass skips over it and updates a full-precision copy of each weight instead. Once that copy drifts past the halfway mark between two grid levels, the stored weight jumps to the next one.
Inside QAT: one weight, step by step
Follow a single weight through fake quantization, the straight-through estimator and a few optimizer updates. Illustrative grid, weight and gradient.
Master
0.40
FP32, updated
Stored
0.50
3-bit, used
Updates
0
none yet
The grid
Forward vs backward
Two copies of every weight
Training keeps a full-precision master weight (0.40). The model only ever uses the value it maps to on the low-bit grid. Here that's a 3-bit grid with steps of 0.25 from -1.00 to 0.75, and 0.40 is closest to 0.50.
Learned clip ranges and step sizes, measured
ResNet-18 on ImageNet, weights and activations both quantized, top-1 accuracy. Full-precision baselines: 70.2% (PACT paper) and 70.5% (LSQ paper). LSQ keeps the first and last layers at 8 bits.
| Bits (W/A) | PACT (2018) | LSQ (2020) |
|---|---|---|
| 2-bit | 64.4% | 67.6% |
| 3-bit | 68.1% | 70.2% |
| 4-bit | 69.2% | 71.1% |
With a learned step size, 3-bit ResNet-18 lands within 0.3 points of full precision and 4-bit slightly above it.
The same loop in today's LLMs
- Gemma 3: QAT checkpoints (int4) made with about 5,000 extra training steps.
- Kimi K2 Thinking: QAT to INT4 on its MoE weights, about 2x faster generation.
- Nemotron 3 Nano at NVFP4: AIME25 89.1 (BF16), 85.0 (PTQ), 83.3 (QAT), 87.9 (QAD). For this reasoning model, training the low-bit model to match the BF16 model's outputs (QAD) recovered more than plain QAT.
QAT is usually a short extra phase. Under 1% of the original pretraining time is often enough. More recipes on Quantization & Precision.
How low can the bits go
With training, weights hold up at surprisingly few bits: one bit plus a scale, or three levels (-1, 0 and +1). Binarize the activations too and accuracy starts to drop for real. Ternary weights have since come back in language models such as BitNet b1.58.
How low can weights go?
Eight weights from one filter, pushed down to one or two bits. Illustrative weights; accuracy figures are from the papers.
Mean sq. error
0.5995
vs the original 8
Levels used
2
every weight becomes ±1
BinaryConnect keeps only the sign: w → +1 or -1. Small and large weights end up the same size, so the error is huge. With training it still works, because the network learns around it.
Training works like QAT. The forward and backward passes use the binary weights, and the updates go to real-valued copies (clipped to [-1, 1]). According to the authors, SGD needs good-precision weights during the updates to work at all.
BinaryConnect (2015)
Small CNN on CIFAR-10, test accuracy (100 - test error). Binary weights in the forward and backward pass, real-valued weights kept for the updates. On ImageNet AlexNet, BinaryConnect reached 35.4% top-1 in the XNOR-Net paper's comparison.
So how low?
With a full-precision master copy during training, weights hold up at one or two bits. Binary weights with a scale matched full-precision AlexNet, and ternary weights came within about 3 points on ResNet-18. Binarizing the activations as well cost about 12 points of top-1 accuracy.
Ternary LLMs
BitNet b1.58 (2024) trains LLMs from scratch with ternary {-1, 0, +1} weights (scaled by mean |w|) and 8-bit activations. At 3B parameters it matched a same-size LLaMA baseline's perplexity (9.91 vs 10.04) with 3.55x less memory and 2.71x lower latency in benchmarks.
Uneven grids: codebooks and NF4
Let the weights choose their levels
Weights bunch up near zero, so evenly spaced levels waste the outer ones. Deep Compression clusters each layer's weights with k-means instead and stores a small codebook of shared values, plus a short index per weight (log2(k) bits for k clusters). Pruned AlexNet kept full accuracy with 8-bit codebooks in the convolution layers and 5-bit codebooks in the fully connected ones. With pruning and Huffman coding on top, AlexNet went from 240 MB to 6.9 MB. The downside is that a lookup returns a float: you save storage and bandwidth, but the math still runs in floating point.
NF4: a fixed codebook for normal weights
QLoRA's uses the same idea with a single codebook shared by every model. Pretrained weights roughly follow a normal distribution centered on zero, so NF4 puts its 16 levels at quantiles of a normal distribution, scaled to [-1, 1]. Each level then covers about the same number of weights. Every block of 64 weights gets its own absmax scale.
Why LLMs are harder: activation outliers
Large language models bring a new problem. In every token, a few activation channels are about 100 times larger than the rest. One shared scale spends the whole grid on them, and clipping them breaks the model. A separate scale per input channel would work, but as the integer matmul section showed, that scale can't be pulled out of the sum. So the fixes either give the outlier channels extra precision or reshape the numbers before rounding.
Outlier channels in LLM activations
8 tokens × 16 channels of activations entering a linear layer. Two channels run about 100x larger than the rest, in every token. Try the naive schemes, then the fixes. Illustrative matrix; errors computed live.
What the quantizer sees
largest channel vs median: 108x
After the round trip (INT8)
color clamps at ±2; outlier cells saturate
Normal channels
48%
relative error, 14 channels
Outlier channels
0.3%
relative error, 2 channels
One scale for the whole matrix: the step is 0.62, set by the outliers. Most normal values round to 0 or one step either side.
What the papers measured
- In OPT-13B, a few activation channels exceed 70 while the weights stay flat. Outliers run about 100x larger than most activations and stay in the same channels for every token.
- With one per-tensor INT8 scale, a normal channel is left with only 2-3 effective levels.
- From about 6.7B parameters, outliers appear in every layer: about 150,000 per sequence in a 6.7B model, concentrated in just 6 feature dimensions.
OPT-175B in INT8
Average accuracy, four zero-shot tasks
| FP16 | 71.6% |
| INT8, one scale per tensor | 32.3% |
| INT8, scale per token | 31.7% |
| INT8, scale per channelaccurate, but not usable in INT8 matmul kernels | 71.4% |
WikiText perplexity (lower is better)
| FP16 | 10.99 |
| Naive W8A8 | 93,080 |
| LLM.int8() | 11.10 |
| SmoothQuant W8A8 | 11.17 |
GPTQ, AWQ, SmoothQuant, QAT and QAD, and the models that ship with them, are mapped on Quantization & Precision.
Sources
- Horowitz, Computing's energy problem (ISSCC 2014) ↗
- Jacob et al., Quantization and training for integer-arithmetic-only inference (2018) ↗
- Krishnamoorthi, Quantizing deep convolutional networks for efficient inference (2018) ↗
- Wu et al., Integer quantization for deep learning inference (2020) ↗
- Banner et al., Post-training 4-bit quantization (ACIQ, 2019) ↗
- OCP Microscaling (MX) formats specification v1.0 ↗
- NVIDIA, Introducing NVFP4 ↗
- Nagel et al., Up or down? Adaptive rounding (AdaRound, 2020) ↗
- Bengio et al., Estimating or propagating gradients through stochastic neurons (2013) ↗
- Choi et al., PACT (2018) ↗
- Esser et al., Learned step size quantization (LSQ, 2020) ↗
- Courbariaux et al., BinaryConnect (2015) ↗
- Rastegari et al., XNOR-Net (2016) ↗
- Li et al., Ternary Weight Networks (2016) ↗
- Zhu et al., Trained Ternary Quantization (2017) ↗
- Ma et al., The era of 1-bit LLMs (BitNet b1.58, 2024) ↗
- Han et al., Deep Compression (2016) ↗
- Dettmers et al., QLoRA (2023) ↗
- Dettmers et al., LLM.int8() (2022) ↗
- Xiao et al., SmoothQuant (2022) ↗
Demo values are illustrative and labeled. Accuracy figures come from the papers above.