advanced24 min studyLesson 728 in Engineering

Match training-time fake quantization to the deployed operator

Distinguish PTQ reconstruction from QAT updates and make surrogate-gradient assumptions explicit.

On this page

What you’ll understand

  • Separate PTQ conversion from training with quantization in the forward path.
  • Explain why a straight-through gradient is a chosen surrogate.
  • Align fake-quantization ranges, granularity and freeze policy with deployment.

Post-training quantization begins with an existing trained model and constructs a lower-precision representation without ordinary model finetuning. It can still perform calibration, search and sophisticated reconstruction. Quantization-aware training instead updates parameters while the forward computation emulates selected quantization effects. The distinction concerns which model parameters are trained under which numerical path, not whether one workflow uses a clever algorithm or a large calibration set.

The forward contract comes first

A fake-quantization operation quantizes and reconstructs a value while keeping a floating-point tensor interface. That lets surrounding training code see the coarse forward values without requiring the final integer inference kernel at every step. The emulation must match deployed code ranges, scale granularity, zero points, rounding and clipping closely enough for the intended comparison. Training a symmetric per-channel operator and exporting an affine per-tensor operator changes the problem. Integer accumulation and backend-specific conversion may require additional verification beyond fake-quantized operands.

Observer state and fake quantization are independent controls in PyTorch 2.8 FakeQuantize. Statistics can keep updating while fake quantization is disabled, or be frozen while quantization remains active. A training schedule must say when each changes and which parameters or scales are learnable. Otherwise calibration drift can change the forward mapping throughout a supposedly stable evaluation. Export the actual frozen parameters and compare converted outputs with the final training-time emulation on held-out inputs before relying on task metrics.

F(w)=s ⁣[clip⁡[qmin⁡,qmax⁡] ⁣(RNE⁡(w/s)+z)−z]F(w)=s\!\left[\operatorname{clip}_{[q_{\min},q_{\max}]}\!\left(\operatorname{RNE}(w/s)+z\right)-z\right]
This lesson’s fake quantizer returns a reconstructed real-valued tensor with scale s>0, integer z and the same quotient-first tie convention as its affine lesson. It does not itself emulate every accumulator or packed-kernel detail.↔ Scroll wide equations horizontally.

Surrogate derivatives are not exact derivatives

Rounding makes this forward function piecewise constant, with zero classical derivative away from its jumps and no classical derivative at a jump. A straight-through estimator deliberately substitutes another derivative to permit updates. One illustrative choice passes gradient one inside a declared clipping interval and zero outside. That is an algorithmic surrogate, not calculus proving the rounded function has slope one. Different implementations choose different endpoint behavior and scale gradients; these choices belong in the training configuration and reproducibility record.

A floating-point latent weight can move without changing its reconstructed code until it crosses a rounding boundary. Conversely, one small latent update near a boundary can change the forward value by a whole step. Loss need not decrease at every update, and a successful smooth optimizer setting does not prove stability of a quantized network. PTQ methods such as GPTQ use a reconstruction strategy without turning into QAT merely because they solve an optimization problem. Compare their actual update rules rather than using “optimization” as the classification criterion.

wt+1=wt−η ∂L∂F∣F(wt)gSTE(wt),η>0w_{t+1}=w_t-\eta\,\left.\frac{\partial L}{\partial F}\right|_{F(w_t)}g_{\rm STE}(w_t),\qquad\eta>0
w is a latent weight and g_STE is an explicitly chosen surrogate, not the true derivative of rounding. Units of eta must make the update have weight units. The worked example uses g_STE=1 strictly inside its reconstructed clipping interval.↔ Scroll wide equations horizontally.

Validate the converted model, not only training loss

Allocate data and compute for retention tests as well as the finetuning objective. QAT can adapt to a coarse representation but can also overfit its training mixture or forget previously useful behavior. Keep the final held-out set independent of observer tuning and hyperparameter selection. Measure converted model quality, memory and latency with the actual kernel shapes. A lower fake-quantized training loss neither guarantees backend agreement nor establishes a quality advantage over a cheaper PTQ configuration.

The optimization lab is a deliberately smooth comparison. Reset and set both step size η=0.5 and initial position x₀=2; predict the first Step reaches x=1 with f=0.5. Separately Reset and set both η=2.5 and x₀=2; predict x=-3 with f=4.5, above initial f=2. Step is one iteration here. This exact quadratic illustrates sensitivity to update size, but it contains no rounding, observer state or straight-through estimator and cannot recommend a QAT learning rate.

A latent update can leave a quantized value unchanged

Use s=0.5, z=0, codes -2…2 and nearest-even rounding. Let L=½(F(w)-0.9)², latent w₀=0.6, η=0.25 and the illustrative STE derivative one strictly inside [-1,1]. Perform two updates with scale frozen.

  1. At w₀=0.6, the quotient 1.2 rounds to 1, so F(w₀)=0.5. The forward loss is 0.5 times (0.5-0.9)²=0.08. The selected surrogate gradient is 0.5-0.9=-0.4.

  2. The first latent update gives w₁=0.6-0.25 times (-0.4)=0.7. Its quotient 1.4 still rounds to 1, so F(w₁) remains 0.5 and loss remains 0.08 despite a nonzero parameter update.

  3. The same surrogate gradient gives w₂=0.8. Now the quotient 1.6 rounds to 2, so F(w₂)=1.0 and loss becomes 0.5 times (1.0-0.9)²=0.005. The forward value changes discretely when the latent weight crosses a grid boundary.

The result

These are updates under a declared surrogate and a frozen quantizer. They illustrate boundary crossing; they are not exact gradient descent on the discontinuous forward loss or a universal QAT guarantee.

Explore the model

Interactive lab

Interactive lab · optimization

A smooth control case for update size

Fresh Reset → set η=0.5 and x₀=2 → Step predicts x=1, f=0.5. Separate Reset → set η=2.5 and x₀=2 → Step predicts x=-3, f=4.5.

A smooth control case for update sizeGradient descent on f(x) = x²/2: xₙ = (1 − η)ⁿx₀. Playback advances two discrete iterations per second. Values outside |x| ≤ 4 are explicitly reported. Current readings: Position x0: 2; Objective x²/2: 2; Iteration behavior: Stable step. All parameters are adjustable with the labeled controls below.−4−202402.174.356.528.7Parameter xf(x) = x²/2n = 0
ObjectiveDiscrete iterate
Position x0
2
Objective x²/2
2
Iteration behavior
Stable step
n = 0 · t = 0 s

Gradient descent on f(x) = x²/2: xₙ = (1 − η)ⁿx₀. Playback advances two discrete iterations per second. Values outside |x| ≤ 4 are explicitly reported. Parameter changes pause playback and recompute the model at the current elapsed time. Reset restores both parameters and returns time to zero. Playback starts paused. Adjust either parameter, then choose Play or Step.

Look forThe lab minimizes f=x²/2 exactly. It has no fake quantizer or STE and supplies a comparison of update dynamics rather than QAT hyperparameters.

Check your understanding

Which statement correctly describes the STE used in the worked example?

Try it yourself

Keep the same quantizer, η=0.25 and interior STE, but set w₀=0.8 and target 0.6. Perform one update and compare the initial and resulting forward losses.

Take a moment to make a prediction or work it out on paper. Open a hint when you need one.

Give me a hint

The initial reconstruction is 1.0, so the surrogate gradient is 0.4. Update the latent value before requantizing.

Compare with the solution

The latent update is 0.8-0.25×0.4=0.7. Reconstruction moves from 1.0 to 0.5. Initial loss is ½(1.0-0.6)²=0.08; resulting loss is ½(0.5-0.6)²=0.005. The code jump helps this example, but it does not establish monotonic loss decrease for other targets, updates or networks.

Take it with you

  • PTQ reconstruction and QAT parameter training are distinct workflows.
  • Fake-quantization and observer schedules need separate configuration.
  • An STE is a chosen backward surrogate, not the derivative of rounding.

Read further

Original sources and deeper explanations to follow at your own pace.

Optional progress, saved in this browser when available. Revisit any lesson at any time.

Lesson not marked complete.