Your approach would work out of the box in PyTorch.
You could just add both of your losses and call backward on it.
This will make sure that the tails of your model will use the appropriate gradients, while the common part uses the accumulated gradient.