Skip to content

refactor: vendor bessel_kve into autoarray and drop tfp-nightly - #695

Merged
Jammy2211 merged 2 commits into
mainfrom
feature/matern-vendor-bessel-kve
Nov 15, 2026
Merged

Jammy2211 merged 2 commits into
mainfrom
feature/matern-vendor-bessel-kve

Conversation

@Jammy2211

Copy link
Copy Markdown
Collaborator

Summary

Closes #694 (phase 1, library). The JAX Matérn-kernel path (kv_xp) needed tensorflow-probability only for the modified-Bessel K_v. This PR vendors tfp's bessel_kve K-branch — Temme power series (z ≤ 2), Steed continued fraction (z > 2), forward recurrence, Olver asymptotics (ν ≥ 50) — as plain jax.numpy/lax in autoarray/inversion/regularization/bessel.py (Apache-2.0 header + attribution preserved, list of modifications in the header), rewires kv_xp's JAX branch to it, and removes tfp-nightly==0.26.0.dev20260713 from the optional extras. This removes the whole tfp/JAX version-incompatibility class (PyAutoArray #385) and a heavy, prune-prone nightly dependency.

Not ported (only feed I_v, unused by kve): bessel_ive, bessel_iv_ratio, _bessel_ive_shared, _compute_general_continued_fraction, the log-space variants and the negative-order ive corrections.

API Changes

No public API change. kv_xp(v, z, xp) keeps its signature and NumPy behaviour (scipy.special.kv); the JAX branch no longer raises ImportError asking for tfp. New internal module autoarray.inversion.regularization.bessel (bessel_kve, kv). tfp-nightly is dropped from autoarray[optional].
See full details below.

Decision flagged (supervised --auto)

  • ν-gradient semantics preserved, not fixed. tfp defines no gradient of K_v in its order; the vendored custom_jvp keeps that (behaviour-preserving refactor). Independent review measured that grad(matern_kernel) w.r.t. ν is therefore wrong, not zero (other ν terms still contribute: −0.803 vs FD +0.059 at r=0.7, l=1.3, ν=1.7). Docstrings now say so and that ν must be treated as static. A correct order-derivative is a candidate follow-up if ν is ever sampled with gradient-based samplers.
  • Repo-claim override. Started while PyAutoArray is also claimed by galaxy-duplicate-pytree-registration (fix: duplicate Galaxy pytree registration after a JAX Fitness #693) on the human's explicit approval; file sets are disjoint.

Test Plan

  • pytest test_autoarray — 2409 passed, 1 skipped (before the follow-up commit); test_autoarray/inversion/regularizations — 135 passed at head.
  • New library tests: kv_xp JAX vs scipy.special.kv (rtol 1e-10) either side of z=2, near half-integers and in the ν≥50 Olver regime; reverse-mode dK/dz vs -(K_{ν-1}+K_{ν+1})/2; Matérn covariance scale gradient through the Cholesky vs central FD.
  • Off-CI parity sweep (float64, jit): max rel err vs scipy.special.kve 9e-14 over ν ∈ [0.5, 5.5], 1.5e-13 overall (ν up to 120, z 1e-6…500); review sweep 461 orders × 226 z: 2.3e-13; second derivatives in z 2.3e-13; no non-finite output where scipy is finite.
  • Smoke: autogalaxy_workspace_test/scripts/misc/jax_assertions/matern_regularization.py passes with JAX_ENABLE_X64=True (the smoke profile).
  • Phase 2 (workspace_test): bessel_kve_parity.py grid + grad-vs-FD script; retitle tfp prose in matern_regularization.py, autolens_workspace_test jax_grad/regularization.py, knn.py, and smoke_install.sh comments.

Validation checklist (--auto run — plan was not pre-approved)

  • Effective level: supervised (header: supervised, cap: refactor) — plan was approved by the human on refactor: vendor bessel_kve into autoarray and drop tfp-nightly #694 before the run
  • Plan: on the issue (refactor: vendor bessel_kve into autoarray and drop tfp-nightly #694), written at start; additions beyond it: two extra test cases + ν-gradient docstring warning (from review)
  • Gate: tests 2409 passed / 1 skipped (full), 135 passed (regularizations @ head) · smoke matern_regularization.py PASS (x64) · review CLEAN (independent agent; two non-blocking notes, both addressed in e433232) · Heart YELLOW (published board 2026-11-15T12:29Z; sole blocker "PyAutoArray: open PR 12d old" — human-only row; PyAutoArray main CI green at 102f024)
  • Human: plan sound in hindsight?
  • Human: diff matches plan (no scope creep)?
  • Human: merge, amend, or reject — then log the outcome
Full API Changes (for automation & release notes)

Removed

  • tfp-nightly from autoarray[optional]; kv_xp's JAX-path ImportError for missing tfp.

Added

  • autoarray.inversion.regularization.bessel.bessel_kve(v, z) — exponentially scaled K_v(z)·e^{|z|}, pure JAX, differentiable in z.
  • autoarray.inversion.regularization.bessel.kv(v, z) — unscaled K_v(z).

Migration

  • None for users. Environments that installed tfp-nightly only for autoarray may uninstall it.

Generated by the PyAutoLabs agent workflow.


Generated by Claude Code

The JAX Matern-kernel path (kv_xp) needed tensorflow-probability only for
the modified-Bessel K_v. Port tfp's bessel_kve K-branch (Temme series,
Steed continued fraction, forward recurrence, Olver asymptotics) to plain
jax.numpy/lax in autoarray/inversion/regularization/bessel.py, keeping
the Apache-2.0 header and tfp's custom JVP (gradient in z; none in v).

kv_xp's JAX branch now uses the vendored kv; tfp-nightly is removed from
the optional extras. Adds a library spot check vs scipy.special.kv and a
finite-difference-certified Matern scale gradient.

Refs #694

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016nhQ8MuV8FPuFqiY6gLYLT
… and analytic dK/dz

Addresses independent-review notes on #694: kv_xp and MaternKernel docstrings
now agree that nu must be treated as static (the order derivative of K_v is
zero, as in tfp), the scipy spot check reaches the nu >= 50 Olver regime, and
reverse-mode dK/dz is checked against -(K_{nu-1} + K_{nu+1}) / 2.

Refs #694

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016nhQ8MuV8FPuFqiY6gLYLT
@Jammy2211 Jammy2211 added the pending-release PR queued for the next release build label Nov 15, 2026 — with Claude
@Jammy2211
Jammy2211 merged commit c18f20c into main Nov 15, 2026
4 checks passed
@Jammy2211
Jammy2211 deleted the feature/matern-vendor-bessel-kve branch November 15, 2026 18:58

Copy link
Copy Markdown
Collaborator Author

Workspace PRs for Phase 2:


Generated by Claude Code

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: vendor bessel_kve into autoarray and drop tfp-nightly

2 participants