One of the differentiated Tensors appears to not have been used in the graph

I am running a one layer linear network. I want to use autograd.grad to calculate the gradients of output with respect to input. However I get the “One of the differentiated Tensors appears to not have been used in the graph” error when running as follows:

input = input_list[i] # input coming from cpu
input_cuda = Variable(input.cuda(“cuda:1”), requires_grad=True)
output = model(input_cuda)
grad = torch.autograd.grad(output, input_cuda, torch.ones_like(output), create_graph=True)

I’m wondering what is wrong with this code.
Thanks.