You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
fix: simulating no longer turns on deterministic cuDNN - #2177
simulate_for_sbi and simulate_in_batches seeded through seed_all_backends, which also sets torch.backends.cudnn.deterministic = True and torch.backends.cudnn.benchmark = False. Those flags are global to the process, so one call to a simulation helper switched the rest of the session to deterministic cuDNN algorithms and could slow down later training of convolutional embedding nets.
This separates the two concerns. seed_random_states draws the seed and seeds random, numpy, torch and torch.cuda. seed_all_backends is now that call plus the two cuDNN lines, so its behavior is unchanged for the autouse fixture in tests/conftest.py and for the existing test call sites. The simulation paths use seed_random_states: simulate_for_sbi before it samples the parameters, simulate_in_batches before it splits them, and its simulator_seeded closure per batch.
Seeding itself does not change: with seed=None the seed is still drawn from PyTorch's random state, so an upstream torch.manual_seed keeps reproducing a simulate_for_sbi(seed=None) call, and the per-batch seeds used when num_workers > 1 are drawn from the same state as before.
The issue suggests an option of seed_all_backends. I used a second function instead: a call like seed_all_backends(seed, deterministic_cudnn=False) reads as a contradiction, and this way seed_all_backends keeps exactly its current meaning for the people who do want bit-reproducible convolutions. Say the word if you would rather have the flag.
Rebased onto current main (95a4f5e5). Two changes landed since the first version of this PR. PR #1738 rewrote the simulator call path, and simulate_from_theta now seeds each call through the CPU-only _seed_cpu_random_states, which already leaves cuDNN alone. This PR changes the remaining call sites: the single seed of simulate_for_sbi before it samples, and both in ABC's simulate_in_batches. After it, seed_all_backends is the only place in the package that sets the cuDNN flags.
I did not export seed_random_states in sbi/utils/__init__.py, matching seed_all_backends, which the tests that use it import from sbi.utils.sbiutils. No public API is added or changed.
The two cuDNN lines in seed_all_backends are the only place the package touched cuDNN, and no page in docs/ mentions it. The parallel-simulations how-to says simulate_for_sbi seeds PyTorch's GPU generators once before it samples, which is still true.
User visible part: anybody who was relying on a simulate_for_sbi or simulate_in_batches call to quietly make convolutional training deterministic now gets the torch default, and has to call seed_all_backends(seed) or set the flags themselves. That is the point of the issue.
How I checked it:
The repro from the issue (simulate_for_sbi(lambda t: t, BoxUniform(...), 5) then printing torch.backends.cudnn.deterministic) prints True on main and False here. simulate_in_batches leaves the flags alone too.
test_simulating_leaves_cudnn_flags fails on main for both seed=None and seed=42 and passes with the change. The fixture sets deterministic=False, benchmark=True before the call on purpose: at the all-False default a test cannot tell "left alone" from "set to False", so it would pass without the fix. With num_workers > 1 the seeded simulator runs in a worker process, whose flags the parent cannot see, so the helper is also asserted directly.
Reproducibility unchanged: simulate_for_sbi and simulate_in_batches return identical x for seed=42 across two calls, and seed=None is still reproduced by an upstream torch.manual_seed(0).
The new tests take about 0.04 s each, except the num_workers=2 case, which takes about 1.3 s for the joblib workers; none is marked slow. Focused suites (simulator_utils_test.py, simulation_utils_test.py, sbiutils_test.py, multiprocessing_test.py) pass with only expected XFAILs, and ruff check, ruff format --check, pyright sbi and git diff --check pass.
No actionable comments were generated in the recent review.
Walkthrough
Random-state seeding is separated from cuDNN configuration. Simulation functions now seed random states without changing cuDNN flags. Tests cover both seeding helpers and simulation behavior.
seed_random_states seeds random states without changing cuDNN flags. seed_all_backends calls it and sets deterministic cuDNN flags. Tests verify both behaviors.
Simulation functions use seed_random_states before sampling or simulator calls. Tests check that simulation leaves cuDNN flags unchanged for omitted and supplied seeds. The changelog describes this behavior and the option to set cuDNN flags directly.
Simulation helpers will no longer change global cuDNN settings. Users who relied on simulation to enable deterministic convolutions must call seed_all_backends or set the flags themselves, and the changelog says so. No merge-blocking risk was identified.
Pre-merge checks: 4
Check name
Status
Explanation
Title check
The title names the main change: simulation helpers no longer enable deterministic cuDNN. The changeset adds seed_random_states, which seeds random states without touching cuDNN flags, and switches si…
Linked Issues check
Issue #2159 requires simulation helpers to seed random states without changing process-wide cuDNN flags, and requires seed=None to stay reproducible through torch.manual_seed. At head 73f4b88, sbi/sim…
Out of Scope Changes check
The changes map to #2159. sbi/utils/sbiutils.py splits seeding from cuDNN configuration. sbi/simulators/simutils.py and sbi/utils/simulation_utils.py switch simulation paths to seed_random_states. tes…
Description check
The description states the problem (global cuDNN flags switched by simulation helpers), the fix (split seeding into seed_random_states, keep cuDNN setting in seed_all_backends), and the linked issue (…
Full details: Title check
Explanation
The title names the main change: simulation helpers no longer enable deterministic cuDNN. The changeset adds seed_random_states, which seeds random states without touching cuDNN flags, and switches simulate_for_sbi and simulate_in_batches to it. The title is short, specific, and matches the primary behavior change.
Full details: Linked Issues check
Explanation
Issue #2159 requires simulation helpers to seed random states without changing process-wide cuDNN flags, and requires seed=None to stay reproducible through torch.manual_seed. At head 73f4b88, sbi/simulators/simutils.py imports seed_random_states and calls it in simulate_in_batches and in the seeded simulator_seeded closure. It does not call seed_all_backends. The raw summary states that seed_random_states in sbi/utils/sbiutils.py seeds Python, NumPy, PyTorch and CUDA only, that seed=None is drawn from PyTorch's random state, and that seed_all_backends delegates to it and then sets cudnn.deterministic=True and cudnn.benchmark=False. The raw summary also states that sbi/utils/simulation_utils.py simulate_for_sbi now calls seed_random_states. The previous review verified the same behavior on the whole-PR diff at c63e784. The new tests in tests/simulator_utils_test.py cover the cuDNN flag behavior for seed=None and seed=42. Limitation: the incremental diff from c63e784 to 73f4b88 could not be read (diff tool unavailable), so changes between those commits are known only from the summary. The head file read of sbiutils.py was truncated before seed_random_states, so its body was not read directly at head. The test results come from the PR author's report and were not re-run.
Full details: Out of Scope Changes check
Explanation
The changes map to #2159. sbi/utils/sbiutils.py splits seeding from cuDNN configuration. sbi/simulators/simutils.py and sbi/utils/simulation_utils.py switch simulation paths to seed_random_states. tests/simulator_utils_test.py adds tests for the flag behavior. The CHANGELOG.md entry (+4 lines, per the raw summary) documents the behavior change for users, which is documentation that supports this objective. The only new name is seed_random_states, which the PR states is not exported publicly. No unrelated logic change was found in the inspected head content. Limitation: the incremental diff since c63e784 could not be read, so the CHANGELOG and any other later edits are assessed from the summary only.
Full details: Description check
Explanation
The description states the problem (global cuDNN flags switched by simulation helpers), the fix (split seeding into seed_random_states, keep cuDNN setting in seed_all_backends), and the linked issue (Fixes #2159). It covers the template's 'What does this PR do' and 'Does this close any issues' sections well. It also lists testing, reproducibility checks, and a user-visible impact. The checklist is present and checked off. The template's 'Anything else' section is filled in. The AI-assistance disclosure comment from the template is not addressed. This is a minor gap, so the description is mostly complete.
Finishing Touches
Generate unit tests (beta)
Create a new PR
Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
File: No configuration file from this repository applied
Commit: Unavailable
Layers: Organization UI
Review profile: CHILL
Plan: Advanced
Run ID: c6eb0ba8-3e1a-4481-8583-b2cb6e62a8f1
Commits
Reviewing files that changed from the base of the PR and between c63e784 and 73f4b88.
Files selected for processing: 2
CHANGELOG.md
sbi/utils/sbiutils.py
Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 3 remain after this review.
❌ Patch coverage is 87.50000% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 90.99%. Comparing base (95a4f5e) to head (05743dc). ⚠️ Report is 11 commits behind head on main.
Already reviewed the last commit. Use @coderabbitai full review to rerun a review of the entire changeset.
Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.
simulate_for_sbi and simulate_in_batches seeded through seed_all_backends, which
also sets torch.backends.cudnn.deterministic and clears benchmark. Both flags are
global to the process, so simulating switched convolutional training running later
in the same session onto deterministic cuDNN algorithms.
The seeding moves to seed_random_states in sbi/utils/sbiutils.py, and
seed_all_backends calls it before setting the two flags, which keeps its behavior
for the autouse fixture in tests/conftest.py and for existing callers. The
simulation paths, including the simulator_seeded closures used with num_workers >
1, call seed_random_states instead.
Seeding itself is unchanged: with seed=None the seed is still drawn from the torch
random state, so an upstream torch.manual_seed keeps reproducing a
simulate_for_sbi(seed=None) call, and the per-batch seeds are drawn from the same
state as before.
Fixessbi-dev#2159
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
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.
What does this PR do?
simulate_for_sbiandsimulate_in_batchesseeded throughseed_all_backends, which also setstorch.backends.cudnn.deterministic = Trueandtorch.backends.cudnn.benchmark = False. Those flags are global to the process, so one call to a simulation helper switched the rest of the session to deterministic cuDNN algorithms and could slow down later training of convolutional embedding nets.This separates the two concerns.
seed_random_statesdraws the seed and seedsrandom, numpy, torch and torch.cuda.seed_all_backendsis now that call plus the two cuDNN lines, so its behavior is unchanged for the autouse fixture intests/conftest.pyand for the existing test call sites. The simulation paths useseed_random_states:simulate_for_sbibefore it samples the parameters,simulate_in_batchesbefore it splits them, and itssimulator_seededclosure per batch.Seeding itself does not change: with
seed=Nonethe seed is still drawn from PyTorch's random state, so an upstreamtorch.manual_seedkeeps reproducing asimulate_for_sbi(seed=None)call, and the per-batch seeds used whennum_workers > 1are drawn from the same state as before.Does this close any issues?
Fixes #2159
Anything else we should know?
seed_all_backends. I used a second function instead: a call likeseed_all_backends(seed, deterministic_cudnn=False)reads as a contradiction, and this wayseed_all_backendskeeps exactly its current meaning for the people who do want bit-reproducible convolutions. Say the word if you would rather have the flag.main(95a4f5e5). Two changes landed since the first version of this PR. PR #1738 rewrote the simulator call path, andsimulate_from_thetanow seeds each call through the CPU-only_seed_cpu_random_states, which already leaves cuDNN alone. This PR changes the remaining call sites: the single seed ofsimulate_for_sbibefore it samples, and both in ABC'ssimulate_in_batches. After it,seed_all_backendsis the only place in the package that sets the cuDNN flags.seed_random_statesinsbi/utils/__init__.py, matchingseed_all_backends, which the tests that use it import fromsbi.utils.sbiutils. No public API is added or changed.seed_all_backendsare the only place the package touched cuDNN, and no page indocs/mentions it. The parallel-simulations how-to sayssimulate_for_sbiseeds PyTorch's GPU generators once before it samples, which is still true.simulate_for_sbiorsimulate_in_batchescall to quietly make convolutional training deterministic now gets the torch default, and has to callseed_all_backends(seed)or set the flags themselves. That is the point of the issue.How I checked it:
simulate_for_sbi(lambda t: t, BoxUniform(...), 5)then printingtorch.backends.cudnn.deterministic) printsTrueonmainandFalsehere.simulate_in_batchesleaves the flags alone too.test_simulating_leaves_cudnn_flagsfails onmainfor bothseed=Noneandseed=42and passes with the change. The fixture setsdeterministic=False, benchmark=Truebefore the call on purpose: at the all-Falsedefault a test cannot tell "left alone" from "set to False", so it would pass without the fix. Withnum_workers > 1the seeded simulator runs in a worker process, whose flags the parent cannot see, so the helper is also asserted directly.simulate_for_sbiandsimulate_in_batchesreturn identicalxforseed=42across two calls, andseed=Noneis still reproduced by an upstreamtorch.manual_seed(0).num_workers=2case, which takes about 1.3 s for the joblib workers; none is markedslow. Focused suites (simulator_utils_test.py,simulation_utils_test.py,sbiutils_test.py,multiprocessing_test.py) pass with only expected XFAILs, andruff check,ruff format --check,pyright sbiandgit diff --checkpass.Checklist
uv run pytest -n auto -m "not slow and not gpu"passes.uv run pre-commit run --all-filespasses (ruff and formatting).uv run pyright sbipasses.pytest.mark.slow.