hi

you can use or form,
loss_fct = nn.CrossEntropyLoss(reduction=‘none’)
masked_lm_loss = loss_fct(torch.transpose(outputs.logits.cpu().detach(), 1, 2), target_ids)

and then mean over last dim
masked_lm_loss.mean(-1)

you should have 3 positive losses, one for each sentence.