Prefer numeric zero over ZeroTangent for numeric arrays #1524
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.
If Zygote produces an
Array{Union{Nothing, <: Number}}
, when passed throughwrap_chainrules_input
, produces anArray{Any}
because thenothing
objects are mapped toZeroTangent
objects. However, ChainRules uses thezero
of the type whenever possible so that the array maintains a concrete element type. This PR provides such a method.Related to JuliaDiff/ChainRules.jl#807
Note: It's possible that this case was only possible due to an upstream issue. Currently, I suspect that when Zygote provides an array with Tangents with NamedTuple components and ZeroTangents, the components of the NamedTuple are extracted and the ZeroTangent is mapped to
nothing
instead of performing thezero(T)
optimization. But I wasn't able to track down the responsible code yet.Also added tests to cover the related methods.
PR Checklist