# Debugging non-finite Jacobian elements (w/ ForwardDiff)

**URL:** <https://discourse.julialang.org/t/debugging-non-finite-jacobian-elements-w-forwarddiff/127448>\
**Category:** Numerics\
**Tags:** question, ad\
**Created:** [March 28, 2025, 11:05am UTC](https://discourse.julialang.org/t/debugging-non-finite-jacobian-elements-w-forwarddiff/127448 "2025-03-28T11:05:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [March 28, 2025, 11:05am UTC](https://discourse.julialang.org/t/debugging-non-finite-jacobian-elements-w-forwarddiff/127448/1 "2025-03-28T11:05:04Z")

</div>

I have a rather complex calculation that maps a vector of floats to a longer vector of floats.

At some inputs, ForwardDiff gives me a Jacobian where _some_ elements are `NaN`. Inputs are of course finite, and so is the function value.

When checked with FiniteDifferences and that works fine. I would like to understand what is going on (I am suspecting a numerical issue with a rule or something). I have thought of adding

```julia
_finiteD(x::Real) = true
_finiteD(x::ForwardDiff.Dual) = all(isfinite, x.partials)
_finiteD(a::AbstractArray) = all(_finiteD, a)

```

and then peppering the code with

```julia
@assert _finiteD(some_interim_value)

```

but this will be tedious. Is there a shortcut?

---

<div class="post-metadata">

**Author:** ![rveltz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rveltz/32/2707_2.png) [@rveltz](https://discourse.julialang.org/u/rveltz)\
**Post date:** [March 28, 2025, 2:20pm UTC](https://discourse.julialang.org/t/debugging-non-finite-jacobian-elements-w-forwarddiff/127448/2 "2025-03-28T14:20:52Z")

</div>

maybe use DifferentiationInterface to try different backends?

---

<div class="post-metadata">

**Author:** ![KnutAM](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/knutam/32/37720_2.png) [@KnutAM](https://discourse.julialang.org/u/KnutAM)\
**Post date:** [March 29, 2025, 6:35am UTC](https://discourse.julialang.org/t/debugging-non-finite-jacobian-elements-w-forwarddiff/127448/3 "2025-03-29T06:35:44Z")

</div>

Sometimes using NaN safe mode has fixed such issues for me, [Advanced Usage Guide · ForwardDiff](https://juliadiff.org/ForwardDiff.jl/stable/user/advanced/#Fixing-NaN/Inf-Issues).

But otherwise I’ve done as you and resorted to manually print if I detect nan/inf and then figure out why. Sometimes, it was needed to modify the equation with `eps()` (to avoid division by zero, for example, for square roots). Would be nice if there was an automatic detection that did require so much manual insertion of checks!

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [March 31, 2025, 12:03pm UTC](https://discourse.julialang.org/t/debugging-non-finite-jacobian-elements-w-forwarddiff/127448/4 "2025-03-31T12:03:30Z")

</div>

> [@rveltz](#):
>
> maybe use DifferentiationInterface to try different backends?

I tried Enzyme which fails (reported the issue), is there any other backend suitable for forward mode?

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [April 8, 2025, 12:02pm UTC](https://discourse.julialang.org/t/debugging-non-finite-jacobian-elements-w-forwarddiff/127448/5 "2025-04-08T12:02:04Z")

</div>

In case anyone is curious, the MWE for the offending code is

```julia
function f(param)
    decay = exp(param) # make it positive
    α = exp(-decay)
end

```

This “works” (for a given value of “works” 😃) for eg `param = 800.0` but produces `NaN`s with AD. This is called inside a numerical solver where the solution is around `param = 0.7`, but large values are visited while the parameter space is explored.

Since the purpose of the first `exp` is to convert a parameter in \mathbb{R} to a positive number, I replaced it with `logaddexp(zero(param), param)` which does not suffer from numerical issues.

In the end I found it with the `_finiteD` checks as above.
