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

Add a warning message when using prng_key outside of seed #1760

Merged
merged 4 commits into from
Mar 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions numpyro/primitives.py
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,10 @@ def prng_key():
:return: a PRNG key of shape (2,) and dtype unit32.
"""
if not _PYRO_STACK:
warnings.warn(
"Cannot generate JAX PRNG key outside of `seed` handler.",
stacklevel=find_stack_level(),
)
return

initial_msg = {
Expand Down
3 changes: 2 additions & 1 deletion test/test_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -778,7 +778,8 @@ def guide():


def test_prng_key():
assert numpyro.prng_key() is None
with pytest.warns(Warning, match="outside of `seed`"):
assert numpyro.prng_key() is None

with handlers.seed(rng_seed=0):
rng_key = numpyro.prng_key()
Expand Down
Loading