Repository: pytorch
Issue: [compiled-autograd][guard-validation] Guard validation fails with duplicate tensors found
Labels: triaged,oncall: pt2,module: compiled autograd

`pytest test/inductor/test_compiled_autograd.py::TestAutogradWithCompiledAutograd::test_backward_twice_without_saved_values`

~~~
Traceback (most recent call last):
  File "/home/anijain/local/miniconda3/envs/pytorch/lib/python3.10/unittest/case.py", line 59, in testPartExecutor
    yield
  File "/home/anijain/local/miniconda3/envs/pytorch/lib/python3.10/unittest/case.py", line 591, in run
    self._callTestMethod(testMethod)
  File "/home/anijain/local/miniconda3/envs/pytorch/lib/python3.10/unittest/case.py", line 549, in _callTestMethod
    method()
  File "/data/users/anijain/pytorch/torch/testing/_internal/common_utils.py", line 2739, in wrapper
    method(*args, **kwargs)
  File "/data/users/anijain/pytorch/test/inductor/test_compiled_autograd.py", line 2187, in wrapped
    out = fn(self)
  File "/data/users/anijain/pytorch/test/test_autograd.py", line 3032, in test_backward_twice_without_saved_values
    c.backward(torch.tensor([1, 1, 1], dtype=torch.double))
  File "/data/users/anijain/pytorch/torch/_tensor.py", line 522, in backward
    torch.autograd.backward(
  File "/data/users/anijain/pytorch/torch/autograd/__init__.py", line 288, in backward
    _engine_run_backward(
  File "/data/users/anijain/pytorch/torch/autograd/graph.py", line 768, in _engine_run_backward
    return Variable._execution_engine.run_backward(  # Calls into the C++ engine to run the backward pass
  File "/data/users/anijain/pytorch/torch/_dynamo/compiled_autograd.py", line 305, in runtime_wrapper
    return compiled_fn(inputs, sizes, hooks)
  File "/data/users/anijain/pytorch/torch/nn/modules/module.py", line 1714, in _wrapped_call_impl
    return self._call_impl(*args, **kwargs)
  File "/data/users/anijain/pytorch/torch/nn/modules/module.py", line 1725, in _call_impl
    return forward_call(*args, **kwargs)
  File "/data/users/anijain/pytorch/torch/_dynamo/eval_frame.py", line 434, in _fn
    return fn(*args, **kwargs)
  File "/data/users/anijain/pytorch/torch/fx/graph_module.py", line 738, in call_wrapped
    return self._wrapped_call(self, *args, **kwargs)
  File "/data/users/anijain/pytorch/torch/fx/graph_module.py", line 316, in __call__
    raise e
  File "/data/users/anijain/pytorch/torch/fx/graph_module.py", line 303, in __call__
    return super(self.cls, obj).__call__(*args, **kwargs)  # type: ignore[misc]
  File "/data/users/anijain/pytorch/torch/nn/modules/module.py", line 1714, in _wrapped_call_impl
    return self._call_impl(*args, **kwargs)
  File "/data/users/anijain/pytorch/torch/nn/modules/module.py", line 1725, in _call_impl
    return forward_call(*args, **kwargs)
  File "/data/users/anijain/pytorch/torch/_dynamo/convert_frame.py", line 1121, in __call__
    return self._torchdynamo_orig_callable(
  File "/data/users/anijain/pytorch/torch/_dynamo/convert_frame.py", line 472, in __call__
    return _compile(
  File "/data/users/anijain/pytorch/torch/_utils_internal.py", line 85, in wrapper_function
    return StrobelightCompileTimeProfiler.profile_compile_time(
  File "/data/users/anijain/pytorch/torch/_strobelight/compile_time_profiler.py", line 129, in profile_compile_time
    return func(*args, **kwargs)
  File "/home/anijain/local/miniconda3/envs/pytorch/lib/python3.10/contextlib.py", line 79, in inner
    return func(*args, **kwds)
  File "/data/users/anijain/pytorch/torch/_dynamo/convert_frame.py", line 817, in _compile
    guarded_code = compile_inner(code, one_graph, hooks, transform)
  File "/data/users/anijain/pytorch/torch/_dynamo/utils.py", line 233, in time_wrapper
    r = func(*args, **kwargs)
  File "/data/users/anijain/pytorch/torch/_dynamo/convert_frame.py", line 726, in compile_inner
    check_fn = CheckFunctionManager(
  File "/data/users/anijain/pytorch/torch/_dynamo/guards.py", line 2137, in __init__
    raise AssertionError(f"Guard check failed: {reasons}")
AssertionError: Guard check failed: Duplicate tensors found: ["L['inputs'][2]", "L['inputs'][1].grad"]
~~~

cc @ezyang @chauhang @penguinwu