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

Unhelpful error with failure on Base function

Open moble opened this issue 2 months ago • 1 comments

Motivation and description

Base.hypot fails with more than 2 arguments. Presumably this is due to a missing rule in ChainRules, for which I've opened a PR. EDIT: That PR has now merged so I've added a specification of the older version to demonstrate the error.

The point here is that the error message was very unhelpful. Here's a MWE:

using Pkg
Pkg.add(name="ChainRules", version="1.72.5")
Pkg.add("Zygote")

using Zygote

f(x) = hypot(x, 2x, 3x)
f(10), f'(10)

The error is

julia> f(10), f'(10)
ERROR: UndefVarError: `j` not defined in `Zygote`
Suggestion: check for spelling errors or missing imports.
Stacktrace:
  [1] macro expansion
    @ ~/.julia/packages/Zygote/55SqB/src/compiler/interface2.jl:0 [inlined]
  [2] (::Zygote.Pullback{Tuple{Core.IntrinsicFunction, Float64, Float64}, Tuple{Core.IntrinsicFunction}})(Δ::Float64)
    @ Zygote ~/.julia/packages/Zygote/55SqB/src/compiler/interface2.jl:100
  [3] add_fast
    @ ./math.jl:800 [inlined]
  [4] (::Zygote.Pullback{Tuple{…}, Tuple{…}})(Δ::Float64)
    @ Zygote ~/.julia/packages/Zygote/55SqB/src/compiler/interface2.jl:0
  [5] BottomRF
    @ ./reduce.jl:86 [inlined]
  [6] MappingRF
    @ ./reduce.jl:100 [inlined]
  [7] (::Zygote.Pullback{Tuple{…}, Tuple{…}})(Δ::Float64)
    @ Zygote ~/.julia/packages/Zygote/55SqB/src/compiler/interface2.jl:0
  [8] afoldl
    @ ./operators.jl:555 [inlined]
  [9] (::Zygote.Pullback{Tuple{typeof(Base.afoldl), Base.MappingRF{…}, Base._InitialValue, Float64, Float64, Float64}, Any})(Δ::Float64)
    @ Zygote ~/.julia/packages/Zygote/55SqB/src/compiler/interface2.jl:0
 [10] #305
    @ ~/.julia/packages/Zygote/55SqB/src/lib/lib.jl:214 [inlined]
 [11] (::Zygote.var"#2189#back#307"{Zygote.var"#305#306"{Tuple{Tuple{…}, Tuple{…}}, Zygote.Pullback{Tuple{…}, Any}}})(Δ::Float64)
    @ Zygote ~/.julia/packages/ZygoteRules/CkVIK/src/adjoint.jl:72
 [12] _foldl_impl
    @ ./reduce.jl:68 [inlined]
 [13] (::Zygote.Pullback{Tuple{typeof(Base._foldl_impl), Base.MappingRF{…}, Base._InitialValue, Tuple{…}}, Any})(Δ::Float64)
    @ Zygote ~/.julia/packages/Zygote/55SqB/src/compiler/interface2.jl:0
 [14] foldl_impl
    @ ./reduce.jl:48 [inlined]
 [15] (::Zygote.Pullback{Tuple{typeof(Base.foldl_impl), Base.MappingRF{…}, Base._InitialValue, Tuple{…}}, Any})(Δ::Float64)
    @ Zygote ~/.julia/packages/Zygote/55SqB/src/compiler/interface2.jl:0
 [16] mapfoldl_impl
    @ ./reduce.jl:44 [inlined]
 [17] (::Zygote.Pullback{Tuple{…}, Tuple{…}})(Δ::Float64)
    @ Zygote ~/.julia/packages/Zygote/55SqB/src/compiler/interface2.jl:0
 [18] mapfoldl
    @ ./reduce.jl:175 [inlined]
 [19] (::Zygote.Pullback{Tuple{…}, Tuple{…}})(Δ::Float64)
    @ Zygote ~/.julia/packages/Zygote/55SqB/src/compiler/interface2.jl:0
 [20] #mapreduce#341
    @ ./reduce.jl:307 [inlined]
 [21] (::Zygote.Pullback{Tuple{…}, Any})(Δ::Float64)
    @ Zygote ~/.julia/packages/Zygote/55SqB/src/compiler/interface2.jl:0
 [22] mapreduce
    @ ./reduce.jl:307 [inlined]
 [23] (::Zygote.Pullback{Tuple{…}, Tuple{…}})(Δ::Float64)
    @ Zygote ~/.julia/packages/Zygote/55SqB/src/compiler/interface2.jl:0
 [24] _hypot
    @ ./math.jl:801 [inlined]
 [25] (::Zygote.Pullback{Tuple{typeof(Base.Math._hypot), Tuple{Float64, Float64, Float64}}, Any})(Δ::Float64)
    @ Zygote ~/.julia/packages/Zygote/55SqB/src/compiler/interface2.jl:0
 [26] hypot
    @ ./math.jl:707 [inlined]
 [27] (::Zygote.Pullback{Tuple{…}, Tuple{…}})(Δ::Float64)
    @ Zygote ~/.julia/packages/Zygote/55SqB/src/compiler/interface2.jl:0
 [28] f
    @ ./REPL[9]:1 [inlined]
 [29] (::Zygote.Pullback{Tuple{typeof(f), Int64}, Tuple{Zygote.ZBack{…}, Zygote.ZBack{…}, Zygote.Pullback{…}}})(Δ::Float64)
    @ Zygote ~/.julia/packages/Zygote/55SqB/src/compiler/interface2.jl:0
 [30] (::Zygote.var"#88#89"{Zygote.Pullback{Tuple{typeof(f), Int64}, Tuple{Zygote.ZBack{…}, Zygote.ZBack{…}, Zygote.Pullback{…}}}})(Δ::Float64)
    @ Zygote ~/.julia/packages/Zygote/55SqB/src/compiler/interface.jl:97
 [31] (::Zygote.var"#94#95"{typeof(f)})(x::Int64)
    @ Zygote ~/.julia/packages/Zygote/55SqB/src/compiler/interface.jl:161
 [32] top-level scope
    @ REPL[11]:1
Some type information was truncated. Use `show(err)` to see complete types.

I think it would have taken me a long time to figure out the root cause, were it not for the fact that I had already run into (and solved with PRs) the exact same problem in Mooncake and Enzyme.

Possible Implementation

No response

moble avatar Sep 29 '25 18:09 moble

I suspect that j is a bug, also encountered here: https://github.com/FluxML/Zygote.jl/issues/1573

mcabbott avatar Oct 03 '25 21:10 mcabbott