[torch.compile] torch._dynamo.exc.Unsupported: setattr(UserDefinedObjectVariable) for yolov7
Labels: triaged, bug, oncall: pt2, module: dynamo

### 🐛 Describe the bug

My minimal repro:

```
import torch._dynamo
import torch
import torch.nn as nn


models = nn.Sequential(nn.Linear(3, 3))


@torch._dynamo.optimize("eager", nopython=True)
def run():
    models[0].training = False
    x = torch.randn(1, 3)
    return models(x)


run()
```

stacktrace:
```
Traceback (most recent call last):
  File "test.py", line 39, in <module>
    run()
  File "/home/yj/pytorch/torch/_dynamo/eval_frame.py", line 280, in _fn
    return fn(*args, **kwargs)
  File "/home/yj/pytorch/torch/_dynamo/eval_frame.py", line 433, in catch_errors
    return callback(frame, cache_size, hooks, frame_state)
  File "/home/yj/pytorch/torch/_dynamo/convert_frame.py", line 122, in _fn
    return fn(*args, **kwargs)
  File "/home/yj/pytorch/torch/_dynamo/convert_frame.py", line 355, in _convert_frame_assert
    return _compile(
  File "/home/yj/pytorch/torch/_dynamo/utils.py", line 177, in time_wrapper
    r = func(*args, **kwargs)
  File "/home/yj/pytorch/torch/_dynamo/convert_frame.py", line 425, in _compile
    out_code = transform_code_object(code, transform)
  File "/home/yj/pytorch/torch/_dynamo/bytecode_transformation.py", line 1000, in transform_code_object
    transformations(instructions, code_options)
  File "/home/yj/pytorch/torch/_dynamo/convert_frame.py", line 410, in transform
    tracer.run()
  File "/home/yj/pytorch/torch/_dynamo/symbolic_convert.py", line 2010, in run
    super().run()
  File "/home/yj/pytorch/torch/_dynamo/symbolic_convert.py", line 703, in run
    and self.step()
  File "/home/yj/pytorch/torch/_dynamo/symbolic_convert.py", line 663, in step
    getattr(self, inst.opname)(inst)
  File "/home/yj/pytorch/torch/_dynamo/symbolic_convert.py", line 385, in wrapper
    return inner_fn(self, inst)
  File "/home/yj/pytorch/torch/_dynamo/symbolic_convert.py", line 1095, in CALL_FUNCTION
    self.call_function(fn, args, {})
  File "/home/yj/pytorch/torch/_dynamo/symbolic_convert.py", line 554, in call_function
    self.push(fn.call_function(self, args, kwargs))
  File "/home/yj/pytorch/torch/_dynamo/variables/nn_module.py", line 331, in call_function
    return tx.inline_user_function_return(
  File "/home/yj/pytorch/torch/_dynamo/symbolic_convert.py", line 590, in inline_user_function_return
    result = InliningInstructionTranslator.inline_call(self, fn, args, kwargs)
  File "/home/yj/pytorch/torch/_dynamo/symbolic_convert.py", line 2115, in inline_call
    return cls.inline_call_(parent, func, args, kwargs)
  File "/home/yj/pytorch/torch/_dynamo/symbolic_convert.py", line 2193, in inline_call_
    tracer.run()
  File "/home/yj/pytorch/torch/_dynamo/symbolic_convert.py", line 703, in run
    and self.step()
  File "/home/yj/pytorch/torch/_dynamo/symbolic_convert.py", line 663, in step
    getattr(self, inst.opname)(inst)
  File "/home/yj/pytorch/torch/_dynamo/symbolic_convert.py", line 385, in wrapper
    return inner_fn(self, inst)
  File "/home/yj/pytorch/torch/_dynamo/symbolic_convert.py", line 1135, in CALL_FUNCTION_EX
    self.call_function(fn, argsvars.items, kwargsvars.items)
  File "/home/yj/pytorch/torch/_dynamo/symbolic_convert.py", line 554, in call_function
    self.push(fn.call_function(self, args, kwargs))
  File "/home/yj/pytorch/torch/_dynamo/variables/functions.py", line 306, in call_function
    return super().call_function(tx, args, kwargs)
  File "/home/yj/pytorch/torch/_dynamo/variables/functions.py", line 269, in call_function
    return super().call_function(tx, args, kwargs)
  File "/home/yj/pytorch/torch/_dynamo/variables/functions.py", line 102, in call_function
    return tx.inline_user_function_return(
  File "/home/yj/pytorch/torch/_dynamo/symbolic_convert.py", line 590, in inline_user_function_return
    result = InliningInstructionTranslator.inline_call(self, fn, args, kwargs)
  File "/home/yj/pytorch/torch/_dynamo/symbolic_convert.py", line 2115, in inline_call
    return cls.inline_call_(parent, func, args, kwargs)
  File "/home/yj/pytorch/torch/_dynamo/symbolic_convert.py", line 2193, in inline_call_
    tracer.run()
  File "/home/yj/pytorch/torch/_dynamo/symbolic_convert.py", line 703, in run
    and self.step()
  File "/home/yj/pytorch/torch/_dynamo/symbolic_convert.py", line 663, in step
    getattr(self, inst.opname)(inst)
  File "/home/yj/pytorch/torch/_dynamo/symbolic_convert.py", line 385, in wrapper
    return inner_fn(self, inst)
  File "/home/yj/pytorch/torch/_dynamo/symbolic_convert.py", line 1095, in CALL_FUNCTION
    self.call_function(fn, args, {})
  File "/home/yj/pytorch/torch/_dynamo/symbolic_convert.py", line 554, in call_function
    self.push(fn.call_function(self, args, kwargs))
  File "/home/yj/pytorch/torch/_dynamo/variables/nn_module.py", line 266, in call_function
    tx.call_function(
  File "/home/yj/pytorch/torch/_dynamo/symbolic_convert.py", line 554, in call_function
    self.push(fn.call_function(self, args, kwargs))
  File "/home/yj/pytorch/torch/_dynamo/variables/nn_module.py", line 700, in call_function
    return variables.UserFunctionVariable(
  File "/home/yj/pytorch/torch/_dynamo/variables/functions.py", line 269, in call_function
    return super().call_function(tx, args, kwargs)
  File "/home/yj/pytorch/torch/_dynamo/variables/functions.py", line 102, in call_function
    return tx.inline_user_function_return(
  File "/home/yj/pytorch/torch/_dynamo/symbolic_convert.py", line 590, in inline_user_function_return
    result = InliningInstructionTranslator.inline_call(self, fn, args, kwargs)
  File "/home/yj/pytorch/torch/_dynamo/symbolic_convert.py", line 2115, in inline_call
    return cls.inline_call_(parent, func, args, kwargs)
  File "/home/yj/pytorch/torch/_dynamo/symbolic_convert.py", line 2193, in inline_call_
    tracer.run()
  File "/home/yj/pytorch/torch/_dynamo/symbolic_convert.py", line 703, in run
    and self.step()
  File "/home/yj/pytorch/torch/_dynamo/symbolic_convert.py", line 663, in step
    getattr(self, inst.opname)(inst)
  File "/home/yj/pytorch/torch/_dynamo/symbolic_convert.py", line 385, in wrapper
    return inner_fn(self, inst)
  File "/home/yj/pytorch/torch/_dynamo/symbolic_convert.py", line 1135, in CALL_FUNCTION_EX
    self.call_function(fn, argsvars.items, kwargsvars.items)
  File "/home/yj/pytorch/torch/_dynamo/symbolic_convert.py", line 554, in call_function
    self.push(fn.call_function(self, args, kwargs))
  File "/home/yj/pytorch/torch/_dynamo/variables/misc.py", line 418, in call_function
    return self.obj.call_method(tx, self.name, args, kwargs).add_options(self)
  File "/home/yj/pytorch/torch/_dynamo/variables/nn_module.py", line 753, in call_method
    return super().call_method(tx, name, args, kwargs)
  File "/home/yj/pytorch/torch/_dynamo/variables/user_defined.py", line 272, in call_method
    return UserMethodVariable(
  File "/home/yj/pytorch/torch/_dynamo/variables/functions.py", line 306, in call_function
    return super().call_function(tx, args, kwargs)
  File "/home/yj/pytorch/torch/_dynamo/variables/functions.py", line 269, in call_function
    return super().call_function(tx, args, kwargs)
  File "/home/yj/pytorch/torch/_dynamo/variables/functions.py", line 102, in call_function
    return tx.inline_user_function_return(
  File "/home/yj/pytorch/torch/_dynamo/symbolic_convert.py", line 590, in inline_user_function_return
    result = InliningInstructionTranslator.inline_call(self, fn, args, kwargs)
  File "/home/yj/pytorch/torch/_dynamo/symbolic_convert.py", line 2115, in inline_call
    return cls.inline_call_(parent, func, args, kwargs)
  File "/home/yj/pytorch/torch/_dynamo/symbolic_convert.py", line 2193, in inline_call_
    tracer.run()
  File "/home/yj/pytorch/torch/_dynamo/symbolic_convert.py", line 703, in run
    and self.step()
  File "/home/yj/pytorch/torch/_dynamo/symbolic_convert.py", line 663, in step
    getattr(self, inst.opname)(inst)
  File "/home/yj/pytorch/torch/_dynamo/symbolic_convert.py", line 1189, in STORE_ATTR
    BuiltinVariable(setattr)
  File "/home/yj/pytorch/torch/_dynamo/variables/builtin.py", line 584, in call_function
    result = handler(tx, *args, **kwargs)
  File "/home/yj/pytorch/torch/_dynamo/variables/builtin.py", line 1108, in call_setattr
    unimplemented(
  File "/home/yj/pytorch/torch/_dynamo/exc.py", line 134, in unimplemented
    raise Unsupported(msg)
torch._dynamo.exc.Unsupported: setattr(UserDefinedObjectVariable) <function Module.__setattr__ at 0x7fe00fbd2790>

from user code:
   File "/home/yj/pytorch/torch/nn/modules/module.py", line 1501, in _call_impl
    return forward_call(*args, **kwargs)
  File "test.py", line 16, in forward
    self.training |= self.export

Set torch._dynamo.config.verbose=True or TORCHDYNAMO_VERBOSE=1 for more information
```


It is interesting that if we move `model = Outer()` into the `def run()`, a different error is thrown, which looks like #101030 . My guess on the root cause of this problem is that `torch.compile` doesn't work so well with `nn.Sequential`, and therefore doesn't recognize the inner module as a mutable local variable which then caused the graph break. If we only decorate the inner module by `model.model[0] = dynamo.optimize("eager", nopython=True)model.model[0]`, then the inner module is marked correctly as a mutable local and will not cause a graph break.

### Versions

PyTorch version: 2.0.0a0+gitc263bd4
Is debug build: False
CUDA used to build PyTorch: 11.8
ROCM used to build PyTorch: N/A

OS: Ubuntu 20.04.5 LTS (x86_64)
GCC version: (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0
Clang version: Could not collect
CMake version: version 3.25.0
Libc version: glibc-2.31

Python version: 3.8.15 (default, Nov 24 2022, 15:19:38)  [GCC 11.2.0] (64-bit runtime)
Python platform: Linux-5.15.0-69-generic-x86_64-with-glibc2.17
Is CUDA available: True
CUDA runtime version: 11.8.89
CUDA_MODULE_LOADING set to: LAZY
GPU models and configuration: GPU 0: NVIDIA GeForce RTX 3070
Nvidia driver version: 520.61.05
cuDNN version: Probably one of the following:
/usr/lib/x86_64-linux-gnu/libcudnn.so.8.6.0
/usr/lib/x86_64-linux-gnu/libcudnn_adv_infer.so.8.6.0
/usr/lib/x86_64-linux-gnu/libcudnn_adv_train.so.8.6.0
/usr/lib/x86_64-linux-gnu/libcudnn_cnn_infer.so.8.6.0
/usr/lib/x86_64-linux-gnu/libcudnn_cnn_train.so.8.6.0
/usr/lib/x86_64-linux-gnu/libcudnn_ops_infer.so.8.6.0
/usr/lib/x86_64-linux-gnu/libcudnn_ops_train.so.8.6.0
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True

cc @ezyang @soumith @msaroufim @wconstab @ngimel @bdhirsh @anijain2305 @voznesenskym @penguinwu @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @Xia-Weiwen @wenzhe-nrv @jiayisunx @desertfire