Repository: pytorch
Issue: [bug]When the variable in the dataset is of type cuda, calling the dataloader will result in 0
Labels: module: dataloader,triaged,module: data

### 🐛 Describe the bug

```python
import torch
from torch.utils.data import Dataset
# from datasets.base_dataset import BaseDataset
class dataset(Dataset):
    def __init__(self):
        self.class_weights = torch.tensor([0.8373, 0.918, 0.866, 1.0345,
                                           1.0166, 0.9969, 0.9754, 1.0489,
                                           0.8786, 1.0023, 0.9539, 0.9843,
                                           1.1116, 0.9037, 1.0865, 1.0955,
                                           1.0865, 1.1529, 1.0507]).cuda()
    def __getitem__(self, index):
        return index
    def __len__(self):
        return 10
def main():

    datasets = dataset()
    trainloader = torch.utils.data.DataLoader(
        datasets,
        batch_size=2,
        shuffle=True,
        num_workers=1,
        pin_memory=True,
        drop_last=True,
    )
    print(trainloader.dataset.class_weights)
    for _ in trainloader:
        print(trainloader.dataset.class_weights)
if __name__ == '__main__':
    main()
```
Output:
```shell
tensor([0.8373, 0.9180, 0.8660, 1.0345, 1.0166, 0.9969, 0.9754, 1.0489, 0.8786,
        1.0023, 0.9539, 0.9843, 1.1116, 0.9037, 1.0865, 1.0955, 1.0865, 1.1529,
        1.0507], device='cuda:0')
tensor([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       device='cuda:0')
tensor([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       device='cuda:0')
tensor([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       device='cuda:0')
tensor([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       device='cuda:0')
tensor([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       device='cuda:0')
```

### Versions
```
PyTorch version: 1.10.0+cu113
Is debug build: False
CUDA used to build PyTorch: 11.3
ROCM used to build PyTorch: N/A

OS: Microsoft Windows 11 专业版
GCC version: Could not collect
Clang version: Could not collect
CMake version: version 3.29.0
Libc version: N/A

Python version: 3.8.19 (default, Mar 20 2024, 19:55:45) [MSC v.1916 64 bit (AMD64)] (64-bit runtime)
Python platform: Windows-10-10.0.22631-SP0
Is CUDA available: True
CUDA runtime version: 11.8.89
CUDA_MODULE_LOADING set to:
GPU models and configuration: GPU 0: NVIDIA GeForce RTX 3090
Nvidia driver version: 551.23
cuDNN version: C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.8\bin\cudnn_ops_train64_8.dll
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True

CPU:
Architecture=9
CurrentClockSpeed=3500
DeviceID=CPU0
Family=205
L2CacheSize=20480
L2CacheSpeed=
Manufacturer=GenuineIntel
MaxClockSpeed=3500
Name=13th Gen Intel(R) Core(TM) i5-13600KF
ProcessorType=3
Revision=

Versions of relevant libraries:
[pip3] numpy==1.24.4
[pip3] torch==1.10.0+cu113
[pip3] torchaudio==0.10.0+cu113
[pip3] torchvision==0.11.1+cu113
[conda] numpy                     1.24.4                   pypi_0    pypi
[conda] torch                     1.10.0+cu113             pypi_0    pypi
[conda] torchaudio                0.10.0+cu113             pypi_0    pypi
[conda] torchvision               0.11.1+cu113             pypi_0    pypi
```

cc @andrewkho @gokulavasan @SsnL @VitalyFedyunin @dzhulgakov