Guard resample() against unbounded output allocations (#1295)#1297
Merged
brendancol merged 2 commits intomainfrom Apr 28, 2026
Merged
Guard resample() against unbounded output allocations (#1295)#1297brendancol merged 2 commits intomainfrom
brendancol merged 2 commits intomainfrom
Conversation
resample() did not bound the output dimensions derived from user-supplied scale_factor or target_resolution. _output_shape returned (round(in_h * scale_y), round(in_w * scale_x)) and that was handed to the eager numpy and cupy backends, which allocated np.empty / cupy.empty / map_coordinates buffers of that size with no memory check. scale_factor=1e9 on a 4x4 raster requested ~190 EB before this commit. Add _available_memory_bytes() / _available_gpu_memory_bytes() and _check_resample_memory / _check_resample_gpu_memory helpers (12 B/cell budget covering the float64 working buffer, float32 output, and the scipy/cupyx map_coordinates temporary), wired into resample() before backend dispatch. The eager numpy and cupy paths run the guard; dask paths skip it because per-chunk allocations are already bounded by chunk size. Same memory-guard pattern as kde / line_density (#1287), focal (#1284), geodesic (#1283), cost_distance (#1262), and diffuse (#1267). Tests in TestMemoryGuard cover huge scale_factor, huge inverse target_resolution, the aggregate-method ValueError still winning over MemoryError, normal inputs still working, error message contents, and the dask path bypassing the guard.
Contributor
Author
|
@copilot resolve the merge conflicts in this pull request |
Co-authored-by: brendancol <433221+brendancol@users.noreply.github.com>
Contributor
This was referenced Apr 28, 2026
brendancol
added a commit
that referenced
this pull request
Apr 29, 2026
…#1319) Fixes #1318. flow_accumulation() on the numpy and cupy backends had no memory check. _flow_accum_cpu allocated accum (8 B/px) + in_degree (4 B/px) + valid (1 B/px) + queue_r/queue_c (8 B/px each) ~ 29 B/pixel of working memory plus the caller's input array. _flow_accum_cupy did the same shape on the device at ~16 B/pixel. A 50000x50000 numpy raster asked for ~72 GB of host memory before anything errored out. Adds _available_memory_bytes / _available_gpu_memory_bytes helpers and _check_memory / _check_gpu_memory budget checks at 50% of available RAM/VRAM. Wires them into the public flow_accumulation_d8() dispatch before the eager numpy and cupy paths run. Dask paths skip the guard because per-tile allocations are bounded by chunk size. Mirrors the pattern from sieve (#1298), kde (#1289), resample (#1297), sky_view_factor (#1300), surface_distance (#1305).
This was referenced Apr 29, 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.
Summary
Closes #1295.
resample()did not bound the output dimensions derived from user-suppliedscale_factorortarget_resolution. The eager numpy and cupy backends allocated the full(out_h, out_w)working buffer up front, soscale_factor=1e9on a 4x4 raster requested ~190 EB before any guard ran.This adds
_available_memory_bytes()/_available_gpu_memory_bytes()and_check_resample_memory/_check_resample_gpu_memoryhelpers (12 B/cell budget covering the float64 working buffer, float32 output, and the scipy/cupyxmap_coordinatestemporary). The guard runs inresample()after the output shape is computed but before backend dispatch. Eager numpy and cupy paths run the guard; dask paths skip it because per-chunk allocations are already bounded by chunk size.Same pattern as #1287 (kde / line_density), #1284 (focal), #1283 (geodesic), #1262 (cost_distance), #1267 (diffuse).
Test plan
pytest xrspatial/tests/test_resample.pypasses (62 tests, 6 new inTestMemoryGuard)scale_factor=1e9raisesMemoryErrortarget_resolution=1e-9raisesMemoryErrorValueErrorstill wins overMemoryErrorscale_factor=2.0) still workscale_factorandtarget_resolution