Gradient definitions & supertypes for Zygote, continued - #169
Merged
Merged
Conversation
mcabbott
reviewed
Sep 30, 2021
mcabbott
reviewed
Sep 30, 2021
Comment on lines
+46
to
+52
| # Define a new species of projection operator for this type: | ||
| ChainRulesCore.ProjectTo(x::VectorOfArray) = ChainRulesCore.ProjectTo{VectorOfArray}() | ||
|
|
||
| # Gradient from iteration will be e.g. Vector{Vector}, this makes it another AbstractMatrix | ||
| (::ChainRulesCore.ProjectTo{VectorOfArray})(dx::AbstractVector{<:AbstractArray}) = VectorOfArray(dx) | ||
| # Gradient from broadcasting will be another AbstractArray | ||
| (::ChainRulesCore.ProjectTo{VectorOfArray})(dx::AbstractArray) = dx |
Contributor
There was a problem hiding this comment.
I believe this may not be necessary at all. One thing I thought to test was whether iteration like this worked without it, but it does, it hits the @adjoint getindex rule:
julia> function iter(vofa)
s = 0
for a in vofa
s += prod(a)
end
s
end;
julia> gradient(iter, va)[1]
VectorOfArray{Float64,3}:
2-element Vector{Matrix{Float64}}:
[0.007377548303139424 0.0014293014720444096 0.0004998127128840348; 0.0005414269337139141 0.0007721441834498009 0.0006559506948612249; 0.0042378737935180105 0.0006765914005991947 0.00045986425172967415]
[0.002774992305796606 0.002978041675310144 0.004412709924140469; 0.0056425205408066285 0.005228088118453952 0.0036646150274027; 0.0036825199036535465 0.004902176341789764 0.045170987413739046]
mcabbott
reviewed
Sep 30, 2021
|
|
||
| ZygoteRules.@adjoint function getindex(VA::AbstractVectorOfArray, i::Union{Int,AbstractArray{Int},CartesianIndex,Colon,BitArray,AbstractArray{Bool}}) | ||
| function AbstractVectorOfArray_getindex_adjoint(Δ) | ||
| Δ′ = [ (i == j ? Δ : zero(x)) for (x,j) in zip(VA.u, 1:length(VA))] |
Contributor
There was a problem hiding this comment.
Unrelated to the PR, but this seems like it allocates quite a bit, when iterating a VectorOfArray. I guess that using Fill(0.0, size(Δ)) would often make Δ′ have an abstract type?
Member
Author
There was a problem hiding this comment.
Yes, it would make it an abstract type and sometimes hurt inference. Then we'd have to rely on union optimizations and pray.
Member
Author
There was a problem hiding this comment.
Relying on union optimizations might be the right idea here though, I'll have to check.
Co-authored-by: Michael Abbott <[email protected]>
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.
Continues #168
@mcabbott
I didn't have the write permissions for some reason so continuing it here.