MCPcopy Create free account
hub / github.com/DragonisCV/RAM / CUDAPrefetcher

Class CUDAPrefetcher

ram/data/utils/prefetch_dataloader.py:84–125  ·  view source on GitHub ↗

CUDA prefetcher. Ref: https://github.com/NVIDIA/apex/issues/304# It may consums more GPU memory. Args: loader: Dataloader. opt (dict): Options.

Source from the content-addressed store, hash-verified

82
83
84class CUDAPrefetcher():
85 """CUDA prefetcher.
86
87 Ref:
88 https://github.com/NVIDIA/apex/issues/304#
89
90 It may consums more GPU memory.
91
92 Args:
93 loader: Dataloader.
94 opt (dict): Options.
95 """
96
97 def __init__(self, loader, opt):
98 self.ori_loader = loader
99 self.loader = iter(loader)
100 self.opt = opt
101 self.stream = torch.cuda.Stream()
102 self.device = torch.device('cuda' if opt['num_gpu'] != 0 else 'cpu')
103 self.preload()
104
105 def preload(self):
106 try:
107 self.batch = next(self.loader) # self.batch is a dict
108 except StopIteration:
109 self.batch = None
110 return None
111 # put tensors to gpu
112 with torch.cuda.stream(self.stream):
113 for k, v in self.batch.items():
114 if torch.is_tensor(v):
115 self.batch[k] = self.batch[k].to(device=self.device, non_blocking=True)
116
117 def next(self):
118 torch.cuda.current_stream().wait_stream(self.stream)
119 batch = self.batch
120 self.preload()
121 return batch
122
123 def reset(self):
124 self.loader = iter(self.ori_loader)
125 self.preload()

Callers 1

train_pipelineFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected