Thanks Abhibha for the suggestion.
Indeed that would solve this particular example.
The problem is that I still need net2 to be optimizable. In this minimal example I omitted some parts in the sake of brevity but I have other losses in the network that should behave normally and backpropagate to both net1 and net2.
I just need this one particular loss to not be propagated into net2 as I need the “hidden” tensor to be modified but the non-linear function that net2 computes should not be aware of the jacobian loss.
I guess my confusion is more in the direction of either calling backward() two times, one with the normal loss and and requires grad=True and another time with the jacobian loss and switching to requires grad=False as you suggested.
The other alternative is using hooks but I am not sure if I’m missing something there as using
output.register_hook(lambda grad: torch.zeros_like(grad))
loss.backward()

still propagates gradients into net2.