Make geodesic slope memory guard backend-aware for dask#2779
Merged
Conversation
The geodesic slope path checked working-buffer memory against the full raster before backend dispatch. That fits eager NumPy/CuPy, which build a (3, H, W) float64 stack up front, but the dask backends run that stack chunk by chunk via map_overlap, so peak memory tracks the largest chunk plus its 1-cell halo. Sizing against the full raster rejected large-but-chunked rasters that would stream through fine. Add _check_geodesic_memory_backend_aware: eager backends still check the full raster; dask backends check the largest spatial chunk (+2 per dim for the depth-1 halo). A single whole-raster chunk is still rejected.
brendancol
commented
Jun 1, 2026
Contributor
Author
brendancol
left a comment
There was a problem hiding this comment.
PR Review: Make geodesic slope memory guard backend-aware for dask
Blockers (must fix before merge)
None.
Suggestions (should fix, not blocking)
aspect.py:427has the same eager-only guard (_check_geodesic_memory(rows, cols)) and so has the same bug. The PR scopes itself to slope per the issue and says so in the body, which is fair. File a follow-up so the aspect path doesn't quietly keep the wrong behavior.
Nits (optional improvements)
geodesic.py: the guard usesmax(row_chunks)andmax(col_chunks)independently, so the two maxima can come from different chunks and the bound can slightly overestimate the largest real chunk. That over-rejects rather than under-rejects, which is the safe direction for a memory guard, so it's fine as-is. Noting it so the conservatism is on record.
What looks good
- The
+2per spatial dim matches the depth-1map_overlaphalo. The chunk function allocates against the haloed chunk shape, so the byte budget tracks the real peak. getattr(data, "chunks", None)splits eager (nochunksattribute) from dask without importing dask in the guard.- Tests pin all three behaviors: eager rejected, chunked allowed, single whole-raster chunk still rejected. The chunked test first checks that the same raster is rejected eagerly, which makes the contrast explicit.
Checklist
- Contract matches the issue: yes
- Both backend classes covered: yes (eager full-raster, dask per-chunk)
- NaN handling: unchanged, n/a
- Edge cases tested: empty chunk tuple guarded with
... else 0 - Dask chunk boundaries: yes,
+2halo accounted for - No premature materialization: guard reads
.chunksmetadata only, no compute - Benchmark: not needed (cheap metadata check)
- README matrix: not needed (no API change)
- Docstrings: present on the new helper
Contributor
Author
|
Review follow-up: the aspect.py suggestion is tracked as a separate issue (#2783) to keep this PR scoped to slope. The |
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.
Closes #2765
What changed
slope(method='geodesic')sized its memory guard against the full raster before dispatch. That's correct for eager NumPy/CuPy (they build a(3, H, W)float64 stack up front), but wrong for dask, which streams that stack chunk by chunk viamap_overlap. The full-raster check rejected large-but-chunked rasters that would run fine._check_geodesic_memory_backend_aware: eager backends keep the full-raster check; dask backends check the largest spatial chunk plus its 1-cellmap_overlaphalo (+2 per spatial dim). A single whole-raster chunk has no memory advantage and is still rejected.Backend coverage
The guard runs before dispatch, so the new path covers all four backends: numpy and cupy use the eager full-raster check; dask+numpy and dask+cupy use the per-chunk check.
aspect(method='geodesic')has the same eager-only guard and is left for a separate change to keep this PR scoped to the reported finding.Test plan
MemoryError(existing test).test_geodesic_slope.pypasses (23 tests).test_geodesic_aspect.pystill passes (sharedgeodesic.pyuntouched in behaviour).