class_docstr: DataLoader subclass that always does Poisson sampling and supports empty batches
  by default.
  
  Typically instantiated via ``DPDataLoader.from_data_loader()`` method based
  on another DataLoader. DPDataLoader would preserve the behaviour of the original
  data loader, except for the two aspects.
  
  First, it switches ``batch_sampler`` to ``UniformWithReplacementSampler``, thus enabling
  Poisson sampling (i.e. each element in the dataset is selected to be in the
  next batch with a certain probability defined by ``sample_rate`` parameter).
  NB: this typically leads to a batches of variable size.
  NB2: By default, ``sample_rate`` is calculated based on the ``batch_size`` of the
  original data loader, so that the average batch size stays the same
  
  Second, it wraps collate function with support for empty batches.
  Most PyTorch modules will happily process tensors of shape ``(0, N, ...)``,
  but many collate functions will fail to produce such a batch. As with the
  Poisson sampling empty batches become a possibility, we need a DataLoader that
  can handle them.
class_docstr_tok: ["DataLoader","subclass","that","always","does","Poisson","sampling","and","supports","empty","batches","by","default",".","Typically","instantiated","via","``","DPDataLoader.from_data_loader","(",")","``","method","based","on","another","DataLoader",".","DPDataLoader","would","preserve","the","behaviour","of","the","original","data","loader",",","except","for","the","two","aspects",".","First",",","it","switches","``","batch_sampler","``","to","``","UniformWithReplacementSampler","``",",","thus","enabling","Poisson","sampling","(","i.e",".","each","element","in","the","dataset","is","selected","to","be","in","the","next","batch","with","a","certain","probability","defined","by","``","sample_rate","``","parameter",")",".","NB",":","this","typically","leads","to","a","batches","of","variable","size",".","NB2",":","By","default",",","``","sample_rate","``","is","calculated","based","on","the","``","batch_size","``","of","the","original","data","loader",",","so","that","the","average","batch","size","stays","the","same","Second",",","it","wraps","collate","function","with","support","for","empty","batches",".","Most","PyTorch","modules","will","happily","process","tensors","of","shape","``","(","0",",","N",",","...",")","``",",","but","many","collate","functions","will","fail","to","produce","such","a","batch",".","As","with","the","Poisson","sampling","empty","batches","become","a","possibility",",","we","need","a","DataLoader","that","can","handle","them","."]
docstr: Creates new ``DPDataLoader`` based on passed ``data_loader`` argument.
  
  Args:
      data_loader: Any DataLoader instance. Must not be over an ``IterableDataset``
      distributed: set ``True`` if you'll be using DPDataLoader in a DDP environment
      generator: Random number generator used to sample elements. Defaults to
          generator from the original data loader.
  
  Returns:
      New DPDataLoader instance, with all attributes and parameters inherited
      from the original data loader, except for sampling mechanism.
  
  Examples:
      >>> x, y = torch.randn(64, 5), torch.randint(0, 2, (64,))
      >>> dataset = TensorDataset(x,y)
      >>> data_loader = DataLoader(dataset, batch_size=4)
      >>> dp_data_loader = DPDataLoader.from_data_loader(data_loader)
docstr_tok: ["Creates","new","``","DPDataLoader","``","based","on","passed","``","data_loader","``","argument",".","Args",":","data_loader",":","Any","DataLoader","instance",".","Must","not","be","over","an","``","IterableDataset","``","distributed",":","set","``","True","``","if","you","'ll","be","using","DPDataLoader","in","a","DDP","environment","generator",":","Random","number","generator","used","to","sample","elements",".","Defaults","to","generator","from","the","original","data","loader",".","Returns",":","New","DPDataLoader","instance",",","with","all","attributes","and","parameters","inherited","from","the","original","data","loader",",","except","for","sampling","mechanism",".","Examples",":",">",">",">","x",",","y","=","torch.randn","(","64",",","5",")",",","torch.randint","(","0",",","2",",","(","64",",",")",")",">",">",">","dataset","=","TensorDataset","(","x",",","y",")",">",">",">","data_loader","=","DataLoader","(","dataset",",","batch_size=4",")",">",">",">","dp_data_loader","=","DPDataLoader.from_data_loader","(","data_loader",")"]
code: def from_data_loader(
      cls,
      data_loader: DataLoader,
      *,
      distributed: bool = False,
      generator=None,
  ):
      """
      Creates new ``DPDataLoader`` based on passed ``data_loader`` argument.
  
      Args:
          data_loader: Any DataLoader instance. Must not be over an ``IterableDataset``
          distributed: set ``True`` if you'll be using DPDataLoader in a DDP environment
          generator: Random number generator used to sample elements. Defaults to
              generator from the original data loader.
  
      Returns:
          New DPDataLoader instance, with all attributes and parameters inherited
          from the original data loader, except for sampling mechanism.
  
      Examples:
          >>> x, y = torch.randn(64, 5), torch.randint(0, 2, (64,))
          >>> dataset = TensorDataset(x,y)
          >>> data_loader = DataLoader(dataset, batch_size=4)
          >>> dp_data_loader = DPDataLoader.from_data_loader(data_loader)
      """
  
      if isinstance(data_loader.dataset, IterableDataset):
          raise ValueError(
              "Uniform sampling is not supported for IterableDataset"
          )
  
      return cls(
          dataset=data_loader.dataset,
          sample_rate=1 / len(data_loader),
          num_workers=data_loader.num_workers,
          collate_fn=data_loader.collate_fn,
          pin_memory=data_loader.pin_memory,
          drop_last=data_loader.drop_last,
          timeout=data_loader.timeout,
          worker_init_fn=data_loader.worker_init_fn,
          multiprocessing_context=data_loader.multiprocessing_context,
          generator=generator if generator else data_loader.generator,
          prefetch_factor=data_loader.prefetch_factor,
          persistent_workers=data_loader.persistent_workers,
          distributed=distributed,
      )
  
file_path: opacus/opacus/data_loader.py
imports: import logging
  from functools import partial
  from typing import Any, List, Optional, Sequence, Tuple, Type, Union
  import torch
  from opacus.utils.uniform_sampler import DistributedUniformWithReplacementSampler, UniformWithReplacementSampler
  from torch.utils.data import BatchSampler, DataLoader, Dataset, IterableDataset, Sampler
  from torch.utils.data._utils.collate import default_collate
  from torch.utils.data.dataloader import _collate_fn_t
label_desc: Use image node_id 2 for calling the DPDataLoader obj's underlying member method code with example usage: obj.from_data_loader(cls, data_loader) and returns: cls