Skip to content
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 type stability of ProjectTo(non-numeric array). #557

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

ToucheSir
Copy link
Contributor

Use broadcasting instead of map for non-numeric array projectors. This improves type stability when elements hit ProjectTo(::Any).

I found this while trying to use https://github.com/JuliaDiff/ChainRules.jl/blob/v1.35.3/src/rulesets/Base/mapreduce.jl#L425 with a vector of callables. It's unfortunate that inference can't figure out the map call, but at least broadcasting works.

This improves type stability when elements hit `ProjectTo(::Any)`.
@codecov-commenter
Copy link

codecov-commenter commented Jun 21, 2022

Codecov Report

Merging #557 (543002d) into main (117910b) will not change coverage.
The diff coverage is 100.00%.

@@           Coverage Diff           @@
##             main     #557   +/-   ##
=======================================
  Coverage   93.02%   93.02%           
=======================================
  Files          15       15           
  Lines         860      860           
=======================================
  Hits          800      800           
  Misses         60       60           
Impacted Files Coverage Δ
src/projection.jl 97.30% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 117910b...543002d. Read the comment docs.

@mcabbott
Copy link
Member

Are there any relevant weird cases where these may differ?

An irrelevant one is this (because adjoint vectors have their own path):

julia> map(sqrt, [1,2,3]')
1×3 adjoint(::Vector{Float64}) with eltype Float64:
 1.0  1.41421  1.73205

julia> sqrt.([1,2,3]')
1×3 Matrix{Float64}:
 1.0  1.41421  1.73205

@ToucheSir
Copy link
Contributor Author

I thought SparseArrays might, but it appears map and broadcast behave similarly for them. One concern I did find though while writing up FluxML/Zygote.jl#1248 (comment) is that this PR appears to add a few seconds of latency to the MWE there. Will have to do a proper investigation to confirm that.

@mcabbott
Copy link
Member

OK, that's interesting. Nothing about ProjectTo was closely looked at for latency, sadly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants