Repository: pytorch/pytorch

Issue:
share_memory() on CUDA tensors no longer no-ops and instead crashes in version 1.12.0
### 🐛 Describe the bug

In PyTorch version 1.12.0, `model.share_memory()` on a CUDA tensor no longer no-ops and instead crashes with error `RuntimeError: _share_fd_: only available on CPU`. Version 1.11.0 correctly no-ops

```
import torch

model = torch.nn.Linear(3, 1)
model.to(torch.device('cuda:0'))
model.share_memory()
```

Running above gets the output

```
Traceback (most recent call last):
  File "/home/andrew_huggingface_co/test.py", line 5, in <module>
    model.share_memory()
  File "/opt/conda/envs/fail/lib/python3.9/site-packages/torch/nn/modules/module.py", line 1912, in share_memory
    return self._apply(lambda t: t.share_memory_())
  File "/opt/conda/envs/fail/lib/python3.9/site-packages/torch/nn/modules/module.py", line 602, in _apply
    param_applied = fn(param)
  File "/opt/conda/envs/fail/lib/python3.9/site-packages/torch/nn/modules/module.py", line 1912, in <lambda>
    return self._apply(lambda t: t.share_memory_())
  File "/opt/conda/envs/fail/lib/python3.9/site-packages/torch/_tensor.py", line 515, in share_memory_
    self.storage().share_memory_()
  File "/opt/conda/envs/fail/lib/python3.9/site-packages/torch/storage.py", line 595, in share_memory_
    self._storage.share_memory_()
  File "/opt/conda/envs/fail/lib/python3.9/site-packages/torch/storage.py", line 194, in share_memory_
    self._share_fd_cpu_()
RuntimeError: _share_fd_: only available on CPU
```

The problem seems to be in the `_StorageBase.share_memory_` function in `storage.py`. `self.is_cuda` is being evaluated as False which then executes `self._share_fd_cpu_()`

### Versions

Collecting environment information...
PyTorch version: 1.12.0
Is debug build: False
CUDA used to build PyTorch: 10.2
ROCM used to build PyTorch: N/A

OS: Debian GNU/Linux 10 (buster) (x86_64)
GCC version: (Debian 8.3.0-6) 8.3.0
Clang version: Could not collect
CMake version: version 3.13.4
Libc version: glibc-2.28

Python version: 3.9.13 | packaged by conda-forge | (main, May 27 2022, 16:58:50)  [GCC 10.3.0] (64-bit runtime)
Python platform: Linux-4.19.0-20-cloud-amd64-x86_64-with-glibc2.28
Is CUDA available: True
CUDA runtime version: 11.3.109
GPU models and configuration: GPU 0: Tesla T4
Nvidia driver version: 470.57.02
cuDNN version: Could not collect
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True

Versions of relevant libraries:
[pip3] numpy==1.22.3
[pip3] torch==1.12.0
[pip3] torchvision==0.13.0
[conda] blas                      1.0                         mkl
[conda] cudatoolkit               10.2.89             h713d32c_10    conda-forge
[conda] ffmpeg                    4.3                  hf484d3e_0    pytorch
[conda] mkl                       2021.4.0           h06a4308_640
[conda] mkl-service               2.4.0            py39h7e14d7c_0    conda-forge
[conda] mkl_fft                   1.3.1            py39h0c7bc48_1    conda-forge
[conda] mkl_random                1.2.2            py39hde0f152_0    conda-forge
[conda] numpy                     1.22.3           py39he7a7128_0
[conda] numpy-base                1.22.3           py39hf524024_0
[conda] pytorch                   1.12.0          py3.9_cuda10.2_cudnn7.6.5_0    pytorch
[conda] pytorch-mutex             1.0                        cuda    pytorch
[conda] torchvision               0.13.0               py39_cu102    pytorch

cc @ezyang @gchanan @zou3519


Discussion:
cc @kurtamohler 

Resolving patch:
diff --git a/torch/storage.py b/torch/storage.py
--- a/torch/storage.py
+++ b/torch/storage.py
@@ -16,7 +16,6 @@
 T = TypeVar('T', bound='Union[_StorageBase, _TypedStorage]')
 class _StorageBase(object):
     _cdata: Any
-    is_cuda: bool = False
     is_sparse: bool = False
     is_sparse_csr: bool = False
     device: torch.device
@@ -65,6 +64,8 @@ def _new_shared_cuda(cls, *args, **kwargs) -> T: ...  # noqa: E704
     def _shared_incref(self, *args, **kwargs): ...  # noqa: E704
     @classmethod
     def _free_weak_ref(cls, *args, **kwargs): ...  # noqa: E704
+    @property
+    def is_cuda(self): ...  # noqa: E704
 
     def __str__(self):
         data_str = ' ' + '\n '.join(str(self[i]) for i in range(self.size()))
@@ -213,6 +214,9 @@ def _untyped(self):
 class _UntypedStorage(torch._C.StorageBase, _StorageBase):
     pass
 
+    @property
+    def is_cuda(self):
+        return self.device.type == 'cuda'
 
 def _load_from_bytes(b):
     return torch.load(io.BytesIO(b))
@@ -428,7 +432,7 @@ def __init__(self, *args, device=None, dtype=None, wrap_storage=None):
 
     @property
     def is_cuda(self):
-        return self._storage.device.type == 'cuda'
+        return self.device.type == 'cuda'
 
     def _untyped(self):
         return self._storage


Test patch:
diff --git a/test/test_torch.py b/test/test_torch.py
--- a/test/test_torch.py
+++ b/test/test_torch.py
@@ -265,6 +265,14 @@ def test_set_storage(self, device, dtype):
                 error_storage = a.to(error_dtype).storage()
                 b = torch.tensor([], device=device, dtype=dtype).set_(error_storage)
 
+    @onlyCUDA
+    def test_module_share_memory(self):
+        # Test fix for issue #80733
+        # See https://github.com/pytorch/pytorch/issues/80733
+        model = torch.nn.Linear(3, 1)
+        model_cuda = model.to('cuda')
+        model.share_memory()
+
     @dtypes(torch.float32, torch.complex64)
     def test_deepcopy(self, device, dtype):
         from copy import deepcopy