TL;DR
Quantization-aware training (QAT) trains a network while it is already being squeezed onto a small integer grid. The forward pass uses the rounded weights, so the loss sees the real rounding error. The updates go to a full-precision copy of the weights, because a small step would vanish if it were applied to a value that rounds back to the same grid point. The Straight-Through Estimator carries the gradient through the rounding step. The result is a model that keeps its accuracy at low bit widths, where simply rounding a trained model would lose too much.
Builds on: Quantization, Straight-Through Estimator · Leads to: BitLinear, BitNet
Where it's used
Phone and edge deployment. A vision or speech model that must run on a phone or a microcontroller is often trained with integer kernels in mind. The training framework inserts fake quantization nodes into the network, and the trained weights are then exported as 8-bit integers. The toolkits in PyTorch and TensorFlow both provide this workflow.
Very low bit widths. At 4 bits or fewer, simple rounding of a trained model often loses too much accuracy, as the Post-Training Quantization lesson explains. QAT is the usual remedy, because the weights can move to positions where their rounded values still give good answers.
Models trained at low precision from the start. Some language models are trained with weights restricted to three values, such as , and , rather than rounded afterwards. The BitNet line of work does this, and it is the extreme end of the same idea: the forward pass always uses the low-precision weights during training.
How it works
Use the grid from the Quantization lesson: the scale is , so the representable weights are , , and . Write for rounding a weight onto this grid, so inside the range. The network has one weight, the input is , the target is , and the loss is with . The learning rate is .
-
Start with a full-precision weight. Let the stored float weight be . This is the master copy. Check: the master weight is not on the grid, since is not one of , , , .
-
Quantize for the forward pass. . The network computes and the loss is . Check: the loss the network sees is the loss of the rounded weight, not of the float weight.
-
Get the gradient through the rounding. The loss gradient is , and , so . The rounding function has zero slope almost everywhere, so the true is zero. The straight-through rule sets it to 1 inside the range. Then . Check: without this step the update would be zero and the weight would never move.
-
Update the master weight, not the rounded one. . The master weight has moved by a small amount that a grid of step 0.5 cannot show.
-
Repeat with the new master weight. At , , so the forward pass is unchanged, the loss is still , and the gradient is still . The master weight moves to .
-
Watch the grid cross a boundary. At , . The rounded weight jumps from to , the output becomes , the loss becomes , and the gradient is zero. The network has learned to reach the target on the grid.
The contrast shows why the master copy matters. Suppose the update were applied to the stored rounded value, and that value were re-rounded to the grid after every update. Starting from , the step gives , and . The weight is back where it started after every step, so it never reaches the boundary, and the loss stays at .
flowchart LR M[Float master weight w] --> Q[Round to grid: Q of w] Q --> F[Forward pass and loss] F --> G[Gradient with straight-through rule] G --> U[Update w, not Q of w] U --> M
The math
For a weight , grid step , and integer range , fake quantization is
The forward pass uses wherever the deployed model would use the integer weight, and the value stays a float, so the same code runs in training.
Step 2 in symbols: the forward pass
The network computes its output with the quantized weight . In the walkthrough, , , and . Activations can be quantized the same way, with their own scale.
Step 3 in symbols: the backward pass
The chain rule gives . The true value of is zero where is flat and undefined at its jumps. The straight-through rule replaces it by
The second case is the clipped version of the rule: a weight outside the representable range gets no gradient, because moving it a little would not change its quantized value. In the walkthrough the weight lies inside , so its derivative is 1, as in step 3.
Step 4 in symbols: the update
The update is applied to the master weight:
With and , the step is . Each step changes by a continuous amount, and changes only when crosses a rounding boundary, which is at and for this grid.
Steps 5 and 6 in symbols: why the master copy works
Let be the size of a single update. The rounded weight changes only if the master moves across a boundary, so a series of small updates that stays on one side of every boundary changes nothing in the forward pass. The master copy accumulates those small updates, and sooner or later it crosses a boundary and the rounded weight moves by one full step. If the update were applied to a stored value that is re-rounded after each step, any smaller than half a step would be erased by rounding, which is the stuck case in the walkthrough.
Worked examples
- Take the master weight on the grid with step .
- Divide by the step: .
- Round: .
- Multiply back by the step: .
- Check the error: , which is below half a step, .
- Interpretation: the forward pass uses , and the rounding error of is part of the loss the network trains against.
- Start with and input , target . The rounded weight is , the output is , and the loss is .
- The gradient through the straight-through rule is .
- Update the master: .
- Round the new master: , which rounds to , so the rounded weight is still . The loss is still .
- Gradient again , update: .
- Round the new master: , which rounds to , so the rounded weight is . The output is , the loss is .
- Interpretation: the master weight moved in small steps, and the rounded weight changed once, at the boundary. The stuck case from the walkthrough would have stayed at forever.
- Take a language model with 7 billion weights. In 16-bit floats each weight takes 2 bytes, so the model takes bytes, which is 14 GB.
- At 8 bits each weight takes 1 byte, so the model takes 7 GB.
- At 4 bits each weight takes half a byte, so the model takes GB.
- Going from 8 bits to 4 bits halves the memory again, but the grid has 16 levels instead of 256, so the rounding error on each weight is much larger.
- A model that was trained in floats and then rounded to 4 bits can lose a large share of its accuracy. Training with the 4-bit forward pass, as QAT does, lets the weights adapt to the grid, at the cost of a training or fine-tuning run that uses the full training pipeline.
- Interpretation: QAT is a trade of training compute for accuracy at a fixed memory budget.
Try it: a 13 billion weight model stored at 4 bits. How many gigabytes does it take? Answer: bytes is bytes, which is 6.5 GB.
Common mistakes and tradeoffs
It is tempting to think QAT only rounds the weights during training. In fact the rounding is only in the forward pass, and the gradient update goes to a float master copy. The worked example shows that updating the rounded value directly freezes the weight, because each small step is erased by rounding.
It is tempting to think QAT is always better than post-training quantization. In fact at 8 bits the two are often close, and QAT needs training data, a training run, and the engineering to keep the simulated arithmetic matching the integer kernel. At 4 bits or fewer, QAT is more often worth that cost.
It is tempting to think the straight-through rule gives the true gradient. In fact it is a biased estimate. The network is optimizing a surrogate, and the surrogate can settle at a point where the rounded weights are poor, even though the float weights look fine. Setting the gradient to zero for weights outside the range helps, but it does not remove the bias.
It is tempting to think that a QAT model is an integer model automatically. In fact the training simulates the integer arithmetic with floats. The exported model still needs integer kernels, and its results match the simulation only if the rounding and the accumulation are done the same way in both.
| Approach | What it changes | When to choose it | Cost |
|---|---|---|---|
| Post-Training Quantization | Rounds a trained model using a small calibration sample | 8-bit deployment with little accuracy loss | Minutes, no training |
| Quantization-aware training | Trains with rounded weights in the forward pass | 4 bits or fewer, or a sensitive model | A full training or fine-tuning run |
| Mixed-Precision Training | Uses 16-bit arithmetic with a 32-bit master copy | Faster training, not a smaller deployed model | Training-time speedup and memory saving |
History
Quantization was used for signal processing long before neural networks. In the late 2010s, deep learning practitioners needed networks that could run on phones with integer arithmetic. Jacob and colleagues showed in 2017 that training with simulated quantization in the forward pass let a network keep its accuracy when run with integer-only arithmetic, and that became the standard recipe for quantization-aware training.
The gradient rule that makes it trainable was older. Bengio and colleagues described the straight-through estimator in 2013, when they needed gradients through a stochastic, discrete step. Hubara and colleagues applied the same idea to binary networks in 2016, and the rule was then used for rounding during quantization.
Quantization-aware training then spread to large language models, where memory is the main limit. The surveys by Gholami and colleagues in 2021 set out the trade-offs between post-training and training-time quantization, and the low-bit model work of the mid-2020s pushed the idea further, to models trained with three-valued weights from the start.
Recap
- Quantization maps float values onto a small integer grid, and the grid step sets the rounding error.
- Post-training rounding of a trained model is cheap, but it loses accuracy at low bit widths.
- Quantization-aware training uses the rounded weights in the forward pass, so the loss sees the real error.
- The Straight-Through Estimator gives the gradient through rounding, and the updates go to a float master copy.
- Without the master copy, small updates are erased by rounding and the weights freeze.
- The cost is a training run, and the payoff is a model that stays accurate at 4 bits or fewer.
Read next: BitLinear for a layer built around this idea, and BitNet for a model trained with three-valued weights from the start.
Check yourself
QuestionWhy does quantization-aware training keep a full-precision master copy of each weight?Show answer
Because the updates are small. If an update were applied to the rounded value, rounding would erase any change smaller than half a grid step, and the weight would never move. The master copy keeps the small changes until the weight crosses a rounding boundary.
QuestionA weight is stored as the master value 0.27 on a grid with step 0.5. What rounded value does the forward pass use?Show answer
0.27 divided by 0.5 is 0.54, which rounds to 1. So the forward pass uses 0.5 times 1, which is 0.5.
QuestionWhy does the straight-through rule set the derivative of rounding to 1 instead of using the true derivative?Show answer
The true derivative of rounding is zero almost everywhere, so backpropagation would give zero updates and nothing would learn. The straight-through rule substitutes 1 inside the representable range, which gives a useful, though biased, gradient that moves the master weight.
QuestionA team has a trained model that loses little accuracy at 8 bits but a lot at 4 bits. Which technique should they try first for 4 bits, and what is its main cost?Show answer
They should try quantization-aware training, since it lets the weights adapt to the 4-bit grid during training. Its main cost is a training or fine-tuning run with the full pipeline, which post-training rounding does not need.
Sources
- Jacob et al. (2017), Quantization and Training of Neural Networks for Efficient Integer-Arithmetic-Only Inference: https://arxiv.org/abs/1712.05877
- Bengio, Léonard, Courville (2013), Estimating or Propagating Gradients Through Stochastic Neurons for Conditional Computation: https://arxiv.org/abs/1308.3432
- Gholami et al. (2021), A Survey of Quantization Methods for Efficient Neural Network Inference: https://arxiv.org/abs/2103.13630