Changing module attributes doesn't retrigger compilation
Labels: high priority, triaged, bug, oncall: pt2, module: dynamo

### 🐛 Describe the bug

When compiling a method that has an execution flow that depends on some module attribute value, changing this attribute value outside of the method, doesn't retrigger compilation. However, replacing the module attribute with `self.training` or a global variable behaves as expected and recompile when the value changes.

The documentation seems to say that mutating attributes should be fully supported: https://pytorch.org/get-started/pytorch-2.0/#reading-and-updating-attributes

 Repro script:
```python
import torch
from torch import nn

import torch._dynamo

import logging
torch._dynamo.config.log_level = logging.DEBUG
torch._dynamo.config.verbose = True

def check(module, attr: str) -> None:
    inp = torch.ones(1)
    compiled_value = m(inp) 
    eager_value = m._orig_mod(inp)
    prefix = "✅" if (compiled_value == eager_value).all().item() else "❌"
    print(f"{prefix} {attr}={getattr(m._orig_mod, attr)}: compiled={compiled_value}, eager: {eager_value}")

print("=== Foo attribute test ===")
class MyModuleFoo(nn.Module):

    foo: bool

    def __init__(self):
        super().__init__()
        self.foo = True

    def forward(self, x: torch.Tensor) -> torch.Tensor: 
        if self.foo:
            return x * 123
        else:
            return x * 0


m = torch.compile(MyModuleFoo())
check(m, "foo")
m._orig_mod.foo = False
check(m, "foo")

print("=== Training attribute test ===")
class MyModuleTraining(nn.Module):

    def forward(self, x: torch.Tensor) -> torch.Tensor: 
        if self.training:
            return x * 123
        else:
            return x * 0

m = torch.compile(MyModuleTraining())
check(m, "training")
m._orig_mod.training = False
check(m, "training")
```

