Internal error: stack overflow in type inference

Hi,

I am trying to train a model for which in @pre I solve a Ax=b for a matrix A that is 32x32. As per the title, when I run fit it throws

Internal error: stack overflow in type inference of (::Pumas.var"#733#734"{Float64, Base.UnitRange{Int64}, Pumas.var"#731#732"{Pumas.ThreadedBayesLogDensity{Pumas.PumasModel{(tv_kf_AT=1, tv_Kd_AT=1, tv_kf_BT=1, tv_Kd_BT=1, tv_kf_CT=1, tv_Kd_CT=1, tv_kf_DT=1, tv_Kd_DT=1, tv_kf_AR=1, tv_Kd_AR=1, tv_kf_BR=1, tv_Kd_BR=1, tv_kf_CR=1, tv_Kd_CR=1, tv_kf_DR=1, tv_Kd_DR=1, tv_k0_TR=1, tv_Kd_0_TR=1, tv_k1_TR=1, tv_Kd_1_TR=1, tv_k2_TR=1, tv_Kd_2_TR=1, tv_k3_TR=1, tv_Kd_3_TR=1, tv_k4_TR=1, tv_Kd_4_TR=1, tv_mα=1, tv_α₀=1, σ=1), 1, (:DMn_s, :CaDMn_s, :DMn_f, :CaDMn_f, :PP, :CaPP, :OGB5, :CaOGB5, :T0, :T_A, :T_B, :T_C, :T_D, :T_AB, :T_AC, :T_AD, :T_BC, :T_BD, :T_CD, :T_ABC, :T_ABD, :T_ACD, :T_BCD, :T_ABCD, :R0, :R_A, :R_B, :R_C, :R_D, :R_AB, :R_AC, :R_AD, :R_BC, :R_BD, :R_CD, :R_ABC, :R_ABD, :R_ACD, :R_BCD, :R_ABCD, :Ca), Pumas.ParamSet{NamedTuple{(:tv_kf_AT, :tv_Kd_AT, :tv_kf_BT, :tv_Kd_BT, :tv_kf_CT, :tv_Kd_CT, :tv_kf_DT, :tv_Kd_DT, :tv_kf_AR, :tv_Kd_AR, :tv_kf_BR, :tv_Kd_BR, :tv_kf_CR, :tv_Kd_CR, :tv_kf_DR, :tv_Kd_DR, :tv_k0_TR, :tv_Kd_0_TR, :tv_k1_TR, :tv_Kd_1_TR, :tv_k2_TR, :tv_Kd_2_TR, :tv_k3_TR, :tv_Kd_3_TR, :tv_k4_TR, :tv_Kd_4_TR, :tv_mα, :tv_α₀, :σ), Tuple{Pumas.RealDomain{Int64, Int64, Float64}, Pumas.RealDomain{Int64, Int64, Float64}, Pumas.RealDomain{Int64, Int64, Float64}, Pumas.RealDomain{Int64, Int64, Float64}, Pumas.RealDomain{Int64, Int64, Float64}, Pumas.RealDomain{Int64, Int64, Float64}, Pumas.RealDomain{Int64, Int64, Float64}, Pumas.RealDomain{Int64, Int64, Float64}, Pumas.RealDomain{Int64, Int64, Float64}, Pumas.RealDomain{Int64, Int64, Float64}, Pumas.RealDomain{Int64, Int64, Float64}, Pumas.RealDomain{Int64, Int64, Float64}, Pumas.RealDomain{Int64, Int64, Float64}, Pumas.RealDomain{Int64, Int64, Float64}, Pumas.RealDomain{Int64, Int64, Float64}, Pumas.RealDomain{Int64, Int64, Float64}, Pumas.RealDomain{Int64, Int64, Float64}, Pumas.RealDomain{Int64, Int64, Float64}, Pumas.RealDomain{Int64, Int64, Float64}, Pumas.RealDomain{Int64, Int64, Float64}, Pumas.RealDomain{Int64, Int64, Float64}, Pumas.RealDomain{Int64, Int64, Float64}, Pumas.RealDomain{Int64, Int64, Float64}, Pumas.RealDomain{Int64, Int64, Float64}, Pumas.RealDomain{Int64, Int64, Float64}, Pumas.RealDomain{Int64, Int64, Float64}, Pumas.RealDomain{Float64, Int64, Float64}, Pumas.RealDomain{TransformVariables.Infinity{false}, TransformVariables.Infinity{true}, Float64}, Pumas.RealDomain{Float64, TransformVariables.Infinity{true}, Int64}}}}, Main.var"#655#664", Pumas.TimeDispatcher{Main.var"#656#665", Main.var"#657#666"}, Nothing, Main.var"#659#668", SciMLBase.ODEProblem{Nothing, Tuple{Nothing, Nothing}, true, SciMLBase.NullParameters, SciMLBase.ODEFunction{true, SciMLBase.FullSpecialize, ModelingToolkit.ODEFunctionClosure{Main.var"#660#669", Main.var"#661#670"}, LinearAlgebra.UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Array{Symbol, 1}, Symbol, Array{Symbol, 1}, typeof(SciMLBase.DEFAULT_OBSERVED), Nothing, Nothing}, Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}, SciMLBase.StandardODEProblem}, Main.var"#662#671", Main.var"#663#672", ModelingToolkit.ODESystem}, NamedTuple{(:tv_kf_AT, :tv_Kd_AT, :tv_kf_BT, :tv_Kd_BT, :tv_kf_CT, :tv_Kd_CT, :tv_kf_DT, :tv_Kd_DT, :tv_kf_AR, :tv_Kd_AR, :tv_kf_BR, :tv_Kd_BR, :tv_kf_CR, :tv_Kd_CR, :tv_kf_DR, :tv_Kd_DR, :tv_k0_TR, :tv_Kd_0_TR, :tv_k1_TR, :tv_Kd_1_TR, :tv_k2_TR, :tv_Kd_2_TR, :tv_k3_TR, :tv_Kd_3_TR, :tv_k4_TR, :tv_Kd_4_TR, :tv_mα, :tv_α₀, :σ), Tuple{Float64, Float64, Float64, Float64, Float64, Float64, Float64, Float64, Float64, Float64, Float64, Float64, Float64, Float64, Float64, Float64, Float64, Float64, Float64, Float64, Float64, Float64, Float64, Float64, Float64, Float64, Float64, Float64, Int64}}, Array{Pumas.Subject{NamedTuple{(:F_F0,), Tuple{Array{Float64, 1}}}, Pumas.ConstantCovar{NamedTuple{(:cov_f_frac, :cov_τ_f, :cov_τ_s, :cov_kon_DMn, :cov_koff_DMn, :cov_Kd_DMn, :cov_koff_PP, :cov_DMn_t, :cov_CaM_t, :cov_OGB5_t, :cov_Ca_free, :PCD), Tuple{Float64, Float64, Float64, Float64, Float64, Float64, Float64, Float64, Float64, Float64, Float64, Int64}}}, Nothing, Array{Float64, 1}}, 1}, Array{Array{Float64, 1}, 1}, Array{Array{Float64, 1}, 1}, Array{ForwardDiff.GradientConfig{ForwardDiff.Tag{Pumas.Tag, Float64}, Float64, 8, Array{ForwardDiff.Dual{ForwardDiff.Tag{Pumas.Tag, Float64}, Float64, 8}, 1}}, 1}, Array{DiffResults.MutableDiffResult{1, Float64, Tuple{Array{Float64, 1}}}, 1}, NamedTuple{(:alg,), Tuple{OrdinaryDiffEq.Rodas5P{0, true, Nothing, typeof(OrdinaryDiffEq.DEFAULT_PRECS), Base.Val{:forward}, true, nothing}}}}, Array{Float64, 1}, TransformVariables.TransformTuple{NamedTuple{(:tv_kf_AT, :tv_Kd_AT, :tv_kf_BT, :tv_Kd_BT, :tv_kf_CT, :tv_Kd_CT, :tv_kf_DT, :tv_Kd_DT, :tv_kf_AR, :tv_Kd_AR, :tv_kf_BR, :tv_Kd_BR, :tv_kf_CR, :tv_Kd_CR, :tv_kf_DR, :tv_Kd_DR, :tv_k0_TR, :tv_Kd_0_TR, :tv_k1_TR, :tv_Kd_1_TR, :tv_k2_TR, :tv_Kd_2_TR, :tv_k3_TR, :tv_Kd_3_TR, :tv_k4_TR, :tv_Kd_4_TR, :tv_mα, :tv_α₀, :σ), Tuple{TransformVariables.ScaledShiftedLogistic{Int64}, TransformVariables.ScaledShiftedLogistic{Int64}, TransformVariables.ScaledShiftedLogistic{Int64}, TransformVariables.ScaledShiftedLogistic{Int64}, TransformVariables.ScaledShiftedLogistic{Int64}, TransformVariables.ScaledShiftedLogistic{Int64}, TransformVariables.ScaledShiftedLogistic{Int64}, TransformVariables.ScaledShiftedLogistic{Int64}, TransformVariables.ScaledShiftedLogistic{Int64}, TransformVariables.ScaledShiftedLogistic{Int64}, TransformVariables.ScaledShiftedLogistic{Int64}, TransformVariables.ScaledShiftedLogistic{Int64}, TransformVariables.ScaledShiftedLogistic{Int64}, TransformVariables.ScaledShiftedLogistic{Int64}, TransformVariables.ScaledShiftedLogistic{Int64}, TransformVariables.ScaledShiftedLogistic{Int64}, TransformVariables.ScaledShiftedLogistic{Int64}, TransformVariables.ScaledShiftedLogistic{Int64}, TransformVariables.ScaledShiftedLogistic{Int64}, TransformVariables.ScaledShiftedLogistic{Int64}, TransformVariables.ScaledShiftedLogistic{Int64}, TransformVariables.ScaledShiftedLogistic{Int64}, TransformVariables.ScaledShiftedLogistic{Int64}, TransformVariables.ScaledShiftedLogistic{Int64}, TransformVariables.ScaledShiftedLogistic{Int64}, TransformVariables.ScaledShiftedLogistic{Int64}, TransformVariables.ScaledShiftedLogistic{Float64}, TransformVariables.Identity, TransformVariables.ShiftedExp{true, Float64}}}}, Array{Float64, 1}}, typeof(Base.:(+)), Tuple{Base.UnitRange{Int64}}, Tuple{Array{Float64, 1}, Array{Float64, 1}, 
DiffResults.MutableDiffResult{1, Float64, Tuple{Array{Float64, 1}}}, ForwardDiff.GradientConfig{ForwardDiff.Tag{Pumas.Tag, Float64}, Float64, 8, Array{ForwardDiff.Dual{ForwardDiff.Tag{Pumas.Tag, Float64}, Float64, 8}, 1}}}})().
This might be caused by recursion over very long tuples or argument lists.

and it is quite clear why (it starts throwing this somewhere around A being 16x32). The model trains, but I’m guessing that due to failed type inference it’d be quite slower than if it didn’t fail. Is there anything I can do about it?

Hi Domas,

I don’t really know what this is but there are two things that I would have tried first if I hit this error.

  1. set
@options begin
  inplace = true
end

(The @option docstring here)

The idea being that your use case might make static arrays unfeasible without triggering a switch to mutating arrays instead.

  1. Do you need it to be in the pre block, or can you try to do it in derived?

With tenuous hopes that this might help,
Niklas

1 Like

Hi Niklas,

Many thanks for your suggestion. Since the system I am working with is around 40D indeed it would have triggered the switch to mutating arrays. Alas, I tried your first suggestion of

@options begin
    inplace = true
end

but it did not remove the error.

Regarding whether I have to do it in @pre - yes, I think so, because the solution to Ax=b (where A depends on the params in @param) is used to set the initial conditions in @init.