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",")","."]
docstr: Validate model.
  
  Args:
      valid_iter: validate data iterator
  
  Returns:
      :obj:``nmt.Statistics``: validation loss statistics
docstr_tok: ["Validate","model",".","Args",":","valid_iter",":","validate","data","iterator","Returns",":",":","obj",":","``","nmt.Statistics","``",":","validation","loss","statistics"]
code: def validate(self, valid_iter, moving_average=None):
      """Validate model.
  
      Args:
          valid_iter: validate data iterator
  
      Returns:
          :obj:``nmt.Statistics``: validation loss statistics"""
  
      valid_model = self.model
      if moving_average:
          # swap model params w/ moving average
          # (and keep the original parameters)
          model_params_data = []
          for avg, param in zip(
              self.moving_average, valid_model.parameters()
          ):
              model_params_data.append(param.data)
              param.data = (
                  avg.data.half()
                  if param.dtype == torch.float16
                  else avg.data
              )
  
      # Set model in validating mode.
      valid_model.eval()
  
      # raw_srcs = []
      # raw_refs = []
      with torch.no_grad():
          stats = onmt.utils.Statistics()
          start = time.time()
          for batch, bucket_idx in valid_iter:
              src = batch["src"]
              src_len = batch["srclen"]
              tgt = batch["tgt"]
  
              with torch.cuda.amp.autocast(enabled=self.optim.amp):
                  # F-prop through the model.
                  model_out, attns = valid_model(
                      src, tgt, src_len, with_align=self.with_align
                  )
  
                  # Compute loss.
                  _, batch_stats = self.valid_loss(
                      batch, model_out, attns
                  )
  
                  stats.update(batch_stats)
          logger.info(
              """valid stats calculation
                             took: {} s.""".format(
                  time.time() - start
              )
          )
  
          # Compute validation metrics (at batch.dataset level)
          if len(self.valid_scorers) > 0:
              computed_metrics = {}
              start = time.time()
              preds, texts_ref = self.scoring_preparator.translate(
                  model=self.model,
                  gpu_rank=self.gpu_rank,
                  step=self.optim.training_step,
              )
              logger.info(
                  """The translation of the valid dataset for dynamic scoring
                                 took : {} s.""".format(
                      time.time() - start
                  )
              )
              for i, metric in enumerate(self.valid_scorers):
                  logger.info("UPDATING VALIDATION {}".format(metric))
                  self.valid_scorers[metric][
                      "value"
                  ] = self._eval_handler(
                      scorer=self.valid_scorers[metric]["scorer"],
                      preds=preds,
                      texts_ref=texts_ref,
                  )
                  computed_metrics[metric] = self.valid_scorers[metric][
                      "value"
                  ]
                  logger.info(
                      "validation {}: {}".format(
                          metric, self.valid_scorers[metric]["value"]
                      )
                  )
                  # Compute stats
                  metric_stats = onmt.utils.Statistics(
                      0, 0, 0, 0, 0, computed_metrics
                  )
  
              # Update statistics.
              stats.update(metric_stats)
  
      if moving_average:
          for param_data, param in zip(
              model_params_data, self.model.parameters()
          ):
              param.data = param_data
  
      # Set model back to training mode.
      valid_model.train()
  
      return stats
  
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 8 for calling the Trainer obj's underlying member method code with example usage: obj.validate(valid_iter, moving_average) and returns: stats