The error means not all the tensors for which requires_grad=True are used in gradient computation. An example of this would be
a = torch.randn(3, 4, requires_grad=True)
b = torch.randn(3, 4, requires_grad=True)
loss = a.sum()
torch.autograd.grad(loss, (a,b))

So try setting allow_unused=True and it should work.