Search

Search papers and lessons

Quantization-Aware Training

Level 3 · 12 min read

Quantization-aware training runs the forward pass with rounded weights while keeping full-precision weights for the updates, so the network learns to work well after rounding.

On this page

  • Colour = topic mix
  • Discrete math
  • Calculus
  • Linear algebra
  • Neural network basics
  • Efficiency
  • Probability
  • ReadReady to readNot yet

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 −1-1, 00 and +1+1, 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 s=0.5s = 0.5, so the representable weights are −1.0-1.0, −0.5-0.5, 00 and 0.50.5. Write Q(w)Q(w) for rounding a weight ww onto this grid, so Q(w)=0.5⋅round⁡(w/0.5)Q(w) = 0.5 \cdot \operatorname{round}(w / 0.5) inside the range. The network has one weight, the input is x=1x = 1, the target is t=0t = 0, and the loss is L=(y−t)2L = (y - t)^2 with y=Q(w)⋅xy = Q(w) \cdot x. The learning rate is 0.10.1.

  1. Start with a full-precision weight. Let the stored float weight be w=0.37w = 0.37. This is the master copy. Check: the master weight is not on the grid, since 0.370.37 is not one of −1.0-1.0, −0.5-0.5, 00, 0.50.5.

  2. Quantize for the forward pass. Q(0.37)=0.5⋅round⁡(0.74)=0.5⋅1=0.5Q(0.37) = 0.5 \cdot \operatorname{round}(0.74) = 0.5 \cdot 1 = 0.5. The network computes y=0.5⋅1=0.5y = 0.5 \cdot 1 = 0.5 and the loss is L=0.52=0.25L = 0.5^2 = 0.25. Check: the loss the network sees is the loss of the rounded weight, not of the float weight.

  3. Get the gradient through the rounding. The loss gradient is ∂L/∂y=2(y−t)=1.0\partial L / \partial y = 2(y - t) = 1.0, and ∂y/∂Q=x=1\partial y / \partial Q = x = 1, so ∂L/∂Q=1.0\partial L / \partial Q = 1.0. The rounding function has zero slope almost everywhere, so the true ∂Q/∂w\partial Q / \partial w is zero. The straight-through rule sets it to 1 inside the range. Then ∂L/∂w≈1.0\partial L / \partial w \approx 1.0. Check: without this step the update would be zero and the weight would never move.

  4. Update the master weight, not the rounded one. w←0.37−0.1⋅1.0=0.27w \leftarrow 0.37 - 0.1 \cdot 1.0 = 0.27. The master weight has moved by a small amount that a grid of step 0.5 cannot show.

  5. Repeat with the new master weight. At w=0.27w = 0.27, Q(0.27)=0.5⋅round⁡(0.54)=0.5Q(0.27) = 0.5 \cdot \operatorname{round}(0.54) = 0.5, so the forward pass is unchanged, the loss is still 0.250.25, and the gradient is still 1.01.0. The master weight moves to w=0.17w = 0.17.

  6. Watch the grid cross a boundary. At w=0.17w = 0.17, Q(0.17)=0.5⋅round⁡(0.34)=0Q(0.17) = 0.5 \cdot \operatorname{round}(0.34) = 0. The rounded weight jumps from 0.50.5 to 00, the output becomes 00, the loss becomes 00, 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 0.50.5, the step gives 0.5−0.1=0.40.5 - 0.1 = 0.4, and Q(0.4)=0.5⋅round⁡(0.8)=0.5Q(0.4) = 0.5 \cdot \operatorname{round}(0.8) = 0.5. The weight is back where it started after every step, so it never reaches the boundary, and the loss stays at 0.250.25.

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

Definition: Fake quantization

For a weight ww, grid step s>0s \gt 0, and integer range [qmin⁡,qmax⁡][q_{\min}, q_{\max}], fake quantization is

Q(w)=s⋅clip⁡ ⁣(round⁡ ⁣(ws),  qmin⁡,  qmax⁡).Q(w) = s \cdot \operatorname{clip}\!\left(\operatorname{round}\!\left(\frac{w}{s}\right),\; q_{\min},\; q_{\max}\right).

The forward pass uses Q(w)Q(w) 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 w^=Q(w)\hat{w} = Q(w). In the walkthrough, w^=0.5\hat{w} = 0.5, y=w^x=0.5y = \hat{w} x = 0.5, and L=(y−t)2=0.25L = (y - t)^2 = 0.25. Activations can be quantized the same way, with their own scale.

Step 3 in symbols: the backward pass

The chain rule gives ∂L/∂w=(∂L/∂w^)(∂w^/∂w)\partial L / \partial w = (\partial L / \partial \hat{w}) (\partial \hat{w} / \partial w). The true value of ∂w^/∂w\partial \hat{w} / \partial w is zero where round⁡\operatorname{round} is flat and undefined at its jumps. The straight-through rule replaces it by

∂w^∂w≈{1qmin⁡s≤w≤qmax⁡s0otherwise.\frac{\partial \hat{w}}{\partial w} \approx \begin{cases} 1 & q_{\min} s \le w \le q_{\max} s \\ 0 & \text{otherwise.} \end{cases}

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 0.370.37 lies inside [−1.0,0.5][-1.0, 0.5], so its derivative is 1, as in step 3.

Step 4 in symbols: the update

The update is applied to the master weight:

w←w−η ∂L∂w^⋅1.w \leftarrow w - \eta \, \frac{\partial L}{\partial \hat{w}} \cdot 1.

With η=0.1\eta = 0.1 and ∂L/∂w^=1.0\partial L / \partial \hat{w} = 1.0, the step is 0.10.1. Each step changes ww by a continuous amount, and w^\hat{w} changes only when ww crosses a rounding boundary, which is at ±0.25\pm 0.25 and ±0.75\pm 0.75 for this grid.

Steps 5 and 6 in symbols: why the master copy works

Let Δ\Delta 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 Δ\Delta smaller than half a step would be erased by rounding, which is the stuck case in the walkthrough.

Worked examples

Example
1. The tiny case: rounding one weight
  1. Take the master weight w=0.37w = 0.37 on the grid with step s=0.5s = 0.5.
  2. Divide by the step: 0.37/0.5=0.740.37 / 0.5 = 0.74.
  3. Round: round⁡(0.74)=1\operatorname{round}(0.74) = 1.
  4. Multiply back by the step: Q(0.37)=0.5×1=0.5Q(0.37) = 0.5 \times 1 = 0.5.
  5. Check the error: ∣0.37−0.5∣=0.13|0.37 - 0.5| = 0.13, which is below half a step, 0.250.25.
  6. Interpretation: the forward pass uses 0.50.5, and the rounding error of 0.130.13 is part of the loss the network trains against.
Example
2. The standard case: the three-step trace
  1. Start with w=0.37w = 0.37 and input x=1x = 1, target t=0t = 0. The rounded weight is 0.50.5, the output is 0.50.5, and the loss is 0.250.25.
  2. The gradient through the straight-through rule is ∂L/∂w=2(y−t)⋅x=2(0.5)(1)=1.0\partial L / \partial w = 2(y - t) \cdot x = 2(0.5)(1) = 1.0.
  3. Update the master: w←0.37−0.1×1.0=0.27w \leftarrow 0.37 - 0.1 \times 1.0 = 0.27.
  4. Round the new master: 0.27/0.5=0.540.27 / 0.5 = 0.54, which rounds to 11, so the rounded weight is still 0.50.5. The loss is still 0.250.25.
  5. Gradient again 1.01.0, update: w←0.27−0.1=0.17w \leftarrow 0.27 - 0.1 = 0.17.
  6. Round the new master: 0.17/0.5=0.340.17 / 0.5 = 0.34, which rounds to 00, so the rounded weight is 00. The output is 00, the loss is 00.
  7. 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 0.50.5 forever.
Example
3. The realistic case: memory for a deployed model
  1. Take a language model with 7 billion weights. In 16-bit floats each weight takes 2 bytes, so the model takes 7×109×2=14×1097 \times 10^9 \times 2 = 14 \times 10^9 bytes, which is 14 GB.
  2. At 8 bits each weight takes 1 byte, so the model takes 7 GB.
  3. At 4 bits each weight takes half a byte, so the model takes 3.53.5 GB.
  4. 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.
  5. 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.
  6. 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: 13×109×0.513 \times 10^9 \times 0.5 bytes is 6.5×1096.5 \times 10^9 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.

ApproachWhat it changesWhen to choose itCost
Post-Training QuantizationRounds a trained model using a small calibration sample8-bit deployment with little accuracy lossMinutes, no training
Quantization-aware trainingTrains with rounded weights in the forward pass4 bits or fewer, or a sensitive modelA full training or fine-tuning run
Mixed-Precision TrainingUses 16-bit arithmetic with a 32-bit master copyFaster training, not a smaller deployed modelTraining-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