How can I use different losses to update different branches respectively, and sum grad together to update master(main) branch

I‘ve seen several similar problems.’https://discuss.pytorch.org/t/how-to-train-the-network-with-multiple-branches/2152. So I don’t think it is out of the box of pytorch, and I would like to double check whether it works.