Help me pin this bug in `Flux`…

On Julia 1.6.5 and 1.7.2, the following code gives a method error:

using Flux # v0.13.0

x = Flux.onehotbatch([1, 2], 0:9)
# x = float.(x)   ###

model = zeros()
Flux.gradient(Flux.params(model)) do
	y = x
	y = reshape(y, 1, 1, :)    ###
	y = repeat(y, 1)           ###
	sum(y)
end

This seems to arise only when all of these three conditions are satisfied:

  • x isa OneHotMatrix
  • the line y = reshape(y, 1, 1, :) is present
  • the line y = repeat(y, 1) is present

If any of the lines marked with ### are commented out (or in the case of the first, uncommented), then the gradient is computed without an error.

Have I found a bug? I’m a newbie so don’t know whether this should be reported to Flux.jl or Zygote.jl, etc…

Seems like a problem in Zygote to me, I would report it there to start with at least. Here is the stacktrace for anyone interested

julia> Flux.gradient(Flux.params(model)) do
               y = x
               y = reshape(y, 1, 1, :)    ###
               y = repeat(y, 1)           ###
               sum(y)
       end
ERROR: type Nothing has no field method
Stacktrace:
  [1] getproperty
    @ ./Base.jl:42 [inlined]
  [2] matching_cr_sig(t::IRTools.Inner.Meta, s::Nothing)
    @ Zygote ~/.julia/packages/Zygote/DkIUK/src/compiler/chainrules.jl:72
  [3] has_chain_rrule(T::Type)
    @ Zygote ~/.julia/packages/Zygote/DkIUK/src/compiler/chainrules.jl:54
  [4] #s3043#1205
    @ ~/.julia/packages/Zygote/DkIUK/src/compiler/interface2.jl:20 [inlined]
  [5] var"#s3043#1205"(::Any, ctx::Any, f::Any, args::Any)
    @ Zygote ./none:0
  [6] (::Core.GeneratedFunctionStub)(::Any, ::Vararg{Any})
    @ Core ./boot.jl:580
  [7] _pullback
    @ ./REPL[36]:4 [inlined]
  [8] _pullback(::Zygote.Context, ::var"#29#30")
    @ Zygote ~/.julia/packages/Zygote/DkIUK/src/compiler/interface2.jl:0
  [9] pullback(f::Function, ps::Params{Zygote.Buffer{Any, Vector{Any}}})
    @ Zygote ~/.julia/packages/Zygote/DkIUK/src/compiler/interface.jl:352
 [10] gradient(f::Function, args::Params{Zygote.Buffer{Any, Vector{Any}}})
    @ Zygote ~/.julia/packages/Zygote/DkIUK/src/compiler/interface.jl:75
 [11] top-level scope
    @ REPL[36]:1
 [12] macro expansion
    @ ~/.julia/packages/Infiltrator/6XXoh/src/Infiltrator.jl:616 [inlined]
 [13] top-level scope
    @ ~/.julia/packages/CUDA/GGwVa/src/initialization.jl:52

Yes, this is definitely a bug so please file an issue.