Repository navigation
fix: KNN neighbour search for a partial final point block - #684
Merged
Merged
Conversation
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
This was referenced Nov 9, 2026
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.
Closes #609. Stacked on #683 (base
feature/intrinsic-reg-pseudo-det). Merge #683 first; GitHub then retargets this PR tomain.Summary
get_interpolation_weightssliced each point block withlax.dynamic_slice, which clamps the last partial block's start toN − B. Points[N−B, N)were loaded but labelledstart + arange(B), so:N mod Btail vertices were never searched;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_fromhas 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,His a correct intrinsic prior whose constant mode was held only by the 1e-8 jitter. #683 normalises that mode with the pseudo-determinant.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):KNearestNeighbor+ConstantSplit(1.0)KNearestNeighbor+AdaptSplit(0.1, 10)KNNBarycentric+ConstantSplit(1.0)Variant B's large change is the removal of the accidental pull-to-zero penalty. Main's
sᵀHswas 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
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.test_knn_block_boundaries_match_brute_force_eager_and_jit: N ∈ {127, 128, 129, 130, 200, 256, 257, 300, 330} × k ∈ {3, 10}. Eager andjax.jitindices, true distances and tail self-matches agree with a brute-force oracle.test_knn_interpolators_partial_final_block_select_brute_force_neighbors: both production interpolators (KNearestNeighbork = 10,KNNBarycentrick = 3) at N ∈ {130, 257, 330} select the brute-force stencils, including the tail vertices.imaging/jax_grad/knn.pypasses 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%).misc/jax_assertions/{knn_interpolator, knn_barycentric}.py,imaging/jax_grad/regularization.py,imaging/mesh_magnification.pyandimaging/mesh_interpolator_source_recovery.pypass.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 whenNis not a multiple ofpoint_block. This affectsInterpolatorKNearestNeighbor/InterpolatorKNNBarycentricmappings,distance_to_self, split-regularization stencils, and hence KNN likelihoods.Migration
Generated by the PyAutoLabs agent workflow.
Generated by Claude Code