dearn44
dearn44

Reputation: 3420

Correctly converting a NumPy array to a PyTorch tensor running on the gpu

I have created a DataLoader that looks like this

class ToTensor(object):
    def __call__(self, sample):
        return torch.from_numpy(sample).to(device)

class MyDataset(Dataset):
    def __init__(self, data, transform=None):
        self.data = data
        self.transform = transform

    def __len__(self):
        return len(self.data)

    def __getitem__(self, idx):
        sample = self.data[idx, :]

        if self.transform:
            sample = self.transform(sample)

        return sample

I am using this data loader like so

dataset = MLBDataset(
        data=data,
        transform=transforms.Compose([
            ToTensor()
        ]))
dataloader = DataLoader(dataset, batch_size=BATCH_SIZE, shuffle=True, num_workers=4)
dataiter = iter(dataloader)
x = dataiter.next()

This fails with the message

THCudaCheck FAIL file=/opt/conda/conda-bld/pytorch_1549628766161/work/aten/src/THC/THCGeneral.cpp line=55 error=3 : initialization error
THCudaCheck FAIL file=/opt/conda/conda-bld/pytorch_1549628766161/work/aten/src/THC/THCGeneral.cpp line=55 error=3 : initialization error
THCudaCheck FAIL file=/opt/conda/conda-bld/pytorch_1549628766161/work/aten/src/THC/THCGeneral.cpp line=55 error=3 : initialization error
THCudaCheck FAIL file=/opt/conda/conda-bld/pytorch_1549628766161/work/aten/src/THC/THCGeneral.cpp line=55 error=3 : initialization error
...
    torch._C._cuda_init()
RuntimeError: cuda runtime error (3) : initialization error at /opt/conda/conda-bld/pytorch_1549628766161/work/aten/src/THC/THCGeneral.cpp:55

For the return command inside ToTensor(), in fact any attempt to move the tensor te the GPU will fail inside that class. I have tried:

a = np.array([[[1, 2, 3, 4], [5, 6, 7, 8], [25, 26, 27, 28]],
             [[11, 12, np.nan, 14], [15, 16, 17, 18], [35, 36, 37, 38]]])
print(torch.from_numpy(a).to(device))

inside the body of __call__ in ToTensor() and it fails with the same message, whereas it succeeds everywhere else.

Why is this error generated and how can I resolve this?

Upvotes: 4

Views: 8351

Answers (2)

trsvchn
trsvchn

Reputation: 8981

According to link this might be related to multiprocessing issues. You can find the following workaround.

Upvotes: 1

trsvchn
trsvchn

Reputation: 8981

Try this one:

Code:

import numpy as np
import torch
import torch.nn as nn

torch.cuda.set_device(0)

X = np.ones((1, 10), dtype=np.float32)
print(type(X), X)
X = torch.from_numpy(X).cuda(0)
print(type(X), X)

model = nn.Linear(10, 10).cuda(0)
Y = model(X)
print(type(Y), Y)

Output:

<class 'numpy.ndarray'> [[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]]
<class 'torch.Tensor'> tensor([[1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]], device='cuda:0')
<class 'torch.Tensor'> tensor([[ 0.4867, -1.0050,  0.4872, -0.0260, -0.0788,  0.0161,  1.2210, -0.3957,
          0.2097,  0.2296]], device='cuda:0', grad_fn=<AddmmBackward>)

Upvotes: 1

Related Questions