-
Notifications
You must be signed in to change notification settings - Fork 3.6k
feat: Add manual dataloader reloading feature (Closes #21448) #21473
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
Open
arrdel
wants to merge
11
commits into
Lightning-AI:master
Choose a base branch
from
arrdel:feature/manual-dataloader-reload-21448
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+552
−0
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
910a712
Add adapt_checkpoint_hparams hook for customizing checkpoint hyperpar…
arrdel ad1a028
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 998ea3e
refactor(cli): Add subcommand parameter to adapt_checkpoint_hparams h…
arrdel 00e7032
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] b3b1025
fix: Break long line in adapt_checkpoint_hparams docstring example
arrdel fc8cc3a
fix: Replace BoringCkptPathModel with AdaptHparamsModel to fix tensor…
arrdel a0f0d77
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 17d3b30
fix: Change AdaptHparamsModel to inherit from BoringModel
arrdel 204afb7
fix: Pass hidden_dim explicitly in test to fix assertion
arrdel 34eaa02
Add manual dataloader reloading feature
arrdel 0d6c2bb
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1206,6 +1206,60 @@ def print(self, *args: Any, **kwargs: Any) -> None: | |
| if self.local_rank == 0: | ||
| print(*args, **kwargs) | ||
|
|
||
| def reload_dataloaders(self, train: bool = True, val: bool = False) -> None: | ||
| """Manually trigger a reload of dataloaders during training. | ||
|
|
||
| This method allows dynamic reconfiguration of DataLoaders without exiting the ``fit()`` loop. | ||
| It's useful for curriculum learning, adaptive training strategies, or any scenario where | ||
| DataLoader parameters need to change based on training metrics or progress. | ||
|
|
||
| The reload will occur at the start of the next epoch during training. | ||
|
|
||
| Args: | ||
| train: If ``True``, reload the train dataloader. Default: ``True``. | ||
| val: If ``True``, reload the validation dataloader. Default: ``False``. | ||
|
|
||
| Example:: | ||
|
|
||
| # In a callback | ||
| def on_train_epoch_end(self, trainer, pl_module): | ||
| if trainer.current_epoch == 5: | ||
| # Update datamodule parameters | ||
| trainer.datamodule.sequence_length += 10 | ||
| # Trigger reload for next epoch | ||
| trainer.reload_dataloaders(train=True, val=True) | ||
|
|
||
| # In a LightningModule | ||
| def on_train_batch_end(self, outputs, batch, batch_idx): | ||
| if self.trainer.callback_metrics.get('train_loss', 1.0) < 0.5: | ||
| self.trainer.datamodule.unroll_steps += 1 | ||
| self.trainer.reload_dataloaders() | ||
|
|
||
| Raises: | ||
| RuntimeError: If called outside of a ``fit()`` call. | ||
|
|
||
| .. note:: | ||
|
|
||
| The actual reload happens at the beginning of the next training epoch, | ||
| not immediately when this method is called. This ensures training state | ||
| consistency and proper synchronization in distributed settings. | ||
|
|
||
| """ | ||
| if not self.training: | ||
| raise RuntimeError( | ||
| "`trainer.reload_dataloaders()` can only be called during training (inside `trainer.fit()`)." | ||
| ) | ||
|
|
||
| if train: | ||
| # Setting to -inf ensures _should_reload_train_dl returns True | ||
| self.fit_loop._last_train_dl_reload_epoch = float("-inf") | ||
| rank_zero_info("Train dataloader will be reloaded at the start of the next epoch.") | ||
|
|
||
| if val: | ||
| # Setting to -inf ensures _should_reload_val_dl returns True | ||
| self.fit_loop.epoch_loop.val_loop._last_val_dl_reload_epoch = float("-inf") | ||
| rank_zero_info("Validation dataloader will be reloaded at the next validation check.") | ||
|
|
||
|
Comment on lines
+1209
to
+1262
|
||
| """ | ||
| Accelerator properties | ||
| """ | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This PR introduces the
adapt_checkpoint_hparamshook in the CLI, which appears to be an unrelated feature to manual dataloader reloading. Consider splitting this into a separate PR to keep changes focused and make review/maintenance easier. The PR title and description only mention dataloader reloading but don't mention the CLI checkpoint hyperparameters adaptation feature.