Skip to content

Generic rrule for constructors #510

@oxinabox

Description

@oxinabox

Like #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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions