class_docstr: CudaDataPrefetcher prefetches batches and moves them to the device.
  
  This class can be used to interleave data loading, host-to-device copies, and computation more effectively.
  
  Args:
  
      data_iterable: an Iterable containing the data to use for CudaDataPrefetcher construction
      device: the device to which data should be moved
      num_prefetch_batches: number of batches to prefetch
  
  Note:
  
      We recommend users leverage memory pinning when constructing their dataloader:
      https://pytorch.org/docs/stable/data.html#memory-pinning.
  
  Example::
  
      dataloader = ...
      device = torch.device("cuda")
      num_prefetch_batches = 2
      data_prefetcher = CudaDataPrefetcher(dataloader, device, num_prefetch_batches)
      for batch in data_prefetcher:
          # batch is already on device
          # operate on batch
class_docstr_tok: ["CudaDataPrefetcher","prefetches","batches","and","moves","them","to","the","device",".","This","class","can","be","used","to","interleave","data","loading",",","host-to-device","copies",",","and","computation","more","effectively",".","Args",":","data_iterable",":","an","Iterable","containing","the","data","to","use","for","CudaDataPrefetcher","construction","device",":","the","device","to","which","data","should","be","moved","num_prefetch_batches",":","number","of","batches","to","prefetch","Note",":","We","recommend","users","leverage","memory","pinning","when","constructing","their","dataloader",":","https",":","\/\/pytorch.org\/docs\/stable\/data.html","#","memory-pinning",".","Example",":",":","dataloader","=","...","device","=","torch.device","(","``","cuda","''",")","num_prefetch_batches","=","2","data_prefetcher","=","CudaDataPrefetcher","(","dataloader",",","device",",","num_prefetch_batches",")","for","batch","in","data_prefetcher",":","#","batch","is","already","on","device","#","operate","on","batch"]
code: def __next__(self) -> Batch:
      if not self._prefetched:
          self._prefetch()
          self._prefetched = True
      # wait for the prefetch stream to complete host to device copy
      if self._events:
          event = self._events.pop(0)
          event.wait()
      else:
          raise StopIteration
  
      if self._batches:
          # there are pre-fetched batches already from a previous `prefetching` call.
          # consume one
          batch = self._batches.pop(0)
          # refill the consumed batch
          try:
              self._fetch_next_batch(self.data_iter)
          except StopIteration:
              pass
      else:
          # the iterator is empty
          raise StopIteration
  
      return batch
  
file_path: tnt/torchtnt/utils/data/data_prefetcher.py
imports: from typing import Iterable, Iterator, List, TypeVar
  import torch
  from torchtnt.utils.device import copy_data_to_device
label_desc: Use image node_id 6 for calling the CudaDataPrefetcher obj's underlying member method code with example usage: obj.__next__() and returns: batch