Zygote.jl icon indicating copy to clipboard operation
Zygote.jl copied to clipboard

Wrong gradient results for multiple `getindex` chained by `foldl`

Open frankwswang opened this issue 10 months ago • 1 comments

MWE (tested on v0.7.4):

julia> using Zygote

julia> foo1 = x->foldl(getindex, (1, 2), init=x)
#1 (generic function with 1 method)

julia> foo1_ref = x->getindex(getindex(x, 1), 2)
#3 (generic function with 1 method)

julia> v = [[1.1, 2.2]]
1-element Vector{Vector{Float64}}:
 [1.1, 2.2]

julia> foo1(v) == foo1_ref(v) == 2.2
true

julia> Zygote.gradient(foo1_ref, v)
(ChainRules.OneElement{Float64, 1, Tuple{Int64}, Tuple{Base.OneTo{Int64}}}[[0.0, 1.0]],)

julia> Zygote.gradient(foo1, v)
(nothing,)

frankwswang avatar Feb 13 '25 04:02 frankwswang

I think this is https://github.com/JuliaDiff/ChainRules.jl/issues/567 .

mcabbott avatar Feb 13 '25 05:02 mcabbott