class_docstr: This criterion relies on the model to supply losses.
  The losses should be a dictionary of name -> scalar returned by
  the model either by including it in the net_output dict or by
  implementing a get_losses(net_output, sample) method. The final loss is
  a scaled sum of all losses according to weights in loss_weights.
  If no weights are provided, then all losses are scaled by 1.0.
  
  The losses will be automatically logged. Additional keys from
  net_output dict can be logged via the log_keys parameter.
class_docstr_tok: ["This","criterion","relies","on","the","model","to","supply","losses",".","The","losses","should","be","a","dictionary","of","name","-",">","scalar","returned","by","the","model","either","by","including","it","in","the","net_output","dict","or","by","implementing","a","get_losses","(","net_output",",","sample",")","method",".","The","final","loss","is","a","scaled","sum","of","all","losses","according","to","weights","in","loss_weights",".","If","no","weights","are","provided",",","then","all","losses","are","scaled","by","1.0",".","The","losses","will","be","automatically","logged",".","Additional","keys","from","net_output","dict","can","be","logged","via","the","log_keys","parameter","."]
code: def forward(self, model, sample, reduce=True):
      net_output = model(**sample["net_input"])
  
      scaled_losses = {}
  
      if hasattr(model, "get_losses"):
          losses = model.get_losses(net_output, sample)
      elif isinstance(net_output, dict) and "losses" in net_output:
          losses = net_output["losses"]
      else:
          raise Exception("Could not retrieve losses")
  
      for lk, p in losses.items():
          try:
              coef = (
                  1.0
                  if len(self.loss_weights) == 0
                  else self.loss_weights[lk]
              )
          except KeyError:
              logger.error(
                  f"weight for loss {lk} is not in loss_weights ({self.loss_weights})"
              )
              raise
          if coef != 0 and p is not None:
              scaled_losses[lk] = coef * p.float().sum()
  
      loss = sum(scaled_losses.values())
  
      if "sample_size" in net_output:
          sample_size = net_output["sample_size"]
      else:
          sample_size = loss.numel()
  
      if reduce and loss.numel() > 1:
          loss = loss.sum()
  
      logging_output = {
          "loss": loss.data,
          "ntokens": sample_size,
          "nsentences": sample["id"].numel(),
          "sample_size": sample_size,
          "_world_size": 1,
      }
  
      for lk in self.log_keys:
          if lk in net_output and net_output[lk] is not None:
              if (
                  not torch.is_tensor(net_output[lk])
                  or net_output[lk].numel() == 1
              ):
                  logging_output[lk] = float(net_output[lk])
              elif lk.startswith("_"):
                  logging_output[lk] = net_output[lk]
              else:
                  for i, v in enumerate(net_output[lk]):
                      logging_output[f"{lk}_{i}"] = float(v)
  
      if len(scaled_losses) > 1:
          for lk, l in scaled_losses.items():
              if l.numel() > 1:
                  l = l.sum()
              logging_output[f"loss_{lk}"] = l.item()
  
      if "logs" in net_output:
          for lgw in net_output["logs"]:
              logging_output[lgw] = net_output["logs"][lgw]
  
      return loss, sample_size, logging_output
  
file_path: fairseq/fairseq/criterions/model_criterion.py
imports: import logging
  from dataclasses import dataclass, field
  from typing import Dict, List
  import torch
  from fairseq import utils
  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 ModelCriterion obj's underlying member method code with example usage: obj.forward(model, sample, reduce) and returns: loss,  sample_size,  logging_output