You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Enable weight fake quantization in calibration mode (#25)
* feat(quantization): keep weight fake quantization enabled during calibration mode
Quantizer.calibration_mode() previously disabled fake quantization on both
weights and activations. It now only disables activation fake quantization,
so activation observers see the effect of quantized weights when computing
activation ranges.
Adds enable_weight_fake_quant/disable_activation_fake_quant helpers used by
both the eager and graph quantizers, and updates docs/tests accordingly.
* fix(quantization): address review feedback on weight FQ calibration change
- Move enable_weight_fake_quant/disable_activation_fake_quant into a new
_fake_quant_utils module so they can import FakeQuantizeImplBase and
CompressionTargetTensor at module scope instead of via local imports,
keeping _utils.py free of dependencies on other quantization modules.
- Use basic_config (weight + activation quantization) instead of
input_activation_only_config in test_calibration_mode so the weight-FQ
branch is actually exercised; only assert scale drift for activation
modules, since weight ranges are fixed at prepare time.
- Drop the changelog.d entry.
---------
Co-authored-by: Utkarsh Simha <u_simha@apple.com>
Calibration is necessary when activation quantization is enabled. In order to determine proper quantization parameters for activation quantizers, representative data must be passed through the prepared model in the `calibration_mode()` context.
55
55
56
-
Inside `calibration_mode()`, fake quantization is disabled and observers track tensor ranges seen at each activation quantizer.
57
-
Each forward pass updates the activation scales without introducing quantization noise into the output.
58
-
On exit, observers are disabled and fake quantization is re-enabled.
56
+
Inside `calibration_mode()`, activation fake quantization is disabled while weight fake quantization stays on, and observers track tensor ranges seen at each activation quantizer.
57
+
Each forward pass updates the activation scales using activations produced with quantized weights upstream, without injecting activation quantization noise into the observed values.
58
+
On exit, observers are disabled and activation fake quantization is re-enabled.
Copy file name to clipboardExpand all lines: docs/src/quantization/overview.md
+4-3Lines changed: 4 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -111,9 +111,10 @@ Right after `prepare()`, the activation scales come only from `example_inputs`,
111
111
This is what the context manager handles:
112
112
113
113
- Inside the context:
114
-
- fake-quantization is turned **off**: forward pass gives the same output as the unquantized model. Hence without distorting the outputs, the quantization params can be computed.
115
-
- range observers are turned **on**: this means that each forward pass updates the observed activation ranges, and hence the activation quantization scales.
116
-
- After exiting the context manager, observers are turned back off and fake-quantization back on, leaving the model ready for evaluation.
114
+
- activation fake-quantization is turned **off**: activation observers see undistorted activation values, so the observed ranges (and resulting scales) reflect the true distribution rather than already-quantized values.
115
+
- weight fake-quantization stays **on**: activations flowing into each observer are produced with quantized weights upstream, matching what the deployed model will actually see.
116
+
- range observers are turned **on**: each forward pass updates the observed activation ranges, and hence the activation quantization scales.
117
+
- After exiting the context manager, observers are turned back off and activation fake-quantization back on, leaving the model ready for evaluation.
117
118
118
119
A small amount of representative data is typically enough.
Copy file name to clipboardExpand all lines: docs/src/tutorials/mnist_quantization.ipynb
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -748,7 +748,7 @@
748
748
"source": [
749
749
"We now call `calibration_mode()` and feed it representative inputs to populate scale and zero-point value.\n",
750
750
"\n",
751
-
"The `calibration_mode()` context manager enables range observers (to collect activation statistics) while disabling fake quantization (so the forward pass is numerically identical to the unquantized model). After exiting the context, observers are frozen and fake quantization is re-enabled."
751
+
"The `calibration_mode()` context manager enables range observers (to collect activation statistics) and disables activation fake quantization (so the observed ranges reflect undistorted activation values), while leaving weight fake quantization enabled (so activations flowing into each observer are produced with quantized weights upstream). After exiting the context, observers are frozen and activation fake quantization is re-enabled."
0 commit comments