Skip to content

Fix broken example code in the static quantization tutorial - #4794

Open
amanyagami wants to merge 1 commit into
pytorch:mainfrom
amanyagami:fix/3637-static-quant-doc-broken-example
Open

Fix broken example code in the static quantization tutorial#4794
amanyagami wants to merge 1 commit into
pytorch:mainfrom
amanyagami:fix/3637-static-quant-doc-broken-example

Conversation

@amanyagami

Copy link
Copy Markdown

Summary

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 #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.

Test

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 would hit the other two errors in sequence if that one were papered over — traced each independently while debugging).
  • The full corrected pipeline runs cleanly and produces a NaN-free, correctly-shaped output with the fix.
  • Verified the edited .rst still parses cleanly with docutils.

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

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
@pytorch-bot

pytorch-bot Bot commented Aug 19, 2026

Copy link
Copy Markdown

🔗 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.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Aug 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant