Repository navigation
refactor(fitness): compose Fitness._vmap as jit(vmap(call)) so the batch is one cached executable - #1638
Merged
Conversation
…tch is one cached executable `Fitness._vmap` built `jax.vmap(jax.jit(self.call))`. A vmap has no cache of its own, so every call re-entered Python's batching trace and dispatched the inner pjit eagerly underneath it, which is where the two autogalaxy_workspace_test#118 hangs of multi_dataset/jax_likelihood/delaunay.py parked (vmap_f -> flatten_fun_for_vmap -> cache_miss -> _pjit_batcher -> _pjit_call_impl). `jax.jit(jax.vmap(self.call))` is the conventional order, already used by analysis/latent.py: the whole batch is one XLA program keyed on the batch shape, and a same-shape repeat call is served from jit's C++ fast path. The inner jit is dropped because a jit nested inside a jit is inlined. Not a fix for the XLA CPU Eigen-pool FFT deadlock (#1530): that fires at execution time and execution still happens once per call under either order. The only measured effect of the ordering is the 2026-08-23 A/B on rectangular_mge.py (control 8/10 stalls vs 3/10 for jit-of-vmap, Fisher p=0.070), recorded as contributory rather than causal. The executable stays specialised per batch length, neither fixed nor worsened here. `log_on_first_compile`'s wrapper now carries the jitted callable on `__wrapped__`, so `_cache_size()` serves as a compile-count probe. The new test_fitness_vmap_cache.py witnesses: traced once per batch shape, one cache entry after a same-shape repeat call, values bitwise identical to the old composition, and the cache rebuilt after a pickle round-trip. The log strings and the heartbeat contract in jax_compile.py are unchanged. Closes #1636. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01GhSsEfQUSBRDUDfpcLZnaw
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.
Summary
Closes #1636.
Fitness._vmapbuiltjax.vmap(jax.jit(self.call)), vmap of jit. A vmap has no cache of its own, so every call re-entered Python's batching trace and dispatched the inner pjit eagerly underneath it, which is where the two autogalaxy_workspace_test#118 hangs ofmulti_dataset/jax_likelihood/delaunay.pyparked (vmap_f -> flatten_fun_for_vmap -> cache_miss -> _pjit_batcher -> _pjit_call_impl). This PR flips it tojax.jit(jax.vmap(self.call)), the conventional order thatanalysis/latent.pyalready uses: the whole batch is one XLA program keyed on the batch shape, and a same-shape repeat call is served from jit's C++ fast path with no Python in between. The inner jit is dropped because a jit nested inside a jit is inlined and only adds a call boundary.log_on_first_compile's wrapper now carries the jitted callable on__wrapped__, so_cache_size()serves as a compile-count probe for tests and diagnostics. Log strings and the heartbeat contract injax_compile.pyare unchanged.What this is not. The prompt's premise that the outer jit "halves the exposure" to the Eigen-pool deadlock does not hold as stated: the executable was already cached on
main(the inner pjit's jaxpr and lowering caches key on the per-example avals), and the deadlock (#1530, XLA CPUFftThunkre-entering the Eigen pool via ducc0) fires at execution, which still happens once per call under either order. What the ordering changes is the HLO (one fused batch program instead of a batched inner pjit), and the only measurement of that is the 2026-08-23 A/B onrectangular_mge.pyfrom branchexperiment/jax-vmap-jit-ordering: control 8/10 stalls vs 3/10 for jit-of-vmap, Fisher p=0.070, recorded as contributory rather than causal. Cite it as suggestive, never as the deadlock being gone. The executable also stays specialised per batch length (PyAutoMind draftvmap_jit_recompiles_per_nautilus_batch_length.md), neither fixed nor worsened here.API Changes
None — internal changes only.
Fitness._vmapis a privatecached_propertywith the same(B, d) -> (B,)contract; the only production caller (Nautilusviafitness.call_wrap) is untouched.log_on_first_compile's returned wrapper gains a__wrapped__attribute.See full details below.
Test Plan
test_autofit/non_linear/test_fitness_vmap_cache.py(4 tests): traced once per batch shape with_cache_size() == 1after a same-shape repeat call, a third call at a different batch length adds exactly one entry,jit(vmap(call))vsvmap(jit(call))bitwise equal (np.array_equal, max abs diff 0.0 on a(5, d)batch),call_wrappromotes(d,)to a length-1 batch equal to the numpy-path value, cache rebuilt after a pickle round-tripmain(AttributeError: 'function' object has no attribute '__wrapped__', 2 of the 4 tests); the trace-count assertion alone passes onmaintoo, which is why the cache-size read is the witnesstest_jax_compile.py,test_fitness_jax_dispatch.py,test_fitness_ceiling_jax.py,test_fitness_assertions_jax.py,non_linear/search): 380 passed, 30 skippedpython -m pytest test_autofit -q -x: 2807 passed, 48 skipped, 117 sgrep -rn "jax\.vmap(jax\.jit("leaves no other vmap-of-jit site in the reporetime.ymlonmulti_dataset/jax_likelihood/delaunay.py, 5 repeats, 300 s cap, once on the same-named branchfeature/fitness-vmap-outer-jit(the chain checkout picks this PR's PyAutoFit branch) and once onmainas control. Compare the "traced, lowered and compiled in" seconds and the printed_vmapresult between the two; the likelihoods must be bit-identical. Results go on refactor(fitness): jit(vmap(call)) in Fitness._vmap so the batched likelihood is cached #1636.Heart was not consulted (
pyauto-heartis unreachable from this web-github session); the full serial suite above is the substitute gate.Full API Changes (for automation & release notes)
Removed
Added
autofit.non_linear.jax_compile.log_on_first_compile— the returned wrapper now exposes the wrapped (jitted) callable aswrapper.__wrapped__.Changed Behaviour
autofit.non_linear.fitness.Fitness._vmap(private) — composed asjax.jit(jax.vmap(self.call))instead ofjax.vmap(jax.jit(self.call)). Same inputs, same outputs (bitwise on the example analysis); a same-shape repeat call no longer re-enters Python's batching trace.Migration
Generated by the PyAutoLabs agent workflow.
🤖 Generated with Claude Code
https://claude.ai/code/session_01GhSsEfQUSBRDUDfpcLZnaw
Generated by Claude Code