-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
aliasing and sparse broadcast #21693
Comments
Maybe for now it should just throw an error? Seems like this would be difficult to make work unless the sparsity pattern is not changed by the broadcast. |
Agreed, strict in place operation with input-output aliasing doesn't seem possible in general.
My thinking as well. Making copies of the input arguments that alias the output argument would be the alternative / a potential future improvement. Best! |
This should really throw an error instead of silently giving the wrong result. |
Due to a bug in Base Julia: JuliaLang/julia#21693 the in-place operation doesn't work.
Due to a bug in Base Julia: JuliaLang/julia#21693 the in-place operation doesn't work.
It would be nice to do something to prevent this bug from happening in 1.0. It gives a very bad impression of the language's safety. Making a copy of the input when it's |
I think error is the better way to do safety here than copying. One normally uses |
The problem is, the person who wrote the |
Yes, an implementation that does a potentially unnecessary allocation seems way more useful than one that just errors. (But yea, the latter is still better than one that silently gives wrong results.) |
Does this have to block 1.0 given that sparse is now in stdlib? |
Since it's a corruption bug, it can be fixed at any time, even if it was in Base, but it doesn't sound very serious to me to release the 1.0 version of a technical computing language with such a dangerous bug. |
I don't really see what the controversy here is. Why can't we check for aliasing and make a copy when we detect it? Surely making a copy is better than getting complete garbage? If possible, we can improve the performance in the future, but for now giving a correct answer seems like a no-brainer. |
Seems like there is no controversy. Just needs to get done. |
Ok, since this is a bug, it's not a feature freeze blocker, but we really should get it done for 1.0. |
…ignment (#25890) This commit puts together the beginnings of an "interface" for detecting and avoiding aliasing between arrays before performing some mutating operation on one of them. `A′ = unalias(dest, A)` is the main entry point; it checks to see if `dest` and `A` might alias one another, and then either returns `A` or a copy of `A`. When it returns a copy of `A`, it absolutely must preserve the same type. As such, this function calls an internal `unaliascopy` function, which defaults to `copy` but ensures it returns the same type. `mightalias(A, B)` is a quick and conservative check to see if two arrays alias eachother. Instead of requiring every array to know about every other array type, it simply asks both arrays for their `dataids` and then looks for an empty intersection. Finally, `dataids(A)` returns a tuple of `UInt`s that describe something about the region of memory the array occupies. It defaults to simply `(objectid(A),)`. A custom implementation for an array with multiple fields of arrays would look something like `(dataids(A.a)..., dataids(A.b)..., ...)`. Fixes #21693, fixes #15209, and a pre-req to get tests passing for #24368 (since we had spot checks for `Array` aliasing, but moving to broadcast means we need a slightly more generic solution). These functions remain un-exported for now since I'd like us to use them a bit more before we officially support them for all custom arrays.
Much thanks Matt! 🎉 :) |
So does |
Yes, that's correct, with the slight caveat that it will not create a copy in the case where If, however, |
What is the difference now between using broadcast with sparse arrays vs not using it? |
That's an awfully broad question. I'm not sure I fully understand it. But sparse arrays do have specialized implementations of broadcasting that tries very hard to exploit the sparsity, and they do fuse. In-place assignment is only a big win if you already have an output array with the expected output sparsity pattern. |
My question was mostly about fusion. So something like
Right, and this seems hard to predict without knowing the input in advance. |
To avoid allocation, knowing the result's storage size suffices; the pattern isn't strictly necessary. Though I am not certain under what conditions you would know the result's storage size but not its pattern :). |
Sparse
broadcast
does not yet check whether the input arguments alias the output arguments, causing e.g.Best!
The text was updated successfully, but these errors were encountered: