From 04349f4db990b064ef00ec081a41d20dbea6db54 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 9 Nov 2026 09:36:51 +0000 Subject: [PATCH] fix: KNN neighbour search for a partial final point block (#609) 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 Claude-Session: https://claude.ai/code/session_016nhQ8MFwfRFgwBVbYjP9Xd --- autoarray/inversion/mesh/interpolator/knn.py | 12 ++- .../interpolator/test_numerics_audit.py | 78 ++++++++++++++++++- 2 files changed, 84 insertions(+), 6 deletions(-) diff --git a/autoarray/inversion/mesh/interpolator/knn.py b/autoarray/inversion/mesh/interpolator/knn.py index 2d4967ff8..32699b8e3 100644 --- a/autoarray/inversion/mesh/interpolator/knn.py +++ b/autoarray/inversion/mesh/interpolator/knn.py @@ -53,6 +53,13 @@ def get_interpolation_weights( # How many blocks n_blocks = (N + B - 1) // B + # Pad to whole blocks so every block's slice starts at `bi * B`. Slicing the + # unpadded array, `dynamic_slice` clamps the last partial block's start to + # N - B, loading points [N - B, N) while labelling them start + arange(B): + # the tail points were never searched and earlier points came back under + # their indices (PyAutoArray#609). Padded rows are masked out below. + points_padded = jnp.pad(points, ((0, n_blocks * B - N), (0, 0))) + def body_fun(bi, carry): best_vals, best_idx = carry start = bi * B @@ -60,8 +67,9 @@ def body_fun(bi, carry): # How many valid points in this block (<= B) block_n = jnp.minimum(B, N - start) - # Safe because B <= N by construction - p_block = jax.lax.dynamic_slice(points, (start, 0), (B, points.shape[1])) + p_block = jax.lax.dynamic_slice( + points_padded, (start, 0), (B, points.shape[1]) + ) # Mask out padded rows (only matters for last block) mask = jnp.arange(B) < block_n # (B,) diff --git a/test_autoarray/inversion/pixelization/interpolator/test_numerics_audit.py b/test_autoarray/inversion/pixelization/interpolator/test_numerics_audit.py index f26e0815f..a671c944a 100644 --- a/test_autoarray/inversion/pixelization/interpolator/test_numerics_audit.py +++ b/test_autoarray/inversion/pixelization/interpolator/test_numerics_audit.py @@ -352,11 +352,9 @@ def mirrored(*args, **kwargs): assert before[1].tobytes() == after[1][:, [2, 3, 0, 1]].tobytes() -@pytest.mark.xfail( - strict=True, - reason="PyAutoArray#609: partial final KNN block omits/mislabels tail nodes", -) def test_knn_partial_final_block_matches_independent_neighbor_oracle(): + # Regression for PyAutoArray#609 (was a strict xfail witness): at N=130 with the default + # point_block=128 the exact tail vertices 128 and 129 must find themselves at zero distance. if importlib.util.find_spec("jax") is None: pytest.skip("production KNN implementation requires optional jax") from autoarray.inversion.mesh.interpolator.knn import get_interpolation_weights @@ -381,6 +379,78 @@ def test_knn_partial_final_block_matches_independent_neighbor_oracle(): ) +KNN_BLOCK_SIZES = [127, 128, 129, 130, 200, 256, 257, 300, 330] + + +@pytest.mark.parametrize("k_neighbors", [3, 10]) +@pytest.mark.parametrize("n_points", KNN_BLOCK_SIZES) +def test_knn_block_boundaries_match_brute_force_eager_and_jit(n_points, k_neighbors): + """ + PyAutoArray#609: every block-boundary case (fewer points than a block, exact multiples, and partial + final blocks) returns the brute-force k nearest neighbours with their true distances, eagerly and + under jit. k=3 is the selection `KNNBarycentric` makes, k=10 the `KNearestNeighbor` default. + """ + if importlib.util.find_spec("jax") is None: + pytest.skip("production KNN implementation requires optional jax") + import jax + from autoarray.inversion.mesh.interpolator.knn import get_interpolation_weights + + rng = np.random.default_rng(609 + n_points) + nodes = rng.uniform(-1.0, 1.0, (n_points, 2)) + query = np.concatenate([nodes[-5:], rng.uniform(-1.0, 1.0, (40, 2))]) + distance = np.sqrt(((query[:, None] - nodes[None]) ** 2).sum(2)) + oracle_index = np.argsort(distance, axis=1)[:, :k_neighbors] + oracle = np.take_along_axis(distance, oracle_index, axis=1) + + eager = get_interpolation_weights(nodes, query, k_neighbors, 1.5) + jitted = jax.jit( + lambda p, q: get_interpolation_weights(p, q, k_neighbors, 1.5) + )(nodes, query) + + for mappings, _, knn_distance in (eager, jitted): + mappings = np.asarray(mappings) + assert mappings.max() < n_points + np.testing.assert_array_equal( + np.sort(mappings, axis=1), np.sort(oracle_index, axis=1) + ) + np.testing.assert_allclose( + np.take_along_axis(distance, mappings, axis=1), oracle, atol=1e-12 + ) + # the expanded |q|^2 + |p|^2 - 2 q.p form leaves ~1e-16 in a squared + # self-distance, i.e. ~1.5e-8 after the square root + np.testing.assert_allclose(np.asarray(knn_distance), oracle, atol=1e-7) + # the queried tail vertices find themselves at zero distance + np.testing.assert_array_equal( + mappings[:5, 0], np.arange(n_points - 5, n_points) + ) + + +@pytest.mark.parametrize("family, k_neighbors", [("knn", 10), ("knn_barycentric", 3)]) +@pytest.mark.parametrize("n_points", [130, 257, 330]) +def test_knn_interpolators_partial_final_block_select_brute_force_neighbors( + family, k_neighbors, n_points +): + """ + PyAutoArray#609 through both production interpolators: at mesh sizes that leave a partial final + point block, each query's stencil is its brute-force nearest-vertex set, so no tail vertex is hidden + and no earlier vertex comes back under a tail index. + """ + rng = np.random.default_rng(6090 + n_points) + nodes = rng.uniform(-1.0, 1.0, (n_points, 2)) + query = np.concatenate([nodes[-3:] + 1.0e-4, rng.uniform(-1.0, 1.0, (60, 2))]) + + mappings, _ = tables(family, nodes, query, n_points) + distance = np.sqrt(((query[:, None] - nodes[None]) ** 2).sum(2)) + oracle = np.sort(np.argsort(distance, axis=1)[:, :k_neighbors], axis=1) + + np.testing.assert_array_equal( + np.sort(mappings[:, :k_neighbors], axis=1), oracle + ) + # a query just off each tail vertex selects that vertex + for row, vertex in enumerate(range(n_points - 3, n_points)): + assert vertex in mappings[row, :k_neighbors] + + def test_knn_full_block_neighbors_kernel_and_pairing_fault(): nodes = lattice(8) query = np.random.default_rng(81).uniform(-0.7, 0.7, (17, 2))