class_docstr: Calculates Cross-entropy loss with label smoothing for a batch of sequences.
  
  SmoothedCrossEntropyLoss:
  1) excludes padding tokens from loss calculation
  2) allows to use label smoothing regularization
  3) allows to calculate loss for the desired number of last tokens
  4) per_token_reduction - if False disables reduction per token
  
  Args:
      label_smoothing (float): label smoothing regularization coefficient
      predict_last_k (int): parameter which sets the number of last tokens to calculate the loss for, for example
          0: (default) calculate loss on the entire sequence (e.g., NMT)
          1: calculate loss on the last token only (e.g., LM evaluation)
          Intermediate values allow to control the trade-off between eval
          time (proportional to the number of batches) and eval performance
          (proportional to the number of context tokens)
      pad_id (int): padding id
      eps (float): the small eps number to avoid division buy zero
class_docstr_tok: ["Calculates","Cross-entropy","loss","with","label","smoothing","for","a","batch","of","sequences",".","SmoothedCrossEntropyLoss",":","1",")","excludes","padding","tokens","from","loss","calculation","2",")","allows","to","use","label","smoothing","regularization","3",")","allows","to","calculate","loss","for","the","desired","number","of","last","tokens","4",")","per_token_reduction","-","if","False","disables","reduction","per","token","Args",":","label_smoothing","(","float",")",":","label","smoothing","regularization","coefficient","predict_last_k","(","int",")",":","parameter","which","sets","the","number","of","last","tokens","to","calculate","the","loss","for",",","for","example","0",":","(","default",")","calculate","loss","on","the","entire","sequence","(","e.g.",",","NMT",")","1",":","calculate","loss","on","the","last","token","only","(","e.g.",",","LM","evaluation",")","Intermediate","values","allow","to","control","the","trade-off","between","eval","time","(","proportional","to","the","number","of","batches",")","and","eval","performance","(","proportional","to","the","number","of","context","tokens",")","pad_id","(","int",")",":","padding","id","eps","(","float",")",":","the","small","eps","number","to","avoid","division","buy","zero"]
docstr: Args:
      log_probs: float tensor of shape batch_size x seq_len x vocab_size, values should be log probabilities
      labels: int tensor of shape batch_size x seq_len
      output_mask: binary tensor of shape batch_size x seq_len
      eps: epsilon param to avoid divide by zero in loss calculation
docstr_tok: ["Args",":","log_probs",":","float","tensor","of","shape","batch_size","x","seq_len","x","vocab_size",",","values","should","be","log","probabilities","labels",":","int","tensor","of","shape","batch_size","x","seq_len","output_mask",":","binary","tensor","of","shape","batch_size","x","seq_len","eps",":","epsilon","param","to","avoid","divide","by","zero","in","loss","calculation"]
code: def forward(self, log_probs, labels, output_mask=None):
      """
      Args:
          log_probs: float tensor of shape batch_size x seq_len x vocab_size, values should be log probabilities
          labels: int tensor of shape batch_size x seq_len
          output_mask: binary tensor of shape batch_size x seq_len
          eps: epsilon param to avoid divide by zero in loss calculation
      """
      if output_mask is None and self._pad_id is None:
          raise ValueError("Both output_mask and pad_id are None")
      if output_mask is None and self._pad_id is not None:
          output_mask = (labels != self._pad_id).to(log_probs.dtype)
  
      if output_mask.dtype is not log_probs.dtype:
          output_mask = output_mask.to(log_probs.dtype)
  
      batch_size, seq_len, vocab_size = log_probs.size()
      smoothing = vocab_size * self._label_smoothing / (vocab_size - 1)
      target_log_probs = log_probs.gather(
          2, labels.unsqueeze(2)
      ).squeeze(2)
  
      smoothing_log_probs = log_probs.mean(dim=-1)
      neg_log_likelihood = (
          1.0 - smoothing
      ) * target_log_probs + smoothing * smoothing_log_probs
      neg_log_likelihood = neg_log_likelihood[
          :, -self._predict_last_k :
      ]
      output_mask = output_mask[:, -self._predict_last_k :]
  
      # when False avoid per token reduction
      if self._per_token_reduction:
          neg_log_likelihood = -torch.sum(
              neg_log_likelihood * output_mask
          )
          neg_log_likelihood = neg_log_likelihood / (
              output_mask.sum() + self._eps
          )
      else:
          neg_log_likelihood = -(neg_log_likelihood * output_mask)
  
      return neg_log_likelihood
  
file_path: NeMo/nemo/collections/common/losses/smoothed_cross_entropy.py
imports: from typing import Optional
  import torch
  from nemo.core.classes import Exportable, Loss, NeuralModule, typecheck
  from nemo.core.neural_types import LabelsType, LogprobsType, LossType, MaskType, NeuralType
label_desc: Use image node_id 4 for calling the SmoothedCrossEntropyLoss obj's underlying member method code with example usage: obj.forward(log_probs, labels, output_mask) and returns: neg_log_likelihood