Guard _idw knearest against unbounded grid x k allocation (#1377)#1380
Merged
brendancol merged 2 commits intomainfrom Apr 30, 2026
Merged
Guard _idw knearest against unbounded grid x k allocation (#1377)#1380brendancol merged 2 commits intomainfrom
brendancol merged 2 commits intomainfrom
Conversation
The k-nearest path in `idw()` calls `cKDTree.query(query_pts, k=k)`, which returns a `(grid_pixels, k)` float64 distance array and an int64 index array. Peak allocation is `grid_pixels * k * 16` bytes before any IDW arithmetic runs. A 50000 x 50000 template with k=12 needs about 480 GB and OOMs the process with no message naming the inputs that caused it. Add `_check_idw_memory(grid_pixels, k)` and call it at the top of the public `idw()` entrypoint when k is set on a numpy-backed template. Dask templates dispatch `_idw_knearest_numpy` per chunk via `map_blocks`, so chunk size already bounds the per-chunk allocation; the guard skips dask paths to avoid refusing legitimate chunked workloads. GPU backends reject k early. Same shape as the kriging guard from #1309.
Contributor
Author
|
@copilot resolve the merge conflicts in this pull request |
…licts Co-authored-by: brendancol <433221+brendancol@users.noreply.github.com>
Contributor
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 #1377.
Summary
_check_idw_memory(grid_pixels, k)inxrspatial/interpolate/_idw.py. Estimatesgrid_pixels * k * 16bytes (the float64 distance array plus the int64 index array returned byscipy.spatial.cKDTree.query) and raisesMemoryErrorif it exceeds0.8 * _available_memory_bytes().idw()entrypoint whenkis set, gated to non-dask templates. The dask+numpy backend dispatches_idw_knearest_numpyper chunk viamap_blocks, so chunk size already bounds the per-chunk allocation. GPU backends rejectkearly.grid_pixelsandk.Why
Without the guard, a 50000 x 50000 template with
k=12allocates about 480 GB inside scipy with no message that identifies the template grid ork. Same shape as the kriging guard from #1309 and the spline guard from #1374.Test plan
pytest xrspatial/tests/test_interpolation.pypasses (37 tests, 3 new)_available_memory_bytes