Guard flow_path_mfd() against unbounded memory allocations (#1365)#1369
Merged
brendancol merged 1 commit intomainfrom Apr 29, 2026
Merged
Guard flow_path_mfd() against unbounded memory allocations (#1365)#1369brendancol merged 1 commit intomainfrom
brendancol merged 1 commit intomainfrom
Conversation
The numpy and cupy backends eagerly allocate ~80 B/px of working memory before tracing any paths, dominated by the (8, H, W) float64 copy of the input fractions. Large rasters can exhaust host or device RAM with no error reaching the caller. Add per-module memory guards mirroring #1351 / #1337: pre-flight checks raise MemoryError when projected use exceeds 50% of available host or GPU RAM, with a message pointing to the dask backends. Dask and dask+cupy paths skip the guard.
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 #1365. The numpy and cupy
flow_path_mfdpaths allocate ~80 B/px of host working memory before any path tracing, driven by the(8, H, W)float64 cast of the input fractions plus the float64 start-points and output rasters. Without a pre-flight check, a large raster can drive the process into OOM.This change adds the same memory guard pattern used in #1351 (flow_length_mfd) and #1337 (stream_link_mfd):
_BYTES_PER_PIXEL = 80and_GPU_BYTES_PER_PIXEL = 88constants with a comment breaking down the count._available_memory_bytes()reads/proc/meminfo, falls back topsutil, then a 2 GiB default._available_gpu_memory_bytes()queriescupy.cuda.runtime.memGetInfo, returns 0 when CUDA is unavailable._check_memory()and_check_gpu_memory()raiseMemoryErrorwhen projected use exceeds 50% of available host / GPU RAM, with a message that points to the dask backend.flow_path_mfdahead of the eager allocations. The dask and dask+cupy paths skip the guard.Tests
test_numpy_huge_raster_raisestest_numpy_normal_input_succeedstest_dask_path_skips_guardtest_error_message_mentions_dimensionstest_cupy_huge_raster_raises(skipped without CUDA)Test plan
pytest xrspatial/hydro/tests/test_flow_path_mfd.py(21 passed)