class_docstr: Implementation for the loss used in masked language model (MLM) training.
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
      """
      masked_tokens = sample["target"].ne(self.padding_idx)
      sample_size = masked_tokens.int().sum()
  
      # Rare: when all tokens are masked, project all tokens.
      # We use torch.where to avoid device-to-host transfers,
      # except on CPU where torch.where is not well supported
      # (see github.com/pytorch/pytorch/issues/26247).
      if self.tpu:
          masked_tokens = None  # always project all tokens on TPU
      elif masked_tokens.device == torch.device("cpu"):
          if not masked_tokens.any():
              masked_tokens = None
      else:
          masked_tokens = torch.where(
              masked_tokens.any(),
              masked_tokens,
              masked_tokens.new([True]),
          )
  
      logits = model(
          **sample["net_input"], masked_tokens=masked_tokens
      )[0]
      targets = model.get_targets(sample, [logits])
      if masked_tokens is not None:
          targets = targets[masked_tokens]
  
      loss = modules.cross_entropy(
          logits.view(-1, logits.size(-1)),
          targets.view(-1),
          reduction="sum",
          ignore_index=self.padding_idx,
      )
  
      logging_output = {
          "loss": loss if self.tpu else loss.data,
          "ntokens": sample["ntokens"],
          "nsentences": sample["nsentences"],
          "sample_size": sample_size,
      }
      return loss, sample_size, logging_output
  
file_path: fairseq/fairseq/criterions/masked_lm.py
imports: from dataclasses import dataclass
  import math
  from omegaconf import II
  import torch
  from fairseq import modules, utils
  from fairseq.logging import metrics
  from fairseq.criterions import FairseqCriterion, register_criterion
  from fairseq.dataclass import FairseqDataclass
label_desc: Use image node_id 2 for calling the MaskedLmLoss obj's underlying member method code with example usage: obj.forward(model, sample, reduce) and returns: loss,  sample_size,  logging_output