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

Generic rrule for constructors

Open oxinabox opened this issue 4 years ago • 0 comments

Like https://github.com/JuliaDiff/ChainRules.jl/issues/154

I am pretty sure there is a generic rrule for constructors. Pretty sure I even wrote it down somewhere.

It is something like

function rrule(::Type{T}, args...) where T
    constructor_pullback(dx::Tangent{T}) = (NoTangent(), canonicalize(dx)...)
    
    # Special case for Zygote, which loses the primal type but also is always in canonical form anyway.
    constructor_pullback(dx::Tangent{Any}) = (NoTangent(), dx...)
    
    return T(args...), constructor_pullback
end

OTOH we only want to hit this for the default constructor (what Zygote basically has as __new__ I think)

Adding this more generally might break like all kinds of random constructors that we should be leaving alone? idk

oxinabox avatar Aug 24 '21 22:08 oxinabox