Repository navigation
Description
Overview
The JAX Matérn-kernel regularization path (kv_xp in matern_kernel.py) depends on tensorflow_probability.substrates.jax.math.bessel_kve, currently via a pinned tfp-nightly==0.26.0.dev20260713 (stable tfp 0.25.0 breaks under modern JAX; see #385). Pinned nightlies get pruned from PyPI and drag in the full TF-probability stack. This task vendors tfp's bessel_kve into autoarray as plain jax.numpy and drops the tfp dependency entirely, removing this class of version-incompatibility permanently.
Plan
- Vendor tfp's modified-Bessel
bessel_kve(only the parts the JAX path uses) into autoarray as a smalljax.numpymodule, keeping the Apache-2.0 header/attribution and keeping it differentiable (custom_jvp). - Point
kv_xp's JAX branch at the vendored function; the NumPy branch keepsscipy.special.kv. - Drop
tfp-nightlyfromPyAutoArray[optional]and scrub every tfp mention from docstrings/comments/ImportError. - Phase 2 (workspace): add a parity + gradient script in workspace_test — vendored
kv/kvevsscipy.special.kveto rtol 1e-10 overv ∈ [0.5, 5.5]× azrange, plusjax.gradvs finite differences — and update the scripts / smoke-install comments that still reference tfp. - Tier: glance — merge mode: auto-merge on green if the Witness passes, post-merge summary
Detailed implementation plan
Work Classification
Both — library first (PyAutoArray), then workspace (autogalaxy_workspace_test, autolens_workspace_test).
Affected Repositories
- PyAutoArray (primary)
- autogalaxy_workspace_test, autolens_workspace_test (phase 2)
Branch Survey
| Repository | Current Branch | Dirty? |
|---|---|---|
| ./PyAutoArray | main | clean (claimed by active task galaxy-duplicate-pytree-registration, #693) |
Suggested branch: feature/matern-vendor-bessel-kve
Worktree root: ~/Code/PyAutoLabs-wt/matern-vendor-bessel-kve/
Implementation Steps
Phase 1 — PyAutoArray
- New module
autoarray/inversion/regularization/bessel.py(or underautoarray/numpy_wrapper/if that fits better): portbessel_kve+ helpers fromtensorflow_probability/substrates/jax/math/bessel.py—_temme_expansion,_temme_series,_continued_fraction_kv,_evaluate_temme_coeffs,_olver_asymptotic_uniform,_bessel_ive_shared(K branch only),_sqrt1px2,_compute_general_continued_fraction. Translatetf.*shims tojnp/lax(lax.while_loopfor series/continued fractions);jax.custom_jvpfor the z-gradient. Lazyjaximport soimport autoarraystays light. Preserve the Apache-2.0 header. matern_kernel.py::kv_xp: JAX branch →bessel_kve(v, z) * exp(-|z|)from the vendored module; drop the tfptry/except ImportError; rewrite docstrings (~L14–30, ~L290–295).- Docstring tfp references in
matern_adapt_kernel.py:53,matern_adapt_power_kernel.py:46. pyproject.toml: removetfp-nightly==0.26.0.dev20260713and its comment block (L88–96); update the L79 comment.test_autoarray/inversion/regularizations/test_kernel_jax_gradients.py: Matern no longer needs an extra dep — add a cheapkv_xpJAX-vs-scipy spot check and update the module docstring (full parity grid stays in workspace_test).- Verify
grep -ri "tfp\|tensorflow"is empty; existing Matern unit tests pass unchanged.
Phase 2 — workspace_test (after phase 1 merges)
autogalaxy_workspace_test/scripts/misc/jax_assertions/: newbessel_kve_parity.py(grid parity rtol 1e-10 + grad-vs-FD); retitlematern_regularization.pydocstring from "tfp path" to "vendored kve".autolens_workspace_test: update tfp text inscripts/imaging/jax_grad/regularization.py,knn.py, and.github/scripts/smoke_install.shcomments (the same smoke_install comment exists in 5 other workspaces — sweep or file a hygiene follow-up).
Risks
Numerical delicacy near half-integer v and at small/large z, where the algorithm switches Temme ↔ continued fraction ↔ Olver asymptotics — the parity grid must straddle those regime boundaries. kv_xp has no callers outside autoarray; its public signature is unchanged.
Key Files
autoarray/inversion/regularization/matern_kernel.py—kv_xp, Matern covarianceautoarray/inversion/regularization/matern_adapt_kernel.py,matern_adapt_power_kernel.py— docstringspyproject.toml—optionalextrastest_autoarray/inversion/regularizations/test_kernel_jax_gradients.py
Original Prompt
Click to expand starting prompt
Vendor bessel_kve into autoarray and drop the tensorflow-probability dependency
Type: refactor
Target: autoarray
Repos:
- PyAutoArray
Themes: - pixelization
- hygiene
Difficulty: large
Autonomy: supervised
Priority: medium
Status: formalised
Consequence: glance
Witness: The vendoredkve/kvmatchscipy.special.kveto rtol 1e-10 on a (v in [0.5, 5.5]) x z grid withjax.gradmatching finite differences (workspace_test), the existing Matern-kernel tests pass unchanged, andtensorflow-probability/tfp-nightlyappear in neitherpyproject.tomlnor any import.
Review-minutes: 3
Unattended: ready
Lane: any
Filed: 2026-07-13 (backfilled from git)
Durable follow-up to the Matern-kernel tfp/jax incompatibility (PyAutoArray #385).
That bug was unblocked by pinning tfp-nightly==0.26.0.dev20260713 in place of the
broken stable tensorflow-probability==0.25.0 (0.25.0 references
jax.interpreters.xla.pytype_aval_mappings, removed from modern JAX). The nightly
works but is a fragile dependency: PyPI eventually prunes old nightlies, so the
pinned build may 404 for new installs in future, and an unpinned nightly makes
installs non-reproducible and can silently re-break on tfp drift.
The JAX Matern path (autoarray/inversion/regularization/matern_kernel.py, kv_xp)
needs tensorflow_probability.substrates.jax.math.bessel_kve only for the
modified-Bessel K_v term. jax.scipy.special has no kv/kve of arbitrary real
order, and nu is a continuous free prior (Uniform(0.5, 5.5)), so closed-form
half-integer Matern expressions are not sufficient.
Goal: vendor tfp's bessel_kve implementation into autoarray and remove the
entire tensorflow-probability / tfp-nightly dependency (a heavy dep — it drags
in the full TF-probability stack). This eliminates the whole version-incompatibility
class of bug permanently and drops a large optional dependency.
Scope / notes:
- Port
bessel_kveand its interdependent helpers from
tensorflow_probability/substrates/jax/math/bessel.py:_temme_expansion,
_temme_series,_continued_fraction_kv,_evaluate_temme_coeffs,
_olver_asymptotic_uniform,_bessel_ive_shared,_sqrt1px2,
_compute_general_continued_fraction, plus thecustom_gradient/custom_jvp
plumbing (the JAX path is used with gradients — the port must stay
differentiable). Translate thetf-on-jax shims (tf.where,tf.math.exp, …)
to plainjax.numpy. Preserve the Apache-2.0 licence header / attribution. - The vendored module is
xp-threaded like the rest of autoarray; the NumPy path
keeps usingscipy.special.kv(unaffected). - Parity test (the hard requirement): compare the vendored
kv/kveagainst
scipy.special.kveacross a(v, z)grid spanning the prior range
(v ∈ [0.5, 5.5], a range ofz) to a tight tolerance, plus a gradient check
(jax.gradvs finite-difference). Lives in workspace_test (JAX), not
test_autoarray/. - On completion, remove
tfp-nightlyfromPyAutoArray/pyproject.tomland update
thekv_xpdocstring/ImportErroraccordingly.
This is large and numerically delicate; keep it separate from the release-blocking
pin swap. See [[project_kernel_cdf_mesh_shipped]] and [[feedback_dependency_graph]].
Activity
docs+test: warn that the nu-gradient is incorrect; cover Olver branch…
Jammy2211 commentedon Nov 15, 2026
Library PR Created
Phase 1 is up: #695 (
feature/matern-vendor-bessel-kve). tfp'sbessel_kveis now vendored as pure JAX,kv_xpuses it, andtfp-nightlyhas been removed.scipy.special.kveto 9e-14 over ν ∈ [0.5, 5.5] and to 2.3e-13 over ν from 0 to 60, with z from 1e-8 to 1e3.matern_regularization.pypasses with x64 on; independent review CLEAN.Workspace impact: option (ii), docs and validation only, with no API migration. Phase 2 adds
bessel_kve_parity.pytoautogalaxy_workspace_testand updates the tfp text inautogalaxy_workspace_test/autolens_workspace_testscripts and thesmoke_install.shcomments. It runs through/start_workspaceon the next launch.The run was supervised
--auto, so it stops at PR-open and the human merges via/prm.Generated by Claude Code
Jammy2211 commentedon Nov 15, 2026
Shipped
All three PRs are merged via
/prm:c18f20c3. Vendors a pure-JAXbessel_kveinautoarray/inversion/regularization/bessel.py;kv_xpuses it;tfp-nightlyis removed fromautoarray[optional].5be44ee. Adds thebessel_kve_parity.pycertification (in smoke) and removes the tfp wording.0840039. Docstrings only.Witness (all three conditions met):
kve/kvmatchscipy.special.kveto rtol 1e-10 over ν ∈ [0.5, 5.5] × z, with actual max relative error 1.65e-13.jax.gradmatches finite differences and the closed form. This is checked in workspace_test, and smoke passed on 3.12 and 3.13.tensorflow-probability/tfp-nightlyappear in neitherpyproject.tomlnor any import.Follow-ups, not filed:
smoke_install.shcomment still exists in five other workspaces.Generated by Claude Code