Repository: pytorch
Issue: Bug in `torch.compile` with standard type checking tools beartype
Labels: triaged,OSS contribution wanted,oncall: pt2,module: dynamo

### 🐛 Describe the bug

Reproduction script: 

```python
import torch 
from torch import nn
from beartype import beartype 
from jaxtyping import Float

class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.conv1 = nn.Conv2d(1, 20, 5)
        self.conv2 = nn.Conv2d(20, 20, 5)

    def forward(self, x):
        x = nn.functional.relu(self.conv1(x))
        return nn.functional.relu(self.conv2(x))
    

class BearNet(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.conv1 = nn.Conv2d(1, 20, 5)
        self.conv2 = nn.Conv2d(20, 20, 5)

    @beartype
    def forward(self, x):
        x = nn.functional.relu(self.conv1(x))
        return nn.functional.relu(self.conv2(x))
    
if __name__ == "__main__":
    net = Net()
    x = torch.randn(1, 1, 28, 28)
    y = net(x)
    print(y)

    model = torch.compile(net)
    y = model(x)
    print(y)

    net = BearNet()
    x = torch.randn(1, 1, 28, 28)
    y = net(x)
    print('with beartype: ', y)

    model = torch.compile(net)
    y = model(x)
    print('with beartype: ', y)
```

First this throws an error that asks for installing something (Mac OS 14):

```
OpenMP support not found. Please try one of the following solutions:
(1) Set the `CXX` environment variable to a compiler other than Apple clang++/g++ that has builtin OpenMP support;
(2) install OpenMP via conda: `conda install llvm-openmp`;
(3) install libomp via brew: `brew install libomp`;
(4) manually setup OpenMP and set the `OMP_PREFIX` environment variable to point to a path with `include/omp.h` under it.
```

After installing libomp: `brew install libomp`, this works. 

However, then this next reproduction script breaks: 

```
import torch 
from torch import nn
from beartype import beartype 
from jaxtyping import Float
from torch import Tensor

class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.conv1 = nn.Conv2d(1, 20, 5)
        self.conv2 = nn.Conv2d(20, 20, 5)

    def forward(self, x):
        x = nn.functional.relu(self.conv1(x))
        return nn.functional.relu(self.conv2(x))
    

class BearNet(nn.Module):
    def __init__(self):
        super(BearNet, self).__init__()
        self.conv1 = nn.Conv2d(1, 20, 5)
        self.conv2 = nn.Conv2d(20, 20, 5)

    @beartype
    def forward(self, x):
        x = nn.functional.relu(self.conv1(x))
        return nn.functional.relu(self.conv2(x))
    

class BearNetJaxTyped(nn.Module):
    def __init__(self):
        super(BearNetJaxTyped, self).__init__()
        self.conv1 = nn.Conv2d(1, 20, 5)
        self.conv2 = nn.Conv2d(20, 20, 5)

    @beartype
    def forward(self, x: Float[Tensor, "1 1 28 28"]):
        x = nn.functional.relu(self.conv1(x))
        return nn.functional.relu(self.conv2(x))

if __name__ == "__main__":
    net = Net()
    x = torch.randn(1, 1, 28, 28)
    y = net(x)
    print(y)

    model = torch.compile(net)
    y = model(x)
    print(y)

    net = BearNet()
    x = torch.randn(1, 1, 28, 28)
    y = net(x)
    print('with beartype: ', y)

    model = torch.compile(net)
    y = model(x)
    print('with beartype: ', y)

    net = BearNetJaxTyped()
    x = torch.randn(1, 1, 28, 28)
    y = net(x)
    print('with beartype: ', y)

    model = torch.compile(net)
    y = model(x)
    print('with beartype: ', y)
```

Results:

```
  File "/Users/me/projects/phare-sandbox/.venv/lib/python3.11/site-packages/torch/_dynamo/guards.py", line 235, in get
    return eval(name, self.scope, CLOSURE_VARS)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<string>", line 1, in <module>
torch._dynamo.exc.InternalTorchDynamoError: '_thread._local' object has no attribute 'value'


You can suppress this exception and fall back to eager by setting:
    import torch._dynamo
    torch._dynamo.config.suppress_errors = True
```

This script runs fine when disabling beartype:

```
PYTHONOPTIMIZE=1 python test_compile.py
... prints out tensors
```

This is also an issue on Ubuntu virtual machines (e.g. on AWS), and on Jupyter notebooks; workaround for now: https://github.com/beartype/beartype/issues/341 

Discussion: https://github.com/beartype/beartype/issues/343

From @justinchuby : 

```quote
If I have to guess, is it this line?

https://github.com/pytorch/pytorch/blob/6d9588a12b5834f29bd8970936749a9725e7f609/torch/_dynamo/guards.py#L316

t = type(self.get(guard.name))

and somehow in eval(name, self.scope, CLOSURE_VARS) it is trying to access value which does not exist?
```

### Versions

```
❯ python collect_env.py 
Collecting environment information...
PyTorch version: 2.1.2
Is debug build: False
CUDA used to build PyTorch: None
ROCM used to build PyTorch: N/A

OS: macOS 14.4 (arm64)
GCC version: Could not collect
Clang version: 17.0.6
CMake version: Could not collect
Libc version: N/A

Python version: 3.11.5 (main, Sep 11 2023, 08:31:25) [Clang 14.0.6 ] (64-bit runtime)
Python platform: macOS-14.4-arm64-arm-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:
Apple M3 Pro

Versions of relevant libraries:
[pip3] mypy-extensions==1.0.0
[pip3] numpy==1.26.2
[pip3] torch==2.1.2
[pip3] torchaudio==2.1.2
[pip3] torcheval==0.0.7
[pip3] torchvision==0.16.2
[conda] No relevant packages
```

cc @ezyang @msaroufim @bdhirsh @anijain2305 @zou3519 @chauhang @voznesenskym @penguinwu @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @chenyang78 @kadeng