-
Notifications
You must be signed in to change notification settings - Fork 3.6k
feat: allow custom samplers to opt out of automatic shuffling #21449
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
base: master
Are you sure you want to change the base?
feat: allow custom samplers to opt out of automatic shuffling #21449
Conversation
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.
Pull request overview
This PR introduces an opt-out mechanism for custom samplers to prevent Lightning from automatically applying shuffling during training. Custom samplers can now set disable_auto_shuffle = True as an instance attribute to indicate they fully control iteration order and should not be modified by Lightning's distributed sampling logic.
- Adds support for
disable_auto_shuffleattribute on custom samplers to preserve their intended iteration order - Updates the CHANGELOG to document this new feature
- Adds documentation with an example showing how to use this feature
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| src/lightning/pytorch/trainer/connectors/data_connector.py | Modifies _process_dataloader to check for disable_auto_shuffle attribute on samplers and respects it when determining whether to apply shuffling |
| src/lightning/pytorch/CHANGELOG.md | Adds entry documenting the new feature under the "Added" section |
| docs/source-pytorch/common/trainer.rst | Adds new subsection explaining the feature with a code example showing how to create a custom sampler that opts out of automatic shuffling |
Comments suppressed due to low confidence (1)
src/lightning/pytorch/CHANGELOG.md:30
- The CHANGELOG has a formatting issue. There are two "### Fixed" sections (lines 14 and 28), which creates an inconsistent structure. The CHANGELOG entries should be organized properly with each section appearing only once in the unreleased changes. Additionally, line 17 has a lone hyphen that should be removed. The structure should follow: Added, Fixed, Deprecated, Removed sections in that order without duplication.
### Fixed
- Fixed ``ModelParallelStrategy`` single-file checkpointing when ``torch.compile`` wraps the model so optimizer states no longer raise ``KeyError`` during save ([#21357](https://github.com/Lightning-AI/pytorch-lightning/issues/21357))
-
### Deprecated
- Deprecated `to_torchscript` method due to deprecation of TorchScript in PyTorch ([#21397](https://github.com/Lightning-AI/pytorch-lightning/pull/21397))
### Removed
---
- Removed support for Python 3.9 due to end-of-life status ([#21398](https://github.com/Lightning-AI/pytorch-lightning/pull/21398))
### Fixed
- Sanitize profiler filenames when saving to avoid crashes due to invalid characters ([#21395](https://github.com/Lightning-AI/pytorch-lightning/pull/21395))
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Codecov Report✅ All modified and coverable lines are covered by tests.
Additional details and impacted files@@ Coverage Diff @@
## master #21449 +/- ##
=========================================
- Coverage 87% 79% -8%
=========================================
Files 270 267 -3
Lines 24059 24006 -53
=========================================
- Hits 20862 18954 -1908
- Misses 3197 5052 +1855 |
What does this PR do?
Fixes #21447
fixes #21131
Before submitting
This PR adds an explicit opt-out mechanism for custom samplers to prevent Lightning from applying automatic shuffling during training.
Custom samplers can now set
disable_auto_shuffle = Trueto indicate that they fully control iteration order and should not be wrapped or replaced by Lightning (for example, in DDP).Why is this needed?
During training, Lightning currently assumes that data should be shuffled and may override the sampler configuration. However, it is not possible to reliably infer whether a custom sampler expects shuffling or fully controls the iteration order, especially since custom samplers may derive from PyTorch’s built-in sampler classes.
Relying on type or behavior inspection is fragile and can lead to unintended reordering. Introducing an explicit opt-out property allows custom samplers to clearly signal that they manage ordering themselves, avoiding implicit assumptions and preserving intended behavior.
What changed?
disable_auto_shuffle = Trueon custom samplers during trainingBackward compatibility
This change is fully backward compatible. Existing samplers are unaffected unless they explicitly set
disable_auto_shuffle = True.PR review
Anyone in the community is welcome to review the PR.
Before you start reviewing, make sure you have read the review guidelines. In short, see the following bullet-list:
Reviewer checklist
📚 Documentation preview 📚: https://pytorch-lightning--21449.org.readthedocs.build/en/21449/