Guard stream_link_mfd() against unbounded memory allocations (#1337)#1341
Merged
brendancol merged 1 commit intomainfrom Apr 29, 2026
Merged
Guard stream_link_mfd() against unbounded memory allocations (#1337)#1341brendancol merged 1 commit intomainfrom
brendancol merged 1 commit intomainfrom
Conversation
Add a 50% RAM/VRAM check to the numpy and cupy branches of stream_link_mfd before any H*W allocation. MFD's working set is ~97 B/px on the CPU (driven by the (8, H, W) fractions input copy) and ~93 B/px on the GPU; the guard uses 100 B/px as a conservative GPU budget. Mirrors the helper layout from #1335 (stream_link_d8). The dask and dask+cupy paths process per-tile and are not gated.
This was referenced Apr 29, 2026
brendancol
added a commit
that referenced
this pull request
Apr 29, 2026
…#1347) Add memory guards to the eager numpy and cupy paths of stream_link_dinf, mirroring the pattern from #1335 (stream_link_d8) and #1341 (stream_link_mfd). D-inf encodes one continuous downstream angle per cell, so there is no (8, H, W) per-neighbor weight buffer like in MFD -- working set matches the d8 budget at 32 B/px (CPU) and 40 B/px (GPU). Guard raises MemoryError when projected usage exceeds 50% of available host or device memory, with the dimensions and a pointer to the dask path. Dask and dask+cupy paths bypass the guard since they process per-tile within chunk-bounded memory.
brendancol
added a commit
that referenced
this pull request
Apr 29, 2026
…1353) Eager numpy and cupy backends allocated H*W kernel locals plus a (8, H, W) float64 input copy with no upfront budget check. A 50000x50000 grid asks for ~232 GB before anything errors out. Adds _check_memory and _check_gpu_memory helpers wired into the numpy and cupy dispatch in flow_length_mfd(). 50% threshold against MemAvailable / cuda free. Dask paths skip the guard since per-tile allocations are bounded by chunk size. Mirrors PR #1332 (flow_length_d8) and PR #1341 (stream_link_mfd). Five tests: oversize numpy rejection, normal pass-through, dask bypass, error message format, oversize cupy rejection.
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 #1337.
Summary
_BYTES_PER_PIXEL = 97and_GPU_BYTES_PER_PIXEL = 100toxrspatial/hydro/stream_link_mfd.pyplus the four helpers (_available_memory_bytes,_available_gpu_memory_bytes,_check_memory,_check_gpu_memory) used in Guard stream_link_d8() against unbounded memory allocations (#1333) #1335._check_memory/_check_gpu_memoryinto the numpy and cupy branches ofstream_link_mfdbefore any H*W allocation. Dask and dask+cupy are left untouched (per-tile allocation already bounded).Byte budget
CPU peak working set per pixel:
Total ~97 B/px (vs. ~32 B/px for stream_link_d8). The (8, H, W) fractions copy dominates.
GPU peak working set per pixel: fractions_f64 (8, H, W) 64 + stream_mask_i8 1 + in_degree 4 + orig_indeg 4 + state 4 + link_id 8 + fa_cp 8 = ~93 B/px. Use 100 B/px as the budget.
Test plan
pytest xrspatial/hydro/tests/test_stream_link_mfd.py(11 passed)test_basin_d8.py::test_basin_dask_temp_cleanupdeselected)