Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AttributeError: module 'torch' has no attribute "device"

Tags:

python

pytorch

---> 13 device = torch.device({"cuda"} if torch.cuda.is_available() else {"cpu"})
     14
     15

AttributeError: module 'torch' has no attribute 'device'

I'm 99% sure this is because I didn't upgrade pytorch from 0.31 to 0.4 however I can't upgrade pytorch for now.

I need to translate .device (0.4) to something that works in 0.31.

I check the migration document however it doesn't provide how I can convert torch.device in retrospect. Please help!

like image 557
aerin Avatar asked Aug 24 '26 01:08

aerin


1 Answers

torch.cuda.device() is a context manager.

torch.cuda.set_device(0)
# On device 0
with torch.cuda.device(1):
    print("Inside device is 1")    
    # On device 1
print("Outside is still 0")
# On device 0

And the above works from 0.2 version.

like image 71
Wasi Ahmad Avatar answered Aug 25 '26 16:08

Wasi Ahmad



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!