-
-
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
improve cat
inferrability
#45028
Merged
Merged
improve cat
inferrability
#45028
Conversation
This file contains 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
aviatesk
force-pushed
the
avi/infcat
branch
2 times, most recently
from
April 20, 2022 05:35
bf99cde
to
10f787a
Compare
aviatesk
added a commit
to aviatesk/SparseArrays.jl
that referenced
this pull request
Apr 20, 2022
After JuliaLang/julia#45028, it will be more recommended to use this more type stable version of `cat_t`.
Make `cat` inferrable even if its arguments are not fully constant: ```julia julia> r = rand(Float32, 56, 56, 64, 1); julia> f(r) = cat(r, r, dims=(3,)) f (generic function with 1 method) julia> @inferred f(r); julia> last(@code_typed f(r)) Array{Float32, 4} ``` After descending into its call graph, I found that constant propagation is prohibited at `cat_t(::Type{T}, X...; dims)` due to the method instance heuristic, i.e. its body is considered to be too complex for successful inlining although it's explicitly annotated as `@inline`. But for this case, the constant propagation is greatly helpful both for abstract interpretation and optimization since it can improve the return type inference. Since it is not an easy task to improve the method instance heuristic, which is our primary logic for constant propagation, this commit does a quick fix by helping inference with the `@constprop` annotation. There is another issue that currently there is no good way to properly apply `@constprop`/`@inline` effects to a keyword function (as a note, this is a general issue of macro annotations on a method definition). So this commit also changes some internal helper functions of `cat` so that now they are not keyword ones: the changes are also necessary for the `@inline` annotation on `cat_t` to be effective to trick the method instance heuristic.
aviatesk
added a commit
to aviatesk/SparseArrays.jl
that referenced
this pull request
Apr 20, 2022
After JuliaLang/julia#45028, it will be more recommended to use this more type stable version of `cat_t`.
Closed
aviatesk
added a commit
to aviatesk/SparseArrays.jl
that referenced
this pull request
Apr 26, 2022
After JuliaLang/julia#45028, it will be more recommended to use this more type stable version of `cat_t`.
KristofferC
pushed a commit
that referenced
this pull request
Aug 6, 2022
Make `cat` inferrable even if its arguments are not fully constant: ```julia julia> r = rand(Float32, 56, 56, 64, 1); julia> f(r) = cat(r, r, dims=(3,)) f (generic function with 1 method) julia> @inferred f(r); julia> last(@code_typed f(r)) Array{Float32, 4} ``` After descending into its call graph, I found that constant propagation is prohibited at `cat_t(::Type{T}, X...; dims)` due to the method instance heuristic, i.e. its body is considered to be too complex for successful inlining although it's explicitly annotated as `@inline`. But for this case, the constant propagation is greatly helpful both for abstract interpretation and optimization since it can improve the return type inference. Since it is not an easy task to improve the method instance heuristic, which is our primary logic for constant propagation, this commit does a quick fix by helping inference with the `@constprop` annotation. There is another issue that currently there is no good way to properly apply `@constprop`/`@inline` effects to a keyword function (as a note, this is a general issue of macro annotations on a method definition). So this commit also changes some internal helper functions of `cat` so that now they are not keyword ones: the changes are also necessary for the `@inline` annotation on `cat_t` to be effective to trick the method instance heuristic. (cherry picked from commit 65b9be4)
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.
Make
cat
inferrable even if its arguments are not fully constant:After descending into its call graph, I found that constant propagation
is prohibited at
cat_t(::Type{T}, X...; dims)
due to the method instanceheuristic, i.e. its body is considered to be too complex for successful
inlining although it's explicitly annotated as
@inline
.But for this case, the constant propagation is greatly helpful both for
abstract interpretation and optimization since it can improve the return
type inference.
Since it is not an easy task to improve the method instance heuristic,
which is our primary logic for constant propagation, this commit
does a quick fix by helping inference with the
@constprop
annotation.There is another issue that tcurrently there is no good way to properly apply
@constprop
/@inline
effects to a keyword function (as a note, thisis a general issue of macro annotations on a method definition).
So this commit also changes some internal helper functions for
cat
sothat now they are not keyword ones: these changes are also necessary
for the
@inline
annotation oncat_t
to be effective to trickthe method instance heuristic.