Skip to content

fix: simulating no longer turns on deterministic cuDNN - #2177

Open
BHARATH0153 wants to merge 1 commit into
sbi-dev:mainfrom
BHARATH0153:fix-simulation-cudnn-flags
Open

BHARATH0153 wants to merge 1 commit into
sbi-dev:mainfrom
BHARATH0153:fix-simulation-cudnn-flags

Conversation

@BHARATH0153

@BHARATH0153 BHARATH0153 commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

What does this PR do?

simulate_for_sbi and simulate_in_batches seed the random states with
seed_all_backends, which also sets torch.backends.cudnn.deterministic = True and
torch.backends.cudnn.benchmark = False. Those 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
test call sites. The simulation paths use seed_random_states.

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.

Does this close any issues?

Fixes #2159

Anything else we should know?

  • 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.
  • feat: parallel simulation with simulate_from_theta and parallelize_simulator #1738 rewrites the same simulator_seeded closures. That PR is still open, so this one
    targets main; I will rebase once feat: parallel simulation with simulate_from_theta and parallelize_simulator #1738 lands to keep the two in sync.
  • I did not export seed_random_states in sbi/utils/__init__.py, matching
    seed_all_backends, which is imported from sbi.utils.sbiutils by the tests that use
    it. No public API is added or changed.
  • The two lines in seed_all_backends are the only place the package touches cuDNN, and no
    notebook or page in docs/ mentions it, so there is nothing else to update.
  • User visible part: anybody who was relying on a simulate_for_sbi call to quietly make
    their 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, but
    it is still a behavior change worth a line in the release notes.

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.

  • 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 that call site is covered by the direct seed_random_states test
    rather than by pretending the end-to-end test observes it.
  • Reproducibility checked locally: simulate_for_sbi at num_workers 1 and 2 and
    simulate_in_batches at num_workers=2 return identical x for seed=42 across two
    calls, and seed=None is still reproduced by an upstream torch.manual_seed(0).
  • The three new tests take 0.02s each, so none is marked slow. pytest -n auto -m "not slow and not gpu" gives 7180 passed, 25 skipped, 116 xfailed in 5:31, and ruff,
    pyright sbi and pre-commit run --all-files pass.

I used Claude to explore the code, sanity-check the plan and draft this
patch and its tests, and verified the behavior locally as described above.

Checklist

  • I have read the contributing guide.
  • uv run pytest -n auto -m "not slow and not gpu" passes.
  • uv run pre-commit run --all-files passes (ruff and formatting).
  • uv run pyright sbi passes.
  • I added or updated tests for the changed behavior.
  • I used Google-style docstrings for new or changed public functions.
  • (If applicable) I reported how long new tests run and marked slow ones
    with pytest.mark.slow.

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.

Fixes sbi-dev#2159
@coderabbitai

coderabbitai Bot commented Nov 13, 2026 •

Copy link
Copy Markdown

Explain Change →

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.

Changes: Seeding and simulation behavior

Layer / File(s) Summary
Seeding helper behavior
sbi/utils/sbiutils.py, tests/simulator_utils_test.py
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 seeding call sites
sbi/simulators/simutils.py, sbi/utils/simulation_utils.py, tests/simulator_utils_test.py
Simulation functions use seed_random_states before sampling or simulator calls. Tests check that simulation leaves cuDNN flags unchanged for omitted and supplied seeds.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Suggested reviewers: janfb

Merge Risk: Minimal · up to 26ce8

