Ho,
So it has nothing to do with np.gradient().
In that case if you create y = f(x). Then you can get the gradient of y wrt to x by doing:
x.requires_grad_()
y = f(x)
grad = torch.autograd.grad(y, x)[0]

Note that if y contains more than one element, you will need to provide a grad_output=v and it will compute the dot product between v and the Jacobian of f. See here 4 for what the Jacobian is.