Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use SeedSequence spawning in Model.next_rng #5733

Closed
ricardoV94 opened this issue Apr 28, 2022 · 1 comment · Fixed by #5787
Closed

Use SeedSequence spawning in Model.next_rng #5733

ricardoV94 opened this issue Apr 28, 2022 · 1 comment · Fixed by #5787
Assignees

Comments

@ricardoV94
Copy link
Member

Numpy docs advise using SeedSequence for spawning independent bit_generators: https://numpy.org/doc/stable/reference/random/parallel.html

Their method should be quite more robust to collisions than our naive random.integers(2**30) strategy, we use here and in a couple other places:

new_seed = self.rng_seeder.randint(2**30, dtype=np.int64)

import math

unique_seeds = 2**30
for n_seeds in (10, 100, 1_000, 10_000, 100_000):
    # birthday paradox probability: https://en.wikipedia.org/wiki/Birthday_problem
    p_collision = 1-math.perm(unique_seeds, n_seeds) / unique_seeds**n_seeds
    print(f"{n_seeds=}, {p_collision=}")
n_seeds=10, p_collision=4.1909515080540416e-08
n_seeds=100, p_collision=4.610036260510597e-06
n_seeds=1000, p_collision=0.0004650875835883195
n_seeds=10000, p_collision=0.04549425469529611
n_seeds=100000, p_collision=0.9905023499278603

Similar to aesara-devs/aesara#936

@ricardoV94
Copy link
Member Author

Waiting for #5757 to be able to use Aesara's RandomStream (probably subclassing it so that it doesn't create default_updates).

This will also simplify the construction of SymbolicDistributions so that we can pass a RandomStream directly to them instead of having to ask how many RandomState variables it needs in advance:

# Instead of passing individual RNG variables we could pass a RandomStream
# and let the classes create as many RNGs as they need
rngs = [model.next_rng() for _ in range(cls.num_rngs(*args, **kwargs))]

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

Successfully merging a pull request may close this issue.

1 participant