JuliaDiff / JuliaDiff/ChainRulesCore.jl

Cannot generate `frule` seed via `one(x)`

Open
#618 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

design enhancement
Dominant language
Julia
Stars
267
Forks
66
PR merge metrics
No merged PRs in 30d

Description

Calling frule_via_ad(cfg, (NoTangent(), one(x)), f, x) to work out the derivative works for numbers but not in general. So this path:

https://github.com/JuliaDiff/ChainRules.jl/blob/5855c10bdbe691fc07822752f5b5865b9cea44d3/src/rulesets/Base/broadcast.jl#L104-L110

fails if broadcasting over an array of Ref, or almost any struct:

julia> using ChainRules, ChainRulesTestUtils

julia> CFG = ChainRulesTestUtils.TestConfig();

julia> ChainRules.split_bc_forwards(CFG, only, [Ref(1.0), Ref(2.0)])
ERROR: MethodError: no method matching one(::Base.RefValue{Float64})
Stacktrace:
  [1] (::ChainRules.var"#1732#1734"{typeof(frule_via_ad), ChainRulesTestUtils.TestConfig, typeof(only)})(a::Base.RefValue{Float64})
    @ ChainRules ~/.julia/dev/ChainRules/src/rulesets/Base/broadcast.jl:109
...
 [10] StructArray
    @ ~/.julia/packages/StructArrays/dNQpc/src/structarray.jl:254 [inlined]
 [11] unzip_broadcast(f::ChainRules.var"#1732#1734"{typeof(frule_via_ad), ChainRulesTestUtils.TestConfig, typeof(only)}, args::Vector{Base.RefValue{Float64}})
    @ ChainRules ~/.julia/dev/ChainRules/src/unzipped.jl:40
 [12] split_bc_inner
    @ ~/.julia/dev/ChainRules/src/rulesets/Base/broadcast.jl:108 [inlined]
 [13] split_bc_forwards(cfg::ChainRulesTestUtils.TestConfig, f::typeof(only), arg::Vector{Base.RefValue{Float64}})

julia> struct A594 x::Float64 end;

julia> ChainRules.split_bc_forwards(CFG, x -> x.x, A594.(1:3))
ERROR: MethodError: no method matching one(::A594)

I don't see a requirement to define one (or perhaps oneunit) here https://juliadiff.org/ChainRulesCore.jl/dev/rule_author/tangents.html#Operations-on-a-tangent-type but perhaps there ought to be such a thing?

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 reproducing the Ref and A594 examples in the issue, then read the linked broadcast.jl lines 104-110 and the ChainRulesCore tangent-type documentation. Determine the intended behavior for generating an frule seed when one(x) is unavailable; done means the behavior is specified and the reported cases are resolved with coverage for the relevant path.

Written by the indexing model from the issue text.

Assessment

Tech stack
julia
Domain
machine-learning
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.