8000 [dynamo] raise observed exception for module attribute errors · pytorch/pytorch@660888a · GitHub
[go: up one dir, main page]

Skip to content

Commit 660888a

Browse files
committed
[dynamo] raise observed exception for module attribute errors
ghstack-source-id: 42de38f Pull Request resolved: #153659
1 parent 9ccd601 commit 660888a

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
@@ -7176,6 +7176,23 @@ def func(a):
71767176
output = capturedOutput.getvalue()
71777177
self.assertNotIn("class GraphModule", output)
71787178

7179+
def test_module_attribute_error(self):
7180+
@torch.compile(backend="eager")
7181+
def f1(x):
7182+
return torch._bar(x)
7183+
7184+
@torch.compile(backend="eager")
7185+
def f2(x):
7186+
try:
7187+
return torch._bar(x)
7188+
except AttributeError:
7189+
return x + 1
7190+
7191+
with self.assertRaises(AttributeError):
7192+
f1(torch.ones(3))
7193+
7194+
self.assertEqual(f2(torch.ones(3)), torch.ones(3) + 1)
7195+
71797196

71807197
instantiate_parametrized_tests(ReproTests)
71817198

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)
0