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: Returns a subclass which wraps around an nn.Module subclass
  (such as nn.NLLLoss) which defines the loss criterion. The LossCompute
  object passes relevant data to a Statistics object which handles
  training/validation logging.
  The Criterion and LossCompute options are triggered by opt settings.
docstr_tok: ["Returns","a","subclass","which","wraps","around","an","nn.Module","subclass","(","such","as","nn.NLLLoss",")","which","defines","the","loss","criterion",".","The","LossCompute","object","passes","relevant","data","to","a","Statistics","object","which","handles","training\/validation","logging",".","The","Criterion","and","LossCompute","options","are","triggered","by","opt","settings","."]
code: def from_opts(cls, opt, model, vocab, train=True):
      """
      Returns a subclass which wraps around an nn.Module subclass
      (such as nn.NLLLoss) which defines the loss criterion. The LossCompute
      object passes relevant data to a Statistics object which handles
      training/validation logging.
      The Criterion and LossCompute options are triggered by opt settings.
      """
      device = torch.device(
          "cuda" if onmt.utils.misc.use_gpu(opt) else "cpu"
      )
  
      padding_idx = vocab[DefaultTokens.PAD]
      unk_idx = vocab[DefaultTokens.UNK]
  
      if opt.lambda_coverage != 0:
          assert opt.coverage_attn, (
              "--coverage_attn needs to be set in "
              "order to use --lambda_coverage != 0"
          )
  
      tgt_shift_idx = 1 if opt.model_task == ModelTask.SEQ2SEQ else 0
  
      if opt.copy_attn:
          criterion = onmt.modules.CopyGeneratorLoss(
              len(vocab),
              opt.copy_attn_force,
              unk_index=unk_idx,
              ignore_index=padding_idx,
          )
      else:
          if opt.generator_function == "sparsemax":
              criterion = SparsemaxLoss(
                  ignore_index=padding_idx, reduction="sum"
              )
          else:
              criterion = nn.CrossEntropyLoss(
                  ignore_index=padding_idx,
                  reduction="sum",
                  label_smoothing=opt.label_smoothing,
              )
  
      lm_prior_lambda = opt.lm_prior_lambda
      lm_prior_tau = opt.lm_prior_tau
      if opt.lm_prior_model:
          if opt.lm_prior_model[-3:] == ".pt":
              opt.gpu = 0
              opt.fp32 = False
              opt.int8 = False
              _, lm_prior_model, lm_model_opt = load_test_model(
                  opt, model_path=opt.lm_prior_model
              )
              lm_prior_model.to(torch.device("cuda", opt.gpu))
              lm_prior_model.eval()
              lm_generator = None
          else:
              lm_prior_model = None
              try:
                  import ctranslate2
  
                  lm_generator = ctranslate2.Generator(
                      opt.lm_prior_model,
                      device="cuda",
                      compute_type="float16",
                  )
              except ImportError:
                  raise ImportError("Could not import ctranslate2")
      else:
          lm_generator = None
          lm_prior_model = None
  
      compute = cls(
          criterion,
          model.generator,
          copy_attn=opt.copy_attn,
          lambda_coverage=opt.lambda_coverage,
          lambda_align=opt.lambda_align,
          tgt_shift_index=tgt_shift_idx,
          vocab=vocab,
          lm_generator=lm_generator,
          lm_prior_lambda=lm_prior_lambda,
          lm_prior_tau=lm_prior_tau,
          lm_prior_model=lm_prior_model,
      )
      compute.to(device)
  
      return compute
  
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 2 for calling the LossCompute obj's underlying member method code with example usage: obj.from_opts(cls, opt, model, vocab, train) and returns: compute