Skip to content

broken iteration over dictionary #1065

@CarloLucibello

Description

@CarloLucibello

Issue related to #725 :

julia> d = Dict(:a => 5, :b => 6);

julia> function f(d)
         s = 0
         for (k,v) in d
           s += v
         end
         s
       end
f (generic function with 1 method)

julia> gradient(f, d)
ERROR: MethodError: no method matching getindex(::Dict{Any, Any})
Closest candidates are:
  getindex(::Dict{K, V}, ::Any) where {K, V} at dict.jl:480
  getindex(::AbstractDict, ::Any) at abstractdict.jl:494
  getindex(::AbstractDict, ::Any, ::Any, ::Any...) at abstractdict.jl:504
Stacktrace:
  [1] (::Zygote.var"#back#222"{:vals, Zygote.Context, Dict{Symbol, Int64}, Vector{Int64}})(Δ::Zygote.OneElement{Int64, 1, Tuple{Int64}, Tuple{Base.OneTo{Int64}}})
    @ Zygote ~/.julia/dev/Zygote/src/lib/lib.jl:233
  [2] (::Zygote.var"#1789#back#223"{Zygote.var"#back#222"{:vals, Zygote.Context, Dict{Symbol, Int64}, Vector{Int64}}})(Δ::Zygote.OneElement{Int64, 1, Tuple{Int64}, Tuple{Base.OneTo{Int64}}})
    @ Zygote ~/.julia/packages/ZygoteRules/OjfTt/src/adjoint.jl:59
  [3] Pullback
    @ ./Base.jl:33 [inlined]
  [4] (::typeof((getproperty)))(Δ::Zygote.OneElement{Int64, 1, Tuple{Int64}, Tuple{Base.OneTo{Int64}}})
    @ Zygote ~/.julia/dev/Zygote/src/compiler/interface2.jl:0
  [5] Pullback
    @ ~/.julia/packages/ZygoteRules/OjfTt/src/ZygoteRules.jl:11 [inlined]
  [6] (::typeof((literal_getproperty)))(Δ::Zygote.OneElement{Int64, 1, Tuple{Int64}, Tuple{Base.OneTo{Int64}}})
    @ Zygote ~/.julia/dev/Zygote/src/compiler/interface2.jl:0
  [7] Pullback
    @ ./dict.jl:684 [inlined]
  [8] Pullback
    @ ./dict.jl:688 [inlined]
  [9] (::typeof((iterate)))(Δ::Tuple{NamedTuple{(:first, :second), Tuple{Nothing, Int64}}, Nothing})
    @ Zygote ~/.julia/dev/Zygote/src/compiler/interface2.jl:0
 [10] Pullback
    @ ./REPL[5]:4 [inlined]
 [11] (::typeof((f)))(Δ::Int64)
    @ Zygote ~/.julia/dev/Zygote/src/compiler/interface2.jl:0
 [12] (::Zygote.var"#50#51"{typeof((f))})(Δ::Int64)
    @ Zygote ~/.julia/dev/Zygote/src/compiler/interface.jl:41
 [13] gradient(f::Function, args::Dict{Symbol, Int64})
    @ Zygote ~/.julia/dev/Zygote/src/compiler/interface.jl:76
 [14] top-level scope
    @ REPL[6]:1

Also iteration using values is broken:

julia> function fval(d)
         s = 0
         for v in values(d)
           s += v
         end
         s
       end
fval (generic function with 1 method)

julia> gradient(fval, d)
ERROR: MethodError: no method matching getindex(::Dict{Any, Any})
Closest candidates are:
  getindex(::Dict{K, V}, ::Any) where {K, V} at dict.jl:480
  getindex(::AbstractDict, ::Any) at abstractdict.jl:494
  getindex(::AbstractDict, ::Any, ::Any, ::Any...) at abstractdict.jl:504
Stacktrace:
  [1] (::Zygote.var"#back#222"{:vals, Zygote.Context, Dict{Symbol, Int64}, Vector{Int64}})(Δ::Zygote.OneElement{Int64, 1, Tuple{Int64}, Tuple{Base.OneTo{Int64}}})
    @ Zygote ~/.julia/dev/Zygote/src/lib/lib.jl:233
  [2] (::Zygote.var"#1789#back#223"{Zygote.var"#back#222"{:vals, Zygote.Context, Dict{Symbol, Int64}, Vector{Int64}}})(Δ::Zygote.OneElement{Int64, 1, Tuple{Int64}, Tuple{Base.OneTo{Int64}}})
    @ Zygote ~/.julia/packages/ZygoteRules/OjfTt/src/adjoint.jl:59
  [3] Pullback
    @ ./Base.jl:33 [inlined]
  [4] (::typeof((getproperty)))(Δ::Zygote.OneElement{Int64, 1, Tuple{Int64}, Tuple{Base.OneTo{Int64}}})
    @ Zygote ~/.julia/dev/Zygote/src/compiler/interface2.jl:0
  [5] Pullback
    @ ~/.julia/packages/ZygoteRules/OjfTt/src/ZygoteRules.jl:11 [inlined]
  [6] (::typeof((literal_getproperty)))(Δ::Zygote.OneElement{Int64, 1, Tuple{Int64}, Tuple{Base.OneTo{Int64}}})
    @ Zygote ~/.julia/dev/Zygote/src/compiler/interface2.jl:0
  [7] Pullback
    @ ./dict.jl:697 [inlined]
  [8] (::typeof((iterate)))(Δ::Tuple{Int64, Nothing})
    @ Zygote ~/.julia/dev/Zygote/src/compiler/interface2.jl:0
  [9] Pullback
    @ ./REPL[13]:4 [inlined]
 [10] (::typeof((fval)))(Δ::Int64)
    @ Zygote ~/.julia/dev/Zygote/src/compiler/interface2.jl:0
 [11] (::Zygote.var"#50#51"{typeof((fval))})(Δ::Int64)
    @ Zygote ~/.julia/dev/Zygote/src/compiler/interface.jl:41
 [12] gradient(f::Function, args::Dict{Symbol, Int64})
    @ Zygote ~/.julia/dev/Zygote/src/compiler/interface.jl:76
 [13] top-level scope
    @ REPL[14]:1

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions