From 0c9014d2b19d5cd38f815724790b422431ec4331 Mon Sep 17 00:00:00 2001 From: William Wen Date: Thu, 9 May 2024 16:25:17 -0700 Subject: [PATCH 1/4] Update [ghstack-poisoned] --- test/dynamo/test_repros.py | 14 ++++++++++++++ torch/_dynamo/variables/builtin.py | 22 ++++++++++++---------- torch/_dynamo/variables/dicts.py | 2 +- 3 files changed, 27 insertions(+), 11 deletions(-) diff --git a/test/dynamo/test_repros.py b/test/dynamo/test_repros.py index 590fe22ff0564..c8efcfd85faa3 100644 --- a/test/dynamo/test_repros.py +++ b/test/dynamo/test_repros.py @@ -4845,6 +4845,20 @@ def ladder(x): opt_ladder = torch.compile(ladder, fullgraph=True, backend="eager") self.assertEqual(opt_ladder(data), ladder(data)) + def test_const_dict_keyerror(self): + d = {} + + def fn(x): + try: + y = d[0] + except KeyError: + y = 1 + return x + y + + opt_fn = torch.compile(fn, backend="eager") + inp = torch.randn(3, 3) + self.assertEqual(fn(inp), opt_fn(inp)) + instantiate_parametrized_tests(ReproTests) diff --git a/torch/_dynamo/variables/builtin.py b/torch/_dynamo/variables/builtin.py index fe2e2a10f42c0..91a1da13db895 100644 --- a/torch/_dynamo/variables/builtin.py +++ b/torch/_dynamo/variables/builtin.py @@ -787,27 +787,29 @@ def call_self_handler(tx, args, kwargs): def constant_fold_handler(tx, args, kwargs): # fast path - return builder( - tx, - fn( + try: + res = fn( *[x.as_python_constant() for x in args], - ), - ) + ) + except Exception as exc: + unimplemented(f"constant fold exception: {repr(exc)}") + return builder(tx, res) else: def constant_fold_handler(tx, args, kwargs): # path with a runtime check if check_unspec_or_constant_args(args, kwargs): - return builder( - tx, - fn( + try: + res = fn( *[x.as_python_constant() for x in args], **{ k: v.as_python_constant() for k, v in kwargs.items() }, - ), - ) + ) + except Exception as exc: + unimplemented(f"constant fold exception: {repr(exc)}") + return builder(tx, res) handlers.append(constant_fold_handler) diff --git a/torch/_dynamo/variables/dicts.py b/torch/_dynamo/variables/dicts.py index db97004d545c0..60fda2146432f 100644 --- a/torch/_dynamo/variables/dicts.py +++ b/torch/_dynamo/variables/dicts.py @@ -196,7 +196,7 @@ def reconstruct(self, codegen): def getitem_const(self, arg: VariableTracker): key = ConstDictVariable._HashableTracker(arg) if key not in self.items: - raise KeyError(arg.value) + unimplemented(f"dict KeyError: {arg.value}") return self.items[key] def call_method( From 5aaab3464164233a313ded6a7c73939424b62fab Mon Sep 17 00:00:00 2001 From: William Wen Date: Thu, 9 May 2024 17:47:12 -0700 Subject: [PATCH 2/4] Update [ghstack-poisoned] --- test/dynamo/test_repros.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/dynamo/test_repros.py b/test/dynamo/test_repros.py index c8efcfd85faa3..6e402593f0a89 100644 --- a/test/dynamo/test_repros.py +++ b/test/dynamo/test_repros.py @@ -3321,7 +3321,7 @@ def fn(x): x = {"a": torch.tensor([1]), "b": torch.tensor([1])} # FIXME It should be KeyError here - self.assertRaises(torch._dynamo.exc.InternalTorchDynamoError, lambda: fn(x)) + self.assertRaises(KeyError, lambda: fn(x)) def test_attached_attribute_in_dir(self): class MyModule(torch.nn.Module): From e7e303cce352b5ddc6445cdabc78db866e177bac Mon Sep 17 00:00:00 2001 From: William Wen Date: Thu, 9 May 2024 17:48:38 -0700 Subject: [PATCH 3/4] Update [ghstack-poisoned] --- test/dynamo/test_repros.py | 1 - .../TestTensorBoardEmbedding.test_embedding | 0 2 files changed, 1 deletion(-) delete mode 100644 test/dynamo_expected_failures/TestTensorBoardEmbedding.test_embedding diff --git a/test/dynamo/test_repros.py b/test/dynamo/test_repros.py index 6e402593f0a89..be9cc2218004d 100644 --- a/test/dynamo/test_repros.py +++ b/test/dynamo/test_repros.py @@ -3320,7 +3320,6 @@ def fn(x): return y x = {"a": torch.tensor([1]), "b": torch.tensor([1])} - # FIXME It should be KeyError here self.assertRaises(KeyError, lambda: fn(x)) def test_attached_attribute_in_dir(self): diff --git a/test/dynamo_expected_failures/TestTensorBoardEmbedding.test_embedding b/test/dynamo_expected_failures/TestTensorBoardEmbedding.test_embedding deleted file mode 100644 index e69de29bb2d1d..0000000000000 From 440e4d4069e24255797e5809316663a7cf23b0bf Mon Sep 17 00:00:00 2001 From: William Wen Date: Fri, 10 May 2024 10:38:13 -0700 Subject: [PATCH 4/4] Update [ghstack-poisoned] --- .../TestTensorBoardEmbedding.test_embedding_64 | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 test/dynamo_expected_failures/TestTensorBoardEmbedding.test_embedding_64 diff --git a/test/dynamo_expected_failures/TestTensorBoardEmbedding.test_embedding_64 b/test/dynamo_expected_failures/TestTensorBoardEmbedding.test_embedding_64 deleted file mode 100644 index e69de29bb2d1d..0000000000000