From cdb9df5efe78142b7a612ae9c938ddf8a8850d10 Mon Sep 17 00:00:00 2001 From: Animesh Jain Date: Fri, 23 Aug 2024 11:20:43 -0700 Subject: [PATCH] [dynamo][guards] De-dupe DUPLICATE_INPUT guard (#134354) Hard to write a test. Pull Request resolved: https://github.com/pytorch/pytorch/pull/134354 Approved by: https://github.com/jansel ghstack dependencies: #134272 --- torch/_dynamo/guards.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/torch/_dynamo/guards.py b/torch/_dynamo/guards.py index d61ec7f188c98..d9cc7eb6d44d1 100644 --- a/torch/_dynamo/guards.py +++ b/torch/_dynamo/guards.py @@ -575,6 +575,8 @@ def __init__( str, torch._C._dynamo.guards.GuardManager ] = {} + self._cached_duplicate_input_guards: Set[Tuple[str, str]] = set() + def guard_on_dict_keys_and_ignore_order(self, example_value, guard): dict_mgr = self.get_guard_manager(guard) if isinstance(dict_mgr, DictGuardManager): @@ -1663,6 +1665,13 @@ def DUPLICATE_INPUT(self, guard, source_b): self._set_guard_export_info(guard, code) if config.enable_cpp_guard_manager: + # Check that the guard has not been inserted already + key = (ref_a, ref_b) + if key in self._cached_duplicate_input_guards: + return + self._cached_duplicate_input_guards.add((ref_a, ref_b)) + self._cached_duplicate_input_guards.add((ref_b, ref_a)) + install_object_aliasing_guard( self.get_guard_manager(guard), self.get_guard_manager_from_source(source_b),