Repository: pytorch
Issue: The real AttributeError information
Labels: module: nn,triaged

### 🐛 Describe the bug

subclass of nn.Module can't show correct attribute error message. Just see the code:
```python
import torch.nn as nn

class ModelOptions:
    def __init__(self, idx, name):
        self.idx = idx
        self._name = name


MODEL_OPT = ModelOptions(1, "unet")

class SomeModel(nn.Module):
    def __init__(self):
        super(SomeModel, self).__init__()
        self.model1 = None
        self.model2 = None

    @property
    def inner_model(self):
        if MODEL_OPT.idx == 1:
            print("Using Model {}".format(MODEL_OPT.name))  # Note: here i typed wrong  `name` on purpose
            return self.model1

model = SomeModel()
print(model.inner_model)  # Note: the bug is here, it should print 'MODEL_OPT' object has no attribute 'name', NOT AttributeError: 'SomeModel' object has no attribute 'inner_model'
```


### Versions

PyTorch version: 2.3.1+cu121
Is debug build: False
CUDA used to build PyTorch: 12.1
ROCM used to build PyTorch: N/A

OS: Microsoft Windows 11 专业版
GCC version: (Rev5, Built by MSYS2 project) 13.2.0
Clang version: 17.0.6
CMake version: version 3.28.3
Libc version: N/A

Python version: 3.11.8 | packaged by Anaconda, Inc. | (main, Feb 26 2024, 21:34:05) [MSC v.1916 64 bit (AMD64)] (64-bit runtime)
Python platform: Windows-10-10.0.22631-SP0
Is CUDA available: True
CUDA runtime version: 12.3.107
CUDA_MODULE_LOADING set to: LAZY
GPU models and configuration: GPU 0: NVIDIA GeForce RTX 4070 Ti SUPER
Nvidia driver version: 560.70
cuDNN version: C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.3\bin\cudnn_ops_train64_8.dll
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True

CPU:
Architecture=9
CurrentClockSpeed=3400
DeviceID=CPU0
Family=198
L2CacheSize=24576
L2CacheSpeed=
Manufacturer=GenuineIntel
MaxClockSpeed=3400
Name=13th Gen Intel(R) Core(TM) i7-13700K
ProcessorType=3
Revision=

Versions of relevant libraries:
[pip3] clip-anytorch==2.6.0
[pip3] dctorch==0.1.2
[pip3] ema-pytorch==0.5.1
[pip3] numpy==1.26.2
[pip3] onnx==1.15.0
[pip3] onnxruntime-gpu==1.17.1
[pip3] open-clip-torch==2.20.0
[pip3] optree==0.12.1
[pip3] pytorch-fid==0.3.0
[pip3] pytorch-lightning==1.9.4
[pip3] torch==2.3.1+cu121
[pip3] torchaudio==2.3.1+cu121
[pip3] torchdiffeq==0.2.3
[pip3] torchmetrics==1.3.1
[pip3] torchsde==0.2.6
[pip3] torchvision==0.18.1
[conda] blas                      1.0                         mkl
[conda] clip-anytorch             2.6.0                    pypi_0    pypi
[conda] dctorch                   0.1.2                    pypi_0    pypi
[conda] ema-pytorch               0.5.1                    pypi_0    pypi
[conda] mkl                       2021.4.0                 pypi_0    pypi
[conda] mkl-service               2.4.0           py311h2bbff1b_1
[conda] mkl_fft                   1.3.8           py311h2bbff1b_0
[conda] mkl_random                1.2.4           py311h59b6b97_0
[conda] numpy                     1.26.2                   pypi_0    pypi
[conda] open-clip-torch           2.20.0                   pypi_0    pypi
[conda] optree                    0.12.1                   pypi_0    pypi
[conda] pytorch-cuda              12.1                 hde6ce7c_5    pytorch
[conda] pytorch-fid               0.3.0                    pypi_0    pypi
[conda] pytorch-lightning         1.9.4                    pypi_0    pypi
[conda] pytorch-mutex             1.0                        cuda    pytorch
[conda] torch                     2.3.1+cu121              pypi_0    pypi
[conda] torchaudio                2.3.1+cu121              pypi_0    pypi
[conda] torchdiffeq               0.2.3                    pypi_0    pypi
[conda] torchmetrics              1.3.1                    pypi_0    pypi
[conda] torchsde                  0.2.6                    pypi_0    pypi
[conda] torchvision               0.18.1                   pypi_0    pypi

cc @albanD @mruberry @jbschlosser @walterddr @mikaylagawarecki