torch.augograd.grad() just returns the gradient. It doesn’t modify the variables/store grad in .grad, as far as I know. You need to set x.requires_grad to indicate the autograd.grad() function that x is a leaf variable.