class_docstr: In training, we only care about the "infinite stream" of training data.
  So this sampler produces an infinite stream of indices and
  all workers cooperate to correctly shuffle the indices and sample different indices.
  
  The samplers in each worker effectively produces `indices[worker_id::num_workers]`
  where `indices` is an infinite stream of indices consisting of
  `shuffle(range(size)) + shuffle(range(size)) + ...` (if shuffle is True)
  or `range(size) + range(size) + ...` (if shuffle is False)
  
  Note that this sampler does not shard based on pytorch DataLoader worker id.
  A sampler passed to pytorch DataLoader is used only with map-style dataset
  and will not be executed inside workers.
  But if this sampler is used in a way that it gets execute inside a dataloader
  worker, then extra work needs to be done to shard its outputs based on worker id.
  This is required so that workers don't produce identical data.
  :class:`ToIterableDataset` implements this logic.
  This note is true for all samplers in detectron2.
class_docstr_tok: ["In","training",",","we","only","care","about","the","``","infinite","stream","''","of","training","data",".","So","this","sampler","produces","an","infinite","stream","of","indices","and","all","workers","cooperate","to","correctly","shuffle","the","indices","and","sample","different","indices",".","The","samplers","in","each","worker","effectively","produces","`","indices","[","worker_id",":",":num_workers","]","`","where","`","indices","`","is","an","infinite","stream","of","indices","consisting","of","`","shuffle","(","range","(","size",")",")","+","shuffle","(","range","(","size",")",")","+","...","`","(","if","shuffle","is","True",")","or","`","range","(","size",")","+","range","(","size",")","+","...","`","(","if","shuffle","is","False",")","Note","that","this","sampler","does","not","shard","based","on","pytorch","DataLoader","worker","id",".","A","sampler","passed","to","pytorch","DataLoader","is","used","only","with","map-style","dataset","and","will","not","be","executed","inside","workers",".","But","if","this","sampler","is","used","in","a","way","that","it","gets","execute","inside","a","dataloader","worker",",","then","extra","work","needs","to","be","done","to","shard","its","outputs","based","on","worker","id",".","This","is","required","so","that","workers","do","n't","produce","identical","data",".",":","class",":","`","ToIterableDataset","`","implements","this","logic",".","This","note","is","true","for","all","samplers","in","detectron2","."]
docstr: Args:
      size (int): the total number of data of the underlying dataset to sample from
      shuffle (bool): whether to shuffle the indices or not
      seed (int): the initial seed of the shuffle. Must be the same
          across all workers. If None, will use a random seed shared
          among workers (require synchronization among all workers).
docstr_tok: ["Args",":","size","(","int",")",":","the","total","number","of","data","of","the","underlying","dataset","to","sample","from","shuffle","(","bool",")",":","whether","to","shuffle","the","indices","or","not","seed","(","int",")",":","the","initial","seed","of","the","shuffle",".","Must","be","the","same","across","all","workers",".","If","None",",","will","use","a","random","seed","shared","among","workers","(","require","synchronization","among","all","workers",")","."]
code: def __init__(
      self, size: int, shuffle: bool = True, seed: Optional[int] = None
  ):
      """
      Args:
          size (int): the total number of data of the underlying dataset to sample from
          shuffle (bool): whether to shuffle the indices or not
          seed (int): the initial seed of the shuffle. Must be the same
              across all workers. If None, will use a random seed shared
              among workers (require synchronization among all workers).
      """
      if not isinstance(size, int):
          raise TypeError(
              f"TrainingSampler(size=) expects an int. Got type {type(size)}."
          )
      if size <= 0:
          raise ValueError(
              f"TrainingSampler(size=) expects a positive int. Got {size}."
          )
      self._size = size
      self._shuffle = shuffle
      if seed is None:
          seed = comm.shared_random_seed()
      self._seed = int(seed)
  
      self._rank = comm.get_rank()
      self._world_size = comm.get_world_size()
  
imports: import itertools
  import logging
  import math
  from collections import defaultdict
  from typing import Optional
  import torch
  from torch.utils.data.sampler import Sampler
  from detectron2.utils import comm
label_desc: Use image node_id 1 to create a new TrainingSampler object from inherited base classes: Sampler with example: obj = TrainingSampler(size, shuffle, seed)