class_docstr: Class that controls the training process.
  
  Args:
      model(:py:class:`onmt.models.model.NMTModel`): model to train
      train_loss(:obj:`onmt.utils.loss.LossComputeBase`):
        training loss computation
      valid_loss(:obj:`onmt.utils.loss.LossComputeBase`):
        training loss computation
      scoring_preparator(:obj:`onmt.translate.utils.ScoringPreparator`):
        preparator for the calculation of metrics via the
        _eval_handler method
      valid_scorers (dict): keeps in memory the current values
        of the validation metrics
      optim(:obj:`onmt.utils.optimizers.Optimizer`):
        the optimizer responsible for update
      trunc_size(int): length of truncated back propagation
        through time
      accum_count(list): accumulate gradients this many times.
      accum_steps(list): steps for accum gradients changes.
      n_gpu (int): number of gpu.
      gpu_rank (int): ordinal rank of the gpu in the list.
      report_manager(:obj:`onmt.utils.ReportMgrBase`):
        the object that creates reports, or None
      with_align (bool): whether to jointly lear alignment
        (Transformer)
      model_saver(:obj:`onmt.models.ModelSaverBase`): the saver is
        used to save a checkpoint.
        Thus nothing will be saved if this parameter is None.
      average_decay (float): cf opt.average_decay
      average_every (int): average model every x steps.
      model_dtype (str): fp32 or fp16.
      earlystopper (:obj:`onmt.utils.EarlyStopping`): add early
        stopping mecanism
      dropout (float): dropout value in RNN or FF layers.
      attention_dropout (float): dropaout in attention layers.
      dropout_steps (list): dropout values scheduling in steps.
      zero_out_prompt_loss (bool): whether to zero-out the prompt loss
          (mostly for LLM finetuning).
class_docstr_tok: ["Class","that","controls","the","training","process",".","Args",":","model","(",":","py",":","class",":","`","onmt.models.model.NMTModel","`",")",":","model","to","train","train_loss","(",":","obj",":","`","onmt.utils.loss.LossComputeBase","`",")",":","training","loss","computation","valid_loss","(",":","obj",":","`","onmt.utils.loss.LossComputeBase","`",")",":","training","loss","computation","scoring_preparator","(",":","obj",":","`","onmt.translate.utils.ScoringPreparator","`",")",":","preparator","for","the","calculation","of","metrics","via","the","_eval_handler","method","valid_scorers","(","dict",")",":","keeps","in","memory","the","current","values","of","the","validation","metrics","optim","(",":","obj",":","`","onmt.utils.optimizers.Optimizer","`",")",":","the","optimizer","responsible","for","update","trunc_size","(","int",")",":","length","of","truncated","back","propagation","through","time","accum_count","(","list",")",":","accumulate","gradients","this","many","times",".","accum_steps","(","list",")",":","steps","for","accum","gradients","changes",".","n_gpu","(","int",")",":","number","of","gpu",".","gpu_rank","(","int",")",":","ordinal","rank","of","the","gpu","in","the","list",".","report_manager","(",":","obj",":","`","onmt.utils.ReportMgrBase","`",")",":","the","object","that","creates","reports",",","or","None","with_align","(","bool",")",":","whether","to","jointly","lear","alignment","(","Transformer",")","model_saver","(",":","obj",":","`","onmt.models.ModelSaverBase","`",")",":","the","saver","is","used","to","save","a","checkpoint",".","Thus","nothing","will","be","saved","if","this","parameter","is","None",".","average_decay","(","float",")",":","cf","opt.average_decay","average_every","(","int",")",":","average","model","every","x","steps",".","model_dtype","(","str",")",":","fp32","or","fp16",".","earlystopper","(",":","obj",":","`","onmt.utils.EarlyStopping","`",")",":","add","early","stopping","mecanism","dropout","(","float",")",":","dropout","value","in","RNN","or","FF","layers",".","attention_dropout","(","float",")",":","dropaout","in","attention","layers",".","dropout_steps","(","list",")",":","dropout","values","scheduling","in","steps",".","zero_out_prompt_loss","(","bool",")",":","whether","to","zero-out","the","prompt","loss","(","mostly","for","LLM","finetuning",")","."]
code: def __init__(
      self,
      model,
      train_loss,
      valid_loss,
      scoring_preparator,
      valid_scorers,
      optim,
      trunc_size=0,
      norm_method="sents",
      accum_count=[1],
      accum_steps=[0],
      n_gpu=1,
      gpu_rank=1,
      parallel_mode="data_parallel",
      report_manager=None,
      with_align=False,
      model_saver=None,
      average_decay=0,
      average_every=1,
      model_dtype="fp32",
      earlystopper=None,
      dropout=[0.3],
      attention_dropout=[0.1],
      dropout_steps=[0],
      zero_out_prompt_loss=False,
  ):
      # Basic attributes.
  
      self.model = model
      self.train_loss = train_loss
      self.valid_loss = valid_loss
  
      self.scoring_preparator = scoring_preparator
      self.valid_scorers = valid_scorers
      self.optim = optim
      self.trunc_size = trunc_size
      self.norm_method = norm_method
      self.accum_count_l = accum_count
      self.accum_count = accum_count[0]
      self.accum_steps = accum_steps
      self.n_gpu = n_gpu
      self.gpu_rank = gpu_rank
      self.parallel_mode = parallel_mode
      self.report_manager = report_manager
      self.with_align = with_align
      self.model_saver = model_saver
      self.average_decay = average_decay
      self.moving_average = None
      self.average_every = average_every
      self.model_dtype = model_dtype
      self.earlystopper = earlystopper
      self.dropout = dropout
      self.attention_dropout = attention_dropout
      self.dropout_steps = dropout_steps
      self.zero_out_prompt_loss = zero_out_prompt_loss
  
      for i in range(len(self.accum_count_l)):
          assert self.accum_count_l[i] > 0
  
      # Set model in training mode.
      self.model.train()
  
imports: import time
  import sys
  import torch
  import traceback
  import onmt.utils
  from onmt.utils.loss import LossCompute
  from onmt.utils.logging import logger
  from onmt.utils.scoring_utils import ScoringPreparator
  from onmt.scorers import get_scorers_cls, build_scorers
label_desc: Use image node_id 1 to create a new Trainer object from inherited base classes: object with example: obj = Trainer(model, train_loss, valid_loss, scoring_preparator, valid_scorers, optim, trunc_size, norm_method, accum_count, accum_steps, n_gpu, gpu_rank, parallel_mode, report_manager, with_align, model_saver, average_decay, average_every, model_dtype, earlystopper, dropout, attention_dropout, dropout_steps, zero_out_prompt_loss)