I tried it on a different model which doesn’t have multiple losses and it is fine. In this case that I add multiple losses, the error comes.

I might have misunderstand the use case. Adding up multiple losses should work, and this is different from running layer-by-layer backward, right? Would I be correct if I assume the code snippet you shared above is adding two losses together instead of doing layer-by-layer backward?
It would be helpful if you could share a minimum repro for this error. Thanks!