Output:
```console
=== Foo attribute test ===
[2023-04-12 11:40:57,882] torch._dynamo.eval_frame: [DEBUG] skipping __init__ /Volumes/Data/bin/miniconda3/envs/torch2/lib/python3.8/contextlib.py
[2023-04-12 11:40:57,882] torch._dynamo.eval_frame: [DEBUG] skipping __enter__ /Volumes/Data/bin/miniconda3/envs/torch2/lib/python3.8/contextlib.py
[2023-04-12 11:40:57,883] torch._dynamo.eval_frame: [DEBUG] skipping __init__ /Volumes/Data/bin/miniconda3/envs/torch2/lib/python3.8/contextlib.py
[2023-04-12 11:40:57,883] torch._dynamo.eval_frame: [DEBUG] skipping __enter__ /Volumes/Data/bin/miniconda3/envs/torch2/lib/python3.8/contextlib.py
[2023-04-12 11:40:57,883] torch._dynamo.eval_frame: [DEBUG] skipping enable_dynamic /Volumes/Data/bin/miniconda3/envs/torch2/lib/python3.8/site-packages/torch/_dynamo/eval_frame.py
[2023-04-12 11:40:57,899] torch._dynamo.symbolic_convert: [INFO] Step 1: torchdynamo start tracing forward
[2023-04-12 11:40:57,899] torch._dynamo.symbolic_convert: [DEBUG] TRACE starts_line test_compile_repro.py:27
[2023-04-12 11:40:57,899] torch._dynamo.symbolic_convert: [DEBUG] TRACE LOAD_FAST self []
[2023-04-12 11:40:57,899] torch._dynamo.symbolic_convert: [DEBUG] TRACE LOAD_ATTR foo [NNModuleVariable()]
[2023-04-12 11:40:57,900] torch._dynamo.symbolic_convert: [DEBUG] TRACE POP_JUMP_IF_FALSE 14 [ConstantVariable(bool)]
[2023-04-12 11:40:57,900] torch._dynamo.symbolic_convert: [DEBUG] TRACE starts_line test_compile_repro.py:28
[2023-04-12 11:40:57,900] torch._dynamo.symbolic_convert: [DEBUG] TRACE LOAD_FAST x []
[2023-04-12 11:40:57,900] torch._dynamo.symbolic_convert: [DEBUG] TRACE LOAD_CONST 123 [TensorVariable()]
[2023-04-12 11:40:57,901] torch._dynamo.symbolic_convert: [DEBUG] TRACE BINARY_MULTIPLY None [TensorVariable(), ConstantVariable(int)]
[2023-04-12 11:40:57,909] torch._dynamo.symbolic_convert: [DEBUG] TRACE RETURN_VALUE None [TensorVariable()]
[2023-04-12 11:40:57,909] torch._dynamo.symbolic_convert: [INFO] Step 1: torchdynamo done tracing forward (RETURN_VALUE)
[2023-04-12 11:40:57,909] torch._dynamo.symbolic_convert: [DEBUG] RETURN_VALUE triggered compile
[2023-04-12 11:40:57,909] torch._dynamo.output_graph: [DEBUG] COMPILING GRAPH due to GraphCompileReason(reason='return_value', user_stack=[<FrameSummary file test_compile_repro.py, line 28 in forward>])
[2023-04-12 11:40:57,910] torch._dynamo.output_graph: [INFO] Step 2: calling compiler function debug_wrapper
[2023-04-12 11:40:59,805] torch._inductor.compile_fx: [INFO] Step 3: torchinductor compiling FORWARDS graph 0
[2023-04-12 11:40:59,973] torch._inductor.compile_fx: [INFO] Step 3: torchinductor done compiling FORWARDS graph 0
[2023-04-12 11:40:59,975] torch._dynamo.output_graph: [INFO] Step 2: done compiler function debug_wrapper
[2023-04-12 11:40:59,980] torch._dynamo.eval_frame: [DEBUG] skipping _fn /Volumes/Data/bin/miniconda3/envs/torch2/lib/python3.8/site-packages/torch/_dynamo/eval_frame.py
[2023-04-12 11:40:59,980] torch._dynamo.eval_frame: [DEBUG] skipping nothing /Volumes/Data/bin/miniconda3/envs/torch2/lib/python3.8/site-packages/torch/_dynamo/eval_frame.py
[2023-04-12 11:40:59,981] torch._dynamo.eval_frame: [DEBUG] skipping __exit__ /Volumes/Data/bin/miniconda3/envs/torch2/lib/python3.8/contextlib.py
[2023-04-12 11:40:59,981] torch._dynamo.eval_frame: [DEBUG] skipping __exit__ /Volumes/Data/bin/miniconda3/envs/torch2/lib/python3.8/contextlib.py
✅ foo=True: compiled=tensor([123.]), eager: tensor([123.])
❌ foo=False: compiled=tensor([123.]), eager: tensor([0.])
=== Training attribute test ===
[2023-04-12 11:41:00,039] torch._dynamo.symbolic_convert: [INFO] Step 1: torchdynamo start tracing forward
[2023-04-12 11:41:00,039] torch._dynamo.symbolic_convert: [DEBUG] TRACE starts_line test_compile_repro.py:42
[2023-04-12 11:41:00,039] torch._dynamo.symbolic_convert: [DEBUG] TRACE LOAD_FAST self []
[2023-04-12 11:41:00,039] torch._dynamo.symbolic_convert: [DEBUG] TRACE LOAD_ATTR training [NNModuleVariable()]
[2023-04-12 11:41:00,040] torch._dynamo.symbolic_convert: [DEBUG] TRACE POP_JUMP_IF_FALSE 14 [ConstantVariable(bool)]
[2023-04-12 11:41:00,040] torch._dynamo.symbolic_convert: [DEBUG] TRACE starts_line test_compile_repro.py:43
[2023-04-12 11:41:00,040] torch._dynamo.symbolic_convert: [DEBUG] TRACE LOAD_FAST x []
[2023-04-12 11:41:00,040] torch._dynamo.symbolic_convert: [DEBUG] TRACE LOAD_CONST 123 [TensorVariable()]
[2023-04-12 11:41:00,041] torch._dynamo.symbolic_convert: [DEBUG] TRACE BINARY_MULTIPLY None [TensorVariable(), ConstantVariable(int)]
[2023-04-12 11:41:00,042] torch._dynamo.symbolic_convert: [DEBUG] TRACE RETURN_VALUE None [TensorVariable()]
[2023-04-12 11:41:00,043] torch._dynamo.symbolic_convert: [INFO] Step 1: torchdynamo done tracing forward (RETURN_VALUE)
[2023-04-12 11:41:00,043] torch._dynamo.symbolic_convert: [DEBUG] RETURN_VALUE triggered compile
[2023-04-12 11:41:00,043] torch._dynamo.output_graph: [DEBUG] COMPILING GRAPH due to GraphCompileReason(reason='return_value', user_stack=[<FrameSummary file test_compile_repro.py, line 43 in forward>])
[2023-04-12 11:41:00,044] torch._dynamo.output_graph: [INFO] Step 2: calling compiler function debug_wrapper
[2023-04-12 11:41:00,053] torch._inductor.compile_fx: [INFO] Step 3: torchinductor compiling FORWARDS graph 1
[2023-04-12 11:41:00,061] torch._inductor.compile_fx: [INFO] Step 3: torchinductor done compiling FORWARDS graph 1
[2023-04-12 11:41:00,061] torch._dynamo.output_graph: [INFO] Step 2: done compiler function debug_wrapper
✅ training=True: compiled=tensor([123.]), eager: tensor([123.])
[2023-04-12 11:41:00,064] torch._dynamo.symbolic_convert: [INFO] Step 1: torchdynamo start tracing forward
[2023-04-12 11:41:00,064] torch._dynamo.symbolic_convert: [DEBUG] TRACE starts_line test_compile_repro.py:42
[2023-04-12 11:41:00,064] torch._dynamo.symbolic_convert: [DEBUG] TRACE LOAD_FAST self []
[2023-04-12 11:41:00,064] torch._dynamo.symbolic_convert: [DEBUG] TRACE LOAD_ATTR training [NNModuleVariable()]
[2023-04-12 11:41:00,064] torch._dynamo.symbolic_convert: [DEBUG] TRACE POP_JUMP_IF_FALSE 14 [ConstantVariable(bool)]
[2023-04-12 11:41:00,064] torch._dynamo.symbolic_convert: [DEBUG] TRACE starts_line test_compile_repro.py:45
[2023-04-12 11:41:00,064] torch._dynamo.symbolic_convert: [DEBUG] TRACE LOAD_FAST x []
[2023-04-12 11:41:00,065] torch._dynamo.symbolic_convert: [DEBUG] TRACE LOAD_CONST 0 [TensorVariable()]
[2023-04-12 11:41:00,065] torch._dynamo.symbolic_convert: [DEBUG] TRACE BINARY_MULTIPLY None [TensorVariable(), ConstantVariable(int)]
[2023-04-12 11:41:00,065] torch._dynamo.symbolic_convert: [DEBUG] TRACE RETURN_VALUE None [TensorVariable()]
[2023-04-12 11:41:00,065] torch._dynamo.symbolic_convert: [INFO] Step 1: torchdynamo done tracing forward (RETURN_VALUE)
[2023-04-12 11:41:00,066] torch._dynamo.symbolic_convert: [DEBUG] RETURN_VALUE triggered compile
[2023-04-12 11:41:00,066] torch._dynamo.output_graph: [DEBUG] COMPILING GRAPH due to GraphCompileReason(reason='return_value', user_stack=[<FrameSummary file test_compile_repro.py, line 45 in forward>])
[2023-04-12 11:41:00,066] torch._dynamo.output_graph: [INFO] Step 2: calling compiler function debug_wrapper
[2023-04-12 11:41:00,073] torch._inductor.compile_fx: [INFO] Step 3: torchinductor compiling FORWARDS graph 2
[2023-04-12 11:41:00,082] torch._inductor.compile_fx: [INFO] Step 3: torchinductor done compiling FORWARDS graph 2
[2023-04-12 11:41:00,082] torch._dynamo.output_graph: [INFO] Step 2: done compiler function debug_wrapper
✅ training=False: compiled=tensor([0.]), eager: tensor([0.])
```

