Skip to content

Guard fill_d8() against unbounded memory allocations (#1334)#1336

Merged
brendancol merged 1 commit into
mainfrom
issue-1334
Apr 29, 2026
Merged

Guard fill_d8() against unbounded memory allocations (#1334)#1336
brendancol merged 1 commit into
mainfrom
issue-1334

Conversation

@brendancol
Copy link
Copy Markdown
Contributor

Summary

  • Adds _check_memory / _check_gpu_memory budget checks (32 B/px CPU, 32 B/px GPU, 50% threshold) to the eager numpy and cupy backends in fill_d8.py.
  • Dask backends skip the guard because per-tile allocations are already bounded by chunk size.
  • Adds 5 memory-guard tests (numpy raise, normal-input pass, dask bypass, error message, cupy raise).

Fixes #1334.

Background

The eager numpy path allocated dem_f64 (float64 copy), ring ((h+2, w+2) float64), fill (kernel output, float64), and an extra np.where(...) copy when z_limit is set, ~32 B/pixel of working memory plus the caller's input. _fill_cupy allocated dem_f64 + fill + a cp.where(...) output + an optional z_limit cp.where(...), ~32 B/pixel of GPU memory. Neither backend checked against available memory, so a 50000x50000 numpy DEM asked for ~60 GB of host memory before anything errored out.

Same guard pattern was added in flow_accumulation (#1318/#1319), sieve (#1296), kde (#1287), resample (#1295), focal (#1286), geodesic (#1283), mahalanobis (#1288), true_color (#1291), diffuse (#1267), erode (#1275), emerging_hotspots (#1274), dasymetric (#1261), sky_view_factor (#1299), surface_distance (#1303).

fill_d8 is the depression-filling preprocessing step most hydrology pipelines run first on the full DEM, so it needs the same guard.

Test plan

  • pytest xrspatial/hydro/tests/test_fill_d8.py -- 24 passed
  • Full hydro suite: pytest xrspatial/hydro/tests/ -- 636 passed

Add _check_memory / _check_gpu_memory budget checks (32 B/px CPU,
32 B/px GPU, 50% threshold) to the eager numpy and cupy backends in
fill_d8.py.  Dask backends skip the guard because per-tile
allocations are already bounded by chunk size.

Adds 5 memory-guard tests (numpy raise, normal-input pass, dask
bypass, error message, cupy raise).
@github-actions github-actions Bot added the performance PR touches performance-sensitive code label Apr 29, 2026
@brendancol brendancol merged commit dc1441c into main Apr 29, 2026
11 checks passed
@brendancol brendancol deleted the issue-1334 branch May 4, 2026 13:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

performance PR touches performance-sensitive code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fill_d8(): numpy and cupy backends have no memory guard

1 participant