(self)
| 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) |