feat: xp-API dense kernels + iterative LM engine (potential correction phase 4)#622
Merged
Merged
Conversation
…phase 4) Refactors the jax_ops kernels of Cao et al. 2025's potential_correction package onto the PyAuto xp API: al.pc.dense_util writes each dense kernel once (xp=np default, jax.numpy for accelerator execution) — evidence variants incl. the fixed-curvature Cholesky fast path, and the Levenberg-Marquardt kernels. Adds al.pc.IterFitDpsiSrcImaging, the iterative joint source+dpsi LM engine (re-ray-traces through the corrected lens via the phase-2 InputPotential each accepted step; optional gauge constraints via a KKT step) and its IterDpsiSrcInvAnalysis. Fixes an upstream NaN bug in split_cross_from (percentile over unbounded-cell sentinels). Phase 4 of #618; stacked on #621. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Jammy2211
changed the base branch from
feature/potential-correction-port
to
main
July 17, 2026 11:18
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
Phase 4 of the potential-correction port (#618): the dense/JAX layer and the iterative solver. Stacked on #621 (base =
feature/potential-correction-port; retargets to main when it merges).Per the ecosystem convention, the JAX layer is not a parallel jnp codebase: Cao's
jax_opskernels are refactored intoal.pc.dense_util— dense linear-algebra kernels written once against thexpAPI (xp=npdefault; passxp=jax.numpyfor accelerator-ready, jit-compatible execution). Verified numerically identical under both backends and jit-able (session check; unit tests stay numpy-only per workspace policy).On top of them,
al.pc.IterFitDpsiSrcImagingports the iterative Levenberg–Marquardt engine from the author's updated 2026 tree: joint optimization of the combined state [s | δψ], re-ray-tracing the image grid through the corrected lens (macro model + anInputPotentialbuilt from the current δψ — the Phase 2 profile) at every accepted step, with optional gauge constraints (⟨δψ,1⟩ = ⟨δψ,x⟩ = ⟨δψ,y⟩ = 0 via a KKT step) and a Laplace-approximation evidence.IterDpsiSrcInvAnalysiswraps it for evidence-based hyper-parameter sampling. Upstream's two duplicated LM loops (numpy + jax) collapse into onexp-parameterized loop.One genuine upstream bug found and fixed during the port:
split_cross_fromtook its clipping percentile over all Voronoi cell areas including the −1 unbounded-cell sentinels, so meshes with mostly-unbounded cells produced a negative clip → NaN gradient-arm lengths → interpolator crash. The port takes the percentile over bounded cells only, with a nearest-neighbour-spacing fallback (regression-tested). Relevant to the Phase 5 bug hunt: this NaN path fired precisely inside the iterative loop's source-gradient evaluation.API Changes
Added only — nothing removed or changed.
See full details below.
Test Plan
dense_utilkernel verified against the sparse/Phase-3 reference formulations (joint + δψ-only + fixed-curvature evidence parity, LM Hessian/gradient/step/cost hand-computed, constrained step satisfies the constraint);IterFitDpsiSrcImagingend-to-end on the real 7×7 fixture (finite optimized state, cost beats the zero state, gauge constraints satisfied to 1e-6, finite Laplace evidence);split_cross_fromNaN regression.test_autolenssuite run before commit (execution contract).xp=npandxp=jax.numpy, jit-compatible.Full API Changes (for automation & release notes)
Added
al.pc.dense_util(new module, all functions takexp=np):as_dense(matrix, xp)— numpy/scipy-sparse/jax → densexparray.dense_block_diag_from(*blocks, xp).inverse_noise_variance_from(noise_slim, xp).source_gradient_matrix_dense_from(source_gradient, xp)/dpsi_gradient_matrix_dense_from(itp_mat, Hx, Hy, xp)/dpsi_mapping_matrix_from(psf, src_grad, dpsi_grad, xp)— dense forms of the Phase 3 sparse operators.log_evidence_joint_dense_from(...),log_evidence_dpsi_dense_from(...)— evidence with terms decomposed;validflag instead of raising (JIT-compatible).log_evidence_from_fixed_curvature(...)— Cholesky fast path when only regularizations change between samples.lm_cost_from,lm_hessian_and_gradient_from,solve_lm_step_from(damped; optional equality-constrained KKT),log_evidence_lm_from(Laplace evidence at an LM solution).al.pc.IterFitDpsiSrcImaging(masked_imaging, lens_start, dpsi_pixelization, src_pixelization, gauge_constraints=False, ..., n_iter=20, tol=1e-6)— the iterative LM engine;solve_joint_optimization(xp=np),log_evidence(s, dpsi, xp=np),show(...).al.pc.IterDpsiSrcInvAnalysis(...)—af.Analysisover the iterative engine (failed inversions → logged −1e30 penalty).Migration
Generated by the PyAutoLabs agent workflow.