Skip to content

Commit 5bb156a

Browse files
williamwen42pytorchmergebot
authored andcommitted
[dynamo] raise observed exception for module attribute errors (pytorch#153659)
Fixes pytorch#153605 Pull Request resolved: pytorch#153659 Approved by: https://github.com/StrongerXi
1 parent db1f331 commit 5bb156a

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

test/dynamo/test_repros.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7254,6 +7254,23 @@ def f(x):
72547254
actual = torch.autograd.grad(c_out.sum(), inputs=(x,))
72557255
self.assertEqual(expected, actual)
72567256

7257+
def test_module_attribute_error(self):
7258+
@torch.compile(backend="eager")
7259+
def f1(x):
7260+
return torch._bar(x)
7261+
7262+
@torch.compile(backend="eager")
7263+
def f2(x):
7264+
try:
7265+
return torch._bar(x)
7266+
except AttributeError:
7267+
return x + 1
7268+
7269+
with self.assertRaises(AttributeError):
7270+
f1(torch.ones(3))
7271+
7272+
self.assertEqual(f2(torch.ones(3)), torch.ones(3) + 1)
7273+
72577274

72587275
instantiate_parametrized_tests(ReproTests)
72597276

torch/_dynamo/variables/misc.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1270,7 +1270,10 @@ def var_getattr(self, tx: "InstructionTranslator", name):
12701270
return tx.output.side_effects.load_attr(self, name)
12711271

12721272
if self.is_torch or name not in self.value.__dict__:
1273-
attr_value = getattr(self.value, name)
1273+
try:
1274+
attr_value = getattr(self.value, name)
1275+
except AttributeError:
1276+
raise_observed_exception(AttributeError, tx)
12741277
else:
12751278
attr_value = self.value.__dict__[name]
12761279

0 commit comments

Comments
 (0)