Your can execute multiple forward passes, calculate different losses, combine them, and calculate the gradients using the accumulated loss, so your code looks alright.
Are you seeing any unexpected behavior using this approach?