Hi,
I am afraid this will be tricky as autograd does not build a single formula but compute the gradient as a chain of operations.
You can use things like https://github.com/szagoruyko/pytorchviz/ 3 to see what is the set of functions that will be executed in the backward pass. But you will have to manually “unpack” these and write down the formula for each and then combine all of them together.
Note that if you want to make sure your custom Function (with the manual gradient) computes the right thing, you can use autograd.gradcheck().