Skip to content

refactor: route the remaining direct Tracer checks through not_jax_tracer - #2435

Merged
juanitorduz merged 2 commits into
pyro-ppl:masterfrom
nstarman:fix/route-tracer-checks
Nov 17, 2026
Merged

juanitorduz merged 2 commits into
pyro-ppl:masterfrom
nstarman:fix/route-tracer-checks

Conversation

@nstarman

@nstarman nstarman commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

This is a follow-up to #2428, from the review there. Two places still checked isinstance(x, Tracer) directly:

  • numpyro/distributions/util.py, in multinomial: the check that a traced n needs total_count_max. It now calls not_jax_tracer, like the other callers.
  • numpyro/infer/reparam.py, in LocScaleReparam.__init__: the type assert on centered. Tracers are already jax.Array instances, so the jax.core.Tracer entry in the tuple never mattered. I removed it and left a plain type check. Routing it through not_jax_tracer would also let containers holding a tracer through once fix(util): make not_jax_tracer see tracers inside pytrees #2428 lands, and those then fail later in the constraint check.

The unused Tracer and jax imports are gone. For plain arrays and tracers nothing changes.

Tests:

  • test_multinomial_traced_n_requires_total_count_max checks that a traced n without total_count_max still raises.
  • test_loc_scale_traced_centered checks that a traced centered is still accepted under jit.

Both guard behaviour that is already correct, so they pass on master too. test/test_distributions_util.py and test/infer/test_reparam.py pass, apart from one test that needs tensorflow_probability, which isn't installed in my local env. ruff check and ruff format --check are clean.

🤖 Generated with Claude Code

@github-actions github-actions Bot added the awaiting review Awaiting review from maintainers label Nov 15, 2026
@github-actions

github-actions Bot commented Nov 15, 2026 •

Copy link
Copy Markdown

Benchmark report

this PR fix/route-tracer-checks at 9f60f2c3 vs baseline master at eff7cf3b

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

Significant changes (2)

                                ──────── run time ───────     ────── compile time ──────
  benchmark                     baseline   this PR      Δ     baseline   this PR       Δ
────────────────────────────────────────────────────────────────────────────────────────
+ stick_breaking_transform        4.8 ms    4.5 ms  -7.5%     129.2 ms  148.3 ms  +14.8%
- predictive_forward_sampling   372.5 ms  368.3 ms  -1.1%      88.1 ms  131.9 ms  +49.8%

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            2.8 ms    2.9 ms    +3.3%     147.9 ms  144.8 ms   -2.1%
  categorical_log_prob             1.4 ms    1.4 ms    -1.8%      53.0 ms   47.7 ms  -10.0%
  dirichlet_log_prob               454 µs    420 µs  (-7.6%)     999.1 ms  968.5 ms   -3.1%
  dirichlet_sample               115.2 ms  113.5 ms    -1.5%     587.1 ms  590.6 ms   +0.6%
  gamma_log_prob                   1.4 ms    1.4 ms    -1.3%       4.78 s    4.61 s   -3.5%
  gamma_sample                    36.8 ms   36.9 ms    +0.3%     545.6 ms  529.8 ms   -2.9%
  lkj_cholesky_sample              4.1 ms    4.1 ms    -1.3%     795.8 ms  739.3 ms   -7.1%
  mixture_same_family_log_prob     614 µs    584 µs    -4.9%      55.2 ms   58.5 ms   +6.0%
  multivariate_normal_log_prob     481 µs    449 µs  (-6.6%)     342.6 ms  343.4 ms   +0.2%
  normal_log_prob                  934 µs    924 µs    -1.1%      47.6 ms   50.0 ms   +5.1%
  normal_sample                   44.3 ms   44.5 ms    +0.4%     125.8 ms  127.7 ms   +1.6%
+ stick_breaking_transform         4.8 ms    4.5 ms    -7.5%     129.2 ms  148.3 ms  +14.8%
  student_t_log_prob               2.1 ms    2.1 ms    +1.3%     103.7 ms  107.9 ms   +4.1%
  truncated_normal_log_prob        931 µs    920 µs    -1.2%      40.8 ms   43.2 ms   +6.0%

handlers

                                  ───────── run time ─────────     ────── compile time ──────
  benchmark                       baseline   this PR         Δ     baseline   this PR       Δ
─────────────────────────────────────────────────────────────────────────────────────────────
  initialize_model_hierarchical    30.9 ms   31.2 ms     +1.1%       2.41 s    2.42 s   +0.4%
  log_density_hierarchical          2.5 ms    2.5 ms     -0.8%     795.5 ms  792.8 ms   -0.3%
  nested_handler_stack              1.1 ms    1.1 ms     -1.9%       577 µs    601 µs   +4.1%
  potential_energy_and_grad          15 µs     18 µs  (+18.7%)      65.7 ms   66.4 ms   +1.0%
- predictive_forward_sampling     372.5 ms  368.3 ms     -1.1%      88.1 ms  131.9 ms  +49.8%
  trace_seeded_model                475 µs    468 µs     -1.3%     338.3 ms  328.8 ms   -2.8%

mcmc

                             ──────── run time ───────     ───── compile time ─────
  benchmark                  baseline   this PR      Δ     baseline  this PR      Δ
───────────────────────────────────────────────────────────────────────────────────
  hmc_logistic_regression    458.6 ms  465.7 ms  +1.5%       1.87 s   1.91 s  +2.0%
  nuts_dense_mass_funnel     702.4 ms  702.0 ms  -0.1%       1.58 s   1.61 s  +2.1%
  nuts_eight_schools         781.8 ms  791.7 ms  +1.3%       1.63 s   1.66 s  +1.9%
  nuts_hierarchical_glm        1.98 s    2.00 s  +1.0%       2.73 s   2.76 s  +1.1%
  nuts_logistic_regression   794.5 ms  805.6 ms  +1.4%       1.89 s   1.96 s  +3.3%
  nuts_vectorized_chains       1.49 s    1.50 s  +0.9%       1.68 s   1.80 s  +7.3%

svi

                                             ──────── run time ───────     ───── compile time ─────
  benchmark                                  baseline   this PR      Δ     baseline  this PR      Δ
───────────────────────────────────────────────────────────────────────────────────────────────────
  svi_autodelta_map_logistic                 193.5 ms  201.0 ms  +3.9%       1.91 s   1.91 s  -0.0%
  svi_autodiagonalnormal_hierarchical        605.2 ms  622.9 ms  +2.9%       3.13 s   3.18 s  +1.7%
  svi_automultivariatenormal_eight_schools   480.1 ms  488.6 ms  +1.8%       3.01 s   3.09 s  +2.7%
  svi_autonormal_logistic                    537.7 ms  547.0 ms  +1.7%       2.14 s   2.25 s  +4.8%
  svi_multi_particle_elbo                    767.7 ms  776.8 ms  +1.2%       2.15 s   2.33 s  +8.6%
  svi_trace_mean_field_elbo                  734.8 ms  728.8 ms  -0.8%       3.38 s   3.38 s  +0.0%
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/route-tracer-checks
commit eff7cf3b 9f60f2c3
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-1016-aws-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 multinomial change looks good. For LocScaleReparam, could you drop the tracer clause instead of routing it through not_jax_tracer?

assert centered is None or isinstance(
    centered, (int, float, np.generic, np.ndarray, jnp.ndarray)
)

Tracers are already jax.Array instances (jnp.ndarray is jax.Array). I checked jit/vmap/grad/jvp tracers on JAX 0.10.2 and 0.12.0.1. So jax.core.Tracer in the old tuple never mattered: removing it on master keeps all -k loc_scale tests passing, including test_loc_scale_traced_centered. After #2428, not not_jax_tracer(centered) only adds containers holding a tracer. Under jit, [c] and an array-like pytree then pass the assert and fail with TracerArrayConversionError at constraints.py:759, and {"a": c} fails with TypeError there. Eagerly they still get AssertionError. This assert is a type check, not a concreteness check, so not_jax_tracer is the wrong tool for it.

Nit: random.key(0) instead of random.PRNGKey(0) in the multinomial test, to match the test above it.

nstarman and others added 2 commits November 17, 2026 00:06
…acer

multinomial and LocScaleReparam checked isinstance(x, Tracer) directly.
Use not_jax_tracer like the other callers, so a traced value inside an
array-like pytree is treated the same way everywhere.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… check

Tracers are jax.Array instances, so the isinstance check already accepts
them; routing it through not_jax_tracer would also let containers holding a
tracer through, as noted in review.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@nstarman
nstarman force-pushed the fix/route-tracer-checks branch from 94e401a to 9f60f2c Compare November 17, 2026 08:09
@nstarman

Copy link
Copy Markdown
Contributor Author

Thanks! I dropped the tracer clause so the LocScaleReparam assert is a plain type check, switched to random.key, and rebased on master to clear the conflict.

@juanitorduz
juanitorduz merged commit b7ef064 into pyro-ppl:master Nov 17, 2026
10 checks passed
@nstarman
nstarman deleted the fix/route-tracer-checks branch November 17, 2026 19:08
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