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

[BUG] Check environ before selecting a seed to prevent warning message #4743

Merged
merged 16 commits into from
Jan 12, 2021

Conversation

SeanNaren
Copy link
Contributor

What does this PR do?

Fixes #4741
Make sure we check the environ first before generating a seed to prevent warning message.
Check issue for reprod.

PR review

Anyone in the community is free to review the PR once the tests have passed.
Before you start reviewing make sure you have read Review guidelines. In in short, see following bullet-list:

  • 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; Bugfixes should be including in bug-fix release milestones (m.f.X) and features should be included in (m.X.b) releases.

Did you have fun?

Make sure you had fun coding 🙃

@SeanNaren
Copy link
Contributor Author

Still fixing!

@carmocca
Copy link
Contributor

You could add a pytest.warns test using the reproduction snippet given in the issue

@SeanNaren
Copy link
Contributor Author

Thanks @carmocca! Will do :)

The fix is still WIP (just wanted to test if it worked). Ignores the seed set from cmdline atm. I don't want to add two seed is none checks in a row.

@SeanNaren SeanNaren changed the title [BUG] Check environ before selecting a seed to prevent warning message [WIP] [BUG] Check environ before selecting a seed to prevent warning message Nov 18, 2020
@SeanNaren SeanNaren changed the title [WIP] [BUG] Check environ before selecting a seed to prevent warning message [BUG] Check environ before selecting a seed to prevent warning message Nov 18, 2020
@codecov
Copy link

codecov bot commented Nov 18, 2020

Codecov Report

Merging #4743 (93c231c) into master (f065ea6) will decrease coverage by 0%.
The diff coverage is 100%.

@@           Coverage Diff           @@
##           master   #4743    +/-   ##
=======================================
- Coverage      93%     93%    -0%     
=======================================
  Files         135     135            
  Lines       10015    9873   -142     
=======================================
- Hits         9332    9181   -151     
- Misses        683     692     +9     

pytorch_lightning/utilities/seed.py Outdated Show resolved Hide resolved
tests/utilities/test_seed.py Show resolved Hide resolved
@edenlightning edenlightning modified the milestones: 1.1, 1.0.8 Nov 18, 2020
@carmocca
Copy link
Contributor

carmocca commented Nov 19, 2020

Hey @SeanNaren

I was a bit puzzled about this and checked out the branch. Changed the code to work. I do not have permissions to add commits to this PR so I created a branch on my fork off this branch. You should be able to git cherry-pick the commit here.

https://github.com/carmocca/pytorch-lightning/commit/eca9f4f8052c21dc14b272e1508aff71a79a9c84

Key changes:

  • Avoid calling select_seed_randomly from multiple places. Use the except for this.
  • Move warning to the except
  • Use warnings.warn instead of `logging.warning
  • Add tests for more cases

@Borda Borda modified the milestones: 1.0.8, 1.0.x Nov 20, 2020
@edenlightning edenlightning added this to the 1.1.x milestone Dec 8, 2020
@mergify mergify bot requested a review from a team December 12, 2020 14:58
@SeanNaren
Copy link
Contributor Author

cc @tchaton @awaelchli and @Borda for review! Should be good to go now

pytorch_lightning/utilities/seed.py Show resolved Hide resolved
seed = _select_seed_randomly(min_seed_value, max_seed_value)

log.info(f"Global seed set to {seed}")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will log on each rank, is this desired?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was going back and forth with myself about this, I think this is a good idea to ensure that the seed is set correctly on all processes, but I can be persuaded otherwise

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's fine :)

@SeanNaren SeanNaren enabled auto-merge (squash) January 10, 2021 20:16
Comment on lines 47 to +48
seed = _select_seed_randomly(min_seed_value, max_seed_value)
rank_zero_warn(f"No correct seed found, seed set to {seed}")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
seed = _select_seed_randomly(min_seed_value, max_seed_value)
rank_zero_warn(f"No correct seed found, seed set to {seed}")
seed_new = _select_seed_randomly(min_seed_value, max_seed_value)
rank_zero_warn(f"No correct seed `{seed}` found, seed set to `{seed_new}`")
seed = seed_new

@Borda Borda requested a review from tchaton January 10, 2021 20:55
@SeanNaren SeanNaren disabled auto-merge January 10, 2021 21:34
@Borda Borda added the ready PRs ready to be merged label Jan 11, 2021
@SeanNaren SeanNaren enabled auto-merge (squash) January 11, 2021 15:30
@SeanNaren SeanNaren merged commit 635df27 into master Jan 12, 2021
@SeanNaren SeanNaren deleted the fix/4741-seed-init branch January 12, 2021 04:30
SeanNaren added a commit that referenced this pull request Jan 12, 2021
#4743)

* Check environment var independently to selecting a seed to prevent unnecessary warning message

* Add if statement to check if PL_GLOBAL_SEED has been set

* Added seed test to ensure that the seed stays the same, in case

* if

* Delete global seed after test has finished

* Fix code, add tests

* Ensure seed does not exist before tests start

* Refactor test based on review, add log call

* Ensure we clear the os environ in patched dict

Co-authored-by: Jirka Borovec <[email protected]>
Co-authored-by: Carlos Mocholí <[email protected]>
Co-authored-by: chaton <[email protected]>
(cherry picked from commit 635df27)
SeanNaren added a commit that referenced this pull request Jan 12, 2021
#4743)

* Check environment var independently to selecting a seed to prevent unnecessary warning message

* Add if statement to check if PL_GLOBAL_SEED has been set

* Added seed test to ensure that the seed stays the same, in case

* if

* Delete global seed after test has finished

* Fix code, add tests

* Ensure seed does not exist before tests start

* Refactor test based on review, add log call

* Ensure we clear the os environ in patched dict

Co-authored-by: Jirka Borovec <[email protected]>
Co-authored-by: Carlos Mocholí <[email protected]>
Co-authored-by: chaton <[email protected]>
(cherry picked from commit 635df27)
SeanNaren added a commit that referenced this pull request Jan 13, 2021
#4743)

* Check environment var independently to selecting a seed to prevent unnecessary warning message

* Add if statement to check if PL_GLOBAL_SEED has been set

* Added seed test to ensure that the seed stays the same, in case

* if

* Delete global seed after test has finished

* Fix code, add tests

* Ensure seed does not exist before tests start

* Refactor test based on review, add log call

* Ensure we clear the os environ in patched dict

Co-authored-by: Jirka Borovec <[email protected]>
Co-authored-by: Carlos Mocholí <[email protected]>
Co-authored-by: chaton <[email protected]>
(cherry picked from commit 635df27)
SeanNaren added a commit that referenced this pull request Jan 13, 2021
#4743)

* Check environment var independently to selecting a seed to prevent unnecessary warning message

* Add if statement to check if PL_GLOBAL_SEED has been set

* Added seed test to ensure that the seed stays the same, in case

* if

* Delete global seed after test has finished

* Fix code, add tests

* Ensure seed does not exist before tests start

* Refactor test based on review, add log call

* Ensure we clear the os environ in patched dict

Co-authored-by: Jirka Borovec <[email protected]>
Co-authored-by: Carlos Mocholí <[email protected]>
Co-authored-by: chaton <[email protected]>
(cherry picked from commit 635df27)
SeanNaren added a commit that referenced this pull request Jan 19, 2021
#4743)

* Check environment var independently to selecting a seed to prevent unnecessary warning message

* Add if statement to check if PL_GLOBAL_SEED has been set

* Added seed test to ensure that the seed stays the same, in case

* if

* Delete global seed after test has finished

* Fix code, add tests

* Ensure seed does not exist before tests start

* Refactor test based on review, add log call

* Ensure we clear the os environ in patched dict

Co-authored-by: Jirka Borovec <[email protected]>
Co-authored-by: Carlos Mocholí <[email protected]>
Co-authored-by: chaton <[email protected]>
(cherry picked from commit 635df27)
Borda pushed a commit that referenced this pull request Jan 23, 2021
#4743)

* Check environment var independently to selecting a seed to prevent unnecessary warning message

* Add if statement to check if PL_GLOBAL_SEED has been set

* Added seed test to ensure that the seed stays the same, in case

* if

* Delete global seed after test has finished

* Fix code, add tests

* Ensure seed does not exist before tests start

* Refactor test based on review, add log call

* Ensure we clear the os environ in patched dict

Co-authored-by: Jirka Borovec <[email protected]>
Co-authored-by: Carlos Mocholí <[email protected]>
Co-authored-by: chaton <[email protected]>
(cherry picked from commit 635df27)
Borda pushed a commit that referenced this pull request Jan 25, 2021
#4743)

* Check environment var independently to selecting a seed to prevent unnecessary warning message

* Add if statement to check if PL_GLOBAL_SEED has been set

* Added seed test to ensure that the seed stays the same, in case

* if

* Delete global seed after test has finished

* Fix code, add tests

* Ensure seed does not exist before tests start

* Refactor test based on review, add log call

* Ensure we clear the os environ in patched dict

Co-authored-by: Jirka Borovec <[email protected]>
Co-authored-by: Carlos Mocholí <[email protected]>
Co-authored-by: chaton <[email protected]>
(cherry picked from commit 635df27)
Borda pushed a commit that referenced this pull request Jan 26, 2021
#4743)

* Check environment var independently to selecting a seed to prevent unnecessary warning message

* Add if statement to check if PL_GLOBAL_SEED has been set

* Added seed test to ensure that the seed stays the same, in case

* if

* Delete global seed after test has finished

* Fix code, add tests

* Ensure seed does not exist before tests start

* Refactor test based on review, add log call

* Ensure we clear the os environ in patched dict

Co-authored-by: Jirka Borovec <[email protected]>
Co-authored-by: Carlos Mocholí <[email protected]>
Co-authored-by: chaton <[email protected]>
(cherry picked from commit 635df27)
Borda pushed a commit that referenced this pull request Jan 26, 2021
#4743)

* Check environment var independently to selecting a seed to prevent unnecessary warning message

* Add if statement to check if PL_GLOBAL_SEED has been set

* Added seed test to ensure that the seed stays the same, in case

* if

* Delete global seed after test has finished

* Fix code, add tests

* Ensure seed does not exist before tests start

* Refactor test based on review, add log call

* Ensure we clear the os environ in patched dict

Co-authored-by: Jirka Borovec <[email protected]>
Co-authored-by: Carlos Mocholí <[email protected]>
Co-authored-by: chaton <[email protected]>
(cherry picked from commit 635df27)
Borda pushed a commit that referenced this pull request Jan 26, 2021
#4743)

* Check environment var independently to selecting a seed to prevent unnecessary warning message

* Add if statement to check if PL_GLOBAL_SEED has been set

* Added seed test to ensure that the seed stays the same, in case

* if

* Delete global seed after test has finished

* Fix code, add tests

* Ensure seed does not exist before tests start

* Refactor test based on review, add log call

* Ensure we clear the os environ in patched dict

Co-authored-by: Jirka Borovec <[email protected]>
Co-authored-by: Carlos Mocholí <[email protected]>
Co-authored-by: chaton <[email protected]>
(cherry picked from commit 635df27)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working ready PRs ready to be merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

seed_everything() runs _select_seed_randomly() even if PL_GLOBAL_SEED is set
7 participants