Skip to content

Commit

Permalink
[dynamo] Allow asserts to fail (pytorch#126661)
Browse files Browse the repository at this point in the history
Currently if an assertion is statically known to be false, dynamo converts it to
`_assert_async` which inductor currently ignores. Instead this graph breaks to
raise the original assertion.

Pull Request resolved: pytorch#126661
Approved by: https://github.com/ezyang
peterbell10 authored and pytorchmergebot committed May 21, 2024
1 parent d777685 commit 51c07f9
Showing 4 changed files with 20 additions and 3 deletions.
15 changes: 15 additions & 0 deletions test/dynamo/test_misc.py
Original file line number Diff line number Diff line change
@@ -1358,6 +1358,21 @@ def f(x):

self.assertRaises(torch._dynamo.exc.UserError, lambda: f(torch.tensor([3])))

def test_assert(self):
@torch.compile
def fn1(x):
assert x.shape != x.shape

with self.assertRaises(AssertionError):
a = torch.randn(10)
fn1(a)

def fn2(x):
assert x.shape == x.shape
return x.abs()

torch._dynamo.testing.standard_test(self, fn=fn2, nargs=1, expected_ops=1)

def test_config_obj(self):
class Cfg:
def __init__(self):
Empty file.
Empty file.
8 changes: 5 additions & 3 deletions torch/_dynamo/symbolic_convert.py
Original file line number Diff line number Diff line change
@@ -343,9 +343,11 @@ def inner(self: "InstructionTranslatorBase", inst: Instruction):
):
error_msg: VariableTracker = self.pop()
# Skip over things like `assert True`
if value.is_python_constant() and bool(value.as_python_constant()):
self.jump(inst)
return
if value.is_python_constant():
if bool(value.as_python_constant()):
return self.jump(inst)
else:
jump_graph_break(self, inst, value)

# TODO maybe should respect DtoH sync intention of users later??
# Manually insert torch._assert_async instead of python assert and jump over

0 comments on commit 51c07f9

Please sign in to comment.