Skip to content

reducedim_init MethodError with structs #594

@cossio

Description

@cossio
using Zygote
struct A
    x::Float64
end
f(a,v) = a.x + v
g(A,V) = sum(f.(A,V))
gradient(g, A.(randn(2)), randn(2,2)) # Error

Error message and stacktrace:

ERROR: LoadError: MethodError: no method matching reducedim_init(::typeof(identity), ::typeof(Zygote.accum), ::Array{NamedTuple{(:x,),Tuple{Float64}},2}, ::Tuple{Int64,Int64})
Closest candidates are:
  reducedim_init(::Any, ::Union{typeof(+), typeof(Base.add_sum)}, ::AbstractArray, ::Any) at reducedim.jl:109
  reducedim_init(::Any, ::Union{typeof(*), typeof(Base.mul_prod)}, ::AbstractArray, ::Any) at reducedim.jl:112
  reducedim_init(::Any, ::typeof(min), ::AbstractArray, ::Any) at reducedim.jl:132
  ...
Stacktrace:
 [1] _mapreduce_dim(::Function, ::Function, ::NamedTuple{(),Tuple{}}, ::Array{NamedTuple{(:x,),Tuple{Float64}},2}, ::Tuple{Int64,Int64}) at ./reducedim.jl:317
 [2] #mapreduce#580 at ./reducedim.jl:307 [inlined]
 [3] #reduce#582 at ./reducedim.jl:352 [inlined]
 [4] accum_sum(::Array{NamedTuple{(:x,),Tuple{Float64}},2}; dims::Tuple{Int64,Int64}) at /home/cossio/.julia/packages/Zygote/4tJp5/src/lib/broadcast.jl:36
 [5] unbroadcast at /home/cossio/.julia/packages/Zygote/4tJp5/src/lib/broadcast.jl:48 [inlined]
 [6] map(::typeof(Zygote.unbroadcast), ::Tuple{Array{Main.ZygoteStruct.A,1},Array{Float64,2}}, ::Tuple{Array{NamedTuple{(:x,),Tuple{Float64}},2},Array{Float64,2}}) at ./tuple.jl:177
 [7] (::Zygote.var"#1734#1741"{Tuple{Array{Main.ZygoteStruct.A,1},Array{Float64,2}},Val{3},Array{typeof(∂(f)),2}})(::FillArrays.Fill{Float64,2,Tuple{Base.OneTo{Int64},Base.OneTo{Int64}}}) at /home/cossio/.julia/packages/Zygote/4tJp5/src/lib/broadcast.jl:139
 [8] #4367#back at /home/cossio/.julia/packages/ZygoteRules/6nssF/src/adjoint.jl:49 [inlined]
 [9] (::Zygote.var"#175#176"{Zygote.var"#4367#back#1745"{Zygote.var"#1734#1741"{Tuple{Array{Main.ZygoteStruct.A,1},Array{Float64,2}},Val{3},Array{typeof(∂(f)),2}}},Tuple{NTuple{4,Nothing},Tuple{}}})(::FillArrays.Fill{Float64,2,Tuple{Base.OneTo{Int64},Base.OneTo{Int64}}}) at /home/cossio/.julia/packages/Zygote/4tJp5/src/lib/lib.jl:170
 [10] #344#back at /home/cossio/.julia/packages/ZygoteRules/6nssF/src/adjoint.jl:49 [inlined]
 [11] broadcasted at ./broadcast.jl:1238 [inlined]
 [12] g at /home/cossio/Desktop/julia/zygote_structarray (copy).jl:8 [inlined]
 [13] (::typeof(∂(g)))(::Float64) at /home/cossio/.julia/packages/Zygote/4tJp5/src/compiler/interface2.jl:0
 [14] (::Zygote.var"#36#37"{typeof(∂(g))})(::Float64) at /home/cossio/.julia/packages/Zygote/4tJp5/src/compiler/interface.jl:36
 [15] gradient(::Function, ::Array{Main.ZygoteStruct.A,1}, ::Vararg{Any,N} where N) at /home/cossio/.julia/packages/Zygote/4tJp5/src/compiler/interface.jl:45
 [16] top-level scope at /home/cossio/Desktop/julia/zygote_structarray (copy).jl:10
 [17] include(::String) at ./client.jl:439
 [18] top-level scope at REPL[1]:1
in expression starting at /home/cossio/Desktop/julia/zygote_structarray (copy).jl:10

I originally reported the error using StructArray (see JuliaArrays/StructArrays.jl#124). But later realized the same issue occurs with ordinary structs so I simplified the example.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions