GRUCell does not accept cuda tensors

Apparently GRUCells do not accept CUDA tensors, while GRU RNN do. Is it a known problem or is there something I am doing wrong?

For me, this works:

cell = torch.nn.GRUCell(5,5).cuda()
h = torch.autograd.Variable(torch.rand(1,5)).cuda()
x = torch.autograd.Variable(torch.rand(1,5)).cuda()
cell(x,h)

`Variable containing:
0.3778 -0.1668 0.7313 0.4934 0.0283
[torch.cuda.FloatTensor of size 1x5 (GPU 0)]

I am trying to modify some code that uses GRU rnn to use GRUCells instead (I need to experiment on the output of the cells). In the following code I have variables output and hidden that are given to the GRU rnn without problems; when I try to modify them to sue GRUCell, it does not work unless I call .cpu(). Here is what I do and the error message:

>>> output.size()
torch.Size([1, 1, 256])
>>> hidden.size()
torch.Size([1, 1, 256])
>>> self.gru
GRU(256, 256)
>>> k=self.gru(output, hidden) #no errors
>>> cell = torch.nn.GRUCell(256,256)
>>> o = output.view(1,-1)
>>> h = output.view(1,-1)
>>> o.size()
torch.Size([1, 256])
>>> h.size()
torch.Size([1, 256])
>>> k = cell(o.cpu(), h.cpu()) #no errors
>>> k = cell(o, h)
Traceback (most recent call last):
TypeError: addmm_ received an invalid combination of arguments - got (int, int, torch.cuda.FloatTensor, torch.FloatTensor), but expected one of:
 * (torch.cuda.FloatTensor mat1, torch.cuda.FloatTensor mat2)
 * (torch.cuda.sparse.FloatTensor mat1, torch.cuda.FloatTensor mat2)
 * (float beta, torch.cuda.FloatTensor mat1, torch.cuda.FloatTensor mat2)
 * (float alpha, torch.cuda.FloatTensor mat1, torch.cuda.FloatTensor mat2)
 * (float beta, torch.cuda.sparse.FloatTensor mat1, torch.cuda.FloatTensor mat2)
 * (float alpha, torch.cuda.sparse.FloatTensor mat1, torch.cuda.FloatTensor mat2)
 * (float beta, float alpha, torch.cuda.FloatTensor mat1, torch.cuda.FloatTensor mat2)
      didn't match because some of the arguments have invalid types: (int, int, torch.cuda.FloatTensor, torch.FloatTensor)
 * (float beta, float alpha, torch.cuda.sparse.FloatTensor mat1, torch.cuda.FloatTensor mat2)
      didn't match because some of the arguments have invalid types: (int, int, torch.cuda.FloatTensor, torch.FloatTensor)

As you see with .cpu() it works, if I leave them as cuda tensor this error is raised.

You probably forgot to also send your cell module’s parameters to GPU storage. This should work:

cell = cell.cuda()
k = cell(o, h)
1 Like

Oooh right! Thank you so much! :smiley:

1 Like