    def forward(self, model, sample, reduce=True):
        """Compute the loss for the given sample.

        Returns a tuple with three elements:
        1) the loss
        2) the sample size, which is used as the denominator for the gradient
        3) logging outputs to display while training
        """
        assert (
            hasattr(model, "classification_heads")
            and self.classification_head_name in model.classification_heads
        ), "model must provide sentence classification head for --criterion=sentence_prediction"

        logits, _ = model(
            **sample["net_input"],
            features_only=True,
            classification_head_name=self.classification_head_name,
        )
        targets = model.get_targets(sample, [logits]).view(-1)
        sample_size = targets.numel()

        if not self.regression_target:
            lprobs = F.log_softmax(logits, dim=-1, dtype=torch.float32)
            task_loss = F.nll_loss(lprobs, targets, reduction="sum")
        else:
            logits = logits.view(-1).float()
            targets = targets.float()
            task_loss = F.mse_loss(logits, targets, reduction="sum")

        logging_output = {}
        loss = task_loss
        # mha & ffn regularization update
        if (
            hasattr(model, "args")
            and hasattr(model.args, "mha_reg_scale_factor")
            and model.args.mha_reg_scale_factor != 0.0
        ):
            mha_reg_loss = model._get_adaptive_head_loss()
            loss += mha_reg_loss
            logging_output.update({"mha_reg_loss": mha_reg_loss})
        if (
            hasattr(model, "args")
            and hasattr(model.args, "ffn_reg_scale_factor")
            and model.args.ffn_reg_scale_factor != 0.0
        ):
            ffn_reg_loss = model._get_adaptive_ffn_loss()
            loss += ffn_reg_loss
            logging_output.update({"ffn_reg_loss": ffn_reg_loss})

        logging_output.update(
            {
                "loss": loss.data,
                "ntokens": sample["ntokens"],
                "nsentences": sample_size,
                "sample_size": sample_size,
            }
        )
        if not self.regression_target:
            preds = logits.argmax(dim=1)
            logging_output["ncorrect"] = (preds == targets).sum()
        if self.keep_pred_and_targ and not model.training:
            if self.regression_target:
                logging_output["pred"] = logits.detach().cpu().tolist()
                logging_output["targ"] = targets.detach().cpu().tolist()
            else:
                # remove offset `self.label_dict.nspecial` from OffsetTokensDataset
                preds = self.label_dict.string(preds + self.label_dict.nspecial).split()
                targets = self.label_dict.string(
                    targets + self.label_dict.nspecial
                ).split()
                logging_output["pred"] = list(map(int, preds))
                logging_output["targ"] = list(map(int, targets))

            if self.report_mcc:
                logging_output["report_mcc"] = True
            if self.report_acc_and_f1:
                logging_output["report_acc_and_f1"] = True
            if self.report_pearson_and_spearman:
                logging_output["report_pearson_and_spearman"] = True

        return loss, sample_size, logging_output
Compute the loss for the given sample.

Returns a tuple with three elements:
1) the loss
2) the sample size, which is used as the denominator for the gradient
3) logging outputs to display while training
fairseq/criterions/sentence_prediction.py