class_docstr: 提供给 ``torch`` 框架使用的 ``DataLoader`` 函数，``TorchDataLoader`` 提供了 ``Collator`` 来自动检测 dataset 的每个 field 是否可 pad，
  若是可 pad 的 field 则自动 pad 到相同长度，否则只会将相同 field 的数据收集组成一个 batch 返回。
  具体详见 :class:`~fastNLP.core.collators.Collator`；用户通过 callte_fn 来控制是否使用该功能， collate_fn 只能为 ``['auto', None, Callable]``
  三种取值。
  
      * callate_fn 为 ``'auto'`` 时，``TorchDataLoader`` 使用 :class:`~fastNLP.core.collators.Collator` 作为 collate_fn 的取值。
        此时可以配套使用 ``TorchDataLoader`` 的 ``set_pad`` 和 ``set_ignore`` 方法来设置 pad_val 或 忽略某个 field 的检测。
      * callate_fn 为 ``None`` 时， ``TorchDataLoadr`` 默认使用 torch DataLoader 自带的 collate_fn
      * collate_fn 为 :class:`Callable` 时， 该 Callable 函数应当接受一个 batch 参数作为输入， batch 是一个 List 对象且 List 中的每一条数据都是
        dataset 的一条数据；该 Callable 函数还应当返回一个对象。
  
  :param dataset: 实现了 __getitem__() 和 __len__() 的对象。
  :param batch_size: 批次大小，默认为 ``16`` 且当 batch_sampler 为 None 有效。
  :param non_train_batch_size: 非训练数据集的 ``TorchDataLoader`` 批次大小，默认为 ``16`` 且当 ``batch_sampler`` 为 ``None`` 有效。
  :param shuffle: 是否打乱数据集， 默认为 ``None``, 如果传入的 ``ds_or_db`` 可以判断出哪个是 ``'train'`` 则设置其 shuffle 为 ``True`` ，
      其它的为 False 。
  :param sampler: 实现了 __len__() 和 __iter__() 的实例化对象，其 __iter__() 方法每次都会返回 dataset 的一个下标 index ，
      默认为 ``None``， 当其不为 ``None`` 时， shuffle 参数无效。
  :param non_train_sampler: 非训练数据集的的实现了 __len__() 和 __iter__() 的实例化对象，其 __iter__() 方法每次都会返回 dataset 的一个下标 index ，
      默认为None， 当其不为 None 时， shuffle 参数无效。
  :param batch_sampler: 实现了 __len__() 和 __iter__() 的实例化对象，，其__iter__() 方法每次都会返回一个 List 对象， List中的值为
      dataset 的下标 index ；默认为 ``None``，当其不为 ``None`` 时，``bacth_size``, ``sampler``, ``shuffle`` 参数均失效。
  :param num_workers: 当 ``num_workers > 0`` 时, ``TorchDataLoader`` 会开启 ``num_workers`` 个子进程来处理数据， 可以加快
      数据处理速度，但同时也消耗大量内存。 当 ``num_workers=0`` 时， 不开启子进程。 默认为 ``0``。
  :param collate_fn: 用于从 dataset 取到的一个 batch 数据进行打包处理的 Callable 函数，其值应该为以下三个: ``[None, "auto", Callable]``.
  
      * callate_fn 为 ``None`` 时，需要注意的是此时传进来的 datset 类型不能为 :class:`~fastNLP.core.dataset.DataSet` , 当 collate_fn 为 ``None`` 时，
        ``TorchDataLoader`` 调用默认的 torch 框架的 ``DataLoader`` 自带的 `default_collate_fn` 作为 callate_fn 的默认值， 其无法处理
        :class:`~fastNLP.core.dataset.DataSet` 的dataset对象。
      * callate_fn 为 ``'auto'`` 时，``TorchDataLoader`` 使用 :class:`~fastNLP.core.collators.Collator` 作为 collate_fn 的默认值。
        此时可以配套使用 ``TorchDataLoader`` 的 ``set_pad`` 和 ``set_ignore`` 方法来设置 pad_val 或 忽略某个 field 的检测。
      * collate_fn 为 :class:`Callable` 时， 该 Callable 函数应当接受一个 batch 参数作为输入， batch 是一个 List 对象且 List 中的每一条数据都是
        dataset 的一条数据；该 Callable 函数还应当返回一个对象。
  
  :param pin_memory: 如果其为 ``True``, 那么 ``TorchDataLoader`` 会在返回数据张量之前将其 copy 到 cud a的 pin memory 中。
  :param drop_last: 当 ``drop_last=True`` 时，``TorchDataLoader`` 会扔掉最后一个长度小于 ``batch_size`` 的 batch 数据;
      若 ``drop_last=False`` , 则会返回该 batch 数据。 默认为 ``False`` 。
  :param timeout: 子进程的输出队列获取数据的超时值
  :param worker_init_fn: init 函数，如果不设置为 ``None``，则将会在每个子进程初始化时调用该函数。
  :param multiprocessing_context: 多进程的上下文环境
  :param generator: 如果其不为 ``None``, 将会使用 RandomSampler 去生成随机的 index 且会为每个子进程生成一个 ``base_seed``
  :param prefetch_factor: 每个 worker 提前装载的 samples 数量。``2`` 意味着在所有的进程中会有 2*num_workers 的数据被预取。默认值为 ``2`` .
  :param persistent_workers: 如果其为 ``True``, ``TorchDataLoader`` 在迭代完一次 dataset 后不会关闭所有进程。默认为 ``False``
class_docstr_tok: ["\u63d0\u4f9b\u7ed9","``","torch","``","\u6846\u67b6\u4f7f\u7528\u7684","``","DataLoader","``","\u51fd\u6570\uff0c","``","TorchDataLoader","``","\u63d0\u4f9b\u4e86","``","Collator","``","\u6765\u81ea\u52a8\u68c0\u6d4b","dataset","\u7684\u6bcf\u4e2a","field","\u662f\u5426\u53ef","pad\uff0c","\u82e5\u662f\u53ef","pad","\u7684","field","\u5219\u81ea\u52a8","pad","\u5230\u76f8\u540c\u957f\u5ea6\uff0c\u5426\u5219\u53ea\u4f1a\u5c06\u76f8\u540c","field","\u7684\u6570\u636e\u6536\u96c6\u7ec4\u6210\u4e00\u4e2a","batch","\u8fd4\u56de\u3002","\u5177\u4f53\u8be6\u89c1",":","class",":","`","~fastNLP.core.collators.Collator","`","\uff1b\u7528\u6237\u901a\u8fc7","callte_fn","\u6765\u63a7\u5236\u662f\u5426\u4f7f\u7528\u8be5\u529f\u80fd\uff0c","collate_fn","\u53ea\u80fd\u4e3a","``","[","'auto","'",",","None",",","Callable","]","``","\u4e09\u79cd\u53d6\u503c\u3002","*","callate_fn","\u4e3a","``","'auto","'","``","\u65f6\uff0c","``","TorchDataLoader","``","\u4f7f\u7528",":","class",":","`","~fastNLP.core.collators.Collator","`","\u4f5c\u4e3a","collate_fn","\u7684\u53d6\u503c\u3002","\u6b64\u65f6\u53ef\u4ee5\u914d\u5957\u4f7f\u7528","``","TorchDataLoader","``","\u7684","``","set_pad","``","\u548c","``","set_ignore","``","\u65b9\u6cd5\u6765\u8bbe\u7f6e","pad_val","\u6216","\u5ffd\u7565\u67d0\u4e2a","field","\u7684\u68c0\u6d4b\u3002","*","callate_fn","\u4e3a","``","None","``","\u65f6\uff0c","``","TorchDataLoadr","``","\u9ed8\u8ba4\u4f7f\u7528","torch","DataLoader","\u81ea\u5e26\u7684","collate_fn","*","collate_fn","\u4e3a",":","class",":","`","Callable","`","\u65f6\uff0c","\u8be5","Callable","\u51fd\u6570\u5e94\u5f53\u63a5\u53d7\u4e00\u4e2a","batch","\u53c2\u6570\u4f5c\u4e3a\u8f93\u5165\uff0c","batch","\u662f\u4e00\u4e2a","List","\u5bf9\u8c61\u4e14","List","\u4e2d\u7684\u6bcf\u4e00\u6761\u6570\u636e\u90fd\u662f","dataset","\u7684\u4e00\u6761\u6570\u636e\uff1b\u8be5","Callable","\u51fd\u6570\u8fd8\u5e94\u5f53\u8fd4\u56de\u4e00\u4e2a\u5bf9\u8c61\u3002",":","param","dataset",":","\u5b9e\u73b0\u4e86","__getitem__","(",")","\u548c","__len__","(",")","\u7684\u5bf9\u8c61\u3002",":","param","batch_size",":","\u6279\u6b21\u5927\u5c0f\uff0c\u9ed8\u8ba4\u4e3a","``","16","``","\u4e14\u5f53","batch_sampler","\u4e3a","None","\u6709\u6548\u3002",":","param","non_train_batch_size",":","\u975e\u8bad\u7ec3\u6570\u636e\u96c6\u7684","``","TorchDataLoader","``","\u6279\u6b21\u5927\u5c0f\uff0c\u9ed8\u8ba4\u4e3a","``","16","``","\u4e14\u5f53","``","batch_sampler","``","\u4e3a","``","None","``","\u6709\u6548\u3002",":","param","shuffle",":","\u662f\u5426\u6253\u4e71\u6570\u636e\u96c6\uff0c","\u9ed8\u8ba4\u4e3a","``","None","``",",","\u5982\u679c\u4f20\u5165\u7684","``","ds_or_db","``","\u53ef\u4ee5\u5224\u65ad\u51fa\u54ea\u4e2a\u662f","``","'train","'","``","\u5219\u8bbe\u7f6e\u5176","shuffle","\u4e3a","``","True","``","\uff0c","\u5176\u5b83\u7684\u4e3a","False","\u3002",":","param","sampler",":","\u5b9e\u73b0\u4e86","__len__","(",")","\u548c","__iter__","(",")","\u7684\u5b9e\u4f8b\u5316\u5bf9\u8c61\uff0c\u5176","__iter__","(",")","\u65b9\u6cd5\u6bcf\u6b21\u90fd\u4f1a\u8fd4\u56de","dataset","\u7684\u4e00\u4e2a\u4e0b\u6807","index","\uff0c","\u9ed8\u8ba4\u4e3a","``","None","``","\uff0c","\u5f53\u5176\u4e0d\u4e3a","``","None","``","\u65f6\uff0c","shuffle","\u53c2\u6570\u65e0\u6548\u3002",":","param","non_train_sampler",":","\u975e\u8bad\u7ec3\u6570\u636e\u96c6\u7684\u7684\u5b9e\u73b0\u4e86","__len__","(",")","\u548c","__iter__","(",")","\u7684\u5b9e\u4f8b\u5316\u5bf9\u8c61\uff0c\u5176","__iter__","(",")","\u65b9\u6cd5\u6bcf\u6b21\u90fd\u4f1a\u8fd4\u56de","dataset","\u7684\u4e00\u4e2a\u4e0b\u6807","index","\uff0c","\u9ed8\u8ba4\u4e3aNone\uff0c","\u5f53\u5176\u4e0d\u4e3a","None","\u65f6\uff0c","shuffle","\u53c2\u6570\u65e0\u6548\u3002",":","param","batch_sampler",":","\u5b9e\u73b0\u4e86","__len__","(",")","\u548c","__iter__","(",")","\u7684\u5b9e\u4f8b\u5316\u5bf9\u8c61\uff0c\uff0c\u5176__iter__","(",")","\u65b9\u6cd5\u6bcf\u6b21\u90fd\u4f1a\u8fd4\u56de\u4e00\u4e2a","List","\u5bf9\u8c61\uff0c","List\u4e2d\u7684\u503c\u4e3a","dataset","\u7684\u4e0b\u6807","index","\uff1b\u9ed8\u8ba4\u4e3a","``","None","``","\uff0c\u5f53\u5176\u4e0d\u4e3a","``","None","``","\u65f6\uff0c","``","bacth_size","``",",","``","sampler","``",",","``","shuffle","``","\u53c2\u6570\u5747\u5931\u6548\u3002",":","param","num_workers",":","\u5f53","``","num_workers",">","0","``","\u65f6",",","``","TorchDataLoader","``","\u4f1a\u5f00\u542f","``","num_workers","``","\u4e2a\u5b50\u8fdb\u7a0b\u6765\u5904\u7406\u6570\u636e\uff0c","\u53ef\u4ee5\u52a0\u5feb","\u6570\u636e\u5904\u7406\u901f\u5ea6\uff0c\u4f46\u540c\u65f6\u4e5f\u6d88\u8017\u5927\u91cf\u5185\u5b58\u3002","\u5f53","``","num_workers=0","``","\u65f6\uff0c","\u4e0d\u5f00\u542f\u5b50\u8fdb\u7a0b\u3002","\u9ed8\u8ba4\u4e3a","``","0","``","\u3002",":","param","collate_fn",":","\u7528\u4e8e\u4ece","dataset","\u53d6\u5230\u7684\u4e00\u4e2a","batch","\u6570\u636e\u8fdb\u884c\u6253\u5305\u5904\u7406\u7684","Callable","\u51fd\u6570\uff0c\u5176\u503c\u5e94\u8be5\u4e3a\u4ee5\u4e0b\u4e09\u4e2a",":","``","[","None",",","``","auto","''",",","Callable","]","``",".","*","callate_fn","\u4e3a","``","None","``","\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u7684\u662f\u6b64\u65f6\u4f20\u8fdb\u6765\u7684","datset","\u7c7b\u578b\u4e0d\u80fd\u4e3a",":","class",":","`","~fastNLP.core.dataset.DataSet","`",",","\u5f53","collate_fn","\u4e3a","``","None","``","\u65f6\uff0c","``","TorchDataLoader","``","\u8c03\u7528\u9ed8\u8ba4\u7684","torch","\u6846\u67b6\u7684","``","DataLoader","``","\u81ea\u5e26\u7684","`","default_collate_fn","`","\u4f5c\u4e3a","callate_fn","\u7684\u9ed8\u8ba4\u503c\uff0c","\u5176\u65e0\u6cd5\u5904\u7406",":","class",":","`","~fastNLP.core.dataset.DataSet","`","\u7684dataset\u5bf9\u8c61\u3002","*","callate_fn","\u4e3a","``","'auto","'","``","\u65f6\uff0c","``","TorchDataLoader","``","\u4f7f\u7528",":","class",":","`","~fastNLP.core.collators.Collator","`","\u4f5c\u4e3a","collate_fn","\u7684\u9ed8\u8ba4\u503c\u3002","\u6b64\u65f6\u53ef\u4ee5\u914d\u5957\u4f7f\u7528","``","TorchDataLoader","``","\u7684","``","set_pad","``","\u548c","``","set_ignore","``","\u65b9\u6cd5\u6765\u8bbe\u7f6e","pad_val","\u6216","\u5ffd\u7565\u67d0\u4e2a","field","\u7684\u68c0\u6d4b\u3002","*","collate_fn","\u4e3a",":","class",":","`","Callable","`","\u65f6\uff0c","\u8be5","Callable","\u51fd\u6570\u5e94\u5f53\u63a5\u53d7\u4e00\u4e2a","batch","\u53c2\u6570\u4f5c\u4e3a\u8f93\u5165\uff0c","batch","\u662f\u4e00\u4e2a","List","\u5bf9\u8c61\u4e14","List","\u4e2d\u7684\u6bcf\u4e00\u6761\u6570\u636e\u90fd\u662f","dataset","\u7684\u4e00\u6761\u6570\u636e\uff1b\u8be5","Callable","\u51fd\u6570\u8fd8\u5e94\u5f53\u8fd4\u56de\u4e00\u4e2a\u5bf9\u8c61\u3002",":","param","pin_memory",":","\u5982\u679c\u5176\u4e3a","``","True","``",",","\u90a3\u4e48","``","TorchDataLoader","``","\u4f1a\u5728\u8fd4\u56de\u6570\u636e\u5f20\u91cf\u4e4b\u524d\u5c06\u5176","copy","\u5230","cud","a\u7684","pin","memory","\u4e2d\u3002",":","param","drop_last",":","\u5f53","``","drop_last=True","``","\u65f6\uff0c","``","TorchDataLoader","``","\u4f1a\u6254\u6389\u6700\u540e\u4e00\u4e2a\u957f\u5ea6\u5c0f\u4e8e","``","batch_size","``","\u7684","batch","\u6570\u636e",";","\u82e5","``","drop_last=False","``",",","\u5219\u4f1a\u8fd4\u56de\u8be5","batch","\u6570\u636e\u3002","\u9ed8\u8ba4\u4e3a","``","False","``","\u3002",":","param","timeout",":","\u5b50\u8fdb\u7a0b\u7684\u8f93\u51fa\u961f\u5217\u83b7\u53d6\u6570\u636e\u7684\u8d85\u65f6\u503c",":","param","worker_init_fn",":","init","\u51fd\u6570\uff0c\u5982\u679c\u4e0d\u8bbe\u7f6e\u4e3a","``","None","``","\uff0c\u5219\u5c06\u4f1a\u5728\u6bcf\u4e2a\u5b50\u8fdb\u7a0b\u521d\u59cb\u5316\u65f6\u8c03\u7528\u8be5\u51fd\u6570\u3002",":","param","multiprocessing_context",":","\u591a\u8fdb\u7a0b\u7684\u4e0a\u4e0b\u6587\u73af\u5883",":","param","generator",":","\u5982\u679c\u5176\u4e0d\u4e3a","``","None","``",",","\u5c06\u4f1a\u4f7f\u7528","RandomSampler","\u53bb\u751f\u6210\u968f\u673a\u7684","index","\u4e14\u4f1a\u4e3a\u6bcf\u4e2a\u5b50\u8fdb\u7a0b\u751f\u6210\u4e00\u4e2a","``","base_seed","``",":","param","prefetch_factor",":","\u6bcf\u4e2a","worker","\u63d0\u524d\u88c5\u8f7d\u7684","samples","\u6570\u91cf\u3002","``","2","``","\u610f\u5473\u7740\u5728\u6240\u6709\u7684\u8fdb\u7a0b\u4e2d\u4f1a\u6709","2","*","num_workers","\u7684\u6570\u636e\u88ab\u9884\u53d6\u3002\u9ed8\u8ba4\u503c\u4e3a","``","2","``",".",":","param","persistent_workers",":","\u5982\u679c\u5176\u4e3a","``","True","``",",","``","TorchDataLoader","``","\u5728\u8fed\u4ee3\u5b8c\u4e00\u6b21","dataset","\u540e\u4e0d\u4f1a\u5173\u95ed\u6240\u6709\u8fdb\u7a0b\u3002\u9ed8\u8ba4\u4e3a","``","False","``"]
code: def __init__(
      self,
      dataset,
      batch_size: int = 16,
      shuffle: bool = False,
      sampler=None,
      batch_sampler=None,
      num_workers: int = 0,
      collate_fn: Union[Callable, str, None] = "auto",
      pin_memory: bool = False,
      drop_last: bool = False,
      timeout: float = 0,
      worker_init_fn: Optional[Callable] = None,
      multiprocessing_context=None,
      generator=None,
      prefetch_factor: int = 2,
      persistent_workers: bool = False,
      **kwargs,
  ) -> None:
      if isinstance(dataset, DataSet) and collate_fn is None:
          raise ValueError(
              "When use FastNLP DataSet, collate_fn must be not None"
          )
  
      if not isinstance(dataset, _FDataSet):
          dataset = _FDataSet(dataset)
  
      if num_workers > 0 and multiprocessing_context is None:
          multiprocessing_context = "fork"  # 这里默认使用fork的方式来启动多进程
  
      if batch_sampler is not None:
          batch_size = 1
          shuffle = False
          sampler = None
      elif sampler is None:
          sampler = RandomSampler(dataset, shuffle=shuffle)
          shuffle = False
  
      if isinstance(collate_fn, str):
          if collate_fn == "auto":
              if isinstance(
                  dataset.dataset, DataSet
              ):  # 使用了 fastnlp dataset
                  collate_fn = deepcopy(dataset.dataset.collator)
                  collate_fn.set_backend(backend="torch")
              else:
                  collate_fn = Collator(backend="torch")
          else:
              raise ValueError(
                  f"collate_fn: {collate_fn} must be 'auto'"
              )
  
      dl_kwargs = _match_param(
          TorchDataLoader.__init__,
          DataLoader.__init__,
          fn_name=DataLoader.__name__,
      )
      if dl_kwargs is None:
          super().__init__(
              dataset=dataset,
              batch_size=batch_size,
              shuffle=shuffle,
              sampler=sampler,
              batch_sampler=batch_sampler,
              num_workers=num_workers,
              collate_fn=collate_fn,
              pin_memory=pin_memory,
              drop_last=drop_last,
              timeout=timeout,
              worker_init_fn=worker_init_fn,
              multiprocessing_context=multiprocessing_context,
              generator=generator,
              prefetch_factor=prefetch_factor,
              persistent_workers=persistent_workers,
          )
      else:
          super().__init__(**dl_kwargs)
  
      self.cur_batch_indices = None
  
imports: from typing import Optional, Callable, Sequence, Union, Tuple, Dict, Mapping, List, Any
  from abc import ABC
  from copy import deepcopy
  from fastNLP.core.dataset import DataSet
  from fastNLP.core.collators import Collator
  from fastNLP.core.dataloaders.utils import indice_collate_wrapper
  from fastNLP.envs.imports import _NEED_IMPORT_TORCH
  from fastNLP.core.samplers import ReproducibleBatchSampler, ReproducibleSampler, UnrepeatedSampler, RandomSampler
  from ..utils import _match_param
  from ..utils import HasLenGetitemType
label_desc: Use image node_id 1 to create a new TorchDataLoader object from inherited base classes: DataLoader with example: obj = TorchDataLoader(dataset, batch_size, shuffle, sampler, batch_sampler, num_workers, collate_fn, pin_memory, drop_last, timeout, worker_init_fn, multiprocessing_context, generator, prefetch_factor, persistent_workers)