Evaluate a symint to its uppper bound value. Returns None if symint's symoblic expr's upper bound can not be evaluated to valid integer according to the constraints in shape_env.
(maybe_symint: Union[int, torch.SymInt])
| 36 | |
| 37 | |
| 38 | def eval_upper_bound(maybe_symint: Union[int, torch.SymInt]) -> int: |
| 39 | """ |
| 40 | Evaluate a symint to its uppper bound value. Returns None if symint's symoblic expr's |
| 41 | upper bound can not be evaluated to valid integer according to the constraints in shape_env. |
| 42 | """ |
| 43 | if isinstance(maybe_symint, int): |
| 44 | return maybe_symint |
| 45 | node = maybe_symint.node |
| 46 | shape_env = node.shape_env |
| 47 | expr = node.expr |
| 48 | var_range: ValueRanges = bound_sympy( # pyre-ignore[24] |
| 49 | expr, shape_env.var_to_range |
| 50 | ) |
| 51 | upper_bound = var_range.upper |
| 52 | # This import is needed temporarily until we update the pinned torch version. |
| 53 | |
| 54 | try: |
| 55 | from torch.utils._sympy.numbers import int_oo # @manual |
| 56 | except ImportError: |
| 57 | int_oo = None |
| 58 | |
| 59 | if isinstance(upper_bound, sympy.Integer): |
| 60 | concrete_upper = int(var_range.upper) |
| 61 | assert isinstance( |
| 62 | concrete_upper, int |
| 63 | ), f"Expect upper bound to be a concrete int but got {concrete_upper}" |
| 64 | return concrete_upper |
| 65 | elif int_oo is not None and upper_bound is int_oo: |
| 66 | return int_oo |
| 67 | else: |
| 68 | raise RuntimeError( |
| 69 | f"Expect upper bound to be sympy.Integer or int_oo. but got {upper_bound}" |
| 70 | ) |
| 71 | |
| 72 | |
| 73 | def eval_shape(shape: Iterable[Union[int, torch.SymInt]]): # pyre-ignore[3] |
no outgoing calls