FIX: Running MCMC within JIT causes tracer leaks#2165
Merged
fehiepsi merged 1 commit intopyro-ppl:masterfrom Apr 1, 2026
Merged
FIX: Running MCMC within JIT causes tracer leaks#2165fehiepsi merged 1 commit intopyro-ppl:masterfrom
fehiepsi merged 1 commit intopyro-ppl:masterfrom
Conversation
Member
|
This seems to be the right approach. Let me think a bit more. Thanks for addressing this! after this PR, numpyro seems to be leak-free. :) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #2000
(fixed using Claude code)
This is a path to a solution, @fehiepsi. I am unsure if this is the type of solution we are after.
Root Cause
When MCMC runs inside a
@jitcontext, JAX tracers (abstract values used during tracing) get captured in module-level caches through this chain:Three caching sites were affected:
cached_bydecorator (used by_collect_and_postprocessandfori_collect): Module-level._cacheOrderedDict storing functions that transitively reference traced valuesMCMC._cache: Instance-level cache ofsample_fn/postprocess_fnpairsMCMC._init_state_cache: Instance-level cache of HMC statesFix
Added
_is_under_jax_transform()which usesjax._src.core.trace_state_clean()to detect when code is executing inside a JAX tracing context. When tracing is active, all three caching mechanisms are bypassed:numpyro/util.py:cached_byskips caching when under JIT; new_is_under_jax_transform()helpernumpyro/infer/mcmc.py:_get_cached_fns()and_compile()skip caching when under JITTests
xfailmarkers from existing tests that were expected to fail withJAX_CHECK_TRACER_LEAKS=1test_mcmc_inside_jit_no_tracer_leakregression test that runs MCMC inside@jitand verifies no tracers leak into module-level caches