-
-
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
Delete usage of inference in SubArray code #12409
Conversation
You could replace the tuple indexing with |
I was toying with this a bit last night, too. I came to a nearly identical solution, but it doesn't quite taste right to me. It'd be nice to allow inference to do this properly through a function call boundary. That is, @generated function SubArray{T,N}(A::AbstractArray{T}, I::Tuple{Vararg{ViewIndex}}, dims::NTuple{N,Int})
Ip = I.parameters
LD = subarray_linearindexing_dim(A, I)
strideexpr = stride1expr(A, Ip, :A, :I, LD)
exfirst = first_index_expr(:A, :I, length(Ip))
quote
$exfirst
SubArray{$T,$N,$A,$I,$LD}(A, I, dims, f, $strideexpr)
end
end But of course, that doesn't work. There's more inter-woven logic thoughout |
In any case, 👍 on this solution. |
The comments from @simonster and @mbauman combine with thoughts I've had, wondering if we could rewrite the SubArray code in a more Lispy manner and recurse over the supplied indexes. One of the key reasons the old SubArrays had such poor construction performance was because of type-instability: looping over containers (the indexes tuple) where each element might have a different type makes things slow. The new SubArrays get around that using It would be vastly more complicated to pull this off for SubArrays, but it may merit some serious thought. However, that's not something to tackle now; it's a better project for 0.5. |
I would be interested in learning whether anyone else can reproduce the weird |
@simonster, I tested nested- |
Just tried this against current master. The mysterious codegen failure with In the absence of further commentary, I will probably wait until #14529 gets merged (that PR adds new tests), rebase this and run it the |
4c482a0
to
c104084
Compare
Hmm, bad news: this nearly doubles the time needed for the subarray tests, which are already the slowest tests in the entire suite. I don't think we can merge this unless we trim down the tests or speed up codegen. (It's all codegen: while the first run is many minutes, a second run without recompilation is about 1s.) |
Fixed by #15071 |
It was recently noticed that the subarray code called
Base.return_types
from a@generated
function, and that this is a no-no. This PR eliminates that call.There are a few interesting things to discuss here:
merge_indexes
. I suspect it has to do with the constructs likeV.indexes[2:end]
. As a bandaid, I added a type annotation to its output.@inferrable
to the sub and slice tests, seems to work fine in our standard tests. However, if you run withJULIA_TESTFULL
it seems to be an effective way of uncovering some lurking bug:EDIT: I should add that if you try that supposedly-failing test directly, it works fine:
Because it's history-dependent, it suggests a "deep" bug.
Not sure what to do about that. I could test what happens if we drop the first commit and just do the second. It would be nice, of course, to ensure that these operations remain inferrable.
CC @carnaval, @jakebolewski, @JeffBezanson.