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

add rule for copy

Open CarloLucibello opened this issue 4 years ago • 5 comments

Fixes https://github.com/FluxML/Zygote.jl/issues/1037

CarloLucibello avatar Jul 21 '21 07:07 CarloLucibello

no idea why Zygote is failing

CarloLucibello avatar Jul 21 '21 09:07 CarloLucibello

Codecov Report

Merging #472 (b4663c7) into master (0d55c54) will increase coverage by 0.00%. The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master     #472   +/-   ##
=======================================
  Coverage   98.12%   98.13%           
=======================================
  Files          22       22           
  Lines        2350     2353    +3     
=======================================
+ Hits         2306     2309    +3     
  Misses         44       44           
Impacted Files Coverage Δ
src/rulesets/Base/base.jl 100.00% <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 0d55c54...b4663c7. Read the comment docs.

codecov-commenter avatar Jul 21 '21 09:07 codecov-commenter

Should this be here or in Zygote? i.e. is this a mathematical property of copy or something more specific?

Within Zygote, copy(::Array) could presumably be a no-op, since you can't mutate them. That's Zygote-specific. But copy(::Buffer) means something else...

Finally, testing this by finite-differences doesn't achieve much. But testing that it does what you want on a Dict would at least ensure that an over-zealous cleanup doesn't (say) restrict this to ::AbstractArray.

Not much of a review, but maybe things to think about.

mcabbott avatar Jul 26 '21 23:07 mcabbott

Should this be here or in Zygote? i.e. is this a mathematical property of copy or something more specific?

I would say it is a convention of the language, the Base's docstring reads

    copy(x)

Create a shallow copy of x: the outer structure is copied, but not all
internal values. For example, copying an array produces a new array with
identically-same elements as the original.

Within Zygote, copy(::Array) could presumably be a no-op, since you can't mutate them. That's Zygote-specific. But copy(::Buffer) means something else...

copy(::Buffer) kind of breaks the convention above since it returns the underlying array. But maybe the rrule defined here is still reasonable since Buffer itself is an AbstactArray therefore it is ok it gets an array-like sensitivity?

Finally, testing this by finite-differences doesn't achieve much. But testing that it does what you want on a Dict would at least ensure that an over-zealous cleanup doesn't (say) restrict this to ::AbstractArray.

agree

Not much of a review, but maybe things to think about.

CarloLucibello avatar Jul 28 '21 11:07 CarloLucibello

I think we should do this. But the Zygote error in "nested AD hitting identity(::Tuple) pullback " is weird.

mcabbott avatar Sep 11 '21 09:09 mcabbott