class_docstr: Data loader combines a dataset and a sampler, and provides an iterable over the given dataset.
  
  The :class:`~torch.utils.data.DataLoader` supports both map-style and
  iterable-style datasets with single- or multi-process loading, customizing
  loading order and optional automatic batching (collation) and memory pinning.
  
  See :py:mod:`torch.utils.data` documentation page for more details.
  
  Args:
      dataset (Dataset): dataset from which to load the data.
      batch_size (int, optional): how many samples per batch to load
          (default: ``1``).
      shuffle (bool, optional): set to ``True`` to have the data reshuffled
          at every epoch (default: ``False``).
      sampler (Sampler or Iterable, optional): defines the strategy to draw
          samples from the dataset. Can be any ``Iterable`` with ``__len__``
          implemented. If specified, :attr:`shuffle` must not be specified.
      batch_sampler (Sampler or Iterable, optional): like :attr:`sampler`, but
          returns a batch of indices at a time. Mutually exclusive with
          :attr:`batch_size`, :attr:`shuffle`, :attr:`sampler`,
          and :attr:`drop_last`.
      num_workers (int, optional): how many subprocesses to use for data
          loading. ``0`` means that the data will be loaded in the main process.
          (default: ``0``)
      collate_fn (Callable, optional): merges a list of samples to form a
          mini-batch of Tensor(s).  Used when using batched loading from a
          map-style dataset.
      pin_memory (bool, optional): If ``True``, the data loader will copy Tensors
          into device/CUDA pinned memory before returning them.  If your data elements
          are a custom type, or your :attr:`collate_fn` returns a batch that is a custom type,
          see the example below.
      drop_last (bool, optional): set to ``True`` to drop the last incomplete batch,
          if the dataset size is not divisible by the batch size. If ``False`` and
          the size of dataset is not divisible by the batch size, then the last batch
          will be smaller. (default: ``False``)
      timeout (numeric, optional): if positive, the timeout value for collecting a batch
          from workers. Should always be non-negative. (default: ``0``)
      worker_init_fn (Callable, optional): If not ``None``, this will be called on each
          worker subprocess with the worker id (an int in ``[0, num_workers - 1]``) as
          input, after seeding and before data loading. (default: ``None``)
      multiprocessing_context (str or multiprocessing.context.BaseContext, optional): If
          ``None``, the default `multiprocessing context`_ of your operating system will
          be used. (default: ``None``)
      generator (torch.Generator, optional): If not ``None``, this RNG will be used
          by RandomSampler to generate random indexes and multiprocessing to generate
          ``base_seed`` for workers. (default: ``None``)
      prefetch_factor (int, optional, keyword-only arg): Number of batches loaded
          in advance by each worker. ``2`` means there will be a total of
          2 * num_workers batches prefetched across all workers. (default value depends
          on the set value for num_workers. If value of num_workers=0 default is ``None``.
          Otherwise, if value of ``num_workers > 0`` default is ``2``).
      persistent_workers (bool, optional): If ``True``, the data loader will not shut down
          the worker processes after a dataset has been consumed once. This allows to
          maintain the workers `Dataset` instances alive. (default: ``False``)
      pin_memory_device (str, optional): the device to :attr:`pin_memory` to if ``pin_memory`` is
          ``True``.
  
  
  .. warning:: If the ``spawn`` start method is used, :attr:`worker_init_fn`
               cannot be an unpicklable object, e.g., a lambda function. See
               :ref:`multiprocessing-best-practices` on more details related
               to multiprocessing in PyTorch.
  
  .. warning:: ``len(dataloader)`` heuristic is based on the length of the sampler used.
               When :attr:`dataset` is an :class:`~torch.utils.data.IterableDataset`,
               it instead returns an estimate based on ``len(dataset) / batch_size``, with proper
               rounding depending on :attr:`drop_last`, regardless of multi-process loading
               configurations. This represents the best guess PyTorch can make because PyTorch
               trusts user :attr:`dataset` code in correctly handling multi-process
               loading to avoid duplicate data.
  
               However, if sharding results in multiple workers having incomplete last batches,
               this estimate can still be inaccurate, because (1) an otherwise complete batch can
               be broken into multiple ones and (2) more than one batch worth of samples can be
               dropped when :attr:`drop_last` is set. Unfortunately, PyTorch can not detect such
               cases in general.
  
               See `Dataset Types`_ for more details on these two types of datasets and how
               :class:`~torch.utils.data.IterableDataset` interacts with
               `Multi-process data loading`_.
  
  .. warning:: See :ref:`reproducibility`, and :ref:`dataloader-workers-random-seed`, and
               :ref:`data-loading-randomness` notes for random seed related questions.
  
  .. _multiprocessing context:
      https://docs.python.org/3/library/multiprocessing.html#contexts-and-start-methods
