Skip to content

Product of Inf terms leading to NaNs #745

Description

@Uroc327

ForwardDiff (v1.0.1 as well as v0.10.38) fails to compute the gradient when the inputs are too large for the following function.

julia> using ForwardDiff

julia> foo(a) = a[1] * exp(-a[2])
foo (generic function with 1 method)

julia> ForwardDiff.gradient(foo, [1., -1e3])
2-element Vector{Float64}:
 NaN
 NaN

The correct gradient should be [Inf, -Inf]. This works for values a2 small enough such that exp(a2) is finite.

Activity

  1. changed the title [-]Product Rule and Infinities leading to NaNs[/-] [+]Product of Inf terms leading to NaNs[/+] on Apr 9, 2025
  2. devmotion commented on Oct 1, 2025

    @devmotion
    Member

    The more general issue is #774. As mentioned in #774, until the problem is fixed you can work around this problem by enabling NaN-safe mode AND restricting the chunksize to 1:

    julia> ForwardDiff.gradient(foo, [1., -1e3], ForwardDiff.GradientConfig(foo, [1., -1e3], ForwardDiff.Chunk{1}()))
    2-element Vector{Float64}:
      Inf
     -Inf
  3. linked a pull request that will close this issueFix NaN-safe mode #777on Oct 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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