class_docstr: Class for managing efficient loss computation. Handles
  accumulating multiple loss computations.
  
  Args:
      criterion (:obj:`nn. loss function`) : NLLoss or customed loss
      generator (:obj:`nn.Module`) :
      copy_attn (bool): whether copy attention mechanism is on/off
      lambda_coverage: Hyper-param to apply coverage attention if any
      lambda_align: Hyper-param for alignment loss
      tgt_shift_index (int): 1 for NMT, 0 for LM
      vocab: target vocab (for copy attention score calculation)
           module that maps the output of the decoder to a
           distribution over the target vocabulary.
      lm_generator (:obj:`ctranslate2.Generator`): LM Generator
      lm_prior_lambda (float): weight of LM model in loss
      lm_prior_tau (float): scaler for LM loss
class_docstr_tok: ["Class","for","managing","efficient","loss","computation",".","Handles","accumulating","multiple","loss","computations",".","Args",":","criterion","(",":","obj",":","`","nn",".","loss","function","`",")",":","NLLoss","or","customed","loss","generator","(",":","obj",":","`","nn.Module","`",")",":","copy_attn","(","bool",")",":","whether","copy","attention","mechanism","is","on\/off","lambda_coverage",":","Hyper-param","to","apply","coverage","attention","if","any","lambda_align",":","Hyper-param","for","alignment","loss","tgt_shift_index","(","int",")",":","1","for","NMT",",","0","for","LM","vocab",":","target","vocab","(","for","copy","attention","score","calculation",")","module","that","maps","the","output","of","the","decoder","to","a","distribution","over","the","target","vocabulary",".","lm_generator","(",":","obj",":","`","ctranslate2.Generator","`",")",":","LM","Generator","lm_prior_lambda","(","float",")",":","weight","of","LM","model","in","loss","lm_prior_tau","(","float",")",":","scaler","for","LM","loss"]
docstr: Compute the loss between MT output and LM output
  https://github.com/cbaziotis/lm-prior-for-nmt/blob/master
  /fairseq_extension/user/lm_prior/lm_prior.py#L131-L133
docstr_tok: ["Compute","the","loss","between","MT","output","and","LM","output","https",":","\/\/github.com\/cbaziotis\/lm-prior-for-nmt\/blob\/master","\/fairseq_extension\/user\/lm_prior\/lm_prior.py","#","L131-L133"]
code: def _compute_lm_loss_ct2(self, output, target):
      """
      Compute the loss between MT output and LM output
      https://github.com/cbaziotis/lm-prior-for-nmt/blob/master
      /fairseq_extension/user/lm_prior/lm_prior.py#L131-L133
      """
  
      # rescale with tau (temperature) and apply the log_softmax.
      scores = self.generator(self._bottle(output)) / self.lm_prior_tau
      scores = F.log_softmax(scores.to(torch.float32), dim=-1)
  
      src = target.detach().clone()
      src[src == self.vocab[DefaultTokens.EOS]] = self.padding_idx
      src = src[:, :-1, :]
      src_len = src[:, :, 0].ne(self.padding_idx).sum(1)
      # ct2 expects src with lengths without padding
      lm_scores = self.lm_generator.forward_batch(
          ctranslate2.StorageView.from_array(
              src[:, :, 0].to(torch.int32)
          ),
          ctranslate2.StorageView.from_array(src_len.to(torch.int32)),
          return_log_probs=False,
      )
      lm_scores = torch.as_tensor(lm_scores, device=scores.device)
      # again we use raw probs to rescale with tau and apply log_softmax
      lm_scores = self._bottle(lm_scores) / self.lm_prior_tau
      lm_scores = F.log_softmax(lm_scores.to(torch.float32), dim=-1)
      lm_scores[:, self.vocab[DefaultTokens.UNK]] = -50
      lm_scores[:, self.vocab[DefaultTokens.EOS]] -= 20
      # lm_scores are in log space so log_target=True
      lm_loss = F.kl_div(
          scores, lm_scores, reduction="none", log_target=True
      ).sum(-1)
      non_padding = self._bottle(output).ne(self.padding_idx)[:, 0]
      lm_loss = lm_loss.masked_select(non_padding).sum()
      lm_loss = lm_loss * (self.lm_prior_tau**2)
      return lm_loss
  
imports: import torch
  import torch.nn
  import torch.nn.functional
  import onmt
  from onmt.modules.sparse_losses import SparsemaxLoss
  from onmt.modules.sparse_activations import LogSparsemax
  from onmt.constants import ModelTask, DefaultTokens
  from onmt.modules.copy_generator import collapse_copy_scores
  from onmt.model_builder import load_test_model
label_desc: Use image node_id 7 for calling the LossCompute obj's underlying member method code with example usage: obj._compute_lm_loss_ct2(output, target) and returns: lm_loss