Fix broken example code in the static quantization tutorial - #4794
Open
amanyagami wants to merge 1 commit into
Open
Fix broken example code in the static quantization tutorial#4794amanyagami wants to merge 1 commit into
amanyagami wants to merge 1 commit into
Conversation
docs/source/eager_tutorials/static_quantization.rst's QuantizedLinear example had three compounding bugs that made it non-functional for any reader following it verbatim (tracked under pytorch#3637, updating docs that reference outdated workflows): 1. QuantizedLinear.from_observed() passed an extra `target_dtype` argument into cls(...) that QuantizedLinear.__init__ never accepted (it's unused) - raised: TypeError: QuantizedLinear.__init__() takes 7 positional arguments but 8 were given 2. Int8Tensor.from_hp's static-scale path asserts `scale.ndim == hp_tensor.ndim`, but AffineQuantizedMinMaxObserver.calculate_qparams() returns scale/zero_point with the block dims squeezed out (e.g. shape (64,) for a per-row scale on a (64, 64) weight, not (64, 1)) - raised: AssertionError (in Int8Tensor.from_hp) 3. Once 1-2 are fixed, forward() manually built a *second* Int8Tensor for the activation and passed both quantized tensors into F.linear. Int8Tensor's F.linear dispatch has no support for a pre-quantized activation tensor - only a plain high precision one, optionally paired with the *weight* tensor's act_quant_kwargs/act_quant_scale/ act_quant_zero_point fields for static (fixed-scale) activation quantization. This raised: NotImplementedError: Int8Tensor dispatch: attempting to run unimplemented operator/function: func=aten.view.default, ... Fix: fold the calibrated activation scale/zero_point into the weight tensor via act_quant_kwargs/act_quant_scale/act_quant_zero_point on Int8Tensor.from_hp - the currently supported way to do static activation quantization - and have forward() pass the raw activation straight into F.linear; Int8Tensor's dispatch quantizes it internally using the fixed statistics. Also fixed the reshape needed for bug 2, dropped the dead target_dtype argument for bug 1, and updated the tutorial's illustrative REPL output (repr type name, tensor shape, and where the activation scale now lives) to match reality. Added test/quantization/test_static_quantization_doc_example.py, which mirrors this example (minus torch.compile/CUDA, so it runs on CPU) end to end. Verified it reproduces the original TypeError on the unpatched example code, and the full corrected pipeline runs cleanly and produces a NaN-free, correctly-shaped output with the fix. Also verified the edited .rst still parses cleanly with docutils. Towards pytorch#3637
amanyagami
requested review from
andrewor14,
jerryzh168 and
vkuzo
as code owners
August 19, 2026 20:50
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/ao/4794
Note: Links to docs will display an error until the docs builds have been completed. This comment was automatically generated by Dr. CI and updates every 15 minutes. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
docs/source/eager_tutorials/static_quantization.rst'sQuantizedLinearexample had three compounding bugs that made it non-functional for any reader following it verbatim (tracked under #3637, updating docs that reference outdated workflows):QuantizedLinear.from_observed()passed an extratarget_dtypeargument intocls(...)thatQuantizedLinear.__init__never accepted (it's unused) — raised:Int8Tensor.from_hp's static-scale path assertsscale.ndim == hp_tensor.ndim, butAffineQuantizedMinMaxObserver.calculate_qparams()returns scale/zero_point with the block dims squeezed out (e.g. shape(64,)for a per-row scale on a(64, 64)weight, not(64, 1)) — raised:Once 1–2 are fixed,
forward()manually built a secondInt8Tensorfor the activation and passed both quantized tensors intoF.linear.Int8Tensor'sF.lineardispatch has no support for a pre-quantized activation tensor — only a plain high precision one, optionally paired with the weight tensor'sact_quant_kwargs/act_quant_scale/act_quant_zero_pointfields for static (fixed-scale) activation quantization. This raised:Fix
Fold the calibrated activation scale/zero_point into the weight tensor via
act_quant_kwargs/act_quant_scale/act_quant_zero_pointonInt8Tensor.from_hp— the currently supported way to do static activation quantization — and haveforward()pass the raw activation straight intoF.linear;Int8Tensor's dispatch quantizes it internally using the fixed statistics. Also fixed the reshape needed for bug 2, dropped the deadtarget_dtypeargument for bug 1, and updated the tutorial's illustrative REPL output (repr type name, tensor shape, and where the activation scale now lives) to match reality.Test
Added
test/quantization/test_static_quantization_doc_example.py, which mirrors this example (minustorch.compile/CUDA, so it runs on CPU) end to end.TypeErroron the unpatched example code (and would hit the other two errors in sequence if that one were papered over — traced each independently while debugging)..rststill parses cleanly withdocutils.This leaves one remaining checklist item open in #3637 (updating the linked #3687, which was closed without merging) — that's a separate, larger scope change (a new
Int8StaticActivationInt8WeightConfig) rather than a doc bug, so I've left it for a maintainer to scope.🤖 Generated with Claude Code