PyTorch’s autograd is able to compute the gradients for most of the functions.
Have a look at the Autograd Tutorial 1.5k.
It’s a nice explanation, how autograd works. Also, your code snippet is missing!