Setting requires_grad on gpu doesn't work

I want to set requires_grad to True on the input to find out what information is used.
When doing that on the cpu everything is fine, but when using the gpu it doesn’t like that.

RuntimeError: cudnn RNN backward can only be called in training model

here is an example (the model is simplified):

import torch
import torch.nn as nn
sequencelength = 366
inputlength = 202
batchsize = 128
device="cuda:1"
class model(nn.Module):
    def __init__(self,inputlength,device):
        super(model, self).__init__()
        self.lstm=nn.LSTM(inputlength,inputlength).to(device)
        self.linear=nn.Linear(inputlength,1).to(device)
    def forward(self,x):
        x,_=self.lstm(x)
        return self.linear(x[-1])
net=model(inputlength,device)
net.eval()
sequences=torch.randn([sequencelength,batchsize, inputlength]).to(device)
sequences.requires_grad=True
out = net(sequences)
out[0].backward()
data=torch.mean(torch.mean(sequences.grad.data.abs(),dim=0),dim=1).cpu().numpy()

Hi,

You will have to disable cudnn during the rnn computation for this to work I’m afraid.
cudnn does not support double backward. Hence the error you’re seeing.

1 Like