Repository navigation
refactor: route the remaining direct Tracer checks through not_jax_tracer - #2435
Conversation
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 Δ
────────────────────────────────────────────────────────────────────────────────────────
+ 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
|
| 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
left a comment
There was a problem hiding this comment.
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.
…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>
94e401a to
9f60f2c
Compare
|
Thanks! I dropped the tracer clause so the |
This is a follow-up to #2428, from the review there. Two places still checked
isinstance(x, Tracer)directly:numpyro/distributions/util.py, inmultinomial: the check that a tracednneedstotal_count_max. It now callsnot_jax_tracer, like the other callers.numpyro/infer/reparam.py, inLocScaleReparam.__init__: the type assert oncentered. Tracers are alreadyjax.Arrayinstances, so thejax.core.Tracerentry in the tuple never mattered. I removed it and left a plain type check. Routing it throughnot_jax_tracerwould 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
Tracerandjaximports are gone. For plain arrays and tracers nothing changes.Tests:
test_multinomial_traced_n_requires_total_count_maxchecks that a tracednwithouttotal_count_maxstill raises.test_loc_scale_traced_centeredchecks that a tracedcenteredis still accepted underjit.Both guard behaviour that is already correct, so they pass on master too.
test/test_distributions_util.pyandtest/infer/test_reparam.pypass, apart from one test that needstensorflow_probability, which isn't installed in my local env.ruff checkandruff format --checkare clean.🤖 Generated with Claude Code