class_docstr_tok: ["Data","loader","combines","a","dataset","and","a","sampler",",","and","provides","an","iterable","over","the","given","dataset",".","The",":","class",":","`","~torch.utils.data.DataLoader","`","supports","both","map-style","and","iterable-style","datasets","with","single-","or","multi-process","loading",",","customizing","loading","order","and","optional","automatic","batching","(","collation",")","and","memory","pinning",".","See",":","py",":","mod",":","`","torch.utils.data","`","documentation","page","for","more","details",".","Args",":","dataset","(","Dataset",")",":","dataset","from","which","to","load","the","data",".","batch_size","(","int",",","optional",")",":","how","many","samples","per","batch","to","load","(","default",":","``","1","``",")",".","shuffle","(","bool",",","optional",")",":","set","to","``","True","``","to","have","the","data","reshuffled","at","every","epoch","(","default",":","``","False","``",")",".","sampler","(","Sampler","or","Iterable",",","optional",")",":","defines","the","strategy","to","draw","samples","from","the","dataset",".","Can","be","any","``","Iterable","``","with","``","__len__","``","implemented",".","If","specified",",",":","attr",":","`","shuffle","`","must","not","be","specified",".","batch_sampler","(","Sampler","or","Iterable",",","optional",")",":","like",":","attr",":","`","sampler","`",",","but","returns","a","batch","of","indices","at","a","time",".","Mutually","exclusive","with",":","attr",":","`","batch_size","`",",",":","attr",":","`","shuffle","`",",",":","attr",":","`","sampler","`",",","and",":","attr",":","`","drop_last","`",".","num_workers","(","int",",","optional",")",":","how","many","subprocesses","to","use","for","data","loading",".","``","0","``","means","that","the","data","will","be","loaded","in","the","main","process",".","(","default",":","``","0","``",")","collate_fn","(","Callable",",","optional",")",":","merges","a","list","of","samples","to","form","a","mini-batch","of","Tensor","(","s",")",".","Used","when","using","batched","loading","from","a","map-style","dataset",".","pin_memory","(","bool",",","optional",")",":","If","``","True","``",",","the","data","loader","will","copy","Tensors","into","device\/CUDA","pinned","memory","before","returning","them",".","If","your","data","elements","are","a","custom","type",",","or","your",":","attr",":","`","collate_fn","`","returns","a","batch","that","is","a","custom","type",",","see","the","example","below",".","drop_last","(","bool",",","optional",")",":","set","to","``","True","``","to","drop","the","last","incomplete","batch",",","if","the","dataset","size","is","not","divisible","by","the","batch","size",".","If","``","False","``","and","the","size","of","dataset","is","not","divisible","by","the","batch","size",",","then","the","last","batch","will","be","smaller",".","(","default",":","``","False","``",")","timeout","(","numeric",",","optional",")",":","if","positive",",","the","timeout","value","for","collecting","a","batch","from","workers",".","Should","always","be","non-negative",".","(","default",":","``","0","``",")","worker_init_fn","(","Callable",",","optional",")",":","If","not","``","None","``",",","this","will","be","called","on","each","worker","subprocess","with","the","worker","id","(","an","int","in","``","[","0",",","num_workers","-","1","]","``",")","as","input",",","after","seeding","and","before","data","loading",".","(","default",":","``","None","``",")","multiprocessing_context","(","str","or","multiprocessing.context.BaseContext",",","optional",")",":","If","``","None","``",",","the","default","`","multiprocessing","context","`","_","of","your","operating","system","will","be","used",".","(","default",":","``","None","``",")","generator","(","torch.Generator",",","optional",")",":","If","not","``","None","``",",","this","RNG","will","be","used","by","RandomSampler","to","generate","random","indexes","and","multiprocessing","to","generate","``","base_seed","``","for","workers",".","(","default",":","``","None","``",")","prefetch_factor","(","int",",","optional",",","keyword-only","arg",")",":","Number","of","batches","loaded","in","advance","by","each","worker",".","``","2","``","means","there","will","be","a","total","of","2","*","num_workers","batches","prefetched","across","all","workers",".","(","default","value","depends","on","the","set","value","for","num_workers",".","If","value","of","num_workers=0","default","is","``","None","``",".","Otherwise",",","if","value","of","``","num_workers",">","0","``","default","is","``","2","``",")",".","persistent_workers","(","bool",",","optional",")",":","If","``","True","``",",","the","data","loader","will","not","shut","down","the","worker","processes","after","a","dataset","has","been","consumed","once",".","This","allows","to","maintain","the","workers","`","Dataset","`","instances","alive",".","(","default",":","``","False","``",")","pin_memory_device","(","str",",","optional",")",":","the","device","to",":","attr",":","`","pin_memory","`","to","if","``","pin_memory","``","is","``","True","``",".","..","warning",":",":","If","the","``","spawn","``","start","method","is","used",",",":","attr",":","`","worker_init_fn","`","can","not","be","an","unpicklable","object",",","e.g.",",","a","lambda","function",".","See",":","ref",":","`","multiprocessing-best-practices","`","on","more","details","related","to","multiprocessing","in","PyTorch",".","..","warning",":",":","``","len","(","dataloader",")","``","heuristic","is","based","on","the","length","of","the","sampler","used",".","When",":","attr",":","`","dataset","`","is","an",":","class",":","`","~torch.utils.data.IterableDataset","`",",","it","instead","returns","an","estimate","based","on","``","len","(","dataset",")","\/","batch_size","``",",","with","proper","rounding","depending","on",":","attr",":","`","drop_last","`",",","regardless","of","multi-process","loading","configurations",".","This","represents","the","best","guess","PyTorch","can","make","because","PyTorch","trusts","user",":","attr",":","`","dataset","`","code","in","correctly","handling","multi-process","loading","to","avoid","duplicate","data",".","However",",","if","sharding","results","in","multiple","workers","having","incomplete","last","batches",",","this","estimate","can","still","be","inaccurate",",","because","(","1",")","an","otherwise","complete","batch","can","be","broken","into","multiple","ones","and","(","2",")","more","than","one","batch","worth","of","samples","can","be","dropped","when",":","attr",":","`","drop_last","`","is","set",".","Unfortunately",",","PyTorch","can","not","detect","such","cases","in","general",".","See","`","Dataset","Types","`","_","for","more","details","on","these","two","types","of","datasets","and","how",":","class",":","`","~torch.utils.data.IterableDataset","`","interacts","with","`","Multi-process","data","loading","`","_",".","..","warning",":",":","See",":","ref",":","`","reproducibility","`",",","and",":","ref",":","`","dataloader-workers-random-seed","`",",","and",":","ref",":","`","data-loading-randomness","`","notes","for","random","seed","related","questions",".","..","_multiprocessing","context",":","https",":","\/\/docs.python.org\/3\/library\/multiprocessing.html","#","contexts-and-start-methods"]
code: def __init__(
      self,
      dataset: Dataset[T_co],
      batch_size: Optional[int] = 1,
      shuffle: Optional[bool] = None,
      sampler: Union[Sampler, Iterable, None] = None,
      batch_sampler: Union[Sampler[List], Iterable[List], None] = None,
      num_workers: int = 0,
      collate_fn: Optional[_collate_fn_t] = None,
      pin_memory: bool = False,
      drop_last: bool = False,
      timeout: float = 0,
      worker_init_fn: Optional[_worker_init_fn_t] = None,
      multiprocessing_context=None,
      generator=None,
      *,
      prefetch_factor: Optional[int] = None,
      persistent_workers: bool = False,
      pin_memory_device: str = "",
  ):
      torch._C._log_api_usage_once("python.data_loader")
  
      if num_workers < 0:
          raise ValueError(
              "num_workers option should be non-negative; "
              "use num_workers=0 to disable multiprocessing."
          )
  
      if timeout < 0:
          raise ValueError("timeout option should be non-negative")
  
      if num_workers == 0 and prefetch_factor is not None:
          raise ValueError(
              "prefetch_factor option could only be specified in multiprocessing."
              "let num_workers > 0 to enable multiprocessing, otherwise set prefetch_factor to None."
          )
      elif num_workers > 0 and prefetch_factor is None:
          prefetch_factor = 2
      elif prefetch_factor is not None and prefetch_factor < 0:
          raise ValueError(
              "prefetch_factor option should be non-negative"
          )
  
      if persistent_workers and num_workers == 0:
          raise ValueError(
              "persistent_workers option needs num_workers > 0"
          )
  
      self.dataset = dataset
      self.num_workers = num_workers
      self.prefetch_factor = prefetch_factor
      self.pin_memory = pin_memory
      self.pin_memory_device = pin_memory_device
      self.timeout = timeout
      self.worker_init_fn = worker_init_fn
      self.multiprocessing_context = multiprocessing_context
  
      # Adds forward compatibilities so classic DataLoader can work with DataPipes:
      #   _DataPipeSerializationWrapper container makes it easier to serialize without redefining pickler
      if isinstance(self.dataset, IterDataPipe):
          self.dataset = _IterDataPipeSerializationWrapper(self.dataset)
      elif isinstance(self.dataset, MapDataPipe):
          self.dataset = _MapDataPipeSerializationWrapper(self.dataset)
  
      # Arg-check dataset related before checking samplers because we want to
      # tell users that iterable-style datasets are incompatible with custom
      # samplers first, so that they don't learn that this combo doesn't work
      # after spending time fixing the custom sampler errors.
      if isinstance(dataset, IterableDataset):
          self._dataset_kind = _DatasetKind.Iterable
          # NOTE [ Custom Samplers and IterableDataset ]
          #
          # `IterableDataset` does not support custom `batch_sampler` or
          # `sampler` since the key is irrelevant (unless we support
          # generator-style dataset one day...).
          #
          # For `sampler`, we always create a dummy sampler. This is an
          # infinite sampler even when the dataset may have an implemented
          # finite `__len__` because in multi-process data loading, naive
          # settings will return duplicated data (which may be desired), and
          # thus using a sampler with length matching that of dataset will
          # cause data lost (you may have duplicates of the first couple
          # batches, but never see anything afterwards). Therefore,
          # `Iterabledataset` always uses an infinite sampler, an instance of
          # `_InfiniteConstantSampler` defined above.
          #
          # A custom `batch_sampler` essentially only controls the batch size.
          # However, it is unclear how useful it would be since an iterable-style
          # dataset can handle that within itself. Moreover, it is pointless
          # in multi-process data loading as the assignment order of batches
          # to workers is an implementation detail so users can not control
          # how to batchify each worker's iterable. Thus, we disable this
          # option. If this turns out to be useful in future, we can re-enable
          # this, and support custom samplers that specify the assignments to
          # specific workers.
          if isinstance(dataset, IterDataPipe):
              if shuffle is not None:
                  dataset = torch.utils.data.graph_settings.apply_shuffle_settings(
                      dataset, shuffle=shuffle
                  )
          # We cannot check `shuffle is not None` here, since previously `shuffle=False` was the default.
          elif shuffle not in {False, None}:
              raise ValueError(
                  f"DataLoader with IterableDataset: expected unspecified shuffle option, but got shuffle={shuffle}"
              )
  
          if sampler is not None:
              # See NOTE [ Custom Samplers and IterableDataset ]
              raise ValueError(
                  f"DataLoader with IterableDataset: expected unspecified sampler option, but got sampler={sampler}"
              )
          elif batch_sampler is not None:
              # See NOTE [ Custom Samplers and IterableDataset ]
              raise ValueError(
                  "DataLoader with IterableDataset: expected unspecified "
                  f"batch_sampler option, but got batch_sampler={batch_sampler}"
              )
      else:
          shuffle = bool(shuffle)
          self._dataset_kind = _DatasetKind.Map
  
      if sampler is not None and shuffle:
          raise ValueError(
              "sampler option is mutually exclusive with " "shuffle"
          )
  
      if batch_sampler is not None:
          # auto_collation with custom batch_sampler
          if (
              batch_size != 1
              or shuffle
              or sampler is not None
              or drop_last
          ):
              raise ValueError(
                  "batch_sampler option is mutually exclusive "
                  "with batch_size, shuffle, sampler, and "
                  "drop_last"
              )
          batch_size = None
          drop_last = False
      elif batch_size is None:
          # no auto_collation
          if drop_last:
              raise ValueError(
                  "batch_size=None option disables auto-batching "
                  "and is mutually exclusive with drop_last"
              )
  
      if sampler is None:  # give default samplers
          if self._dataset_kind == _DatasetKind.Iterable:
              # See NOTE [ Custom Samplers and IterableDataset ]
              sampler = _InfiniteConstantSampler()
          else:  # map-style
              if shuffle:
                  sampler = RandomSampler(dataset, generator=generator)  # type: ignore[arg-type]
              else:
                  sampler = SequentialSampler(dataset)  # type: ignore[arg-type]
  
      if batch_size is not None and batch_sampler is None:
          # auto_collation without custom batch_sampler
          batch_sampler = BatchSampler(sampler, batch_size, drop_last)
  
      self.batch_size = batch_size
      self.drop_last = drop_last
      self.sampler = sampler
      self.batch_sampler = batch_sampler
      self.generator = generator
  
      if collate_fn is None:
          if self._auto_collation:
              collate_fn = _utils.collate.default_collate
          else:
              collate_fn = _utils.collate.default_convert
  
      self.collate_fn = collate_fn
      self.persistent_workers = persistent_workers
  
      self.__initialized = True
      self._IterableDataset_len_called = (
          None  # See NOTE [ IterableDataset and __len__ ]
      )
  
      self._iterator = None
  
      self.check_worker_number_rationality()
  
      torch.set_vital("Dataloader", "enabled", "True")
  
imports: import functools
  import itertools
  import logging
  import os
  import queue
  import threading
  import warnings
  from typing import Any, Callable, Iterable, TypeVar, Generic, List, Optional, Union
  import multiprocessing
  import torch
  import torch.distributed
  import torch.multiprocessing
  import torch.utils.data.graph_settings
  from torch._utils import ExceptionWrapper
  from .None import IterDataPipe, MapDataPipe, IterableDataset, Sampler, SequentialSampler, RandomSampler, BatchSampler, Dataset
  from torch.utils.data.datapipes.datapipe import _IterDataPipeSerializationWrapper, _MapDataPipeSerializationWrapper
  from .None import _utils
label_desc: Use image node_id 1 to create a new DataLoader object from inherited base classes: Generic with example: obj = DataLoader(dataset, batch_size, shuffle, sampler, batch_sampler, num_workers, collate_fn, pin_memory, drop_last, timeout, worker_init_fn, multiprocessing_context, generator)