Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions test/export/test_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,35 @@ def forward(self, x):
return (add,)""",
)

def test_int_shape_specialization(self):
class M(torch.nn.Module):
def forward(self, x):
ori_size = (
int(x.shape[-2] / 1),
int(x.shape[-1] / 1),
)
x = F.interpolate(x, size=ori_size, mode="bilinear")
return x

input1 = (torch.rand(1, 3, 28, 28),)
input2 = (torch.rand(1, 3, 56, 56),)
inputs = [input1, input2]
model = M()
dynamic_shapes = {
"x": {2: torch.export.Dim.DYNAMIC, 3: torch.export.Dim.DYNAMIC},
}
with self.assertRaisesRegex(
(
torch.fx.experimental.symbolic_shapes.ConstraintViolationError,
torch._dynamo.exc.UserError,
),
(
r"your code specialized it to be a constant \(28\)(.*\n)*.*"
r"your code specialized it to be a constant \(28\)(.*\n)*.*"
),
):
export(model, input1, dynamic_shapes=dynamic_shapes, strict=False)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

didn't investigate more, but strict=True doesn't emit this guard; the original test case passes


def test_external_call_non_strict_real_tensor(self):
class ExternalMethod:
def add(self, x):
Expand Down
12 changes: 12 additions & 0 deletions torch/fx/experimental/symbolic_shapes.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,13 @@
CleanDiv,
FloorDiv,
FloorToInt,
IntTrueDiv,
IsNonOverlappingAndDenseIndicator,
Max,
Min,
Mod,
PythonMod,
TruncToInt,
)
from torch.utils._sympy.numbers import int_oo
from torch.utils._sympy.printers import CppPrinter, PythonPrinter
Expand Down Expand Up @@ -5990,6 +5992,16 @@ def simplify(self, expr: _SympyT, size_oblivious: bool = False) -> _SympyT:
if min_max_replacements:
expr = expr.xreplace(min_max_replacements)

if expr.has(TruncToInt):
trunc_replacements = {}
for atom in expr.atoms(TruncToInt):
if isinstance(atom.args[0], IntTrueDiv):
base, divisor = atom.args[0].args
if base % divisor == 0:
trunc_replacements[atom] = base // divisor
if trunc_replacements:
expr = expr.xreplace(trunc_replacements)

# TODO it would seem that this pass is not necessary given the
# below replacement of // with /, but for nested FloorDivs
# the non-recursive replacement doesn't work, and
Expand Down
Loading