A few things you can check:

Ensure that this output is like what you would expect (ie the scale is same as tgt).




 Hari_Krishnan:

y_hat = nn.functional.log_softmax(y_hat, dim = -1)



I’m not sure what this part below is doing.




 Hari_Krishnan:

loss = cel.masked_select(mask.reshape(-1)).sum()


Instead can this variable loss be removed and simply cel.backward() be used.
I usually go for criterion = nn.CrossEntropyLoss() to avoid confusion.