RuntimeError: CUDA error: invalid device ordinal

import torch
import time

a = torch.randn(10000,1000)
b = torch.randn(1000,2000)

device = torch.device(‘cuda:1’)
a =
b =

start_time = time.time()
c = torch.matmul(a,b)
end_time2 = time.time()

Why does this code generate such an error? When I call is available, the output is true, my CUDA version is 10.1, and pytorch version is 1.3.1. I hope to get your help

CUDA devices start counting at 0. You need to use torch.device('cuda:0') or better yet torch.device('cuda'), which uses the currently selected device instead of hard-coding it.

1 Like