class_docstr: Computes a cross-entropy loss, normalized by the number of valid (non-pad) tokens.
  Uses an efficient implementation for label smoothing and avoids the obscure SoftmaxOutput op.
class_docstr_tok: ["Computes","a","cross-entropy","loss",",","normalized","by","the","number","of","valid","(","non-pad",")","tokens",".","Uses","an","efficient","implementation","for","label","smoothing","and","avoids","the","obscure","SoftmaxOutput","op","."]
docstr: Computes smoothed NLL as in fairseq, see
  # https://github.com/pytorch/fairseq/blob/db0175a882e8ae0f30d89b5a610373dbe032d528/fairseq/criterions/label_smoothed_cross_entropy.py#L33
code: def _smoothed_loss_as_in_fairseq(self, logits, labels):
      """
      Computes smoothed NLL as in fairseq, see
      # https://github.com/pytorch/fairseq/blob/db0175a882e8ae0f30d89b5a610373dbe032d528/fairseq/criterions/label_smoothed_cross_entropy.py#L33
      """
      pred = pt.log_softmax(logits, dim=-1)
      if labels.dim() == logits.dim() - 1:
          labels = labels.unsqueeze(-1)
      nll = -pred.gather(dim=-1, index=labels.long())
      smooth_loss = pred.sum(dim=-1, keepdim=True)
  
      pad_mask = labels.eq(self.ignore_label)
      nll.masked_fill_(pad_mask, 0.0)
      smooth_loss.masked_fill_(pad_mask, 0.0)
  
      nll = nll.sum()
      smooth_loss = smooth_loss.sum()
  
      alpha_i = self._alpha / (logits.size(-1) - 1)
      nll = (1.0 - self._alpha - alpha_i) * nll - alpha_i * smooth_loss
  
      num_valid = (~pad_mask).sum()
      ce = nll.sum() * self.weight / num_valid
      return ce
  
imports: import logging
  import math
  from abc import ABC, abstractmethod
  from typing import Any, Dict, Optional, Tuple
  import torch
  import numpy
  from .None import constants
  from .None import utils
label_desc: Use image node_id 3 for calling the CrossEntropyLoss obj's underlying member method code with example usage: obj._smoothed_loss_as_in_fairseq(logits, labels) and returns: ce