Skip to content

Conversation

jonathanking
Copy link
Contributor

@jonathanking jonathanking commented Aug 4, 2025

What does this PR do?

Changes behavior of seed_everything to raise a ValueError if and only if the user provides a seed that is out of bounds ([0, 2**32-1]) in order to more clearly match the user's expectations. Prevents misconfiguration of seeds when highly predictable behavior is desired.

This behavior is changed whether the user specified the seed via the seed_everything function call or the PL_GLOBAL_SEED env var.

Fixes #21028
See also #18846 for context.
cc @deependujha

Breaking changes:

  1. If a user provides an integer to seed_everything that is outside of [0, 2**32-1], the code will raise a ValueError where it previously changed the seed to 0.
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--21029.org.readthedocs.build/en/21029/

@github-actions github-actions bot added the fabric lightning.fabric.Fabric label Aug 4, 2025
@deependujha
Copy link
Collaborator

do you intend to make more changes, or should we mark it as ready for review?

@jonathanking
Copy link
Contributor Author

Thanks for reviewing! I have one question to resolve:

I have only changed the behavior to raise a ValueError if a user calls seed_everything with an invalid integer seed. However, I did not change the behavior for if the user specifies an invalid seed via the environment variable.

The benefit of my current implementation is that it changes as little code as possible and mostly maintains the original behavior. The downside of my current implementation is that the behavior is different depending on whether or not the user uses the environment variable or the function argument.

Having thought about this a bit more, I propose to change the behavior also for the case that the user specifies the seed via the environment variable. What do you think?

@deependujha
Copy link
Collaborator

Hi @jonathanking

sorry, I only looked at diff. Let's be consistent. Raise ValueError in env var case too.

@deependujha
Copy link
Collaborator

can you include test for it as well (with env variable)

@jonathanking
Copy link
Contributor Author

Thanks, I agree - just updated the code and test.

@jonathanking
Copy link
Contributor Author

Thanks for the feedback, @deependujha! Will now mark as ready.

@jonathanking jonathanking marked this pull request as ready for review August 4, 2025 22:30
Copy link

codecov bot commented Aug 4, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 87%. Comparing base (e752cb5) to head (90d88f9).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #21029   +/-   ##
=======================================
- Coverage      87%      87%   -0%     
=======================================
  Files         268      268           
  Lines       23303    23301    -2     
=======================================
- Hits        20297    20295    -2     
  Misses       3006     3006           

@deependujha deependujha requested a review from Copilot August 4, 2025 22:50
Copilot

This comment was marked as outdated.

@jonathanking jonathanking requested a review from Copilot August 5, 2025 21:41
Copy link
Contributor

@Copilot Copilot AI left a 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 changes the behavior of seed_everything to raise a ValueError when an invalid or out-of-bounds seed is provided, instead of falling back to a default seed value of 0 with a warning. This makes the function more strict and prevents misconfiguration when predictable seeding behavior is critical.

  • Replace warning-based fallback behavior with strict error handling for invalid seeds
  • Update error messages to be more descriptive and consistent
  • Add comprehensive test coverage for the new validation behavior

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
src/lightning/fabric/utilities/seed.py Updated seed validation logic to raise ValueError instead of warnings with fallback to 0
tests/tests_fabric/utilities/test_seed.py Updated existing tests and added new tests to verify ValueError is raised for invalid seeds
src/lightning/fabric/CHANGELOG.md Added changelog entry documenting the breaking change

Copy link
Collaborator

@bhimrazy bhimrazy left a comment

Choose a reason for hiding this comment

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

Thanks @jonathanking for the update! I agree with the intent here.

Curious to hear what @Borda, @SkafteNicki, and @matsumotosan have to say on this

@deependujha deependujha merged commit 8d847fd into Lightning-AI:master Aug 6, 2025
142 of 144 checks passed
@deependujha
Copy link
Collaborator

Thanks @jonathanking for helping make pytorch lightning even better.

Borda pushed a commit that referenced this pull request Aug 13, 2025
* fix: crash when provided seed is out-of-bounds

* fix: update handling of invalid seeds via PL_GLOBAL_SEED

* fix: update test for invalid env var seed

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* fix: typo

* Apply suggestions from code review

Co-authored-by: Deependu <[email protected]>

* changelog

* update

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Deependu <[email protected]>

(cherry picked from commit 8d847fd)
Borda pushed a commit that referenced this pull request Aug 13, 2025
* fix: crash when provided seed is out-of-bounds

* fix: update handling of invalid seeds via PL_GLOBAL_SEED

* fix: update test for invalid env var seed

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* fix: typo

* Apply suggestions from code review

Co-authored-by: Deependu <[email protected]>

* changelog

* update

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Deependu <[email protected]>

(cherry picked from commit 8d847fd)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fabric lightning.fabric.Fabric
Projects
None yet
Development

Successfully merging this pull request may close these issues.

seed_everything should crash if invalid seeds are provided
4 participants