In your current approach you are adding second_loss and third_loss as Python floats to the total_loss (detached floating point constants), which won’t have any effect on the backward pass.
I’m not familiar with your use case, but if you want these losses to participate in the gradient calculation, you shouldn’t call .item() on them.
Are you running out of memory, if you are adding these losses directly to total_loss, which would be weird as I would assume they should use very little memory compared to the complete model and the intermediate tensors (but it also depends, which criterion you are using)?