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

Prefer numeric zero over ZeroTangent for numeric arrays #1524

Merged
merged 1 commit into from
Sep 23, 2024

Conversation

BioTurboNick
Copy link
Contributor

@BioTurboNick BioTurboNick commented Sep 16, 2024

If Zygote produces an Array{Union{Nothing, <: Number}}, when passed through wrap_chainrules_input, produces an Array{Any} because the nothing objects are mapped to ZeroTangent objects. However, ChainRules uses the zero 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 the zero(T) optimization. But I wasn't able to track down the responsible code yet.

Also added tests to cover the related methods.

PR Checklist

  • Tests are added
  • Documentation, if applicable Doesn't seem to be documented in any way

@BioTurboNick
Copy link
Contributor Author

I believe the failing test is unrelated.

@ToucheSir
Copy link
Member

Do you mind force pushing (good change to rebase and squash too)? Just so we can double-check the GPU tests are unaffected since they failed to run properly last time.

Copy link
Member

@ToucheSir ToucheSir left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@ToucheSir ToucheSir merged commit 512184d into FluxML:master Sep 23, 2024
12 of 13 checks passed
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.

2 participants