RuntimeError: Expected a 'cuda' device type for generator but found 'cpu'

Hello,

I’m getting “RuntimeError: Expected a ‘cuda’ device type for generator but found ‘cpu’” error when I try to iterate over my dataloader created as follows:

transform=transforms.Compose([
                               transforms.Resize(image_size),
                               transforms.CenterCrop(image_size),
                               transforms.ToTensor(),
                               transforms.Normalize(0.5, 0.5),
                           ])
dataset = dset.MNIST(root=dataroot, train=True, download=True, transform=transform)
dataloader = torch.utils.data.DataLoader(dataset, batch_size=batch_size,
                                         shuffle=True, num_workers=workers)

The problem solves if I turn shuffle to False but I would like to mantain it to True. Solutions I’ve found imply to change some pytorch code but I would like to avoid it.

Thanks!

Hi,
Can you please see if this works -

torch.utils.data.DataLoader(
    ...,
    generator=torch.Generator(device='cuda'),
)

It works!

Thank you very much! :slight_smile:

I don’t understand what the issue in the posted code snippet was as it’s working for me locally and it’s not even using the GPU, so which part of the code raised the error?

@srishti-git1110 have you seen this error before being raised in a DataLoader using the CPU only?

I’m sorry I didn’t explain myself properly. The problem only arised when using GPU as device and worked fine when working on CPU.

Hi @ptrblck ,
No, I’ve never run into such an error while using CPU.
Consequently, yes, the posted code doesn’t seem to produce any error.

I just assumed the OP is facing the error when using GPU, hence posted that as the solution. Sorry for not being clear - should’ve mentioned it there.

Not at all. My post wasn’t any criticism as you’ve guessed it perfectly right and @Jorge_Garcia clarified that indeed the GPU was used.

I was just concerned if this might be a known issue of raising CUDA errors when a CPU-only DataLoader is used, but it turns out the code was missing some parts. :wink:

1 Like