JuliaDiff / JuliaDiff/ChainRules.jl

generic frule for constructors?

Open
#154 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

missing rule
Dominant language
Julia
Stars
475
Forks
98
PR merge metrics
No merged PRs in 30d

Description

Like for https://github.com/JuliaDiff/ChainRules.jl/issues/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.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing ChainRules.jl issue #153 and the existing frule handling for default constructors. The issue names no implementation file or test; determine whether matching constructor arguments to field types and producing the requested primal and tangent values needs a generated function, then establish tests for the supported and rejected signatures.

Written by the indexing model from the issue text.

Assessment

Tech stack
julia
Domain
backend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.