ChainRulesCore.jl icon indicating copy to clipboard operation
ChainRulesCore.jl copied to clipboard

Project ZeroTangent to natural tangent for some number types

Open ToucheSir opened this issue 3 years ago • 6 comments

Currently, structured zero tangents are allowed to pass through during projection for (I believe) all number types. This was found while trying to write a rrule that passes https://github.com/FluxML/Zygote.jl/blob/v0.6.43/test/features.jl#L528 (itself found while working on https://github.com/FluxML/Zygote.jl/issues/1284). This PR tries to take a conservative first step towards coercing those zeroes back to natural tangents by focusing on a tangent type which is unambiguously zero (ZeroTangent) and a relatively easy-to-comprehend numeric subspace (the Reals).

ToucheSir avatar Aug 10 '22 00:08 ToucheSir

The purpose of splitting this out into two commits is to show which tests fail and (along with the corresponding behaviour) would need to change. This comment in particular worries me, but the parent PR https://github.com/JuliaDiff/ChainRulesCore.jl/pull/391 is a bit too intimidating to wade through looking for more context.

ToucheSir avatar Aug 10 '22 01:08 ToucheSir

Codecov Report

Merging #574 (54fcbed) into main (fbb4936) will decrease coverage by 0.10%. The diff coverage is 100.00%.

@@            Coverage Diff             @@
##             main     #574      +/-   ##
==========================================
- Coverage   93.15%   93.04%   -0.11%     
==========================================
  Files          15       15              
  Lines         891      892       +1     
==========================================
  Hits          830      830              
- Misses         61       62       +1     
Impacted Files Coverage Δ
src/projection.jl 97.31% <100.00%> (+0.01%) :arrow_up:
src/tangent_types/abstract_zero.jl 92.00% <0.00%> (-4.00%) :arrow_down:

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

codecov-commenter avatar Aug 10 '22 01:08 codecov-commenter

I'm not sure about this. ZeroTangent() is a strong zero, it defeats NaNs. Though that rarely should matter really. But also it is more informative than 0. I am not sure, I want to think about this more.

I think we were doing this in the original draft of ProjectTo and removed it.

@willtebbutt @mcabbott thoughts?

oxinabox avatar Aug 11 '22 16:08 oxinabox

Currently, structured zero tangents are allowed to pass through during projection for (I believe) all number types.

This was certainly the intention. The strong zero should mean no further computation is done.

This was found while trying to write a rrule that passes https://github.com/FluxML/Zygote.jl/blob/v0.6.43/test/features.jl#L528 (itself found while working on https://github.com/FluxML/Zygote.jl/issues/1284).

But why does this issue imply that we should not preserve AbstractZero? I don't see the connection at all, but maybe I miss something. Can you spell it out?

There are some cases where not creating an AbstractZero seems like a good idea (e.g. for type-stability) but not preserving them seems odd to me.

mcabbott avatar Aug 11 '22 16:08 mcabbott

But why does this issue imply that we should not preserve AbstractZero? I don't see the connection at all, but maybe I miss something. Can you spell it out?

There are some cases where not creating an AbstractZero seems like a good idea (e.g. for type-stability) but not preserving them seems odd to me.

The sequence of events was that fixing https://github.com/FluxML/Zygote.jl/issues/1284 required switching keyword argument indexing to use (a modified) path for NamedTuples instead of Dicts. However, this breaks https://github.com/FluxML/Zygote.jl/blob/v0.6.43/test/features.jl#L528 because [gradient].y becomes nothing instead of zero. I thought about converting https://github.com/FluxML/Zygote.jl/blob/4bb6b4dd4a4b6eb0e40126587a7a170216c97448/src/lib/base.jl#L123 to a rrule so as to lean on projection for regaining the natural tangent, but found that it just passes through a ZeroTangent (which then gets converted into nothing by Zygote).

Based on this discussion, it seems like the two options are to change the test and call the existing behaviour a bug, or implement ad-hoc projection logic just for that particular adjoint. I am certainly not qualified to engage in the discussion on zero types brought up in this PR :)

ToucheSir avatar Aug 12 '22 01:08 ToucheSir

However, this breaks https://github.com/FluxML/Zygote.jl/blob/v0.6.43/test/features.jl#L528 because [gradient].y becomes nothing instead of zero.

That test was added fairly recently, in https://github.com/FluxML/Zygote.jl/pull/1059 . I'd probably expect a hard Zero there (i.e. nothing) but 0.0 also seems acceptable. My guess is that the test only has 0.0 because that was the current behaviour.

mcabbott avatar Aug 12 '22 04:08 mcabbott