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

**URL:** <https://discuss.pytorch.org/t/runtimeerror-expected-a-cuda-device-type-for-generator-but-found-cpu/161463>\
**Category:** data\
**Created:** [September 15, 2022, 2:46pm UTC](https://discuss.pytorch.org/t/runtimeerror-expected-a-cuda-device-type-for-generator-but-found-cpu/161463 "2022-09-15T14:46:22Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jorge\_Garcia](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/jorge_garcia/32/48013_2.png) [@Jorge\_Garcia](https://discuss.pytorch.org/u/Jorge_Garcia)\
**Post date:** [September 15, 2022, 2:46pm UTC](https://discuss.pytorch.org/t/runtimeerror-expected-a-cuda-device-type-for-generator-but-found-cpu/161463/1 "2022-09-15T14:46:22Z")

</div>

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:

```auto
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!

---

<div class="post-metadata">

**Author:** ![srishti-git1110](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/srishti-git1110/32/52515_2.png) [@srishti-git1110](https://discuss.pytorch.org/u/srishti-git1110)\
**Post date:** [September 15, 2022, 3:05pm UTC](https://discuss.pytorch.org/t/runtimeerror-expected-a-cuda-device-type-for-generator-but-found-cpu/161463/2 "2022-09-15T15:05:45Z")

</div>

Hi,  
Can you please see if this works -

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

```

---

<div class="post-metadata">

**Author:** ![Jorge\_Garcia](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/jorge_garcia/32/48013_2.png) [@Jorge\_Garcia](https://discuss.pytorch.org/u/Jorge_Garcia)\
**Post date:** [September 15, 2022, 3:26pm UTC](https://discuss.pytorch.org/t/runtimeerror-expected-a-cuda-device-type-for-generator-but-found-cpu/161463/3 "2022-09-15T15:26:54Z")

</div>

It works!

Thank you very much! 🙂

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [September 15, 2022, 8:03pm UTC](https://discuss.pytorch.org/t/runtimeerror-expected-a-cuda-device-type-for-generator-but-found-cpu/161463/4 "2022-09-15T20:03:28Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Jorge\_Garcia](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/jorge_garcia/32/48013_2.png) [@Jorge\_Garcia](https://discuss.pytorch.org/u/Jorge_Garcia)\
**Post date:** [September 15, 2022, 9:03pm UTC](https://discuss.pytorch.org/t/runtimeerror-expected-a-cuda-device-type-for-generator-but-found-cpu/161463/5 "2022-09-15T21:03:52Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![srishti-git1110](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/srishti-git1110/32/52515_2.png) [@srishti-git1110](https://discuss.pytorch.org/u/srishti-git1110)\
**Post date:** [September 16, 2022, 4:54am UTC](https://discuss.pytorch.org/t/runtimeerror-expected-a-cuda-device-type-for-generator-but-found-cpu/161463/6 "2022-09-16T04:54:07Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [September 16, 2022, 5:21am UTC](https://discuss.pytorch.org/t/runtimeerror-expected-a-cuda-device-type-for-generator-but-found-cpu/161463/7 "2022-09-16T05:21:01Z")

</div>

> [@srishti-git1110](#):
>
> 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. 😉

---

<div class="post-metadata">

**Author:** ![Dmitriy\_Lamzin](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/dmitriy_lamzin/32/61976_2.png) [@Dmitriy\_Lamzin](https://discuss.pytorch.org/u/Dmitriy_Lamzin)\
**Post date:** [July 7, 2023, 3:13pm UTC](https://discuss.pytorch.org/t/runtimeerror-expected-a-cuda-device-type-for-generator-but-found-cpu/161463/8 "2023-07-07T15:13:34Z")

</div>

Hello here. I faced the same issue. But looks like the suggestion above will not work:

```auto
/local_disk0/.ephemeral_nfs/envs/pythonEnv-4aa41058-b8da-44ab-8b2d-453f51f9b1ec/lib/python3.8/site-packages/torch/utils/data/dataloader.py in __init__ (self, loader)
    575 shared_rng = torch.Generator()
    576 shared_rng.manual_seed(self._shared_seed)
--> 577 self._dataset = torch.utils.data.graph_settings.apply_random_seed(self._dataset, shared_rng)
    578 self._dataset_kind = loader._dataset_kind
    579 self._IterableDataset_len_called = loader._IterableDataset_len_called

/local_disk0/.ephemeral_nfs/envs/pythonEnv-4aa41058-b8da-44ab-8b2d-453f51f9b1ec/lib/python3.8/site-packages/torch/utils/data/graph_settings.py in apply_random_seed(datapipe, rng)
    151 
    152 for pipe in random_datapipes:
--> 153 random_seed = int(torch.empty((), dtype=torch.int64).random_(generator=rng).item())
    154 pipe.set_seed(random_seed)
    155 

/local_disk0/.ephemeral_nfs/envs/pythonEnv-4aa41058-b8da-44ab-8b2d-453f51f9b1ec/lib/python3.8/site-packages/torch/utils/_device.py in __torch_function__ (self, func, types, args, kwargs)
     60 if func in _device_constructors() and kwargs.get('device') is None:
     61 kwargs['device'] = self.device
---> 62 return func(*args, **kwargs)
     63 
     64 # NB: This is directly called from C++ in torch/csrc/Device.cpp

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

```

Basically here you can see that the generator is initilised inside function right before generating stuff

```auto
453f51f9b1ec/lib/python3.8/site-packages/torch/utils/data/dataloader.py in __init__ (self, loader)
    575 shared_rng = torch.Generator()
    576 shared_rng.manual_seed(self._shared_seed)
--> 577 self._dataset = torch.utils.data.graph_settings.apply_random_seed(self._dataset, shared_rng)

```

In my case it looks more like a bug in the back-compatibility of data pipes from torchdata with DataLoader

---

<div class="post-metadata">

**Author:** ![jamestang0219](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/jamestang0219/32/2308_2.png) [@jamestang0219](https://discuss.pytorch.org/u/jamestang0219)\
**Post date:** [September 5, 2023, 4:17am UTC](https://discuss.pytorch.org/t/runtimeerror-expected-a-cuda-device-type-for-generator-but-found-cpu/161463/9 "2023-09-05T04:17:45Z")

</div>

same problem, it seems the generator will always be initialized using cpu

---

<div class="post-metadata">

**Author:** ![Nikita\_Artemenko](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/nikita_artemenko/32/66486_2.png) [@Nikita\_Artemenko](https://discuss.pytorch.org/u/Nikita_Artemenko)\
**Post date:** [December 25, 2023, 9:29am UTC](https://discuss.pytorch.org/t/runtimeerror-expected-a-cuda-device-type-for-generator-but-found-cpu/161463/10 "2023-12-25T09:29:31Z")

</div>

Alright.  
If you define loader like that:

```auto
    dataloader = DataLoader(
        image_dataset,
        batch_size=batch_size,
        num_workers=num_workers,
        pin_memory=True,
        shuffle=True,
        generator=torch.Generator(device='cuda:0'),
    )

```

And try to use `next(iter(dataloader))`  
You get Error:

```auto
/usr/local/lib/python3.10/dist-packages/torch/utils/data/sampler.py in __iter__ (self)
    163 else:
    164 for _ in range(self.num_samples // n):
--> 165 yield from map(int, torch.randperm(n, generator=generator).numpy())
    166 yield from map(int, torch.randperm(n, generator=generator)[:self.num_samples % n].numpy())
    167 

TypeError: can't convert cuda:0 device type tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first.

```

Seems like instead of numpy there should be .cpu() method in sampler.py

Otherwise, if we don’t specify generator or use ‘cpu’ device it produces error (which is the topic of discussion).

So, what should I do to solve the problem?

---

<div class="post-metadata">

**Author:** ![fdila](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/fdila/32/66594_2.png) [@fdila](https://discuss.pytorch.org/u/fdila)\
**Post date:** [January 7, 2024, 9:05am UTC](https://discuss.pytorch.org/t/runtimeerror-expected-a-cuda-device-type-for-generator-but-found-cpu/161463/11 "2024-01-07T09:05:53Z")

</div>

```auto
/usr/local/lib/python3.10/dist-packages/torch/utils/data/sampler.py in __iter__ (self)
    163 else:
    164 for _ in range(self.num_samples // n):
--> 165 yield from map(int, torch.randperm(n, generator=generator).numpy())
    166 yield from map(int, torch.randperm(n, generator=generator)[:self.num_samples % n].numpy())
    167 

TypeError: can't convert cuda:0 device type tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first.

```

I’m having the same error, did you manage to find a solution?

---

<div class="post-metadata">

**Author:** ![sc0v0ne](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/sc0v0ne/32/67873_2.png) [@sc0v0ne](https://discuss.pytorch.org/u/sc0v0ne)\
**Post date:** [March 27, 2024, 1:13pm UTC](https://discuss.pytorch.org/t/runtimeerror-expected-a-cuda-device-type-for-generator-but-found-cpu/161463/12 "2024-03-27T13:13:07Z")

</div>

Thank you very much!!!

---

<div class="post-metadata">

**Author:** ![Geremia](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/geremia/32/70165_2.png) [@Geremia](https://discuss.pytorch.org/u/Geremia)\
**Post date:** [May 31, 2024, 10:49pm UTC](https://discuss.pytorch.org/t/runtimeerror-expected-a-cuda-device-type-for-generator-but-found-cpu/161463/13 "2024-05-31T22:49:57Z")

</div>

Thanks, that worked.  
I wonder why `torch.set_default_device(torch.device('cuda'))` didn’t tell `torch.utils.data.DataLoader` to use `cuda`. I suppose it only works for setting what tensors should use.  
Is there an analogous command to `set_default_device()` but for `DataLoader`s?

---

<div class="post-metadata">

**Author:** ![raghavm1](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/raghavm1/32/70624_2.png) [@raghavm1](https://discuss.pytorch.org/u/raghavm1)\
**Post date:** [June 20, 2024, 3:25am UTC](https://discuss.pytorch.org/t/runtimeerror-expected-a-cuda-device-type-for-generator-but-found-cpu/161463/14 "2024-06-20T03:25:08Z")

</div>

I’m getting the same error but this doesn’t seem to work for me ☹

Not sure why. I’m using PyTorch 2.3.1. Could this be related to the version of the library?

---

<div class="post-metadata">

**Author:** ![DeerFreak](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/deerfreak/32/70877_2.png) [@DeerFreak](https://discuss.pytorch.org/u/DeerFreak)\
**Post date:** [June 20, 2024, 1:13pm UTC](https://discuss.pytorch.org/t/runtimeerror-expected-a-cuda-device-type-for-generator-but-found-cpu/161463/15 "2024-06-20T13:13:39Z")

</div>

I think you might need to update cuda. This fixed it for me

---

<div class="post-metadata">

**Author:** ![raghavm1](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/raghavm1/32/70624_2.png) [@raghavm1](https://discuss.pytorch.org/u/raghavm1)\
**Post date:** [June 21, 2024, 6:05pm UTC](https://discuss.pytorch.org/t/runtimeerror-expected-a-cuda-device-type-for-generator-but-found-cpu/161463/16 "2024-06-21T18:05:22Z")

</div>

Just solved this issue by removing the line -

`torch.set_default_device('cuda')`

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [June 21, 2024, 8:48pm UTC](https://discuss.pytorch.org/t/runtimeerror-expected-a-cuda-device-type-for-generator-but-found-cpu/161463/17 "2024-06-21T20:48:40Z")

</div>

The used CUDA version (shipped as part of the PyTorch binaries or a local CUDA toolkit if you’ve built PyTorch from source) won’t change the device in your PyTorch code.

---

<div class="post-metadata">

**Author:** ![xuanzera](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/xuanzera/32/76802_2.png) [@xuanzera](https://discuss.pytorch.org/u/xuanzera)\
**Post date:** [May 27, 2025, 2:40am UTC](https://discuss.pytorch.org/t/runtimeerror-expected-a-cuda-device-type-for-generator-but-found-cpu/161463/19 "2025-05-27T02:40:57Z")

</div>

> [@ptrblck](#):
>
> rt of the PyTorch binaries or a local CUDA toolkit if you’ve built PyTorch from source) won’t change t

I faced the same problem. Delete this

```auto
torch.set_default_device('cuda') 

```

(I found this in main.py)  
I think you can find this somewhere!!!

```auto
    generator = torch.Generator(device="cuda").manual_seed(0)
    train_set, val_set = random_split(dataset, [n_train, n_val], generator=generator)
    loader_args = dict(
        batch_size = 1,
        num_workers = os.cpu_count(),
        pin_memory = True if DEVICE.type == 'cuda' else False,
        generator = generator
    )
    train_loader = DataLoader(train_set, shuffle=True, **loader_args)
    val_loader = DataLoader(val_set, shuffle=True, **loader_args)

```

Error:TypeError: can’t convert cuda:0 device type tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first.

```auto
    generator = torch.Generator(device="cpu").manual_seed(0)
    train_set, val_set = random_split(dataset, [n_train, n_val], generator=generator)
    loader_args = dict(
        batch_size = 1,
        num_workers = os.cpu_count(),
        pin_memory = True if DEVICE.type == 'cuda' else False,
        generator = generator
    )
    train_loader = DataLoader(train_set, shuffle=True, **loader_args)
    val_loader = DataLoader(val_set, shuffle=True, **loader_args)

```

TypeError: can’t convert cuda:0 device type tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first.
