class_docstr: ``TrainBatchLoop`` 针对一个 dataloader 的数据完成一个 epoch 的训练迭代过程；
  
  :param batch_step_fn: 您可以传入该参数来替换默认的 ``bath_step_fn``；
docstr: 对传入的 ``dataloader`` 进行一个 epoch 的主要的训练的循环过程；
  
  .. note::
  
      您不需要自己主动地调用该方法，``Trainer`` 会负责调用该方法来完成训练过程；
  
  :param trainer: :class:`~fastNLP.core.controllers.Trainer` 实例；
  :param dataloader: 当前训练所使用的 ``dataloader``；
docstr_tok: ["\u5bf9\u4f20\u5165\u7684","``","dataloader","``","\u8fdb\u884c\u4e00\u4e2a","epoch","\u7684\u4e3b\u8981\u7684\u8bad\u7ec3\u7684\u5faa\u73af\u8fc7\u7a0b\uff1b","..","note",":",":","\u60a8\u4e0d\u9700\u8981\u81ea\u5df1\u4e3b\u52a8\u5730\u8c03\u7528\u8be5\u65b9\u6cd5\uff0c","``","Trainer","``","\u4f1a\u8d1f\u8d23\u8c03\u7528\u8be5\u65b9\u6cd5\u6765\u5b8c\u6210\u8bad\u7ec3\u8fc7\u7a0b\uff1b",":","param","trainer",":",":","class",":","`","~fastNLP.core.controllers.Trainer","`","\u5b9e\u4f8b\uff1b",":","param","dataloader",":","\u5f53\u524d\u8bad\u7ec3\u6240\u4f7f\u7528\u7684","``","dataloader","``","\uff1b"]
code: def run(self, trainer, dataloader):
      r"""
      对传入的 ``dataloader`` 进行一个 epoch 的主要的训练的循环过程；
  
      .. note::
  
          您不需要自己主动地调用该方法，``Trainer`` 会负责调用该方法来完成训练过程；
  
      :param trainer: :class:`~fastNLP.core.controllers.Trainer` 实例；
      :param dataloader: 当前训练所使用的 ``dataloader``；
      """
      get_batch_indices = (
          dataloader.get_batch_indices
          if callable(getattr(dataloader, "get_batch_indices", None))
          else lambda *args, **kwargs: None
      )
      dataloader = iter(dataloader)
      while trainer.batch_idx_in_epoch <= trainer.num_batches_per_epoch:
          try:
              trainer.on_fetch_data_begin()
              batch = next(dataloader)
              indices = get_batch_indices()
          except StopIteration:
              trainer.on_fetch_data_end()
              break
  
          trainer.on_fetch_data_end()
  
          try:
              batch = match_and_substitute_params(
                  trainer.input_mapping, batch
              )
              batch = trainer.move_data_to_device(batch)
  
              trainer.on_train_batch_begin(batch, indices)
              with trainer.get_no_sync_context():  # 在多卡的时候可能需要关闭 sync
                  self.batch_step_fn(trainer, batch)
              trainer.global_forward_batches += 1
              trainer.batch_idx_in_epoch += 1
  
              trainer.check_batch_step_fn()
              trainer.on_train_batch_end()
          except BaseException as e:
              if indices is not None and not isinstance(
                  e, (EarlyStopException, KeyboardInterrupt)
              ):
                  logger.error(
                      f"Exception happens when training on samples: {indices}"
                  )
              raise e
          trainer.step_evaluate()
      trainer.batch_idx_in_epoch = 0
  
file_path: fastNLP/fastNLP/core/controllers/loops/train_batch_loop.py
imports: from typing import Optional, Callable
  from .loop import Loop
  from fastNLP.core.log import logger
  from fastNLP.core.utils import match_and_substitute_params
  from fastNLP.core.utils.exceptions import EarlyStopException
label_desc: Use image node_id 2 for calling the TrainBatchLoop obj's underlying member method code with example usage: obj.run(trainer, dataloader) without return types