JuliaDiff / JuliaDiff/Diffractor.jl

Error trying to differentiate `eachindex`

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

Nobody has claimed this yet.

Dominant language
Julia
Stars
453
Forks
33
PR merge metrics
No merged PRs in 30d

Description

This came up in a test suite:

julia> import AbstractDifferentiation as AD

julia> import Diffractor: DiffractorForwardBackend

julia> h(x::AbstractVector) = eachindex(x) .* x
h (generic function with 1 method)

julia> AD.jacobian(DiffractorForwardBackend(), h, [1, 2])
ERROR: MethodError: no method matching size(::ChainRulesCore.Tangent{Base.OneTo{Int64}, @NamedTuple{stop::Int64}})

Closest candidates are:
  size(::Base.MethodList)
   @ Base reflection.jl:1042
  size(::BitVector)
   @ Base bitarray.jl:104
  size(::BitVector, ::Integer)
   @ Base bitarray.jl:107
  ...

Stacktrace:
  [1] axes
    @ ./abstractarray.jl:98 [inlined]
  [2] call_composed(fs::Tuple{typeof(axes)}, x::Tuple{ChainRulesCore.Tangent{Base.OneTo{…}, @NamedTuple{…}}}, kw::@Kwargs{})
    @ Base ./operators.jl:1045
  [3] call_composed
    @ ./operators.jl:1044 [inlined]
  [4] (::ComposedFunction{…})(x::ChainRulesCore.Tangent{…}; kw::@Kwargs{})
    @ Base ./operators.jl:1041
  [5] map
    @ ./tuple.jl:292 [inlined]
  [6] findconsistentvalue(f::typeof(axes), cols::Tuple{Base.OneTo{…}, ChainRulesCore.Tangent{…}})
    @ StructArrays ~/.julia/packages/StructArrays/CjQ4L/src/utils.jl:208
  [7] (StructArrays.StructVector{…})(c::Tuple{…})
    @ StructArrays ~/.julia/packages/StructArrays/CjQ4L/src/structarray.jl:18
  [8] (StructArrays.StructArray{Diffractor.TangentBundle{…} where P})(c::Tuple{Base.OneTo{…}, ChainRulesCore.Tangent{…}})
    @ StructArrays ~/.julia/packages/StructArrays/CjQ4L/src/structarray.jl:94
  [9] unbundle(atb::Diffractor.TangentBundle{1, Base.OneTo{Int64}, Diffractor.TaylorTangent{Tuple{…}}})
    @ Diffractor ~/.julia/packages/Diffractor/v1WuP/src/tangent.jl:365
 [10] (::Diffractor.var"#205#206"{1, Diffractor.∂☆{…}, Diffractor.TangentBundle{…}})(i::Int64)
    @ Diffractor ~/.julia/packages/Diffractor/v1WuP/src/stage1/broadcast.jl:23
 [11] ntuple(f::Diffractor.var"#205#206"{1, Diffractor.∂☆{…}, Diffractor.TangentBundle{…}}, n::Int64)
    @ Base ./ntuple.jl:19
 [12] (::Diffractor.∂☆{…})(zc::Diffractor.TangentBundle{…}, bc::Diffractor.TangentBundle{…})
    @ Diffractor ~/.julia/packages/Diffractor/v1WuP/src/stage1/broadcast.jl:16
 [13] materialize
    @ ./broadcast.jl:903 [inlined]
 [14] (::Diffractor.∂☆recurse{…})(::Diffractor.TangentBundle{…}, ::Diffractor.TangentBundle{…})
    @ Diffractor ~/.julia/packages/Diffractor/v1WuP/src/stage1/recurse_fwd.jl:0
 [15] (::Diffractor.∂☆internal{…})(::Diffractor.TangentBundle{…}, ::Vararg{…})
    @ Diffractor ~/.julia/packages/Diffractor/v1WuP/src/stage1/forward.jl:114
 [16] ∂☆
    @ ~/.julia/packages/Diffractor/v1WuP/src/stage1/forward.jl:161 [inlined]
 [17] h
    @ ~/Work/GitHub/Julia/DifferentiationInterface.jl/test/diffractor.jl:13 [inlined]
 [18] ∂☆internal
    @ ~/.julia/packages/Diffractor/v1WuP/src/stage1/forward.jl:114 [inlined]
 [19] ∂☆
    @ ~/.julia/packages/Diffractor/v1WuP/src/stage1/forward.jl:161 [inlined]
 [20] (::Diffractor.var"#pushforward#370"{typeof(h), Tuple{Vector{Int64}}})(vs::Tuple{Vector{Int64}})
    @ Diffractor ~/.julia/packages/Diffractor/v1WuP/src/AbstractDifferentiation.jl:17
 [21] #369
    @ ~/.julia/packages/AbstractDifferentiation/RGmzD/src/AbstractDifferentiation.jl:644 [inlined]
 [22] mapslices(f::Diffractor.var"#369#373"{Diffractor.var"#pushforward#370"{…}}, A::Matrix{Int64}; dims::Int64)
    @ Base ./abstractarray.jl:3191
 [23] jacobian(b::DiffractorForwardBackend, f::Function, args::Vector{Int64})
    @ Diffractor ~/.julia/packages/AbstractDifferentiation/RGmzD/src/AbstractDifferentiation.jl:642
 [24] top-level scope
    @ ~/Work/GitHub/Julia/DifferentiationInterface.jl/test/diffractor.jl:15
Some type information was truncated. Use `show(err)` to see complete types.

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

Reproduce the failure from test/diffractor.jl using the eachindex example and inspect the stack trace through unbundle in tangent.jl and the broadcast paths. Done means the example differentiates without the reported MethodError, with a regression test covering the behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
julia
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.