So you are applying F.log_softmax(F.tanh(output), 1) on the model outputs?
How do you calculate the mask?
Since you are dividing by the sum of it, it looks like a good starting point to look for exploding loss values.