Guard kde and line_density against oversize output grids (#1287)#1289
Merged
Conversation
kde() and line_density() accepted user-controlled width/height with no upper bound. The eager numpy and cupy backends allocated the full (height, width) float64 buffer before any check ran, so width=1_000_000, height=1_000_000 requested ~8 TB. Add a local _available_memory_bytes() helper and _check_grid_memory() that raises MemoryError when rows*cols*8 exceeds 50% of available RAM. The error message names width/height so the caller knows which parameter to shrink. Dask paths build per-tile allocations lazily and are already bounded by chunk size, so they skip the guard.
Contributor
Author
|
@copilot resolve the merge conflicts in this pull request |
Agent-Logs-Url: https://github.com/xarray-contrib/xarray-spatial/sessions/4552bed9-0d7d-431f-a7de-abfbf978a1a0 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
_available_memory_bytes()and_check_grid_memory()helpers toxrspatial/kde.py. Call the guard fromkde()(eager paths) and fromline_density()(numpy-only).MemoryErrorbefore allocating the(rows, cols)float64 buffer when it would exceed 50% of available RAM. The message mentionswidth/heightso the caller knows what to shrink._run_kde_dask_numpy,_run_kde_dask_cupy) build per-tile allocations lazily viada.from_delayed, so chunk size already bounds them and the guard is skipped.Same shape as #1223 (rasterize), #1236 (bilateral), #1256 (morphology), #1241 (convolution), #1240 (contour), #1231 (bump), #1229 (viewshed), #1257 (glcm), #1261 (dasymetric), #1262 (cost_distance).
Closes #1287.
Test plan
pytest xrspatial/tests/test_kde.py(50 passed locally, including 5 new memory-guard tests)kde(..., width=1_000_000, height=1_000_000)raisesMemoryErrorline_density(..., width=1_000_000, height=1_000_000)raisesMemoryError