KEMBAR78
Introduce int_oo by ezyang · Pull Request #127693 · pytorch/pytorch · GitHub
Skip to content

Conversation

@ezyang
Copy link
Contributor

@ezyang ezyang commented Jun 1, 2024

Stack from ghstack (oldest at bottom):

In a previous life, we used sympy.oo to represent the lower/upper bounds of integer ranges. Later, we changed this to be sys.maxsize - 1 for a few reasons: (1) sometimes we do tests on a value being exactly sys.maxsize, and we wanted to avoid a data dependent guard in this case, (2) sympy.oo corresponds to floating point infinity, so you get incorrect types for value ranges with oo, and (3) you can do slightly better reasoning if you assume that input sizes fall within representable 64-bit integer range.

After working in the sys.maxsize regime for a bit, I've concluded that this was actually a bad idea. Specifically, the problem is that you end up with sys.maxsize in your upper bound, and then whenever you do any sort of size-increasing computation like size * 2, you end up with 2 * sys.maxsize, and you end up doing a ton of arbitrary precision int computation that is totally unnecessary. A symbolic bound is better.

But especially after #126905, we can't go back to using sympy.oo, because that advertises that it's not an integer, and now your ValueRanges is typed incorrectly. So what do we do? We define a new numeric constant int_oo, which is like sympy.oo but it advertises is_integer. test/test_sympy_utils.py describes some basic properties of the number, and torch/utils/_sympy/numbers.py has the actual implementation.

The rest of the changes of the PR are working out the implications of this change. I'll give more commentary as inline comments.

Fixes #127396

Signed-off-by: Edward Z. Yang ezyang@meta.com

cc @jgong5 @mingfeima @XiaobingSuper @sanchitintel @ashokei @jingxu10 @voznesenskym @penguinwu @EikanWang @Guobing-Chen @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @peterbell10 @ipiszy @yf225 @chenyang78 @kadeng @muchulee8 @ColinPeppler @amjames @desertfire @chauhang

[ghstack-poisoned]
@pytorch-bot
Copy link

pytorch-bot bot commented Jun 1, 2024

🔗 Helpful Links

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

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

✅ You can merge normally! (6 Unrelated Failures)

As of commit 0b36b03 with merge base f681e36 (image):

FLAKY - The following jobs failed but were likely due to flakiness present on trunk:

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

@pytorch-bot pytorch-bot bot added ciflow/inductor module: cpu CPU specific problem (e.g., perf, algorithm) release notes: fx release notes category labels Jun 1, 2024
ezyang added a commit that referenced this pull request Jun 1, 2024
Signed-off-by: Edward Z. Yang <ezyang@meta.com>

ghstack-source-id: 50bb181
Pull Request resolved: #127693
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
ezyang added a commit that referenced this pull request Jun 3, 2024
Signed-off-by: Edward Z. Yang <ezyang@meta.com>

ghstack-source-id: ef3063b
Pull Request resolved: #127693
[ghstack-poisoned]
if val in (sympy.oo, int_oo):
return math.inf
if val == -sympy.oo:
if val in (-sympy.oo, -int_oo):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@angelayi @avikchaudhuri This is a bit suspicious, because math.inf is not an integer. But I guess that's what your code used to do? Another option is when you have bounds like this is to return None.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah None would be better.

@pytorchmergebot
Copy link
Collaborator

@pytorchbot successfully started a revert job. Check the current status here.
Questions? Feedback? Please reach out to the PyTorch DevX Team

pytorchmergebot added a commit that referenced this pull request Jun 11, 2024
This reverts commit 9cab598.

Reverted #127693 on behalf of https://github.com/clee2000 due to sorry executorch CI is a bit weird regarding pins, I'll make a chat with mergen with the choices of what to do and how it'll affect executorch CI, reverting for now to prevent more divergences in the meantime ([comment](#127693 (comment)))
@pytorchmergebot
Copy link
Collaborator

@ezyang your PR has been successfully reverted.

facebook-github-bot pushed a commit to pytorch/executorch that referenced this pull request Jun 12, 2024
Summary: This is needed for pytorch/pytorch#127693 . This code is written so it is compatible before and after this PR.

Differential Revision: D58465158
ezyang added a commit to pytorch/executorch that referenced this pull request Jun 12, 2024
Summary:
Pull Request resolved: #3947

This is needed for pytorch/pytorch#127693 . This code is written so it is compatible before and after this PR.

Differential Revision: D58465158
facebook-github-bot pushed a commit to pytorch/executorch that referenced this pull request Jun 12, 2024
Summary:

This is needed for pytorch/pytorch#127693 . This code is written so it is compatible before and after this PR.

Reviewed By: mergennachin

Differential Revision: D58465158
ezyang added a commit to pytorch/executorch that referenced this pull request Jun 12, 2024
Summary:
Pull Request resolved: #3947

This is needed for pytorch/pytorch#127693 . This code is written so it is compatible before and after this PR.

Reviewed By: mergennachin

Differential Revision: D58465158
facebook-github-bot pushed a commit to pytorch/executorch that referenced this pull request Jun 12, 2024
Summary:
Pull Request resolved: #3947

This is needed for pytorch/pytorch#127693 . This code is written so it is compatible before and after this PR.

Reviewed By: mergennachin, clee2000

Differential Revision: D58465158

fbshipit-source-id: ca0f2a79eb07e78ff2887f78eb62ff38eeea3ede
@ezyang
Copy link
Contributor Author

ezyang commented Jun 13, 2024

@pytorchbot merge

@pytorchmergebot
Copy link
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

@clee2000 clee2000 mentioned this pull request Jun 13, 2024
pytorchmergebot pushed a commit that referenced this pull request Jun 13, 2024
merge at will
After #125968
and #127693
landrace

Pull Request resolved: #128587
Approved by: https://github.com/huydhn
TharinduRusira pushed a commit to TharinduRusira/pytorch that referenced this pull request Jun 14, 2024
In a previous life, we used sympy.oo to represent the lower/upper bounds of integer ranges. Later, we changed this to be sys.maxsize - 1 for a few reasons: (1) sometimes we do tests on a value being exactly sys.maxsize, and we wanted to avoid a data dependent guard in this case, (2) sympy.oo corresponds to floating point infinity, so you get incorrect types for value ranges with oo, and (3) you can do slightly better reasoning if you assume that input sizes fall within representable 64-bit integer range.

After working in the sys.maxsize regime for a bit, I've concluded that this was actually a bad idea. Specifically, the problem is that you end up with sys.maxsize in your upper bound, and then whenever you do any sort of size-increasing computation like size * 2, you end up with 2 * sys.maxsize, and you end up doing a ton of arbitrary precision int computation that is totally unnecessary. A symbolic bound is better.

But especially after pytorch#126905, we can't go back to using sympy.oo, because that advertises that it's not an integer, and now your ValueRanges is typed incorrectly. So what do we do? We define a new numeric constant `int_oo`, which is like `sympy.oo` but it advertises `is_integer`. **test/test_sympy_utils.py** describes some basic properties of the number, and **torch/utils/_sympy/numbers.py** has the actual implementation.

The rest of the changes of the PR are working out the implications of this change. I'll give more commentary as inline comments.

Fixes pytorch#127396

Signed-off-by: Edward Z. Yang <ezyang@meta.com>

Pull Request resolved: pytorch#127693
Approved by: https://github.com/lezcano
ghstack dependencies: pytorch#126905
TharinduRusira pushed a commit to TharinduRusira/pytorch that referenced this pull request Jun 14, 2024
This reverts commit 9cab598.

Reverted pytorch#127693 on behalf of https://github.com/clee2000 due to sorry executorch CI is a bit weird regarding pins, I'll make a chat with mergen with the choices of what to do and how it'll affect executorch CI, reverting for now to prevent more divergences in the meantime ([comment](pytorch#127693 (comment)))
TharinduRusira pushed a commit to TharinduRusira/pytorch that referenced this pull request Jun 14, 2024
In a previous life, we used sympy.oo to represent the lower/upper bounds of integer ranges. Later, we changed this to be sys.maxsize - 1 for a few reasons: (1) sometimes we do tests on a value being exactly sys.maxsize, and we wanted to avoid a data dependent guard in this case, (2) sympy.oo corresponds to floating point infinity, so you get incorrect types for value ranges with oo, and (3) you can do slightly better reasoning if you assume that input sizes fall within representable 64-bit integer range.

After working in the sys.maxsize regime for a bit, I've concluded that this was actually a bad idea. Specifically, the problem is that you end up with sys.maxsize in your upper bound, and then whenever you do any sort of size-increasing computation like size * 2, you end up with 2 * sys.maxsize, and you end up doing a ton of arbitrary precision int computation that is totally unnecessary. A symbolic bound is better.

But especially after pytorch#126905, we can't go back to using sympy.oo, because that advertises that it's not an integer, and now your ValueRanges is typed incorrectly. So what do we do? We define a new numeric constant `int_oo`, which is like `sympy.oo` but it advertises `is_integer`. **test/test_sympy_utils.py** describes some basic properties of the number, and **torch/utils/_sympy/numbers.py** has the actual implementation.

The rest of the changes of the PR are working out the implications of this change. I'll give more commentary as inline comments.

Fixes pytorch#127396

Signed-off-by: Edward Z. Yang <ezyang@meta.com>

Pull Request resolved: pytorch#127693
Approved by: https://github.com/lezcano
ghstack dependencies: pytorch#126905
TharinduRusira pushed a commit to TharinduRusira/pytorch that referenced this pull request Jun 14, 2024
merge at will
After pytorch#125968
and pytorch#127693
landrace

Pull Request resolved: pytorch#128587
Approved by: https://github.com/huydhn
ignaciobartol pushed a commit to ignaciobartol/pytorch that referenced this pull request Jun 14, 2024
This reverts commit 9cab598.

Reverted pytorch#127693 on behalf of https://github.com/clee2000 due to sorry executorch CI is a bit weird regarding pins, I'll make a chat with mergen with the choices of what to do and how it'll affect executorch CI, reverting for now to prevent more divergences in the meantime ([comment](pytorch#127693 (comment)))
ignaciobartol pushed a commit to ignaciobartol/pytorch that referenced this pull request Jun 14, 2024
In a previous life, we used sympy.oo to represent the lower/upper bounds of integer ranges. Later, we changed this to be sys.maxsize - 1 for a few reasons: (1) sometimes we do tests on a value being exactly sys.maxsize, and we wanted to avoid a data dependent guard in this case, (2) sympy.oo corresponds to floating point infinity, so you get incorrect types for value ranges with oo, and (3) you can do slightly better reasoning if you assume that input sizes fall within representable 64-bit integer range.

After working in the sys.maxsize regime for a bit, I've concluded that this was actually a bad idea. Specifically, the problem is that you end up with sys.maxsize in your upper bound, and then whenever you do any sort of size-increasing computation like size * 2, you end up with 2 * sys.maxsize, and you end up doing a ton of arbitrary precision int computation that is totally unnecessary. A symbolic bound is better.

But especially after pytorch#126905, we can't go back to using sympy.oo, because that advertises that it's not an integer, and now your ValueRanges is typed incorrectly. So what do we do? We define a new numeric constant `int_oo`, which is like `sympy.oo` but it advertises `is_integer`. **test/test_sympy_utils.py** describes some basic properties of the number, and **torch/utils/_sympy/numbers.py** has the actual implementation.

The rest of the changes of the PR are working out the implications of this change. I'll give more commentary as inline comments.

Fixes pytorch#127396

Signed-off-by: Edward Z. Yang <ezyang@meta.com>

Pull Request resolved: pytorch#127693
Approved by: https://github.com/lezcano
ghstack dependencies: pytorch#126905
ignaciobartol pushed a commit to ignaciobartol/pytorch that referenced this pull request Jun 14, 2024
merge at will
After pytorch#125968
and pytorch#127693
landrace

Pull Request resolved: pytorch#128587
Approved by: https://github.com/huydhn
@github-actions github-actions bot deleted the gh/ezyang/2796/head branch July 14, 2024 02:01
@bhack
Copy link
Contributor

bhack commented Oct 15, 2024

@ezyang With last nightly I was going to get spammed by

site-packages/torch/utils/_sympy/interp.py:159] [0/8] failed while executing int_truediv([MetaProxy(scalar_tensor_default_7), MetaProxy(scalar_tensor_default_7)])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/inductor ciflow/trunk Trigger trunk jobs on your pull request Merged module: cpu CPU specific problem (e.g., perf, algorithm) module: dynamo module: inductor release notes: fx release notes category Reverted

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants