Repository: pytorch
Issue: [dynamo] Issue with construction nn.Parameter
Labels: triaged,tensor subclass,oncall: pt2,module: dynamo,module: pt2-dispatcher

### 🐛 Describe the bug

With https://github.com/pytorch/pytorch/pull/126578, I am seeing many issues with dynamo tracing of nn.Parameter construction.  My PR does not do anything special with nn.Parameter construction, it just lets Dynamo trace more code, stress testing nn.Parameter codepath and discovering latent issues.

To repro, fetch this PR - https://github.com/pytorch/pytorch/pull/126578
Run this repro (you can also look at the CI failures, but the most common is the following one)

~~~
import torch


def fn(x):
    mod = torch.nn.Linear(3, 3, bias=False)
    return mod(x)


# fn(torch.rand(4, 4))
opt_fn = torch.compile(fn, backend="eager")
opt_fn(torch.rand(3, 3))
~~~



~~~
V0601 16:44:11.056000 139970430354560 torch/_dynamo/convert_frame.py:603] [0/0] [__bytecode] ORIGINAL BYTECODE fn /data/users/anijain/pytorch2/examples/m1.py line 4
V0601 16:44:11.056000 139970430354560 torch/_dynamo/convert_frame.py:603] [0/0] [__bytecode]   4           0 RESUME                   0
V0601 16:44:11.056000 139970430354560 torch/_dynamo/convert_frame.py:603] [0/0] [__bytecode]
V0601 16:44:11.056000 139970430354560 torch/_dynamo/convert_frame.py:603] [0/0] [__bytecode]   5           2 LOAD_GLOBAL              0 (torch)
V0601 16:44:11.056000 139970430354560 torch/_dynamo/convert_frame.py:603] [0/0] [__bytecode]              14 LOAD_ATTR                1 (nn)
V0601 16:44:11.056000 139970430354560 torch/_dynamo/convert_frame.py:603] [0/0] [__bytecode]              24 LOAD_METHOD              2 (Linear)
V0601 16:44:11.056000 139970430354560 torch/_dynamo/convert_frame.py:603] [0/0] [__bytecode]              46 LOAD_CONST               1 (3)
V0601 16:44:11.056000 139970430354560 torch/_dynamo/convert_frame.py:603] [0/0] [__bytecode]              48 LOAD_CONST               1 (3)
V0601 16:44:11.056000 139970430354560 torch/_dynamo/convert_frame.py:603] [0/0] [__bytecode]              50 LOAD_CONST               2 (False)
V0601 16:44:11.056000 139970430354560 torch/_dynamo/convert_frame.py:603] [0/0] [__bytecode]              52 KW_NAMES                 3
V0601 16:44:11.056000 139970430354560 torch/_dynamo/convert_frame.py:603] [0/0] [__bytecode]              54 PRECALL                  3
V0601 16:44:11.056000 139970430354560 torch/_dynamo/convert_frame.py:603] [0/0] [__bytecode]              58 CALL                     3
V0601 16:44:11.056000 139970430354560 torch/_dynamo/convert_frame.py:603] [0/0] [__bytecode]              68 STORE_FAST               1 (mod)
V0601 16:44:11.056000 139970430354560 torch/_dynamo/convert_frame.py:603] [0/0] [__bytecode]
V0601 16:44:11.056000 139970430354560 torch/_dynamo/convert_frame.py:603] [0/0] [__bytecode]   6          70 PUSH_NULL
V0601 16:44:11.056000 139970430354560 torch/_dynamo/convert_frame.py:603] [0/0] [__bytecode]              72 LOAD_FAST                1 (mod)
V0601 16:44:11.056000 139970430354560 torch/_dynamo/convert_frame.py:603] [0/0] [__bytecode]              74 LOAD_FAST                0 (x)
V0601 16:44:11.056000 139970430354560 torch/_dynamo/convert_frame.py:603] [0/0] [__bytecode]              76 PRECALL                  1
V0601 16:44:11.056000 139970430354560 torch/_dynamo/convert_frame.py:603] [0/0] [__bytecode]              80 CALL                     1
V0601 16:44:11.056000 139970430354560 torch/_dynamo/convert_frame.py:603] [0/0] [__bytecode]              90 RETURN_VALUE
V0601 16:44:11.056000 139970430354560 torch/_dynamo/convert_frame.py:603] [0/0] [__bytecode]
V0601 16:44:11.056000 139970430354560 torch/_dynamo/convert_frame.py:603] [0/0] [__bytecode]
Traceback (most recent call last):
  File "/data/users/anijain/pytorch2/examples/m1.py", line 11, in <module>
    opt_fn(torch.rand(3, 3))
  File "/data/users/anijain/pytorch2/torch/_dynamo/eval_frame.py", line 421, in _fn
    return fn(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^
  File "/data/users/anijain/pytorch2/torch/_dynamo/convert_frame.py", line 1077, in catch_errors
    return callback(frame, cache_entry, hooks, frame_state, skip=1)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/users/anijain/pytorch2/torch/_dynamo/convert_frame.py", line 918, in _convert_frame
    result = inner_convert(
             ^^^^^^^^^^^^^^
  File "/data/users/anijain/pytorch2/torch/_dynamo/convert_frame.py", line 456, in _convert_frame_assert
    return _compile(
           ^^^^^^^^^
  File "/data/users/anijain/pytorch2/torch/_utils_internal.py", line 83, in wrapper_function
    return StrobelightCompileTimeProfiler.profile_compile_time(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/users/anijain/pytorch2/torch/_strobelight/compile_time_profiler.py", line 129, in profile_compile_time
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/home/anijain/local/miniconda3/envs/pytorch2/lib/python3.11/contextlib.py", line 81, in inner
    return func(*args, **kwds)
           ^^^^^^^^^^^^^^^^^^^
  File "/data/users/anijain/pytorch2/torch/_dynamo/convert_frame.py", line 799, in _compile
    guarded_code = compile_inner(code, one_graph, hooks, transform)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/users/anijain/pytorch2/torch/_dynamo/utils.py", line 218, in time_wrapper
    r = func(*args, **kwargs)
        ^^^^^^^^^^^^^^^^^^^^^
  File "/data/users/anijain/pytorch2/torch/_dynamo/convert_frame.py", line 618, in compile_inner
    out_code = transform_code_object(code, transform)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/users/anijain/pytorch2/torch/_dynamo/bytecode_transformation.py", line 1184, in transform_code_object
    transformations(instructions, code_options)
  File "/data/users/anijain/pytorch2/torch/_dynamo/convert_frame.py", line 177, in _fn
    return fn(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^
  File "/data/users/anijain/pytorch2/torch/_dynamo/convert_frame.py", line 564, in transform
    tracer.run()
  File "/data/users/anijain/pytorch2/torch/_dynamo/symbolic_convert.py", line 2455, in run
    super().run()
  File "/data/users/anijain/pytorch2/torch/_dynamo/symbolic_convert.py", line 897, in run
    while self.step():
          ^^^^^^^^^^^
  File "/data/users/anijain/pytorch2/torch/_dynamo/symbolic_convert.py", line 809, in step
    self.dispatch_table[inst.opcode](self, inst)
  File "/data/users/anijain/pytorch2/torch/_dynamo/symbolic_convert.py", line 503, in wrapper
    return inner_fn(self, inst)
           ^^^^^^^^^^^^^^^^^^^^
  File "/data/users/anijain/pytorch2/torch/_dynamo/symbolic_convert.py", line 2063, in CALL
    self.call_function(fn, args, kwargs)
  File "/data/users/anijain/pytorch2/torch/_dynamo/symbolic_convert.py", line 747, in call_function
    self.push(fn.call_function(self, args, kwargs))
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/users/anijain/pytorch2/torch/_dynamo/variables/nn_module.py", line 812, in call_function
    return variables.UserFunctionVariable(fn, source=source).call_function(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/users/anijain/pytorch2/torch/_dynamo/variables/functions.py", line 294, in call_function
    return super().call_function(tx, args, kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/users/anijain/pytorch2/torch/_dynamo/variables/functions.py", line 91, in call_function
    return tx.inline_user_function_return(self, [*self.self_args(), *args], kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/users/anijain/pytorch2/torch/_dynamo/symbolic_convert.py", line 753, in inline_user_function_return
    return InliningInstructionTranslator.inline_call(self, fn, args, kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/users/anijain/pytorch2/torch/_dynamo/symbolic_convert.py", line 2670, in inline_call
    return cls.inline_call_(parent, func, args, kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/users/anijain/pytorch2/torch/_dynamo/symbolic_convert.py", line 2786, in inline_call_
    tracer.run()
  File "/data/users/anijain/pytorch2/torch/_dynamo/symbolic_convert.py", line 897, in run
    while self.step():
          ^^^^^^^^^^^
  File "/data/users/anijain/pytorch2/torch/_dynamo/symbolic_convert.py", line 809, in step
    self.dispatch_table[inst.opcode](self, inst)
  File "/data/users/anijain/pytorch2/torch/_dynamo/symbolic_convert.py", line 503, in wrapper
    return inner_fn(self, inst)
           ^^^^^^^^^^^^^^^^^^^^
  File "/data/users/anijain/pytorch2/torch/_dynamo/symbolic_convert.py", line 1504, in CALL_FUNCTION_EX
    self.call_function(fn, argsvars.items, kwargsvars)
  File "/data/users/anijain/pytorch2/torch/_dynamo/symbolic_convert.py", line 747, in call_function
    self.push(fn.call_function(self, args, kwargs))
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/users/anijain/pytorch2/torch/_dynamo/variables/functions.py", line 342, in call_function
    return super().call_function(tx, args, kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/users/anijain/pytorch2/torch/_dynamo/variables/functions.py", line 294, in call_function
    return super().call_function(tx, args, kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/users/anijain/pytorch2/torch/_dynamo/variables/functions.py", line 91, in call_function
    return tx.inline_user_function_return(self, [*self.self_args(), *args], kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/users/anijain/pytorch2/torch/_dynamo/symbolic_convert.py", line 753, in inline_user_function_return
    return InliningInstructionTranslator.inline_call(self, fn, args, kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/users/anijain/pytorch2/torch/_dynamo/symbolic_convert.py", line 2670, in inline_call
    return cls.inline_call_(parent, func, args, kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/users/anijain/pytorch2/torch/_dynamo/symbolic_convert.py", line 2786, in inline_call_
    tracer.run()
  File "/data/users/anijain/pytorch2/torch/_dynamo/symbolic_convert.py", line 897, in run
    while self.step():
          ^^^^^^^^^^^
  File "/data/users/anijain/pytorch2/torch/_dynamo/symbolic_convert.py", line 809, in step
    self.dispatch_table[inst.opcode](self, inst)
  File "/data/users/anijain/pytorch2/torch/_dynamo/symbolic_convert.py", line 503, in wrapper
    return inner_fn(self, inst)
           ^^^^^^^^^^^^^^^^^^^^
  File "/data/users/anijain/pytorch2/torch/_dynamo/symbolic_convert.py", line 2063, in CALL
    self.call_function(fn, args, kwargs)
  File "/data/users/anijain/pytorch2/torch/_dynamo/symbolic_convert.py", line 747, in call_function
    self.push(fn.call_function(self, args, kwargs))
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/users/anijain/pytorch2/torch/_dynamo/variables/torch.py", line 756, in call_function
    tensor_variable = wrap_fx_proxy(
                      ^^^^^^^^^^^^^^
  File "/data/users/anijain/pytorch2/torch/_dynamo/variables/builder.py", line 1713, in wrap_fx_proxy
    return wrap_fx_proxy_cls(target_cls=TensorVariable, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/users/anijain/pytorch2/torch/_dynamo/variables/builder.py", line 1798, in wrap_fx_proxy_cls
    example_value = get_fake_value(proxy.node, tx, allow_non_graph_fake=True)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/users/anijain/pytorch2/torch/_dynamo/utils.py", line 1775, in get_fake_value
    raise TorchRuntimeError(str(e)).with_traceback(e.__traceback__) from None
  File "/data/users/anijain/pytorch2/torch/_dynamo/utils.py", line 1707, in get_fake_value
    ret_val = wrap_fake_exception(
              ^^^^^^^^^^^^^^^^^^^^
  File "/data/users/anijain/pytorch2/torch/_dynamo/utils.py", line 1222, in wrap_fake_exception
    return fn()
           ^^^^
  File "/data/users/anijain/pytorch2/torch/_dynamo/utils.py", line 1708, in <lambda>
    lambda: run_node(tx.output, node, args, kwargs, nnmodule)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/users/anijain/pytorch2/torch/_dynamo/utils.py", line 1843, in run_node
    raise RuntimeError(make_error_message(e)).with_traceback(
  File "/data/users/anijain/pytorch2/torch/_dynamo/utils.py", line 1825, in run_node
    return node.target(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
torch._dynamo.exc.TorchRuntimeError: Failed running call_function <built-in function linear>(*(FakeTensor(..., size=(3, 3)), FakeTensor(..., size=(3, 3), grad_fn=<Invalid>), None), **{}):
Output 0 of TracableCreateParameterBackward is a view and its base or another view of its base has been modified inplace. This view was created inside a custom Function (or because an input was returned as-is) and the autograd logic to handle view+inplace would override the custom backward associated with the custom Function, leading to incorrect gradients. This behavior is forbidden. You can fix this by cloning the output of the custom Function.

from user code:
   File "/data/users/anijain/pytorch2/examples/m1.py", line 6, in fn
    return mod(x)
  File "/data/users/anijain/pytorch2/torch/nn/modules/module.py", line 1541, in _call_impl
    return forward_call(*args, **kwargs)
  File "/data/users/anijain/pytorch2/torch/nn/modules/linear.py", line 116, in forward
    return F.linear(input, self.weight, self.bias)

Set TORCH_LOGS="+dynamo" and TORCHDYNAMO_VERBOSE=1 for more information


You can suppress this exception and fall back to eager by setting:
    import torch._dynamo
    torch._dynamo.config.suppress_errors = True
~~~~

### Error logs

_No response_

### Minified repro

_No response_

### Versions

NA

cc @ezyang @msaroufim @albanD @bdhirsh @chauhang @voznesenskym @penguinwu @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @chenyang78 @kadeng @zou3519