However, it would be nice to know if there is a more general alternative, that works also for non-linear cases. In other words, it would be interesting to know how to perform the following calculation, but reducing to a single invocation of f(x,y), instead of two:
l1 = u(f(x.detach(), y))
l1.backward()
l2 = v(f(x, y.detach()))
l2.backward()

Here, u and v are two functions.