Diagnosing NaNs from ForwardDiff

I am getting a lot of NaNs from ForwardDiff.gradient on a fairly complicated function. I have an analytical function to compute the gradient that works fine (doesn’t give NaN), and coincides with ForwardDiff’s result when it is not NaN.

I already enabled the NANSAFE_MODE_ENABLED setting, but it is not helping. Is there a way to find the point in my code which makes ForwardDiff return a NaN?

Thanks!

1 Like

Do you have calls do divrem (or similar?). That’s the only place I’ve seen ForwardDiff produce NaNs so far (and only at isolated points where the remainder is zero)

1 Like

For any computed real, you can check for NaN partials, eg

noNaNs(x::Real) = true
noNaNs(x::ForwardDiff.Dual) = !any(isnan, ForwardDiff.partials(x))

then

@assert noNaNs(some_value)

in your code.

1 Like

No, I’m not using divrem. But the function I’m trying to differentiate goes through finding a root of a nonlinear equation.

Ah, this seems like a nice and dirty idea. I will springle noNaNs and see where it fails first! Thanks.