Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2X training speed improvement: prefetch next batch from the data loader while running current batch #1987

Closed
wants to merge 6 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion src/super_gradients/training/sg_trainer/sg_trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,31 @@ def _train_epoch(self, context: PhaseContext, silent_mode: bool = False) -> tupl

context.update_context(loss_avg_meter=loss_avg_meter, metrics_compute_fn=self.train_metrics)

for batch_idx, batch_items in enumerate(progress_bar_train_loader):
class PrefetchIterator:
koush marked this conversation as resolved.
Show resolved Hide resolved
def __init__(self, iterator):
self.iterator = iterator
import concurrent.futures
self.executor = concurrent.futures.ThreadPoolExecutor(max_workers=1)
self.prefetch()

def prefetch(self):
self.prefetch_future = self.executor.submit(self._prefetch)

def _prefetch(self):
return next(self.iterator)

def __iter__(self):
return self

def __next__(self):
value = self.prefetch_future.result()
self.prefetch()
return value

def close(self):
koush marked this conversation as resolved.
Show resolved Hide resolved
self.executor.shutdown()

for batch_idx, batch_items in PrefetchIterator(enumerate(progress_bar_train_loader)):
koush marked this conversation as resolved.
Show resolved Hide resolved
if expected_iterations <= batch_idx:
break

Expand Down
Loading