Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

pytorch

I am trying to train PeleeNet pytorch and got the following error

enter image description here

train.py line 80 enter image description here

pelee_voc train configuration enter image description here

like image 213
Fadwa Avatar asked Jan 21 '26 16:01

Fadwa


2 Answers

Reading the link provided in @Dwijay 's answer, I found an answer that does not require you to do any source code change. Indeed, it is very dangerous I would say to change PyTorch source code. But the idea of modifying the Generator is the good one. Indeed by default the random number generator generates numbers on CPU, but we want them on GPU.

Therefore, one should actually modify the data loader instantiation to fit the use of the default cuda device. This is highlighted in this GitHub comment:

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

This fix worked for me in PyTorch 1.11 (and worked for this other user in PyTorch 1.10).

like image 57
Zaccharie Ramzi Avatar answered Jan 23 '26 05:01

Zaccharie Ramzi


Turning the shuffle parameter off in the dataloader solved it. Got the answer form here.

like image 30
Fadwa Avatar answered Jan 23 '26 07:01

Fadwa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!