Skip to content

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

Closed

Description

@Jammy2211

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 small jax.numpy module, 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 keeps scipy.special.kv.
  • Drop tfp-nightly from PyAutoArray[optional] and scrub every tfp mention from docstrings/comments/ImportError.
  • Phase 2 (workspace): add a parity + gradient script in workspace_test — vendored kv/kve vs scipy.special.kve to rtol 1e-10 over v ∈ [0.5, 5.5] × a z range, plus jax.grad vs 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

  1. New module autoarray/inversion/regularization/bessel.py (or under autoarray/numpy_wrapper/ if that fits better): port bessel_kve + 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 (K branch only), _sqrt1px2, _compute_general_continued_fraction. Translate tf.* shims to jnp/lax (lax.while_loop for series/continued fractions); jax.custom_jvp for the z-gradient. Lazy jax import so import autoarray stays light. Preserve the Apache-2.0 header.
  2. matern_kernel.py::kv_xp: JAX branch → bessel_kve(v, z) * exp(-|z|) from the vendored module; drop the tfp try/except ImportError; rewrite docstrings (~L14–30, ~L290–295).
  3. Docstring tfp references in matern_adapt_kernel.py:53, matern_adapt_power_kernel.py:46.
  4. pyproject.toml: remove tfp-nightly==0.26.0.dev20260713 and its comment block (L88–96); update the L79 comment.
  5. test_autoarray/inversion/regularizations/test_kernel_jax_gradients.py: Matern no longer needs an extra dep — add a cheap kv_xp JAX-vs-scipy spot check and update the module docstring (full parity grid stays in workspace_test).
  6. 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/: new bessel_kve_parity.py (grid parity rtol 1e-10 + grad-vs-FD); retitle matern_regularization.py docstring from "tfp path" to "vendored kve".
  • autolens_workspace_test: update tfp text in scripts/imaging/jax_grad/regularization.py, knn.py, and .github/scripts/smoke_install.sh comments (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 covariance
  • autoarray/inversion/regularization/matern_adapt_kernel.py, matern_adapt_power_kernel.py — docstrings
  • pyproject.toml — optional extras
  • test_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 vendored kve/kv match scipy.special.kve to rtol 1e-10 on a (v in [0.5, 5.5]) x z grid with jax.grad matching finite differences (workspace_test), the existing Matern-kernel tests pass unchanged, and tensorflow-probability/tfp-nightly appear in neither pyproject.toml nor 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_kve and 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 the custom_gradient/custom_jvp
    plumbing (the JAX path is used with gradients — the port must stay
    differentiable). Translate the tf-on-jax shims (tf.where, tf.math.exp, …)
    to plain jax.numpy. Preserve the Apache-2.0 licence header / attribution.
  • The vendored module is xp-threaded like the rest of autoarray; the NumPy path
    keeps using scipy.special.kv (unaffected).
  • Parity test (the hard requirement): compare the vendored kv/kve against
    scipy.special.kve across a (v, z) grid spanning the prior range
    (v ∈ [0.5, 5.5], a range of z) to a tight tolerance, plus a gradient check
    (jax.grad vs finite-difference). Lives in workspace_test (JAX), not
    test_autoarray/.
  • On completion, remove tfp-nightly from PyAutoArray/pyproject.toml and update
    the kv_xp docstring/ImportError accordingly.

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

  1. Jammy2211 commented on Nov 15, 2026

    @Jammy2211
    CollaboratorAuthor

    Library PR Created

    Phase 1 is up: #695 (feature/matern-vendor-bessel-kve). tfp's bessel_kve is now vendored as pure JAX, kv_xp uses it, and tfp-nightly has been removed.

    • Parity: matches scipy.special.kve to 9e-14 over ν ∈ [0.5, 5.5] and to 2.3e-13 over ν from 0 to 60, with z from 1e-8 to 1e3.
    • Gradients: the gradient in z matches the analytic dK/dz to 2.3e-13, including second derivatives.
    • Gates: full suite 2409 passed / 1 skipped; smoke matern_regularization.py passes with x64 on; independent review CLEAN.
    • Flagged decision: the gradient with respect to ν is still undefined, as it was in tfp. That makes the Matérn ν-gradient wrong rather than zero, so the docstrings now warn that ν must be treated as static.

    Workspace impact: option (ii), docs and validation only, with no API migration. Phase 2 adds bessel_kve_parity.py to autogalaxy_workspace_test and updates the tfp text in autogalaxy_workspace_test / autolens_workspace_test scripts and the smoke_install.sh comments. It runs through /start_workspace on the next launch.

    The run was supervised --auto, so it stops at PR-open and the human merges via /prm.


    Generated by Claude Code

  2. Jammy2211 commented on Nov 15, 2026

    @Jammy2211
    CollaboratorAuthor

    Shipped

    All three PRs are merged via /prm:

    Witness (all three conditions met):

    • The vendored kve/kv match scipy.special.kve to rtol 1e-10 over ν ∈ [0.5, 5.5] × z, with actual max relative error 1.65e-13. jax.grad matches finite differences and the closed form. This is checked in workspace_test, and smoke passed on 3.12 and 3.13.
    • The existing Matérn tests pass unchanged: full autoarray suite 2409 passed / 1 skipped, and CI was green.
    • tensorflow-probability / tfp-nightly appear in neither pyproject.toml nor any import.

    Follow-ups, not filed:

    • The Matérn gradient with respect to ν is wrong, not zero, because there is no Bessel order-derivative (the same as tfp). It only matters if ν is ever sampled with a gradient-based sampler.
    • The same outdated smoke_install.sh comment still exists in five other workspaces.

    Generated by Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions