Repository navigation
Conversation
Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01LKxc9GKLwnyeV2jaZBxH9N
juanitorduz
left a comment
There was a problem hiding this comment.
Thanks @rpinho! The fix in util.py looks right. I'm requesting changes for the tests: the new test has 8 cases that all catch the same bug, and it compares exact values against a copy of numpyro's internals, so unrelated changes would break it. Details and a smaller alternative are inline.
| return soft_vmap( | ||
| single_prediction, | ||
| (jax.random.key_data(rng_key), posterior_samples), | ||
| (rng_key, posterior_samples), |
There was a problem hiding this comment.
This change looks right to me. Some context for the PR description: key_data was added in #2073 to work around #2071 (Cannot convert_element_type from int32 to key<fry>), which happened because soft_vmap always called jnp.pad on the keys. #2318 now skips the pad when the width is zero, and here chunk_size is either num_samples or 1, so _predictive never pads. That's why typed keys can pass straight through now.
That dependency is already covered: if I put the unconditional pad back in soft_vmap, test_predictive[new-True], test_predictive_with_deterministic[True] and test_predictive_with_batched_model_params[True] fail with the #2071 error.
This line also fixes infer_discrete=True with rbg keys. On master that fails with expected key_data.shape to end with (2,); got shape=(4,), and on this branch it passes. Worth mentioning in the description.
| rng_type = request.param | ||
| if rng_type == "old": | ||
| return random.key(0) | ||
| return random.PRNGKey(0) |
| @pytest.mark.parametrize("impl", ["rbg", "unsafe_rbg"]) | ||
| @pytest.mark.parametrize("parallel", [False, True]) | ||
| @pytest.mark.parametrize("num_samples", [1, 4]) |
There was a problem hiding this comment.
All 8 cases fail on master with the same TypeError: Incorrect type for rng_seed, so they catch one bug 8 times. Most of the grid can go:
unsafe_rbgrepeatsrbg: both have key data of shape(4,), and numpyro has no impl-specific code.- With
num_samples=1,soft_vmapgetschunk_size=1whetherparallelis True or False, so those two cases run the same code. num_samples=1vs4doesn't reach the bug, which isn't in the split/reshape step.parallelis worth keeping. Parallel Predictive with new-style prng keys #2071 broke only theparallel=Truepath, so a typed-key regression in one branch has happened before.
| def sample(key): | ||
| _, subkey = random.split(key) | ||
| return dist.Normal(1.0, 2.0).sample(subkey) | ||
|
|
||
| key = random.key(0, impl=impl) | ||
| if num_samples == 1: | ||
| expected = sample(key)[None] | ||
| else: | ||
| keys = random.split(key, num_samples) | ||
| expected = jax.vmap(sample)(keys) if parallel else jax.lax.map(sample, keys) | ||
| actual = Predictive(model, num_samples=num_samples, parallel=parallel)(key)["y"] | ||
| np.testing.assert_array_equal(actual, expected) |
There was a problem hiding this comment.
I'd drop the exact-value comparison. expected copies three internals: seed's split convention (_, subkey = random.split(key)), _predictive skipping the split when num_samples == 1, and soft_vmap choosing vmap or lax.map.
The last one matters more than it looks. Locally (CPU, jax 0.10.2), jax.vmap(sample)(keys) and jax.lax.map(sample, keys) differ by up to 4.9 for rbg and 3.3 for unsafe_rbg, because rbg random bits change under vmap. That's why expected has to branch on parallel. For threefry the bits match, but the floats still differ by 1 ULP (1.19e-7) between the two compiled paths. So assert_array_equal would fail if Predictive ever chunked differently or XLA compiled the sampling differently, with nothing wrong for users.
The bug users hit is the TypeError, so checking the shape (and maybe that the draws are distinct) is enough. The import jax at the top can go too, since only this test uses it.
| @pytest.mark.parametrize("impl", ["rbg", "unsafe_rbg"]) | ||
| @pytest.mark.parametrize("parallel", [False, True]) | ||
| @pytest.mark.parametrize("num_samples", [1, 4]) | ||
| def test_predictive_with_nondefault_prng(impl, parallel, num_samples): |
There was a problem hiding this comment.
Instead of this test, I'd add an rbg case to the existing rng_key fixture above:
@pytest.fixture(params=["old", "new", "rbg"])
def rng_key(request):
rng_type = request.param
if rng_type == "old":
return random.PRNGKey(0)
elif rng_type == "rbg":
return random.key(0, impl="rbg")
else:
return random.key(0)I tried this locally. On master, test_predictive[rbg-True] and test_predictive[rbg-False] fail with the original TypeError. On this branch all 6 test_predictive cases pass in about 5s. That covers both parallel branches with 2 cases instead of 8 and only checks shapes.
One drawback: test_predictive also runs MCMC with the rbg key, so an MCMC bug with rbg keys would show up there too. If you'd rather keep MCMC out of it, a small prior-predictive test parametrized only on parallel that checks the shape and that the draws are distinct works as well.
Replace the exact-value test with an rbg case in the existing rng_key fixture, as suggested in review. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01LKxc9GKLwnyeV2jaZBxH9N
|
Thanks, that's helpful. I changed those. |
Benchmark reportthis PR run time: unchanged across 32 benchmarks
compile time: unchanged across 32 benchmarksNo significant changesEvery benchmark stayed within ±5% run time and ±25% compile time. 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-predictive-prng |
| commit | fc7691c7 |
ff9c7507 |
| numpyro | 0.22.0 | 0.22.0 |
| jax | 0.12.1 | 0.12.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.
|
@rpinho I think you also need to fix some tests because of the precison and change in rng 🙏 |
juanitorduz
left a comment
There was a problem hiding this comment.
please look into the tests
|
Thanks! We would be glad to help in any way we can. What would be helpful for you? What's a hard problem? |
|
I looked but I don't see it. The 11 failures in test-modeling and test-inference (3.14) also fail on master (fc7691c) with jax 0.12.1, with the same values, and the same jobs fail on #2443 and #2447. None of it calls Predictive. CI passed on 11-17 with jax 0.12.0.1, and test_infer_util.py passes with the rbg case (64/64). So I don't think this PR broke them. Let me know if you disagree. |
Predictiveraises aTypeErrorwhen a key uses therbgimplementation. CallPredictive(model, num_samples=4)(jax.random.key(0, impl="rbg"))for a model that samples a Normal. The patch passes keys through the prediction map with their PRNG type.Context from review:
key_datawas added in #2073 to work around #2071, which came fromsoft_vmapalways padding the keys. Since #2318 the pad is skipped when its width is zero, and_predictiveuses a chunk size ofnum_samplesor 1, so it never pads and typed keys can pass straight through. The same line also fixesinfer_discrete=Truewithrbgkeys, which on master fails withexpected key_data.shape to end with (2,); got shape=(4,).The test adds an
rbgcase to the existingrng_keyfixture:test_predictive[rbg-True]andtest_predictive[rbg-False]fail on master with the originalTypeErrorand pass with the patch. The fixture's"old"case usesrandom.PRNGKey(0)again, as #2073 intended.🤖 Generated with Claude Code
https://claude.ai/code/session_01LKxc9GKLwnyeV2jaZBxH9N