Warning: First function call produced NaNs and ERROR: ArgumentError: matrix contains Infs or NaNs

Hello everyone,

I’m running inference for my model using Turing.jl. I’m getting this warning and error but i’m not sure from where its coming and how can I get to that point.

Can someone explain what it is about? and how can I troubleshoot.

As it was a long error msg i trimmed it by removing some from middle.

┌ Warning: First function call produced NaNs. Exiting. Double check that none of the initial conditions, parameters, or timespan values are NaN.
└ @ OrdinaryDiffEq ~/.julia/packages/OrdinaryDiffEq/SUoOD/src/initdt.jl:131
ERROR: ArgumentError: matrix contains Infs or NaNs
Stacktrace:
  [1] chkfinite
    @ ~/.local/julia-1.9.0/share/julia/stdlib/v1.9/LinearAlgebra/src/lapack.jl:86 [inlined]
  [2] generic_lufact!(A::Matrix{ForwardDiff.Dual{ForwardDiff.Tag{Turing.TuringTag, Float64}, Float64, 11}}, pivot::RowMaximum; check::Bool)
    @ LinearAlgebra ~/.local/julia-1.9.0/share/julia/stdlib/v1.9/LinearAlgebra/src/lu.jl:135
  [3] generic_lufact!
    @ ~/.local/julia-1.9.0/share/julia/stdlib/v1.9/LinearAlgebra/src/lu.jl:133 [inlined]
  [4] do_factorization
    @ ~/.julia/packages/LinearSolve/3dHoj/src/factorization.jl:97 [inlined]
  [5] macro expansion
    @ ~/.julia/packages/LinearSolve/3dHoj/src/LinearSolve.jl:96 [inlined]
  [6] solve!(cache::LinearSolve.LinearCache{Matrix{ForwardDiff.Dual{ForwardDiff.Tag{Turing.TuringTag, Float64}, Float64, 11}}, Vector{ForwardDiff.Dual{ForwardDiff.Tag{Turing.TuringTag, Float64}, Float64, 11}}, Vector{ForwardDiff.Dual{ForwardDiff.Tag{Turing.TuringTag, Float64}, Float64, 11}}, SciMLBase.NullParameters, LinearSolve.DefaultLinearSolver, LinearSolve.DefaultLinearSolverInit{LU{ForwardDiff.Dual{ForwardDiff.Tag{Turing.TuringTag, Float64}, Float64, 11}, Matrix{ForwardDiff.Dual{ForwardDiff.Tag{Turing.TuringTag, Float64}, Float64, 11}}, Vector{Int64}}, LinearAlgebra.QRCompactWY{ForwardDiff.Dual{ForwardDiff.Tag{Turing.TuringTag, Float64}, Float64, 11}, Matrix{ForwardDiff.Dual{ForwardDiff.Tag{Turing.TuringTag, Float64}, Float64, 11}}, Matrix{ForwardDiff.Dual{ForwardDiff.Tag{Turing.TuringTag, Float64}, Float64, 11}}}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, LU{ForwardDiff.Dual{ForwardDiff.Tag{Turing.TuringTag, Float64}, Float64, 11}, Matrix{ForwardDiff.Dual{ForwardDiff.Tag{Turing.TuringTag, Float64}, Float64, 11}}, Vector{Int64}}, Tuple{LU{ForwardDiff.Dual{ForwardDiff.Tag{Turing.TuringTag, Float64}, Float64, 11}, Matrix{ForwardDiff.Dual{ForwardDiff.Tag{Turing.TuringTag, Float64}, Float64, 11}}, Vector{Int64}}, Vector{Int64}}, Nothing, Nothing, Nothing, SVD{ForwardDiff.Dual{ForwardDiff.Tag{Turing.TuringTag, Float64}, Float64, 11}, ForwardDiff.Dual{ForwardDiff.Tag{Turing.TuringTag, Float64}, Float64, 11}, Matrix{ForwardDiff.Dual{ForwardDiff.Tag{Turing.TuringTag, Float64}, Float64, 11}}, Vector{ForwardDiff.Dual{ForwardDiff.Tag{Turing.TuringTag, Float64}, Float64, 11}}}, Cholesky{ForwardDiff.Dual{ForwardDiff.Tag{Turing.TuringTag, Float64}, Float64, 11}, Matrix{ForwardDiff.Dual{ForwardDiff.Tag{Turing.TuringTag, Float64}, Float64, 11}}}, Cholesky{ForwardDiff.Dual{ForwardDiff.Tag{Turing.TuringTag, Float64}, Float64, 11}, Matrix{ForwardDiff.Dual{ForwardDiff.Tag{Turing.TuringTag, Float64}, Float64, 11}}}}, LinearSolve.InvPreconditioner{Diagonal{ForwardDiff.Dual{ForwardDiff.Tag{Turing.TuringTag, Float64}, Float64, 11}, Vector{ForwardDiff.Dual{ForwardDiff.Tag{Turing.TuringTag, Float64}, Float64, 11}}}}, Diagonal{ForwardDiff.Dual{ForwardDiff.Tag{Turing.TuringTag, Float64}, Float64, 11}, Vector{ForwardDiff.Dual{ForwardDiff.Tag{Turing.TuringTag, Float64}, Float64, 11}}}, Float64, Bool}, alg::GenericLUFactorization{RowMaximum}; kwargs::Base.Pairs{Symbol, ForwardDiff.Dual{ForwardDiff.Tag{Turing.TuringTag, Float64}, Float64, 11}, Tuple{Symbol}, NamedTuple{(:reltol,), Tuple{ForwardDiff.Dual{ForwardDiff.Tag{Turing.TuringTag, Float64}, Float64, 11}}}})
    @ LinearSolve ~/.julia/packages/LinearSolve/3dHoj/src/LinearSolve.jl:92
  [7] solve!
    @ ~/.julia/packages/LinearSolve/3dHoj/src/LinearSolve.jl:92 [inlined]
  [8] macro expansion
    @ ~/.julia/packages/LinearSolve/3dHoj/src/default.jl:313 [inlined]
  [9] solve!(::LinearSolve.LinearCache{Matrix{ForwardDiff.Dual{ForwardDiff.Tag{Turing.TuringTag, Float64}, Float64, 11}}, Vector{ForwardDiff.Dual{ForwardDiff.Tag{Turing.TuringTag, Float64}, Float64, 11}}, Vector{ForwardDiff.Dual{ForwardDiff.Tag{Turing.TuringTag, Float64}, Float64, 11}}, SciMLBase.NullParameters, LinearSolve.DefaultLinearSolver, LinearSolve.DefaultLinearSolverInit{LU{ForwardDiff.Dual{ForwardDiff.Tag{Turing.TuringTag, Float64}, Float64, 11}, Matrix{ForwardDiff.Dual{ForwardDiff.Tag{Turing.TuringTag, Float64}, Float64, 11}}, Vector{Int64}}, LinearAlgebra.QRCompactWY{ForwardDiff.Dual{ForwardDiff.Tag{Turing.TuringTag, Float64}, Float64, 11}, Matrix{ForwardDiff.Dual{ForwardDiff.Tag{Turing.TuringTag, Float64}, Float64, 11}}, Matrix{ForwardDiff.Dual{ForwardDiff.Tag{Turing.TuringTag, Float64}, Float64, 11}}}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, LU{ForwardDiff.Dual{ForwardDiff.Tag{Turing.TuringTag, Float64}, Float64, 11}, Matrix{ForwardDiff.Dual{ForwardDiff.Tag{Turing.TuringTag, Float64}, Float64, 11}}, Vector{Int64}}, Tuple{LU{ForwardDiff.Dual{ForwardDiff.Tag{Turing.TuringTag, Float64}, Float64, 11}, Matrix{ForwardDiff.Dual{ForwardDiff.Tag{Turing.TuringTag, Float64}, Float64, 11}}, Vector{Int64}}, Vector{Int64}}, Nothing, Nothing, Nothing, SVD{ForwardDiff.Dual{ForwardDiff.Tag{Turing.TuringTag, Float64}, Float64, 11}, ForwardDiff.Dual{ForwardDiff.Tag{Turing.TuringTag, Float64}, Float64, 11}, Matrix{ForwardDiff.Dual{ForwardDiff.Tag{Turing.TuringTag, Float64}, Float64, 11}}, Vector{ForwardDiff.Dual{ForwardDiff.Tag{Turing.TuringTag, Float64}, Float64, 11}}}, Cholesky{ForwardDiff.Dual{ForwardDiff.Tag{Turing.TuringTag, Float64}, Float64, 11}, Matrix{ForwardDiff.Dual{ForwardDiff.Tag{Turing.TuringTag, Float64}, Float64, 11}}}, Cholesky{ForwardDiff.Dual{ForwardDiff.Tag{Turing.TuringTag, Float64}, Float64, 11}, Matrix{ForwardDiff.Dual{ForwardDiff.Tag{Turing.TuringTag, Float64}, Float64, 11}}}}, LinearSolve.InvPreconditioner{Diagonal{ForwardDiff.Dual{ForwardDiff.Tag{Turing.TuringTag, Float64}, Float64, 11}, Vector{ForwardDiff.Dual{ForwardDiff.Tag{Turing.TuringTag, Float64}, Float64, 11}}}}, Diagonal{ForwardDiff.Dual{ForwardDiff.Tag{Turing.TuringTag, Float64}, Float64, 11}, Vector{ForwardDiff.Dual{ForwardDiff.Tag{Turing.TuringTag, Float64}, Float64, 11}}}, Float64, Bool}, ::LinearSolve.DefaultLinearSolver; assump::OperatorAssumptions{Nothing}, kwargs::Base.Pairs{Symbol, ForwardDiff.Dual{ForwardDiff.Tag{Turing.TuringTag, Float64}, Float64, 11}, Tuple{Symbol}, NamedTuple{(:reltol,), Tuple{ForwardDiff.Dual{ForwardDiff.Tag{Turing.TuringTag, Float64}, Float64, 11}}}})
    @ LinearSolve ~/.julia/packages/LinearSolve/3dHoj/src/default.jl:306
 [10] solve!
    @ ~/.julia/packages/LinearSolve/3dHoj/src/default.jl:306 [inlined]
 [11] #solve!#6
    @ ~/.julia/packages/LinearSolve/3dHoj/src/common.jl:197 [inlined]
 [12] solve!
    @ ~/.julia/packages/LinearSolve/3dHoj/src/common.jl:196 [inlined]
 [13] #dolinsolve#3
    @ ~/.julia/packages/OrdinaryDiffEq/SUoOD/src/misc_utils.jl:107 [inlined]
 [14] dolinsolve
    @ ~/.julia/packages/OrdinaryDiffEq/SUoOD/src/misc_utils.jl:83 [inlined]
 [59] sample
    @ ~/.julia/packages/Turing/FSBW7/src/inference/Inference.jl:242 [inlined]
 [60] #sample#7
    @ ~/.julia/packages/Turing/FSBW7/src/inference/Inference.jl:238 [inlined]
 [61] sample
    @ ~/.julia/packages/Turing/FSBW7/src/inference/Inference.jl:229 [inlined]
 [62] #sample#6
    @ ~/.julia/packages/Turing/FSBW7/src/inference/Inference.jl:225 [inlined]
 [63] sample(model::DynamicPPL.Model{typeof(fit_CTL), (:data, :prob), (), (), Tuple{Vector{Matrix{Union{Missing, Float64}}}, ODEProblem{Vector{Float64}, Tuple{Float64, Float64}, true, Vector{Float64}, ODEFunction{true, SciMLBase.AutoSpecialize, typeof(Affinity), UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, typeof(SciMLBase.DEFAULT_OBSERVED), Nothing, Nothing}, Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}, SciMLBase.StandardODEProblem}}, Tuple{}, DynamicPPL.DefaultContext}, alg::NUTS{Turing.Essential.ForwardDiffAD{0}, (), AdvancedHMC.DiagEuclideanMetric}, ensemble::MCMCSerial, N::Int64, n_chains::Int64)
    @ Turing.Inference ~/.julia/packages/Turing/FSBW7/src/inference/Inference.jl:217

This is a Julia Base issue that should be fixed in the next patch release (1.9.3?). For now a good way around it is to hardcode linsolve = LUFactorization() in the implicit solver choice.

I did this alg = Rodas5P(linsolve = LUFactorization()) but still getting same error.

alg = Rodas5P(linsolve = RFLUFactorization())?

“For now a good way around it is to hardcode linsolve = LUFactorization() in the implicit solver choice.”

you meant that right to put linsolve = LUFactorization() in solver?

Also, I have used same code for inference with different data and i don’t see any error or warning in that case. With this current data that I’m using i face this issue.

Can you explain what is the basis for this error?

Did you try RFLUFactorization? I forget which one exactly avoided the issue.

Julia’s Base linear algebra for the backsolve portion did not correctly omit the check when you do check=false, so the error handling of the solver is avoided because Julia just avoids an error. In all other places we set check=false so we don’t get an error and handle the issue manually (by rejecting the time step), but somehow this one spot didn’t get the error disabling correct.

But we already put a patch in for Julia, it’ll come in the next release. For now you just need to use one of the methods that doesn’t hit the dispatch that has the error check disabling disabled.

1 Like

RFLUFactorization worked thanks :).