Skip to content

'TypeError: expected CPU (got CUDA)' when placing a CUDA tensor on a class that is inheriting torch.Tensor #52265

@ProGamerGov

Description

@ProGamerGov

🐛 Bug

Trying to place a CUDA tensor into a class that is inheriting torch.Tensor causes an error.

To Reproduce

Running this code snippet will cause the error:

import torch

class TestTensor(torch.Tensor):
    def test(self):
          print('test')

x = torch.ones(1,3,4,4).cuda()

x_out = TestTensor(x)

And the resulting error message:

---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

<ipython-input-2-3f89b4e74d63> in <module>()
      7 x = torch.ones(1,3,4,4).cuda()
      8 
----> 9 x_out = TestTensor(x)

TypeError: expected CPU (got CUDA)

Expected behavior

Not sure what is supposed to happening in this situation? But I was advised to make an issue post here. @albanD

Environment

Collecting environment information...
PyTorch version: 1.7.0+cu101
Is debug build: True
CUDA used to build PyTorch: 10.1
ROCM used to build PyTorch: N/A

OS: Ubuntu 18.04.5 LTS (x86_64)
GCC version: (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0
Clang version: 6.0.0-1ubuntu2 (tags/RELEASE_600/final)
CMake version: version 3.12.0

Python version: 3.6 (64-bit runtime)
Is CUDA available: True
CUDA runtime version: 10.1.243
GPU models and configuration: GPU 0: Tesla T4
Nvidia driver version: 460.32.03
cuDNN version: /usr/lib/x86_64-linux-gnu/libcudnn.so.7.6.5
HIP runtime version: N/A
MIOpen runtime version: N/A

Versions of relevant libraries:
[pip3] numpy==1.19.5
[pip3] torch==1.7.0+cu101
[pip3] torchsummary==1.5.1
[pip3] torchtext==0.3.1
[pip3] torchvision==0.8.1+cu101
[conda] Could not collect

cc @gchanan @mruberry

Metadata

Metadata

Assignees

No one assigned

    Labels

    module: tensor creationtriagedThis issue has been looked at a team member, and triaged and prioritized into an appropriate module

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions