4

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?

5
  • github.com/pytorch/pytorch/issues/1666. Look at apaszke answer. Commented Feb 19, 2019 at 19:36
  • First check if CUDA works: import torch x = torch.randn(2, 2, device='cuda:0') print(x) Commented Feb 19, 2019 at 19:41
  • @TarasSavchyn I think CUDA works without issue, at least for any other tests that I have performed. Commented Feb 19, 2019 at 19:48
  • I see.. Maybe try this one: X = torch.from_numpy(X).cuda("cuda:0") Commented Feb 19, 2019 at 19:48
  • @TarasSavchyn I completely re-wrote the question hopefully making it a lot clearer, it was not an issue of not having the correct device unfortunately. Commented Feb 19, 2019 at 20:55

2 Answers 2

1

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>)
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.