Skip to content

Failure to handle nothing output from Zygote.jl #662

@MilesCranmer

Description

@MilesCranmer
julia> using Zygote, DifferentiationInterface

julia> safe_log(x) = x > zero(x) ? log(x) : convert(typeof(x), NaN)
safe_log (generic function with 1 method)

julia> derivative(safe_log, AutoZygote(), 0.0)
ERROR: MethodError: no method matching iterate(::Nothing)
The function `iterate` exists, but no method is defined for this combination of argument types.

Closest candidates are:
  iterate(::Combinatorics.Combinations)
   @ Combinatorics ~/.julia/packages/Combinatorics/Udg6X/src/combinations.jl:13
  iterate(::Combinatorics.Combinations, ::Any)
   @ Combinatorics ~/.julia/packages/Combinatorics/Udg6X/src/combinations.jl:13
  iterate(::Tables.DictRowTable)
   @ Tables ~/.julia/packages/Tables/8p03y/src/dicts.jl:122
  ...

Stacktrace:
 [1] dot(x::Float64, y::Nothing)
   @ LinearAlgebra ~/.julia/juliaup/julia-1.11.2+0.aarch64.apple.darwin14/share/julia/stdlib/v1.11/LinearAlgebra/src/generic.jl:855
 [2] 
   @ DifferentiationInterface ~/.julia/packages/DifferentiationInterface/gjT8p/src/first_order/pushforward.jl:161
 [3] #6
   @ ~/.julia/packages/DifferentiationInterface/gjT8p/src/first_order/pushforward.jl:192 [inlined]
 [4] ntuple
   @ ./ntuple.jl:48 [inlined]
 [5] value_and_pushforward
   @ ~/.julia/packages/DifferentiationInterface/gjT8p/src/first_order/pushforward.jl:191 [inlined]
 [6] pushforward
   @ ~/.julia/packages/DifferentiationInterface/gjT8p/src/first_order/pushforward.jl:220 [inlined]
 [7] derivative
   @ ~/.julia/packages/DifferentiationInterface/gjT8p/src/first_order/derivative.jl:124 [inlined]
 [8] derivative(::typeof(safe_log), ::AutoZygote, ::Float64)
   @ DifferentiationInterface ~/.julia/packages/DifferentiationInterface/gjT8p/src/fallbacks/no_prep.jl:49
 [9] top-level scope
   @ REPL[13]:1
Some type information was truncated. Use `show(err)` to see complete types.

Zygote.jl returns a nothing given an invalid input, so I think this should be correctly handled. Returning a NaN seems like an option?

For AutoForwardDiff, this gives us:

julia> derivative(safe_log, AutoForwardDiff(), 0.0)
0.0

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