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

Refactor data fetcher selection in loops #18494

Merged
merged 4 commits into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 2 additions & 1 deletion src/lightning/pytorch/loops/evaluation_loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,8 @@ def reset(self) -> None:
if fn != TrainerFn.FITTING:
self.batch_progress.reset_on_run()

data_fetcher = _select_data_fetcher(trainer)
assert trainer.state.stage is not None
data_fetcher = _select_data_fetcher(trainer, trainer.state.stage)
combined_loader = self._combined_loader
assert combined_loader is not None

Expand Down
6 changes: 1 addition & 5 deletions src/lightning/pytorch/loops/fit_loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,11 +253,7 @@ def setup_data(self) -> None:

combined_loader.limits = limits

training = trainer.training
trainer.training = True
self._data_fetcher = _select_data_fetcher(trainer)
trainer.training = training

self._data_fetcher = _select_data_fetcher(trainer, RunningStage.TRAINING)
self._data_fetcher.setup(combined_loader)
iter(self._data_fetcher) # creates the iterator inside the fetcher
max_batches = sized_len(combined_loader)
Expand Down
3 changes: 2 additions & 1 deletion src/lightning/pytorch/loops/prediction_loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,8 @@ def reset(self) -> None:
"""Resets the internal state of the loop for a new run."""
self.batch_progress.reset_on_run()

data_fetcher = _select_data_fetcher(self.trainer)
assert self.trainer.state.stage is not None
data_fetcher = _select_data_fetcher(self.trainer, self.trainer.state.stage)
combined_loader = self._combined_loader
assert combined_loader is not None
if combined_loader._mode != "sequential":
Expand Down
10 changes: 5 additions & 5 deletions src/lightning/pytorch/loops/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,15 +131,15 @@ def _reset_progress(loop: _Loop) -> None:
_reset_progress(v)


def _select_data_fetcher(trainer: "pl.Trainer") -> _DataFetcher:
def _select_data_fetcher(trainer: "pl.Trainer", stage: RunningStage) -> _DataFetcher:
lightning_module = trainer.lightning_module
if trainer.testing:
if stage == RunningStage.TESTING:
step_fx_name = "test_step"
elif trainer.training:
elif stage == RunningStage.TRAINING:
step_fx_name = "training_step"
elif trainer.validating or trainer.sanity_checking:
elif stage in (RunningStage.VALIDATING, RunningStage.SANITY_CHECKING):
step_fx_name = "validation_step"
elif trainer.predicting:
elif stage == RunningStage.PREDICTING:
step_fx_name = "predict_step"
else:
raise RuntimeError(f"DataFetcher is unsupported for {trainer.state.stage}")
Expand Down