mirror of
https://github.com/harvard-edge/cs249r_book.git
synced 2026-07-17 08:28:07 -05:00
[PR #1874] [CLOSED] fix(quantization): constant-tensor zero_point clamp silently corrupts values outside [-127,127] #36493
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/harvard-edge/cs249r_book/pull/1874
Author: @Shashank-Tripathi-07
Created: 6/16/2026
Status: ❌ Closed
Base:
main← Head:fix/quantization-constant-tensor-scale📝 Commits (1)
a75a96ffix quantization of constant tensors when the value is outside INT8 range📊 Changes
1 file changed (+16 additions, -7 deletions)
View changed files
📝
tinytorch/src/15_quantization/15_quantization.py(+16 -7)📄 Description
What breaks
quantize_int8()handles constant tensors (all elements equal toc) with:The dequantization identity is
(q - zero_point) * scale = original_value. Withq=0for all elements, this requireszero_point = -c. Butnp.clipsilently capszero_pointat[-128, 127], so for any|c| > 127:No error, no warning -- just silently wrong values.
Fix
Three cases based on
|c|:c == 0:scale=1.0, zero_point=0|c| <= 127:scale=1.0, zero_point=-round(c)(original logic, always in range)|c| > 127: setzero_point = -1(positive c) or+1(negative c), computescale = |c|so that(0 - zero_point) * scale = cexactly -- no clamping neededTest plan
pytest tinytorch/tests/15_quantization/passesdequantize(quantize(Tensor([[500.0, 500.0]])))returns values close to 500.0dequantize(quantize(Tensor([[2.0, 2.0]])))still returns values close to 2.0🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.