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: The main training loop by iterating over ``train_iter`` and possibly
  running validation on ``valid_iter``.
  
  Args:
      train_iter: An iterator that returns the next training batch.
      train_steps: Run training for this many iterations.
      save_checkpoint_steps: Save a checkpoint every this many
        iterations.
      valid_iter: A generator that returns the next validation batch.
      valid_steps: Run evaluation every this many iterations.
  
  Returns:
      :obj:``nmt.Statistics``: training loss statistics
docstr_tok: ["The","main","training","loop","by","iterating","over","``","train_iter","``","and","possibly","running","validation","on","``","valid_iter","``",".","Args",":","train_iter",":","An","iterator","that","returns","the","next","training","batch",".","train_steps",":","Run","training","for","this","many","iterations",".","save_checkpoint_steps",":","Save","a","checkpoint","every","this","many","iterations",".","valid_iter",":","A","generator","that","returns","the","next","validation","batch",".","valid_steps",":","Run","evaluation","every","this","many","iterations",".","Returns",":",":","obj",":","``","nmt.Statistics","``",":","training","loss","statistics"]
code: def train(
      self,
      train_iter,
      train_steps,
      save_checkpoint_steps=5000,
      valid_iter=None,
      valid_steps=10000,
  ):
      """The main training loop by iterating over ``train_iter`` and possibly
      running validation on ``valid_iter``.
  
      Args:
          train_iter: An iterator that returns the next training batch.
          train_steps: Run training for this many iterations.
          save_checkpoint_steps: Save a checkpoint every this many
            iterations.
          valid_iter: A generator that returns the next validation batch.
          valid_steps: Run evaluation every this many iterations.
  
      Returns:
          :obj:``nmt.Statistics``: training loss statistics"""
  
      if valid_iter is None:
          logger.info("Start training loop without validation...")
          valid_stats = None
      else:
          logger.info(
              "Start training loop and validate every %d steps...",
              valid_steps,
          )
      logger.info(
          "Scoring with: {}".format(self.scoring_preparator.transform)
      )
  
      total_stats = onmt.utils.Statistics()
      report_stats = onmt.utils.Statistics()
      self._start_report_manager(start_time=total_stats.start_time)
      # Let's clean the GPUs before training loop
      torch.cuda.empty_cache()
  
      for i, (batches, normalization) in enumerate(
          self._accum_batches(train_iter)
      ):
          step = self.optim.training_step
          # UPDATE DROPOUT
          self._maybe_update_dropout(step)
  
          if self.n_gpu > 1 and self.parallel_mode == "data_parallel":
              normalization = sum(
                  onmt.utils.distributed.all_gather_list(normalization)
              )
  
          self._gradient_accumulation(
              batches, normalization, total_stats, report_stats
          )
  
          if self.average_decay > 0 and i % self.average_every == 0:
              self._update_average(step)
  
          report_stats = self._maybe_report_training(
              step,
              train_steps,
              self.optim.learning_rate(),
              report_stats,
          )
  
          if valid_iter is not None and step % valid_steps == 0:
              if (
                  self.parallel_mode == "tensor_parallel"
                  or self.gpu_rank <= 0
              ):
                  valid_stats = self.validate(
                      valid_iter, moving_average=self.moving_average
                  )
  
          if step % valid_steps == 0 and self.gpu_rank <= 0:
              self._report_step(
                  self.optim.learning_rate(),
                  step,
                  valid_stats=valid_stats,
                  train_stats=total_stats,
              )
  
              # Run patience mechanism
              if self.earlystopper is not None:
                  self.earlystopper(valid_stats, step)
                  # If the patience has reached the limit, stop training
                  if self.earlystopper.has_stopped():
                      logger.info("earlystopper has_stopped!")
                      break
  
          if self.model_saver is not None and (
              save_checkpoint_steps != 0
              and step % save_checkpoint_steps == 0
          ):
              self.model_saver.save(
                  step, moving_average=self.moving_average
              )
  
          if train_steps > 0 and step >= train_steps:
              break
  
      if self.model_saver is not None:
          self.model_saver.save(
              step, moving_average=self.moving_average
          )
      return total_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 7 for calling the Trainer obj's underlying member method code with example usage: obj.train(train_iter, train_steps, save_checkpoint_steps, valid_iter, valid_steps) and returns: total_stats