Skip to content

fix(handlers): accept NumPy integer scalars as the seed of the seed handler - #2425

Merged
juanitorduz merged 2 commits into
pyro-ppl:masterfrom
sohailshk:fix/seed-numpy-integer
Nov 15, 2026
Merged

juanitorduz merged 2 commits into
pyro-ppl:masterfrom
sohailshk:fix/seed-numpy-integer

Conversation

@sohailshk

Copy link
Copy Markdown
Contributor

handlers.seed only accepts a Python int (or a key) as the seed, so a NumPy integer scalar raises:

import numpy as np
import numpyro
import numpyro.distributions as dist
from numpyro import handlers

def model():
    return numpyro.sample("x", dist.Normal())

handlers.seed(model, np.int64(3))()
# TypeError: Incorrect type for rng_seed: <class 'numpy.int64'>

NumPy integers come up naturally as seeds, for example the elements of np.arange(n) in a loop over runs, or the output of np.random.Generator.integers. The same value as a Python int works.

The type check now accepts np.integer as well, and the seed gives the same draws as the equivalent Python int.

test_seed_numpy_integer checks np.int32, np.int64 and np.uint32 seeds (a scalar and an element of a NumPy array) against the draw for the Python int seed. It fails on master and passes here.

…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.
@github-actions github-actions Bot added the awaiting review Awaiting review from maintainers label Nov 11, 2026
@github-actions

github-actions Bot commented Nov 11, 2026 •

Copy link
Copy Markdown

Benchmark report

this PR fix/seed-numpy-integer at d0121599 vs baseline master at 0f987499

+ run time:     1 faster
- compile time: 1 slower, 0 faster

Significant 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

distributions

                                 ───────── 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%
  categorical_log_prob             2.1 ms    2.1 ms    -2.0%      64.2 ms   62.0 ms  -3.4%
  dirichlet_log_prob               608 µs    609 µs    +0.1%       1.53 s    1.57 s  +2.9%
  dirichlet_sample               133.9 ms  136.0 ms    +1.6%     797.9 ms  822.1 ms  +3.0%
  gamma_log_prob                   2.1 ms    2.0 ms    -1.1%       6.33 s    6.45 s  +1.9%
  gamma_sample                    48.6 ms   49.3 ms    +1.4%     737.8 ms  755.0 ms  +2.3%
  lkj_cholesky_sample              6.4 ms    6.5 ms    +2.4%       1.02 s    1.06 s  +4.0%
  mixture_same_family_log_prob     805 µs    833 µs    +3.5%      75.3 ms   75.9 ms  +0.8%
  multivariate_normal_log_prob     833 µs    765 µs  (-8.2%)     497.3 ms  491.7 ms  -1.1%
  normal_log_prob                  1.1 ms    1.1 ms    -1.3%      79.0 ms   80.1 ms  +1.4%
  normal_sample                   70.5 ms   71.8 ms    +1.7%     170.9 ms  171.1 ms  +0.1%
  stick_breaking_transform         6.7 ms    6.7 ms    -0.4%     201.9 ms  201.1 ms  -0.4%
  student_t_log_prob               3.0 ms    3.1 ms    +2.9%     147.4 ms  150.2 ms  +1.9%
  truncated_normal_log_prob        1.3 ms    1.2 ms    -4.2%      57.5 ms   62.6 ms  +8.8%

handlers

                                  ───────── run time ────────     ─────── compile time ───────
  benchmark                       baseline   this PR        Δ     baseline   this PR         Δ
──────────────────────────────────────────────────────────────────────────────────────────────
  initialize_model_hierarchical    45.6 ms   44.9 ms    -1.6%       3.39 s    3.43 s     +1.1%
  log_density_hierarchical          3.8 ms    3.7 ms    -1.0%       1.14 s    1.17 s     +3.1%
  nested_handler_stack              1.4 ms    1.4 ms    -1.3%       574 µs    812 µs  (+41.5%)
  potential_energy_and_grad          22 µs     24 µs  (+5.8%)      90.7 ms   89.9 ms     -0.9%
- predictive_forward_sampling     525.7 ms  541.6 ms    +3.0%     117.6 ms  160.9 ms    +36.9%
  trace_seeded_model                768 µs    798 µs    +3.8%     466.6 ms  467.1 ms     +0.1%

mcmc

                             ──────── run time ───────     ───── compile time ─────
  benchmark                  baseline   this PR      Δ     baseline  this PR      Δ
───────────────────────────────────────────────────────────────────────────────────
  hmc_logistic_regression    636.9 ms  641.7 ms  +0.8%       2.73 s   2.65 s  -3.0%
  nuts_dense_mass_funnel       1.02 s    1.00 s  -1.6%       2.25 s   2.39 s  +5.9%
  nuts_eight_schools           1.11 s    1.08 s  -2.6%       2.44 s   2.46 s  +0.8%
  nuts_hierarchical_glm        2.99 s    3.00 s  +0.6%       4.25 s   3.94 s  -7.2%
  nuts_logistic_regression     1.11 s    1.12 s  +0.8%       2.75 s   2.76 s  +0.2%
  nuts_vectorized_chains       2.16 s    2.17 s  +0.2%       2.67 s   2.54 s  -4.6%

svi

                                             ──────── run time ───────     ───── compile time ─────
  benchmark                                  baseline   this PR      Δ     baseline  this PR      Δ
───────────────────────────────────────────────────────────────────────────────────────────────────
  svi_autodelta_map_logistic                 275.7 ms  267.3 ms  -3.1%       2.82 s   2.70 s  -4.1%
  svi_autodiagonalnormal_hierarchical        859.4 ms  885.9 ms  +3.1%       4.51 s   4.67 s  +3.6%
  svi_automultivariatenormal_eight_schools   698.4 ms  681.9 ms  -2.4%       4.69 s   4.36 s  -7.2%
  svi_autonormal_logistic                    748.2 ms  732.9 ms  -2.0%       3.15 s   3.05 s  -3.2%
  svi_multi_particle_elbo                      1.13 s    1.12 s  -0.9%       3.23 s   3.18 s  -1.5%
  svi_trace_mean_field_elbo                    1.05 s    1.07 s  +1.9%       4.86 s   4.77 s  -1.8%
Methodology and environment

Each benchmark is set up untimed, then called once with the JAX caches cleared and several more times warm. Run is the fastest warm call; compile is the first call minus that, i.e. the tracing, lowering and XLA compilation the warm calls did not have to pay for.

Both refs were measured on the same runner over 2 interleaved round(s), taking the best observation per benchmark. A result is called neutral when it moves less than ±5% (run) or ±25% (compile), or when the measurement itself is under 1 ms (run) / 50 ms (compile) — a shared CI runner cannot resolve changes below that. Compile time gets the looser band because it is measured once per round rather than best-of-N, and swings by roughly 20% even between two runs of identical code. A delta shown in parentheses did clear its threshold, but on a measurement below the resolution floor, so it is reported without being called a change.

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 juanitorduz left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 same seed_type scalar as seed_type(3), so the second assertion adds no coverage.
  • np.int32 adds nothing over np.int64. np.int64 is the common case and np.uint32 covers 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)())

@sohailshk

Copy link
Copy Markdown
Contributor Author

Thanks! Both done in d012159: the annotation is now Array | int | np.integer | None, and the test is your suggested version (np.int64 and np.uint32, one assertion).

@juanitorduz
juanitorduz merged commit 6d8df07 into pyro-ppl:master Nov 15, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting review Awaiting review from maintainers

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants