JuliaDiff / Diffractor.jl

Next-generation AD
MIT License
435 stars 31 forks source link

no method matching setindex!(::ChainRulesCore.NoTangent, ::Float64, ::Int64) #200

Open fredrikekre opened 1 year ago

fredrikekre commented 1 year ago

Simple example of taking the jacobian of the identity function, but the identity function allocates a new vector and assigns by iteration:

function identity_loop(x)
    y = similar(x)
    for i in eachindex(x, y)
        y[i] = x[i]
    end
    return y
end
julia> AD.jacobian(Diffractor.DiffractorForwardBackend(), identity_loop, rand(2))
ERROR: MethodError: no method matching setindex!(::ChainRulesCore.NoTangent, ::Float64, ::Int64)
Stacktrace:
  [1] frule(::Tuple{…}, ::typeof(setindex!), x::Vector{…}, v::Float64, inds::Int64)
    @ ChainRules ~/.julia/packages/ChainRules/9sNmB/src/rulesets/Base/indexing.jl:215
  [2] (::Diffractor.∂☆internal{1})(::ZeroBundle{1, typeof(setindex!)}, ::Vararg{Diffractor.AbstractTangentBundle{1}})
    @ Diffractor ~/.julia/packages/Diffractor/QyO8B/src/stage1/forward.jl:119
  [3] (::Diffractor.∂☆{1})(::ZeroBundle{1, typeof(setindex!)}, ::Vararg{Diffractor.AbstractTangentBundle{1}})
    @ Diffractor ~/.julia/packages/Diffractor/QyO8B/src/stage1/forward.jl:160
  [4] identity_loop
    @ Diffractor ./REPL[11]:4 [inlined]
  [5] (::Diffractor.∂☆recurse{…})(::ZeroBundle{…}, ::Diffractor.TangentBundle{…})
    @ Diffractor ~/.julia/packages/Diffractor/QyO8B/src/stage1/recurse_fwd.jl:0
  [6] (::Diffractor.∂☆internal{1})(::ZeroBundle{1, typeof(identity_loop)}, ::Vararg{Diffractor.AbstractTangentBundle{1}})
    @ Diffractor ~/.julia/packages/Diffractor/QyO8B/src/stage1/forward.jl:121
  [7] (::Diffractor.∂☆{1})(::ZeroBundle{1, typeof(identity_loop)}, ::Vararg{Diffractor.AbstractTangentBundle{1}})
    @ Diffractor ~/.julia/packages/Diffractor/QyO8B/src/stage1/forward.jl:160
  [8] (::Diffractor.var"#pushforward#359")(vs::Any)
    @ Diffractor ~/.julia/packages/Diffractor/QyO8B/src/AbstractDifferentiation.jl:13 [inlined]
  [9] (::Diffractor.var"#358#362")(cols::Any)
    @ Diffractor ~/.julia/packages/AbstractDifferentiation/eEkWP/src/AbstractDifferentiation.jl:526 [inlined]
 [10] mapslices(f::Diffractor.var"#358#362"{Diffractor.var"#pushforward#359"{…}}, A::Matrix{Float64}; dims::Int64)
    @ Base ./abstractarray.jl:3179
 [11] jacobian(b::Diffractor.DiffractorForwardBackend, f::Function, args::Vector{Float64})
    @ Diffractor ~/.julia/packages/AbstractDifferentiation/eEkWP/src/AbstractDifferentiation.jl:524
oxinabox commented 1 year ago

So what is going wrong is we should be creating the tangent for the array as a similar array, but we are instead creating it as a NoTangent. So we need to fix that.

bvdmitri commented 1 year ago

Same error when

AD.hessian(Diffractor.DiffractorForwardBackend(), (x) -> dot(x, ones(2, 2), x), zeros(2))