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

Seed NumPy using np.random.SeedSequence() in pl_worker_init_function() to robustly seed NumPy-dependent dataloader workers #20369

Merged
merged 8 commits into from
Nov 25, 2024

Conversation

amorehead
Copy link
Contributor

@amorehead amorehead commented Oct 28, 2024

What does this PR do?

Fixes an issue where small changes to a user's random seed (specified via seed_everything(), e.g., incrementing seed by 1) would not result in a new (unique) random seed for NumPy (set within pl_worker_init_function()). This could cause multi-process dataloader workers to not change their random state based on a user's desired seed set via seed_everything() before training. In other words, if users were relying on seed_everything() to change e.g., the order of the indices sampled by each dataloader worker (if their dataloader(s) use NumPy's random module for index sampling), this would not work until now.

Below is an example of a seed_sequence that previously would not have generated a distinct random seed for NumPy. For example, 7768447584330995212 & 0xFFFFFFFF and 13249712275147347468 & 0xFFFFFFFF yield the same seed even though these former numbers were generated using different base_seeds. To reproduce more of these issues, set base_seed=42,worker_id=0,global_rank=0 within seed_sequence = _generate_seed_sequence(base_seed, worker_id, global_rank, count=4) and then trying incrementing or decrementing base_seed. Most of these small changes to base_seed yield the same random seed for NumPy.

0 =
7900221878587883521
1 =
15926613439302434606
2 =
2812539921844481303
3 =
7768447584330995212

and

0 =
14998723003968126977
1 =
17951608614340099886
2 =
3289043221663264023
3 =
13249712275147347468

-> 

seed_sequence[3] & 0xFFFFFFFF
1078613516

???
Before submitting
  • Was this discussed/agreed via a GitHub issue? (not for typos and docs)
  • Did you read the contributor guideline, Pull Request section?
  • Did you make sure your PR does only one thing, instead of bundling different changes together?
  • Did you make sure to update the documentation with your changes? (if necessary)
  • Did you write any new necessary tests? (not for typos and docs)
  • Did you verify new and existing tests pass locally with your changes?
  • Did you list all the breaking changes introduced by this pull request?
  • Did you update the CHANGELOG? (not for typos, docs, test updates, or minor internal changes/refactors)

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
  • Is this pull request ready for review? (if not, please submit in draft mode)
  • Check that all items from Before submitting are resolved
  • Make sure the title is self-explanatory and the description concisely explains the PR
  • Add labels and milestones (and optionally projects) to the PR so it can be classified

📚 Documentation preview 📚: https://pytorch-lightning--20369.org.readthedocs.build/en/20369/

@github-actions github-actions bot added the fabric lightning.fabric.Fabric label Oct 28, 2024
Copy link

codecov bot commented Oct 28, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 88%. Comparing base (28d3afd) to head (b9ba94c).
Report is 7 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #20369   +/-   ##
=======================================
  Coverage      88%      88%           
=======================================
  Files         267      267           
  Lines       23203    23203           
=======================================
  Hits        20313    20313           
  Misses       2890     2890           

@amorehead amorehead changed the title Reference PL_GLOBAL_SEED in pl_worker_init_function() to correctly seed dataloader workers Seed NumPy using np.random.SeedSequence() in pl_worker_init_function() to correctly seed dataloader workers Oct 28, 2024
@amorehead amorehead changed the title Seed NumPy using np.random.SeedSequence() in pl_worker_init_function() to correctly seed dataloader workers Seed NumPy using np.random.SeedSequence() in pl_worker_init_function() to robustly seed dataloader workers Oct 28, 2024
@amorehead amorehead changed the title Seed NumPy using np.random.SeedSequence() in pl_worker_init_function() to robustly seed dataloader workers Seed NumPy using np.random.SeedSequence() in pl_worker_init_function() to robustly seed NumPy-dependent dataloader workers Oct 28, 2024
@lantiga
Copy link
Collaborator

lantiga commented Nov 12, 2024

Nice catch! Can you please add a test that verifies the desired behavior so we know it won't regress in the future?

@lantiga lantiga added the waiting on author Waiting on user action, correction, or update label Nov 12, 2024
@lantiga lantiga merged commit 29c0396 into Lightning-AI:master Nov 25, 2024
102 checks passed
@amorehead amorehead deleted the patch-3 branch December 5, 2024 22:09
@amorehead amorehead restored the patch-3 branch December 5, 2024 22:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fabric lightning.fabric.Fabric waiting on author Waiting on user action, correction, or update
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants