Doubt in creating a loss variable

I have multiple losses: Say lossA, lossB
I have an option to configure which losses to pick for training.
My code for getting total loss is:

lossA = 0
lossB = 0
if config[‘use_lossA’]:
lossA = getLossA(input, output)
if config[‘use_lossB’]:
lossB = getLossB(input, output)

totalLoss = lossA + lossB
if training:
//optimiser clear
//optimiser related step

  1. totalLoss might not always be a variable, if both losses are disabled (which won’t generally happen). But for safe keeping, what should be the best method to combine these losses?
    Is total = Variable(lossA +lossB, requires_grad=True) okay?

  2. Is it okay to do backward on total loss?

As a workaround, you can have a switch as below:

if not totalLoss == 0: