Skip to content

NaN-safe mode is not NaN-safe enough #774

@devmotion

Description

@devmotion

NaN-safe mode is actually not NaN-safe enough.

With NaN-safe mode enabled:

julia> using ForwardDiff

julia> log(ForwardDiff.Dual{:tag}(0.0, 0.0))
Dual{:tag}(-Inf,0.0)

julia> log(ForwardDiff.Dual{:tag}(0.0, 0.0, 1.0))
Dual{:tag}(-Inf,NaN,Inf)

This can be triggered also using the ForwardDiff API:

julia> ForwardDiff.derivative(log  zero, 1.0)
0.0

julia> f(x) = log(zero(x[1]) + x[2])
f (generic function with 1 method)

julia> ForwardDiff.gradient(f, [1.0, 0.0])
2-element Vector{Float64}:
 NaN
  Inf

The problem is that NaN-safe mode checks whether ALL partials are zero whereas actually it should be applied for each zero partial. Thus one can work around the issue by enforcing that there's only one partial:

julia> ForwardDiff.gradient(f, [1.0, 0.0], ForwardDiff.GradientConfig(f, [1.0, 0.0], ForwardDiff.Chunk{1}()))
2-element Vector{Float64}:
  0.0
 Inf

Edit: Another instance of this issue is #745

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions