Getting CUDA error even though I'm putting tensors on GPU

Hello. I’m currently trying to run a deep learning model using PyTorch. This is a rather common question, but I’m not able to figure out what the problem is with my code.

Right now, the model block that I’m running looks like this:

self.linear = nn.Sequential(nn.Linear(in_features=2048, out_features=512),
                            nn.ReLU(),
                            nn.Linear(in_features=512, out_features=256),
                            nn.ReLU(),
                            nn.Linear(in_features=256, out_features=128),
                            nn.ReLU(),
                            nn.Linear(in_features=128, out_features=64))

And I have a tensor that’s of shape (10248, 2048).

When I initially ran the program, I got a

*** RuntimeError: Expected object of device type cuda but got device type cpu for argument #1 'self' in call to _th_addmm

I initially thought no big deal, I must have forgotten to put the tensors on GPU. But I noticed I had in fact done that with the tensor = tensor.to('cuda') code. Since this didn’t work I tried changing it to tensor = tensor.cuda() and that still didn’t work.

I tried placing a pdb.set_trace() line to debug it, and after checking the tensor I noticed that it indeed is on GPU. That is, when I print it out, it looks like:

tensor([[0., 0., 0.,  ..., 0., 0., 0.],
        [0., 0., 0.,  ..., 0., 0., 0.],
        [0., 0., 0.,  ..., 0., 0., 0.],
        ...,
        [0., 0., 0.,  ..., 0., 0., 0.],
        [0., 0., 0.,  ..., 0., 0., 0.],
        [0., 0., 0.,  ..., 0., 0., 0.]], device='cuda:0')

What might I have overlooked in this sitaution? Honestly, I’m not sure how to properly interpret the error message either as I can’t find what _th_addmm means. Any tips are appreciated. Thanks in advance.

Did you forget to put the model on GPU?:sweat_smile:

Hello. I wish that were the case, but I also put the model on the GPU with the same method.

Could you post your code here so that we can have a look?
Based on your description, it should work, and we cannot debug any further without seeing the code.

1 Like