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

InexactError when working with `maximum` and indices

Open Uroc327 opened this issue 3 years ago • 1 comments

Zygote seems to forget that the maximum of an array of integers is still discrete.

julia> foo(s) = s / maximum(eachindex([1, 1, 1, 1]))
foo (generic function with 1 method)

julia> bar(s) = s / length([1, 1, 1, 1])
bar (generic function with 1 method)

julia> bar'(1)
0.25

julia> foo'(1)
ERROR: InexactError: Int64(-0.0625)
Stacktrace:
 [1] Int64
   @ ./float.jl:723 [inlined]
 [2] convert
   @ ./number.jl:7 [inlined]
 [3] setindex!
   @ ./array.jl:839 [inlined]
 [4] (::Zygote.var"#643#644"{Base.OneTo{Int64}, Int64})(Δ::Float64)
   @ Zygote ~/.julia/packages/Zygote/3I4nT/src/lib/array.jl:340
 [5] #2754#back
   @ ~/.julia/packages/ZygoteRules/AIbCs/src/adjoint.jl:67 [inlined]
 [6] Pullback
   @ ./REPL[102]:1 [inlined]
 [7] #55
   @ ~/.julia/packages/Zygote/3I4nT/src/compiler/interface.jl:41 [inlined]
 [8] (::Zygote.var"#57#58"{typeof(foo)})(x::Int64)
   @ Zygote ~/.julia/packages/Zygote/3I4nT/src/compiler/interface.jl:83
 [9] top-level scope
   @ REPL[105]:1

Uroc327 avatar Mar 11 '22 17:03 Uroc327

This looks like #1034. Maybe the rule for maximum can just be deleted?

mcabbott avatar Mar 12 '22 02:03 mcabbott