Repository navigation
fix(handlers): accept NumPy integer scalars as the seed of the seed handler - #2425
Conversation
…andler handlers.seed(model, np.int64(3)) raised "Incorrect type for rng_seed", so seeding from NumPy integers, such as the elements of np.arange(n) or the output of Generator.integers, failed while the same Python int worked.
Benchmark reportthis PR + run time: 1 faster
- compile time: 1 slower, 0 fasterSignificant changes (2) ──────── run time ─────── ────── compile time ──────
benchmark baseline this PR Δ baseline this PR Δ
────────────────────────────────────────────────────────────────────────────────────────
+ biject_to_constraints 4.4 ms 4.1 ms -5.8% 202.6 ms 214.3 ms +5.7%
- predictive_forward_sampling 525.7 ms 541.6 ms +3.0% 117.6 ms 160.9 ms +36.9%Red is slower, green is faster; a row is coloured by the worse of its two columns. A delta in parentheses cleared the threshold on a measurement below the resolution floor, so it is shown without being called a change. † marks a benchmark that could not be compared — see below. Full results
|
| baseline | this PR | |
|---|---|---|
| ref | master |
fix/seed-numpy-integer |
| commit | 0f987499 |
d0121599 |
| numpyro | 0.22.0 | 0.22.0 |
| jax | 0.12.0.1 | 0.12.0.1 |
| backend | cpu | cpu |
| python | 3.14.4 | 3.14.4 |
Runner: Linux-7.0.0-1019-azure-x86_64-with-glibc2.43, 4 CPUs.
Produced by this benchmark run.
juanitorduz
left a comment
There was a problem hiding this comment.
Thanks! The fix is right: seed already accepts a 0-d np.ndarray (np.array(3)) but rejects the NumPy scalar np.int64(3), and jax.random.key(s) gives the same key for np.int32/np.int64/np.uint32 scalars as for the Python int. Two changes before merging:
1. Widen the type annotation. The signature at numpyro/handlers.py:867 is still rng_seed: Array | int | None. Array is jax.Array, so a type checker still flags callers that pass np.int64, even though it now works at runtime. Please make it Array | int | np.integer | None.
2. Trim the test. One regression test is worth keeping, since no existing test passes a NumPy scalar seed. But:
np.arange(5, dtype=seed_type)[3]is the sameseed_typescalar asseed_type(3), so the second assertion adds no coverage.np.int32adds nothing overnp.int64.np.int64is the common case andnp.uint32covers unsigned.
Suggested:
@pytest.mark.parametrize("seed_type", [np.int64, np.uint32])
def test_seed_numpy_integer(seed_type):
def model():
return numpyro.sample("x", dist.Normal())
assert_allclose(handlers.seed(model, seed_type(3))(), handlers.seed(model, 3)())|
Thanks! Both done in d012159: the annotation is now |
handlers.seedonly accepts a Pythonint(or a key) as the seed, so a NumPy integer scalar raises:NumPy integers come up naturally as seeds, for example the elements of
np.arange(n)in a loop over runs, or the output ofnp.random.Generator.integers. The same value as a Pythonintworks.The type check now accepts
np.integeras well, and the seed gives the same draws as the equivalent Pythonint.test_seed_numpy_integerchecksnp.int32,np.int64andnp.uint32seeds (a scalar and an element of a NumPy array) against the draw for the Pythonintseed. It fails on master and passes here.