### Error logs

_No response_

### Minified repro

_No response_

### Versions

PyTorch version: 2.0.0
Is debug build: False
CUDA used to build PyTorch: None
ROCM used to build PyTorch: N/A

OS: macOS 13.3 (x86_64)
GCC version: Could not collect
Clang version: 14.0.3 (clang-1403.0.22.8)
CMake version: Could not collect
Libc version: N/A

Python version: 3.8.13 (default, Mar 28 2022, 06:16:26)  [Clang 12.0.0 ] (64-bit runtime)
Python platform: macOS-10.16-x86_64-i386-64bit
Is CUDA available: False
CUDA runtime version: No CUDA
CUDA_MODULE_LOADING set to: N/A
GPU models and configuration: No CUDA
Nvidia driver version: No CUDA
cuDNN version: No CUDA
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True

CPU:
Intel(R) Core(TM) i7-8750H CPU @ 2.20GHz

Versions of relevant libraries:
[pip3] flake8==3.9.2
[pip3] flake8-junit-report==2.1.0
[pip3] mypy==0.812
[pip3] mypy-extensions==0.4.3
[pip3] numpy==1.23.3
[pip3] torch==2.0.0
[pip3] torchvision==0.15.1
[conda] numpy                     1.23.3                   pypi_0    pypi
[conda] torch                     2.0.0                    pypi_0    pypi
[conda] torchvision               0.15.1                   pypi_0    pypi


cc @ezyang @gchanan @zou3519 @soumith @msaroufim @wconstab @ngimel @bdhirsh @voznesenskym @penguinwu @anijain2305 @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @Xia-Weiwen @wenzhe-nrv @jiayisunx @desertfire