Guard stream_order_mfd() against unbounded memory allocations (#1349)#1352
Merged
brendancol merged 1 commit intomainfrom Apr 29, 2026
Merged
Guard stream_order_mfd() against unbounded memory allocations (#1349)#1352brendancol merged 1 commit intomainfrom
brendancol merged 1 commit intomainfrom
Conversation
The eager numpy and cupy backends allocate several H*W working arrays plus an (8, H, W) float64 fractions copy with no upfront check that the result fits in memory. A 50000x50000 raster needs ~262 GB on the CPU path, so the failure mode is an OOM kill rather than a clean exception. Add the per-module memory guard helpers used across the rest of the hydro series and wire them into the public dispatch before any allocations. Threshold is 50% of available memory; dask paths are unaffected because per-tile allocations are bounded. CPU budget 105 B/px (Strahler worst case), GPU budget 105 B/px.
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.
Fixes #1349.
stream_order_mfdallocated severalH*Wworking arrays in its eager numpy and cupy backends and a(8, H, W)float64 fractions copy on top, with no upfront check that the result will fit in memory. A 50000x50000 raster wants ~262 GB on the CPU path, so the failure mode was an OOM kill rather than a clean Python exception.This adds the per-module memory guard helpers (
_BYTES_PER_PIXEL,_GPU_BYTES_PER_PIXEL,_available_memory_bytes,_available_gpu_memory_bytes,_check_memory,_check_gpu_memory) used across the rest of the hydro series and wires them into the public dispatch before any allocations.Bytes per pixel
CPU peak working set, Strahler worst case:
fracinput copy(8,H,W)stream_maskorderin_degreemax_incnt_maxqueue_rqueue_cTotal ~105 B/px.
GPU peak working set ~101 B/px. Use 105 B/px for both as a conservative budget.
Behaviour
MemoryErrorwith a clear message when projected use exceeds 50% of available memory.Tests
Five new tests in
TestMemoryGuard:Full file: 14 tests pass. Full hydro suite: 643/644 pass; the one failure is the known
test_stream_link_dask_temp_cleanupflake unrelated to this change.Companion issues
Same pattern as #1318 / #1319, #1321 / #1324, #1322, #1328 / #1331, #1333, #1337 / #1341, #1343.