Repository navigation
DArray: Fix copyto! with an empty index range past the end - #783
Merged
jpsamaroo merged 1 commit intoOct 23, 2026
Merged
Conversation
`darray_copyto!` derives the chunk range it iterates from the element range as `fld1(start):fld1(stop)`. For an empty element range that is not itself empty: `18:17` over a `Blocks(4, 4)` partitioning gives `fld1(18, 4):fld1(17, 4)`, i.e. `5:5`, so the loop indexes row 5 of a 4x4 chunk grid and throws a `BoundsError`. Whether it throws depends on where the two ends land, which is why the neighbouring `17:16` (chunk range `5:4`, empty) copies fine. Return before computing the chunk ranges when the destination range is empty. Such a copy has nothing to do, and the generic `tril`/`triu` fallbacks reach it for any band offset past the corner of the matrix.
Member
|
Thanks so much! |
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.
darray_copyto!builds the chunk range it iterates over from the element range, asfld1(start):fld1(stop). An empty element range does not necessarily give an emptychunk range, and when it does not, the loop indexes outside the chunk grid:
18:17givesfld1(18, 4):fld1(17, 4)=5:5, which is not empty, soB.chunks[5, 1]is read from a 4x4 grid. Whether it throws depends on where the twoends happen to land, so neighbouring cases behave differently:
1:0,5:4,16:1517:165:4, empty18:17,19:18,20:195:5, not emptyBoundsErrorThis is reachable from ordinary code:
LinearAlgebra's generictril/triufilltheir result column by column with
A[max(firstindex(A,1), col-k):lastindex(A,1), col] = @view M[rows, col], and thatrange is empty and starts past the end for any band offset past the corner of the
matrix.
The fix returns before the chunk ranges are computed when the destination range is
empty, since there is nothing to copy.
Testing
Added a testset covering both the previously-throwing and the previously-working
empty ranges, in rows and in columns.
What I ran locally (
--test array/copyto -p 1 -t 1):master: 650 pass — the baseline.src/array/copy.jlreverted tomaster: 655 pass, 3 errored,on
rows=18:17,rows=20:19andcols=18:17, each theBoundsErrorabove, with thefive neighbouring empty ranges still passing. So the new tests fail without the fix
rather than passing for an unrelated reason.
I have not run the full suite: the GPU and streaming files exhaust memory on my machine,
so I am relying on CI for the rest.
The change and tests were written with the help of Claude Code (Opus 5). I reviewed the
diff, reproduced the bug, and ran the tests above myself.