Skip to content

fix: KNN neighbour search for a partial final point block - #684

Merged
Jammy2211 merged 1 commit into
mainfrom
feature/knn-partial-point-block
Nov 9, 2026
Merged

Jammy2211 merged 1 commit into
mainfrom
feature/knn-partial-point-block

Conversation

@Jammy2211

Copy link
Copy Markdown
Collaborator

Closes #609. Stacked on #683 (base feature/intrinsic-reg-pseudo-det). Merge #683 first; GitHub then retargets this PR to main.

Summary

get_interpolation_weights sliced each point block with lax.dynamic_slice, which clamps the last partial block's start to N − B. Points [N−B, N) were loaded but labelled start + arange(B), so:

  • the N mod B tail vertices were never searched;
  • the same number of earlier vertices came back under the tail indices.

N is rarely a multiple of 128, so this hit nearly every KNN mesh. The appended zeroed edge ring always sits in the hidden tail, plus whatever interior vertices precede it. In jax_grad/knn.py (N = 330) 74 vertices were hidden and 74 mislabelled. Their split-regularization rows lost the −1 self term (reg_split_from has no slot to insert it in a full-width table), which turned smoothing into a pull-to-zero penalty on 72 pixels.

Fix: pad the point array to whole blocks so every slice starts at bi·B. The padded rows were already masked out.

Why this needs #683 (full mechanism on #609): the bug's accidental zeroth-order rows had been lifting the constant null mode of the KNN split H (λ_min 13 on main). With them gone, H is a correct intrinsic prior whose constant mode was held only by the 1e-8 jitter. #683 normalises that mode with the pseudo-determinant.

⚠️ Likelihood values change for KNN meshes (maintainer-approved)

Every KNN fit whose mesh size isn't a multiple of 128 changes, because the stencils are now correct. In jax_grad/knn.py (on top of #683):

variant before (main) after
A: KNearestNeighbor + ConstantSplit(1.0) −1104.0987 (−1094.89 with #683 alone) −1094.9074
B: KNearestNeighbor + AdaptSplit(0.1, 10) −23422.26 −5896.8500
C: KNNBarycentric + ConstantSplit(1.0) −729.4614 −728.9916

Variant B's large change is the removal of the accidental pull-to-zero penalty. Main's sᵀHs was 23646, against 5891 now, and its chi-squared was 25415 against 8097: the source couldn't be reconstructed on the hidden or mislabelled pixels.

API Changes

None. Internal fix; KNN likelihood values change as above.
See full details below.

Test Plan

  • Promoted the audit's strict xfail test_knn_partial_final_block_matches_independent_neighbor_oracle (N = 130, point_block = 128: exact tail queries return 128 and 129 at zero distance) to an ordinary passing test.
  • New test_knn_block_boundaries_match_brute_force_eager_and_jit: N ∈ {127, 128, 129, 130, 200, 256, 257, 300, 330} × k ∈ {3, 10}. Eager and jax.jit indices, true distances and tail self-matches agree with a brute-force oracle.
  • New test_knn_interpolators_partial_final_block_select_brute_force_neighbors: both production interpolators (KNearestNeighbor k = 10, KNNBarycentric k = 3) at N ∈ {130, 257, 330} select the brute-force stencils, including the tail vertices.
  • Discrimination check: with the fix reverted, 19 of these fail (every partial-block size plus the promoted witness), while N = 127, 128 and 256 still pass.
  • Full suite (with fix: pseudo-determinant for intrinsic regularization log det H #683): 2352 passed, 1 skipped, 3 xfailed.
  • The failure that split this out of feat: exact KNN mesh magnification areas + fix partial-block neighbour search (arc phase 4b) #665 is cleared: autolens_workspace_test imaging/jax_grad/knn.py passes all variants with no tolerance changes. Variant B passes eager/jit (it failed at 3.9e-9), and AD = FD on parameters 7, 11 and 13 (e.g. −2958.358 vs −2958.358; it was off 1–3%).
  • autolens_workspace_test misc/jax_assertions/{knn_interpolator, knn_barycentric}.py, imaging/jax_grad/regularization.py, imaging/mesh_magnification.py and imaging/mesh_interpolator_source_recovery.py pass.
  • Not run here (full searches, hours on CPU): imaging/subhalo_recovery{,_iterative,_evidence}.py, which use KNN meshes and whose evidences will move.
Full API Changes (for automation & release notes)

Changed Behaviour

  • autoarray.inversion.mesh.interpolator.knn.get_interpolation_weights: correct k-nearest-neighbour selection when N is not a multiple of point_block. This affects InterpolatorKNearestNeighbor / InterpolatorKNNBarycentric mappings, distance_to_self, split-regularization stencils, and hence KNN likelihoods.

Migration

  • None in code. Stored KNN-mesh likelihoods/evidences from before this fix are not comparable to new ones.

Generated by the PyAutoLabs agent workflow.


Generated by Claude Code

get_interpolation_weights sliced each point block with lax.dynamic_slice,
which clamps the last partial block's start to N - B: points [N-B, N) were
loaded but labelled start + arange(B), so the N mod B tail vertices were
never searched and earlier vertices came back under the tail indices. For
the usual mesh sizes (N rarely a multiple of 128) this hid every appended
edge-ring vertex plus interior ones, and their split-regularization rows
lost the self term (pull-to-zero penalty instead of smoothing).

Pad the point array to whole blocks so every slice starts at bi * B; the
padded rows are already masked. Brute-force oracle tests at block
boundaries (eager + jit, k=3 and k=10, both KNN interpolators); the audit's
strict xfail witness is promoted to a passing regression.

Stacked on #682 (pseudo-determinant), without which the now-correct KNN
split H exposes the jitter-held constant mode in jax_grad/knn.py.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016nhQ8MFwfRFgwBVbYjP9Xd
@Jammy2211 Jammy2211 added the pending-release PR queued for the next release build label Nov 9, 2026 — with Claude
Base automatically changed from feature/intrinsic-reg-pseudo-det to main November 9, 2026 10:44
@Jammy2211
Jammy2211 merged commit 1482dd9 into main Nov 9, 2026
5 of 6 checks passed
@Jammy2211
Jammy2211 deleted the feature/knn-partial-point-block branch November 9, 2026 11:30
@pyautolabs-bot pyautolabs-bot Bot removed the pending-release PR queued for the next release build label Nov 12, 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.

fix: Preserve KNN neighbors in a partial final point block

2 participants