I guess if you save every input/output pair for every function, you can do this by doing. If you have b = foo(a) and c = bar(b)
grad_b = autograd.grad(c, b, grad_c)
grad_a = autograd.grad(b, a, grad_b)
And do that for every op if you have more. That will work, but might not be super efficient.