class_docstr: Sampler that restricts data loading to a subset of the dataset.
  
  It is especially useful in conjunction with
  :class:`torch.nn.parallel.DistributedDataParallel`. In such a case, each
  process can pass a :class:`~torch.utils.data.DistributedSampler` instance as a
  :class:`~torch.utils.data.DataLoader` sampler, and load a subset of the
  original dataset that is exclusive to it.
  
  .. note::
      Dataset is assumed to be of constant size.
  
  Args:
      dataset: Dataset used for sampling.
      num_replicas (int, optional): Number of processes participating in
          distributed training. By default, :attr:`world_size` is retrieved from the
          current distributed group.
      rank (int, optional): Rank of the current process within :attr:`num_replicas`.
          By default, :attr:`rank` is retrieved from the current distributed
          group.
      shuffle (bool, optional): If ``True`` (default), sampler will shuffle the
          indices.
      seed (int, optional): random seed used to shuffle the sampler if
          :attr:`shuffle=True`. This number should be identical across all
          processes in the distributed group. Default: ``0``.
      drop_last (bool, optional): if ``True``, then the sampler will drop the
          tail of the data to make it evenly divisible across the number of
          replicas. If ``False``, the sampler will add extra indices to make
          the data evenly divisible across the replicas. Default: ``False``.
  
  .. warning::
      In distributed mode, calling the :meth:`set_epoch` method at
      the beginning of each epoch **before** creating the :class:`DataLoader` iterator
      is necessary to make shuffling work properly across multiple epochs. Otherwise,
      the same ordering will be always used.
  
  Example::
  
      >>> sampler = DistributedSampler(dataset) if is_distributed else None
      >>> loader = DataLoader(dataset, shuffle=(sampler is None),
      ...                     sampler=sampler)
      >>> for epoch in range(start_epoch, n_epochs):
      ...     if is_distributed:
      ...         sampler.set_epoch(epoch)
      ...     train(loader)
class_docstr_tok: ["Sampler","that","restricts","data","loading","to","a","subset","of","the","dataset",".","It","is","especially","useful","in","conjunction","with",":","class",":","`","torch.nn.parallel.DistributedDataParallel","`",".","In","such","a","case",",","each","process","can","pass","a",":","class",":","`","~torch.utils.data.DistributedSampler","`","instance","as","a",":","class",":","`","~torch.utils.data.DataLoader","`","sampler",",","and","load","a","subset","of","the","original","dataset","that","is","exclusive","to","it",".","..","note",":",":","Dataset","is","assumed","to","be","of","constant","size",".","Args",":","dataset",":","Dataset","used","for","sampling",".","num_replicas","(","int",",","optional",")",":","Number","of","processes","participating","in","distributed","training",".","By","default",",",":","attr",":","`","world_size","`","is","retrieved","from","the","current","distributed","group",".","rank","(","int",",","optional",")",":","Rank","of","the","current","process","within",":","attr",":","`","num_replicas","`",".","By","default",",",":","attr",":","`","rank","`","is","retrieved","from","the","current","distributed","group",".","shuffle","(","bool",",","optional",")",":","If","``","True","``","(","default",")",",","sampler","will","shuffle","the","indices",".","seed","(","int",",","optional",")",":","random","seed","used","to","shuffle","the","sampler","if",":","attr",":","`","shuffle=True","`",".","This","number","should","be","identical","across","all","processes","in","the","distributed","group",".","Default",":","``","0","``",".","drop_last","(","bool",",","optional",")",":","if","``","True","``",",","then","the","sampler","will","drop","the","tail","of","the","data","to","make","it","evenly","divisible","across","the","number","of","replicas",".","If","``","False","``",",","the","sampler","will","add","extra","indices","to","make","the","data","evenly","divisible","across","the","replicas",".","Default",":","``","False","``",".","..","warning",":",":","In","distributed","mode",",","calling","the",":","meth",":","`","set_epoch","`","method","at","the","beginning","of","each","epoch","*","*","before","*","*","creating","the",":","class",":","`","DataLoader","`","iterator","is","necessary","to","make","shuffling","work","properly","across","multiple","epochs",".","Otherwise",",","the","same","ordering","will","be","always","used",".","Example",":",":",">",">",">","sampler","=","DistributedSampler","(","dataset",")","if","is_distributed","else","None",">",">",">","loader","=","DataLoader","(","dataset",",","shuffle=","(","sampler","is","None",")",",","...","sampler=sampler",")",">",">",">","for","epoch","in","range","(","start_epoch",",","n_epochs",")",":","...","if","is_distributed",":","...","sampler.set_epoch","(","epoch",")","...","train","(","loader",")"]
code: def generate_indices(self):
      if self.shuffle:
          # deterministically shuffle based on epoch and seed
          g = torch.Generator()
          g.manual_seed(self.seed + self.epoch)
          indices = torch.randperm(
              len(self.dataset), generator=g
          ).tolist()
      else:
          indices = list(range(len(self.dataset)))
  
      if not self.drop_last:
          # add extra samples to make it evenly divisible
          padding_size = self.total_size - len(indices)
          if padding_size <= len(indices):
              indices += indices[:padding_size]
          else:
              indices += (
                  indices * math.ceil(padding_size / len(indices))
              )[:padding_size]
      else:
          # remove tail of data to make it evenly divisible.
          indices = indices[: self.total_size]
      assert len(indices) == self.total_size
  
      # subsample
      indices = indices[self.rank : self.total_size : self.num_replicas]
      assert len(indices) == self.num_samples
  
      return indices
  
imports: from typing import Iterator, Optional, Sized, Sequence
  import math
  from ....utils import require_not_none
label_desc: Use image node_id 2 for calling the DistributedSampler obj's underlying member method code with example usage: obj.generate_indices() and returns: indices