docstr: 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
docstr_tok: ["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"]
code: 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
  
imports: import math
  from dataclasses import dataclass, field
  from itertools import chain
  import numpy
  import torch
  import torch.nn.functional
  from sklearn.metrics import f1_score
  from sklearn.metrics import matthews_corrcoef
  from scipy.stats import pearsonr, spearmanr
  from fairseq.logging import metrics
  from fairseq.criterions import FairseqCriterion, register_criterion
  from fairseq.dataclass import FairseqDataclass
  from fairseq.logging.meters import safe_round
label_desc: Use image node_id 2 for calling the SentencePredictionCriterion obj's underlying member method code with example usage: obj.forward(model, sample, reduce) and returns: loss,  sample_size,  logging_output