Simulation helpers no longer change global cuDNN settings, and seed_all_backends keeps its previous behavior. Users who relied on simulation to enable deterministic convolutions must now set the flags themselves. No merge-blocking risk remains.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: simulation helpers no longer enable deterministic cuDNN. The changeset separates seed_random_states from seed_all_backends, and the simulation paths now call the new f…
Linked Issues check Passed Issue #2159 requires that simulation helpers seed random states without changing the process-wide cuDNN flags, and that seed=None still reproduces under an upstream torch.manual_seed. At head, sbi/sim…
Out of Scope Changes check Passed All changes map to #2159. The sbi/utils/sbiutils.py change splits seed_all_backends into seed_random_states plus a cuDNN-setting wrapper, which is the requested separation. The call-site edits in sbi/…
Description check Passed The description states the problem (seed_all_backends sets process-global cuDNN flags during simulation), the fix (split seed_random_states from seed_all_backends), and the linked issue (Fixes #2159).…
Full details: Title check

Explanation

The title names the main change: simulation helpers no longer enable deterministic cuDNN. The changeset separates seed_random_states from seed_all_backends, and the simulation paths now call the new function, so the title matches the primary behavior fix. It uses the conventional fix: prefix.

Full details: Linked Issues check

Explanation

Issue #2159 requires that simulation helpers seed random states without changing the process-wide cuDNN flags, and that seed=None still reproduces under an upstream torch.manual_seed. At head, sbi/simulators/simutils.py (simulate_in_batches and its simulator_seeded closure) and sbi/utils/simulation_utils.py (simulate_for_sbi and its simulator_seeded closure) call seed_random_states, which seeds random, numpy, torch and torch.cuda only. seed_random_states draws the seed from torch.randint when seed is None, so the seed=None behavior is kept. seed_all_backends now delegates to seed_random_states and then sets cudnn.deterministic=True and cudnn.benchmark=False, so users who want deterministic cuDNN keep an explicit path. The issue also names simulate_from_theta; no such function appears in the sbi/ file listing at head, so that part of the scope is not present here. The added tests in tests/simulator_utils_test.py cover the cuDNN flag behavior for both seed=None and seed=42. The PR author reports the test and reproducibility results; I did not re-run them.

Full details: Out of Scope Changes check

Explanation

All changes map to #2159. The sbi/utils/sbiutils.py change splits seed_all_backends into seed_random_states plus a cuDNN-setting wrapper, which is the requested separation. The call-site edits in sbi/simulators/simutils.py and sbi/utils/simulation_utils.py switch the simulation paths to the new function. The test additions verify the requested behavior. No unrelated files or logic changes were identified in the summary or in the inspected hunks. Public API change is limited to the new seed_random_states name, which the PR author states is not exported from sbi/utils/init.py.

Full details: Description check

Explanation

The description states the problem (seed_all_backends sets process-global cuDNN flags during simulation), the fix (split seed_random_states from seed_all_backends), and the linked issue (Fixes #2159). It covers the template's Anything else section with design rationale, open coordination with #1738, the user-visible behavior change, and local validation. The checklist is filled in. The AI-assistance disclosure is present. Minor gap: the description does not restate the specific test-run detail as a checklist-linked item, but all template sections are substantively addressed.

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.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: a03e836c-1f64-48c7-b137-edea128ce78e
Commits

Reviewing files that changed from the base of the PR and between 799e324 and 26ce8d2.

Files selected for processing: 4

  • sbi/simulators/simutils.py
  • sbi/utils/sbiutils.py
  • sbi/utils/simulation_utils.py
  • tests/simulator_utils_test.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.

@codecov

codecov Bot commented Nov 13, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 77.77778% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 91.31%. Comparing base (799e324) to head (26ce8d2).

Files with missing lines Patch % Lines
sbi/simulators/simutils.py 66.66% 1 Missing ⚠️
sbi/utils/simulation_utils.py 66.66% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2177   +/-   ##
=======================================
  Coverage   91.30%   91.31%           
=======================================
  Files         148      148           
  Lines       14970    14972    +2     
=======================================
+ Hits        13669    13671    +2     
  Misses       1301     1301           
Flag Coverage Δ
fast 87.66% <77.77%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
sbi/utils/sbiutils.py 90.33% <100.00%> (+0.04%) ⬆️
sbi/simulators/simutils.py 95.34% <66.66%> (ø)
sbi/utils/simulation_utils.py 94.44% <66.66%> (ø)

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Simulating turns on deterministic cuDNN for the rest of the session, even without a seed

1 participant