That looks like it would work. I would suggest this for a performance speed up:
L1 = f1(out1, out2.detach())
L2 = g1(out1.detach(), out2)

L1.backward()
Opt1.step()
Opt1.zero_grad()

L2.backward()
Opt2.step()
Opt2.zero_grad()

If we detach the tensor, gradients won’t flow through it.