I have the following sequence of operations:
x = e(s)
y = f(x)
z1 = g1(y)
z2 = g2(y)

The gradient from z1 would backprop through g1, f and e etc. But I want the gradient from z2 to stop at x.
I can do this by using .detach() on x
z1 = g1(f(x))
z2 = g2(f(x.detach()))

But this means that I now make two calls to f. Is there a better way of doing this? Like detaching and copying the relevant part of the computation graph?