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

generic frule for constructors? #154

Open
oxinabox opened this issue Jan 18, 2020 · 1 comment
Open

generic frule for constructors? #154

oxinabox opened this issue Jan 18, 2020 · 1 comment

Comments

@oxinabox
Copy link
Member

Like for #153
there is a generic frule for default constructors.
It relies on the arguments being the same as the fields so would have to detect that.

Its basically:

function frule(::Type{P}, args..., _, dargs...) where P
     y  = P(args...)
     dy = Composite{P}(; zip(fieldnames(P, dargs)...)
end

the more serious implementation might be:

function frule(::Type{P}, all_args...) where P
     nargs = fieldcount(P)
     length(all_args) == 2nargs + 1 || return nothing
     args = @inbounds all_args[1:nargs]
     all(typeof.(args) .== fieldtypes(P)) || return nothing
     dargs = @inbounds all_args[end-nargs : end]  # skip dself as constructors never functors
     y  = P(args...)
     dy = Composite{P}(; zip(fieldnames(P, dargs)...)
end

But this may well need to be written as a generated function if it doesn't constant fold good.

@oxinabox
Copy link
Member Author

Related:

  1. We need to have mutable composite types. MutableTangent ChainRulesCore.jl#105
  2. We need a way to work out the differential type for a given primal type, so we can declare the differential arrays etc (this can default for Any) Function to find the Differential type for a given primal type ChainRulesCore.jl#106

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants