Skip to content

Commit

Permalink
Add a warning message when using prng_key outside of seed (#1760)
Browse files Browse the repository at this point in the history
* add a warning message when using prng_key outside of seed

* format properly

* Fix typos

* make sure that the test catch warning for prng_key
  • Loading branch information
fehiepsi committed Mar 23, 2024
1 parent fe7b693 commit 9a2f618
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
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

0 comments on commit 9a2f618

Please sign in to comment.