Skip to content

simulate_for_sbi with num_workers > 1 repeats simulator noise because per-batch seeds collide #2109

Open

Description

@aniketpandey05

Bug description

With num_workers > 1, simulate_for_sbi gives every batch its own seed and calls
seed_all_backends(seed) in the worker. The seeds are drawn with replacement from only
10^6 values:

  • sbi/utils/simulation_utils.py:84: np.random.randint(low=0, high=1_000_000, size=(len(batches),))
  • sbi/simulators/simutils.py:56 (simulate_in_batches, used by ABC):
    torch.randint(high=1_000_000, size=(len(batches),))

Two batches that draw the same seed get exactly the same simulator noise. With the default
simulation_batch_size=1, there is one batch per simulation, so by the birthday problem
about N² / (2 · 10^6) simulations repeat the noise of another one:

simulations expected repeats measured
5,000 ~12 11
20,000 ~200 199
100,000 ~5,000 (5%)

From 10^6 simulations on, there are at most 10^6 distinct noise streams. There is no
warning, and the training data silently contain correlated simulations.

Expected: every batch gets independent randomness, and the run stays reproducible for a
given seed.

Minimal code example

import torch

from sbi.inference import simulate_for_sbi
from sbi.utils import BoxUniform


def simulator(theta):
    theta = torch.as_tensor(theta)
    return theta + torch.randn_like(theta)


if __name__ == "__main__":
    prior = BoxUniform(torch.zeros(2), torch.ones(2))
    num_simulations = 5_000
    theta, x = simulate_for_sbi(
        simulator,
        prior,
        num_simulations=num_simulations,
        num_workers=2,
        seed=0,
        show_progress_bar=False,
    )
    noise = x - theta
    num_unique = torch.unique(noise.round(decimals=5), dim=0).shape[0]
    print(f"simulations: {num_simulations}, repeated noise draws: "
          f"{num_simulations - num_unique}")

Error message or traceback

No error. Output on main (7de5b6b5):

simulations: 5000, repeated noise draws: 11

Expected: 0.

sbi version

0.27.0 (main, 7de5b6b)

Python version

3.13.7

Device

CPU

Inference method

Not method-specific, ABC (MCABC / SMCABC)

Single-round or multi-round

Not applicable

Additional context

Tested on Windows 10.

Related: the open PR #1738 refactors this code but keeps the same
np.random.randint(0, 1_000_000) batch seeds, so it does not fix this. Reviewers there
also asked to derive the batch seeds from the user's seed for reproducibility. Spawning
independent child seeds from seed would solve both.

will open a pr once issue is reviewed

Activity

  1. added
    bug
    on Oct 27, 2026
  2. janfb commented on Oct 28, 2026

    @janfb
    Contributor

    @aniketpandey05 thanks for the clear report and the measurements!

    The simulate_for_sbi part is fixed in PR #1738: since its update today, every simulator call gets its own seed, derived from seed and drawn without replacement. Your example now reports 0 repeated noise draws.

    The ABC path in simulate_in_batches still has the problem and is not part of PR #1738. Once that PR is merged, a small PR is welcome that runs simulate_in_batches through simulate_from_theta, so that its own batching and seeding code goes away.

  3. aniketpandey05 commented on Oct 29, 2026

    @aniketpandey05
    ContributorAuthor

    @Ayankkk29 hi I was working on this one

  4. janfb commented on Oct 29, 2026

    @janfb
    Contributor

    @Ayankkk29 thanks for the offer. My note above was for @aniketpandey05, who reported this, so the ABC part stays with them once PR #1738 is merged.

    cheers, Jan

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions