Based on the error message, it seems pred_flat and mask_flat cause this error, since the batch sizes differ.
Could you check the shapes of these tensors before passing them to the criterion and also before reshaping them?