Skip to content

refactor(fitness): compose Fitness._vmap as jit(vmap(call)) so the batch is one cached executable - #1638

Merged
Jammy2211 merged 1 commit into
mainfrom
feature/fitness-vmap-outer-jit
Sep 17, 2026
Merged

Jammy2211 merged 1 commit into
mainfrom
feature/fitness-vmap-outer-jit

Conversation

@Jammy2211

Copy link
Copy Markdown
Collaborator

Summary

Closes #1636.

Fitness._vmap built jax.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 of multi_dataset/jax_likelihood/delaunay.py parked (vmap_f -> flatten_fun_for_vmap -> cache_miss -> _pjit_batcher -> _pjit_call_impl). This PR flips it to jax.jit(jax.vmap(self.call)), the conventional order that analysis/latent.py already 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 in jax_compile.py are 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 CPU FftThunk re-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 on rectangular_mge.py from branch experiment/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 draft vmap_jit_recompiles_per_nautilus_batch_length.md), neither fixed nor worsened here.

API Changes

None — internal changes only. Fitness._vmap is a private cached_property with the same (B, d) -> (B,) contract; the only production caller (Nautilus via fitness.call_wrap) is untouched. log_on_first_compile's returned wrapper gains a __wrapped__ attribute.
See full details below.

Test Plan

  • New test_autofit/non_linear/test_fitness_vmap_cache.py (4 tests): traced once per batch shape with _cache_size() == 1 after a same-shape repeat call, a third call at a different batch length adds exactly one entry, jit(vmap(call)) vs vmap(jit(call)) bitwise equal (np.array_equal, max abs diff 0.0 on a (5, d) batch), call_wrap promotes (d,) to a length-1 batch equal to the numpy-path value, cache rebuilt after a pickle round-trip
  • The probe fails on main (AttributeError: 'function' object has no attribute '__wrapped__', 2 of the 4 tests); the trace-count assertion alone passes on main too, which is why the cache-size read is the witness
  • Focused set (test_jax_compile.py, test_fitness_jax_dispatch.py, test_fitness_ceiling_jax.py, test_fitness_assertions_jax.py, non_linear/search): 380 passed, 30 skipped
  • Full suite serial, python -m pytest test_autofit -q -x: 2807 passed, 48 skipped, 117 s
  • grep -rn "jax\.vmap(jax\.jit(" leaves no other vmap-of-jit site in the repo
  • CI matrix (3.12 / 3.13) green
  • Measurement, dispatched at PR-open: autogalaxy_workspace_test retime.yml on multi_dataset/jax_likelihood/delaunay.py, 5 repeats, 300 s cap, once on the same-named branch feature/fitness-vmap-outer-jit (the chain checkout picks this PR's PyAutoFit branch) and once on main as control. Compare the "traced, lowered and compiled in" seconds and the printed _vmap result 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-heart is unreachable from this web-github session); the full serial suite above is the substitute gate.

Full API Changes (for automation & release notes)

Removed

  • none

Added

  • autofit.non_linear.jax_compile.log_on_first_compile — the returned wrapper now exposes the wrapped (jitted) callable as wrapper.__wrapped__.

Changed Behaviour

  • autofit.non_linear.fitness.Fitness._vmap (private) — composed as jax.jit(jax.vmap(self.call)) instead of jax.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

  • None required.

Generated by the PyAutoLabs agent workflow.

🤖 Generated with Claude Code

https://claude.ai/code/session_01GhSsEfQUSBRDUDfpcLZnaw


Generated by Claude Code

…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
@Jammy2211 Jammy2211 added the pending-release PR queued for the next release build label Sep 17, 2026 — with Claude
@Jammy2211
Jammy2211 merged commit 3abbc13 into main Sep 17, 2026
4 checks passed
@Jammy2211
Jammy2211 deleted the feature/fitness-vmap-outer-jit branch September 17, 2026 16:53
@Jammy2211 Jammy2211 removed the pending-release PR queued for the next release build label Sep 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

refactor(fitness): jit(vmap(call)) in Fitness._vmap so the batched likelihood is cached

1 participant