Skip to content

Commit

Permalink
Introduce int_oo (#127693) (#3936)
Browse files Browse the repository at this point in the history
Summary:

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 pytorch/pytorch#127396

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

X-link: pytorch/pytorch#127693
Approved by: https://github.com/lezcano
ghstack dependencies: #126905

bypass-github-export-checks pushed new commits to forward fix error

Test Plan: contbuild & OSS CI, see https://hud.pytorch.org/commit/pytorch/pytorch/9cab5987bdeb66df8efbc581b3469bfe300e168c

Differential Revision: D58394154

Pulled By: clee2000
  • Loading branch information
ezyang authored and facebook-github-bot committed Jun 11, 2024
1 parent 88e9737 commit 928bbd0
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .ci/docker/ci_commit_pins/pytorch.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
48b6c8dbc376db4406a979b35cd6909bcb428931
70a1e8571802c22c0f09279b77876e6e85c81325
5 changes: 3 additions & 2 deletions exir/sym_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import sympy

import torch
from torch.utils._sympy.numbers import int_oo
from torch.utils._sympy.value_ranges import bound_sympy, ValueRanges


Expand Down Expand Up @@ -47,8 +48,8 @@ def eval_upper_bound(maybe_symint: Union[int, torch.SymInt]) -> Optional[int]:
concrete_upper, int
), f"Expect upper bound to be a concrete int but got {concrete_upper}"
return concrete_upper
elif isinstance(upper_bound, sympy.oo):
return None
elif upper_bound in (sympy.oo, int_oo):
return int_oo
else:
raise RuntimeError(
f"Expect upper bound to be sympy.Integer or sympy.oo. but got {upper_bound}"
Expand Down
2 changes: 1 addition & 1 deletion install_requirements.sh
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ done
# NOTE: If a newly-fetched version of the executorch repo changes the value of
# NIGHTLY_VERSION, you should re-run this script to install the necessary
# package versions.
NIGHTLY_VERSION=dev20240507
NIGHTLY_VERSION=dev20240611

# The pip repository that hosts nightly torch packages.
TORCH_NIGHTLY_URL="https://download.pytorch.org/whl/nightly/cpu"
Expand Down

0 comments on commit 928bbd0

Please sign in to comment.