Seed the NumPy RNG used for initial weight noise in reweight()#93
Merged
Conversation
Contributor
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
2 tasks
MaxGhenis
commented
Apr 17, 2026
Contributor
Author
MaxGhenis
left a comment
There was a problem hiding this comment.
LGTM (cannot self-approve; posting as comment).
- Local
np.random.default_rng(seed)— does not mutate the caller's global numpy state. The dedicated testtest_calibration_does_not_mutate_global_numpy_stateverifies this. - Explicit
seedparameter onreweight(), threaded fromCalibration.calibrate()viaself.seed. Clean API. torch.manual_seed(seed)+torch.cuda.manual_seed_all(seed)guarded byif seed is not None— preserves the previous non-deterministic behaviour when no seed is given.- Three tests (same-seed determinism, different-seed divergence, global-state preservation) cover the reproducibility contract end-to-end with
noise_level > 0so the numpy RNG is actually exercised.
Note: this branch doesn't include the np.maximum(..., 1e-12) guard from #91; once #91 merges, #93 will need a trivial rebase. That's the case for all 7 branches as they're independent off origin/main.
Calibration.__init__ called torch.manual_seed(self.seed) but not np.random.seed(self.seed), yet reweight() drew its initial weight noise from the unseeded global np.random stream. Two runs with the same seed therefore produced different initial log-weights and diverged thereafter, breaking the documented reproducibility guarantee and destabilising CI, hyperparameter tuning, and holdout robustness. reweight() now accepts an explicit `seed` parameter. When provided it seeds torch (including CUDA) and draws initial noise from a local `numpy.random.default_rng(seed)`, which keeps the caller's global numpy RNG state untouched. Calibration.calibrate() threads its `self.seed` through. Adds tests/test_seed_determinism.py covering (a) identical seeds -> identical weights, (b) different seeds -> different weights, and (c) calibration does not mutate the caller's global numpy RNG stream. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Finding #3 (HIGH).
Calibration.__init__calledtorch.manual_seed(self.seed)but nevernp.random.seed(self.seed), yetreweight()drew its initial weight noise from the unseeded globalnp.randomstream. Two runs with the sameseedtherefore produced different initial log-weights and diverged thereafter, breaking the documented reproducibility guarantee and destabilising CI, hyperparameter tuning, and holdout robustness.reweight()now accepts an explicitseedparameter. When provided it seeds torch (including CUDA) and draws initial noise from a localnumpy.random.default_rng(seed), which keeps the caller's global numpy RNG state untouched.Calibration.calibrate()threads itsself.seedthrough.Test plan
tests/test_seed_determinism.py: identical seeds → identical weights, different seeds → different weights, calibration does not mutate the caller's global numpy RNG stream.uv run pytest tests -x -q-> 18 passed).🤖 Generated with Claude Code