Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix integer overflow in quantization #129127

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

Flamefire
Copy link
Collaborator

@Flamefire Flamefire commented Jun 20, 2024

The static_cast<int64_t> can overflow for large float values and/or a small scale (e.g. 9.2e14 & 1e-4)
Fix a similar issue in the mask calculation where std::lrint is used which may convert to a 32 bit float returning an implementation defined value on overflow.

Stay in float mode using std::round and fmin/fmax to avoid this.

Fixes #111471

I actually fixed the CUDA code first and copied that.

I didn't touch the code duplication which can likely be removed by using a fitting AT_DISPATCH but for some reason the zero_point is int32_t while the limits are int64_t which to me doesn't make much sense and the actual type could always be used.

After #113861 failed for some reason

cc @jgong5 @mingfeima @XiaobingSuper @sanchitintel @ashokei @jingxu10

Flamefire and others added 5 commits January 22, 2024 22:56
The `static_cast<int64_t>` can overflow for large float values and/or
a small scale (e.g. 9.2e14 & 1e-4)
Fix a similar issue in the mask calculation where `std::lrint` is used
which may convert to a 32 bit float returning an implementation defined
value on overflow.

Stay in float mode using `std::round` and `fmin/fmax` to avoid this.

Fixes pytorch#111471
@pytorch-bot pytorch-bot bot added module: cpu CPU specific problem (e.g., perf, algorithm) release notes: quantization release notes category labels Jun 20, 2024
Copy link

pytorch-bot bot commented Jun 20, 2024

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/129127

Note: Links to docs will display an error until the docs builds have been completed.

✅ No Failures

As of commit 2af912f with merge base 54b0006 (image):
💚 Looks good so far! There are no failures yet. 💚

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@zou3519 zou3519 added the triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module label Jun 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
module: cpu CPU specific problem (e.g., perf, algorithm) open source release notes: quantization release notes category triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module
Projects
None yet
Development

Successfully merging this pull request may close these issues.

test_learnable_forward_per_channel fails due to integer overflow
3 participants