|
| 1 | +# Copyright 2026 Arm Limited and/or its affiliates. |
| 2 | +# |
| 3 | +# This source code is licensed under the BSD-style license found in the |
| 4 | +# LICENSE file in the root directory of this source tree. |
| 5 | + |
| 6 | +from typing import Optional |
| 7 | + |
| 8 | +import sympy # type: ignore[import-untyped] |
| 9 | +import torch |
| 10 | +from torch.fx.experimental.symbolic_shapes import ShapeEnv |
| 11 | +from torch.utils._sympy.interp import sympy_interp |
| 12 | + |
| 13 | +_MAX_SET_SIZE = 256 |
| 14 | +_ExactValues = Optional[frozenset[sympy.Basic]] |
| 15 | + |
| 16 | + |
| 17 | +def _expr_to_int(sym_expr: sympy.Basic) -> Optional[int]: |
| 18 | + if isinstance(sym_expr, int): |
| 19 | + return sym_expr |
| 20 | + if isinstance(sym_expr, sympy.Integer): |
| 21 | + return int(sym_expr) |
| 22 | + if getattr(sym_expr, "is_integer", False) and sym_expr.is_number: |
| 23 | + return int(sym_expr) |
| 24 | + return None |
| 25 | + |
| 26 | + |
| 27 | +def _symbol_values(symbol: sympy.Symbol, shape_env: ShapeEnv) -> _ExactValues: |
| 28 | + value_range = shape_env.var_to_range.get(symbol) |
| 29 | + if value_range is None or not value_range.is_int: |
| 30 | + return None |
| 31 | + |
| 32 | + lower = _expr_to_int(value_range.lower) |
| 33 | + upper = _expr_to_int(value_range.upper) |
| 34 | + if lower is None or upper is None or upper < lower: |
| 35 | + return None |
| 36 | + if upper - lower + 1 > _MAX_SET_SIZE: |
| 37 | + return None |
| 38 | + |
| 39 | + return frozenset(sympy.Integer(value) for value in range(lower, upper + 1)) |
| 40 | + |
| 41 | + |
| 42 | +def _map_values(values: _ExactValues, fn) -> _ExactValues: |
| 43 | + if values is None: |
| 44 | + return None |
| 45 | + |
| 46 | + result = {sympy.simplify(fn(value)) for value in values} |
| 47 | + if len(result) > _MAX_SET_SIZE: |
| 48 | + return None |
| 49 | + return frozenset(result) |
| 50 | + |
| 51 | + |
| 52 | +def _combine_values(lhs: _ExactValues, rhs: _ExactValues, fn) -> _ExactValues: |
| 53 | + if lhs is None or rhs is None: |
| 54 | + return None |
| 55 | + if len(lhs) * len(rhs) > _MAX_SET_SIZE * _MAX_SET_SIZE: |
| 56 | + return None |
| 57 | + |
| 58 | + result = {sympy.simplify(fn(a, b)) for a in lhs for b in rhs} |
| 59 | + if len(result) > _MAX_SET_SIZE: |
| 60 | + return None |
| 61 | + return frozenset(result) |
| 62 | + |
| 63 | + |
| 64 | +class _ExactValueAnalysis: |
| 65 | + @staticmethod |
| 66 | + def constant(value, dtype) -> frozenset[sympy.Basic]: |
| 67 | + return frozenset({sympy.sympify(value)}) |
| 68 | + |
| 69 | + @staticmethod |
| 70 | + def add(lhs: _ExactValues, rhs: _ExactValues) -> _ExactValues: |
| 71 | + return _combine_values(lhs, rhs, lambda a, b: a + b) |
| 72 | + |
| 73 | + @staticmethod |
| 74 | + def mul(lhs: _ExactValues, rhs: _ExactValues) -> _ExactValues: |
| 75 | + return _combine_values(lhs, rhs, lambda a, b: a * b) |
| 76 | + |
| 77 | + @staticmethod |
| 78 | + def mod(lhs: _ExactValues, rhs: _ExactValues) -> _ExactValues: |
| 79 | + if rhs is None or any(value == 0 for value in rhs): |
| 80 | + return None |
| 81 | + return _combine_values(lhs, rhs, lambda a, b: sympy.Mod(a, b)) |
| 82 | + |
| 83 | + @staticmethod |
| 84 | + def pow(lhs: _ExactValues, rhs: _ExactValues) -> _ExactValues: |
| 85 | + return _combine_values(lhs, rhs, lambda a, b: a**b) |
| 86 | + |
| 87 | + @staticmethod |
| 88 | + def floor_to_int(values: _ExactValues, dtype) -> _ExactValues: |
| 89 | + return _map_values(values, sympy.floor) |
| 90 | + |
| 91 | + @staticmethod |
| 92 | + def sym_sum(args: list[_ExactValues]) -> _ExactValues: |
| 93 | + acc: _ExactValues = frozenset({sympy.Integer(0)}) |
| 94 | + for arg in args: |
| 95 | + acc = _ExactValueAnalysis.add(acc, arg) |
| 96 | + if acc is None: |
| 97 | + return None |
| 98 | + return acc |
| 99 | + |
| 100 | + |
| 101 | +def evaluate_symbolic_expr_values( |
| 102 | + expr: sympy.Basic | torch.SymInt, |
| 103 | + shape_env: ShapeEnv, |
| 104 | +) -> Optional[set[int]]: |
| 105 | + """Return a best-effort finite set of possible integer values. |
| 106 | +
|
| 107 | + The helper first relies on ``bound_sympy`` for cheap singleton detection. |
| 108 | + When interval bounds are not precise enough, it falls back to a small |
| 109 | + exact-set analysis over bounded symbols using ``sympy_interp``. |
| 110 | +
|
| 111 | + """ |
| 112 | + root_expr = sympy.simplify( |
| 113 | + expr.node.expr if isinstance(expr, torch.SymInt) else expr |
| 114 | + ) |
| 115 | + value_range = shape_env.bound_sympy(root_expr) |
| 116 | + if value_range.is_int and value_range.is_singleton(): |
| 117 | + singleton = _expr_to_int(value_range.lower) |
| 118 | + return {singleton} if singleton is not None else None |
| 119 | + |
| 120 | + exact_values = sympy_interp( |
| 121 | + _ExactValueAnalysis, |
| 122 | + { |
| 123 | + symbol: _symbol_values(symbol, shape_env) |
| 124 | + for symbol in root_expr.free_symbols |
| 125 | + }, |
| 126 | + root_expr, |
| 127 | + missing_handler=lambda symbol: _symbol_values(symbol, shape_env), |
| 128 | + ) |
| 129 | + if exact_values is None: |
| 130 | + return None |
| 131 | + |
| 132 | + result: set[int] = set() |
| 133 | + for value in exact_values: |
| 134 | + integer_value = _expr_to_int(sympy.simplify(value)) |
| 135 | + if integer_value is None: |
| 136 | + return None |
| 137 | + result.add(integer_value) |
| 138 | + return result |
0 commit comments