SciML / ModelingToolkit.jl

An acausal modeling framework for automatically parallelized scientific machine learning (SciML) in Julia. A computer algebra system for integrated symbolics for physics-informed machine learning and automated transformations of differential equations
https://mtk.sciml.ai/dev/
Other
1.41k stars 203 forks source link

remake is broken with Bool parameters #2866

Closed acertain closed 3 days ago

acertain commented 1 month ago

I think this is just an issue with the type of narrow_buffer_type_and_fallback_undefs, replacing oldbuf::Vector with oldbuf seems to fix it

Stacktrace:

typeof(paramater_values): ModelingToolkit.MTKParameters{Tuple{Vector{Float64}, BitVector}, Tuple{}, Tuple{}, Tuple{}, Tuple{}, RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(:ˍ₋out, :ˍ₋arg1, :ˍ₋arg2), ModelingToolkit.var"#_RGF_ModTag", ModelingToolkit.var"#_RGF_ModTag", (0x286de1e8, 0x5429cf97, 0xad711212, 0x61198482, 0x72e1544e), Nothing}, RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(:ˍ₋arg1, :ˍ₋arg2), ModelingToolkit.var"#_RGF_ModTag", ModelingToolkit.var"#_RGF_ModTag", (0xa3eacb0b, 0xf0d43f15, 0x0892eac8, 0xcd118675, 0x41ae66ba), Nothing}}
paramater_values.tunable: ([13750.987083139758, 12.333333333333334, 20.35, 212400.0, 0.5, 0.00011666666666666667, 37.0, 9500.0, 0.032, 9500.0, 0.13, 100.0, 0.5, 0.4, 13750.987083139758, 0.5, 0.0016666666666666668, 87120.0, 86634.78025448967, 0.5, 36000.0], Bool[1])
ERROR: MethodError: no method matching narrow_buffer_type_and_fallback_undefs(::BitVector, ::Vector{Any})

Closest candidates are:
  narrow_buffer_type_and_fallback_undefs(::Vector, ::Vector)
   @ ModelingToolkit ~/.julia/packages/ModelingToolkit/U2JaS/src/systems/parameter_buffer.jl:427

Stacktrace:
  [1] _broadcast_getindex_evalf
    @ ./broadcast.jl:709 [inlined]
  [2] _broadcast_getindex
    @ ./broadcast.jl:682 [inlined]
  [3] (::Base.Broadcast.var"#31#32"{Base.Broadcast.Broadcasted{…}})(k::Int64)
    @ Base.Broadcast ./broadcast.jl:1118
  [4] ntuple
    @ ./ntuple.jl:49 [inlined]
  [5] copy
    @ ./broadcast.jl:1118 [inlined]
  [6] materialize(bc::Base.Broadcast.Broadcasted{Base.Broadcast.Style{…}, Nothing, typeof(ModelingToolkit.narrow_buffer_type_and_fallback_undefs), Tuple{…}})
    @ Base.Broadcast ./broadcast.jl:903
  [7] macro expansion
    @ ~/.julia/packages/Setfield/PdKfV/src/sugar.jl:197 [inlined]
  [8] remake_buffer(indp::SciMLBase.ODEProblem{…}, oldbuf::ModelingToolkit.MTKParameters{…}, vals::Dict{…})
    @ ModelingToolkit ~/.julia/packages/ModelingToolkit/U2JaS/src/systems/parameter_buffer.jl:520
  [9] _updated_u0_p_symmap(prob::SciMLBase.ODEProblem{…}, u0::Vector{…}, ::Val{…}, p::Dict{…}, ::Val{…})
    @ SciMLBase ~/.julia/packages/SciMLBase/rR75x/src/remake.jl:494
 [10] _updated_u0_p_internal(prob::SciMLBase.ODEProblem{…}, ::Missing, p::Vector{…}; interpret_symbolicmap::Bool, use_defaults::Bool)
    @ SciMLBase ~/.julia/packages/SciMLBase/rR75x/src/remake.jl:404
 [11] _updated_u0_p_internal
    @ ~/.julia/packages/SciMLBase/rR75x/src/remake.jl:393 [inlined]
 [12] #updated_u0_p#695
    @ ~/.julia/packages/SciMLBase/rR75x/src/remake.jl:556 [inlined]
 [13] updated_u0_p
    @ ~/.julia/packages/SciMLBase/rR75x/src/remake.jl:537 [inlined]
 [14] remake(prob::SciMLBase.ODEProblem{…}; f::Missing, u0::Missing, tspan::Missing, p::Vector{…}, kwargs::Missing, interpret_symbolicmap::Bool, use_defaults::Bool, _kwargs::@Kwargs{})
    @ SciMLBase ~/.julia/packages/SciMLBase/rR75x/src/remake.jl:100
 [15] simulate(__model__::DynamicPPL.Model{…}, __varinfo__::DynamicPPL.UntypedVarInfo{…}, __context__::DynamicPPL.DebugUtils.DebugContext{…}, events::Vector{…}, sys::ModelingToolkit.ODESystem, prob::SciMLBase.ODEProblem{…}; evaluate::Bool)
    @ QSelf ~/Sync/Code/scripts/qself/julia/src/QSelf.jl:71
 [16] simulate
    @ ~/Sync/Code/scripts/qself/julia/src/QSelf.jl:64 [inlined]
 [17] _evaluate!!
    @ ~/.julia/packages/DynamicPPL/Lj3Lu/src/model.jl:973 [inlined]
 [18] evaluate_threadunsafe!!
    @ ~/.julia/packages/DynamicPPL/Lj3Lu/src/model.jl:946 [inlined]
 [19] check_model_and_trace(rng::Random.TaskLocalRNG, model::DynamicPPL.Model{…}; varinfo::DynamicPPL.UntypedVarInfo{…}, context::DynamicPPL.SamplingContext{…}, error_on_failure::Bool, kwargs::@Kwargs{})
    @ DynamicPPL.DebugUtils ~/.julia/packages/DynamicPPL/Lj3Lu/src/debug_utils.jl:598
 [20] check_model_and_trace
    @ ~/.julia/packages/DynamicPPL/Lj3Lu/src/debug_utils.jl:581 [inlined]
 [21] #check_model_and_trace#8
    @ ~/.julia/packages/DynamicPPL/Lj3Lu/src/debug_utils.jl:579 [inlined]
 [22] check_model_and_trace
    @ ~/.julia/packages/DynamicPPL/Lj3Lu/src/debug_utils.jl:578 [inlined]
 [23] check_model
    @ ~/.julia/packages/DynamicPPL/Lj3Lu/src/debug_utils.jl:624 [inlined]
 [24] _check_model
    @ ~/.julia/packages/Turing/axR9Q/src/mcmc/Inference.jl:246 [inlined]
 [25] _check_model
    @ ~/.julia/packages/Turing/axR9Q/src/mcmc/Inference.jl:249 [inlined]
 [26] #sample#6
    @ ~/.julia/packages/Turing/axR9Q/src/mcmc/Inference.jl:299 [inlined]
 [27] sample
    @ ~/.julia/packages/Turing/axR9Q/src/mcmc/Inference.jl:289 [inlined]
 [28] #sample#5
    @ ~/.julia/packages/Turing/axR9Q/src/mcmc/Inference.jl:285 [inlined]
 [29] sample
    @ ~/.julia/packages/Turing/axR9Q/src/mcmc/Inference.jl:277 [inlined]
 [30] #run_inference#37
    @ ~/Sync/Code/scripts/qself/julia/src/QSelf.jl:129 [inlined]
 [31] run_inference(m::DynamicPPL.Model{typeof(QSelf.simulate), (:events, :sys, :prob), (:evaluate,), (), Tuple{…}, Tuple{…}, DynamicPPL.DefaultContext})
    @ QSelf ~/Sync/Code/scripts/qself/julia/src/QSelf.jl:127
 [32] main()
    @ QSelf ~/Sync/Code/scripts/qself/julia/src/QSelf.jl:148
 [33] top-level scope
    @ REPL[18]:1
Some type information was truncated. Use `show(err)` to see complete types.

Additional context

Add any other context about the problem here.

ChrisRackauckas commented 1 month ago

Can you post a reproducer?

cstjean commented 4 days ago

I think I have the same issue?

prob = ODEProblem(model2, [y_input => y_inputs_0], (0.0, 12.0), []);
prob2 = remake(prob; p=[y_input => y_inputs_0])

should work, right? But on the second line, I'm hitting the narrow_buffer_type_and_fallback_undefs error above.

MWE:

using ModelingToolkit
using ModelingToolkit: t_nounits as t, D_nounits as D
using DifferentialEquations: solve, remake
using DataInterpolations
using Symbolics, Test

const ConstantFun = typeof(ConstantInterpolation([10.0, 12, 13], [0, 6, 12]))

y_inputs_0 = ConstantInterpolation([10.0, 12, 13], [0, 6, 12])

eval_interpolation(interp, t) = interp(t)

@register_symbolic eval_interpolation(interp::ConstantFun, t)

@variables y(t)
@parameters y_input::ConstantFun
eqs = [y ~ eval_interpolation(y_input, t)]

@named model = ODESystem(eqs, t)

model2 = structural_simplify(model)

prob = ODEProblem(model2, [y_input => y_inputs_0], (0.0, 12.0), []);

prob2 = remake(prob; p=[y_input => y_inputs_0])

yields

MethodError: no method matching narrow_buffer_type_and_fallback_undefs(::StaticArraysCore.SizedVector{0, Float64, Vector{Float64}}, ::Vector{Any})

Closest candidates are:

  narrow_buffer_type_and_fallback_undefs(!Matched::Vector, ::Vector)

   @ ModelingToolkit C:\Users\a1058035\.julia\packages\ModelingToolkit\GJiqn\src\systems\parameter_buffer.jl:485

Stack trace
Here is what happened, the most recent locations are first:

macro expansion @ sugar.jl:197
remake_buffer(indp::SciMLBase.ODEProblem{Nothing, Tuple{Float64, Float64}, true, ModelingToolkit.MTKParameters{StaticArraysCore.SizedVector{0, Float64, Vector{Float64}}, StaticArraysCore.SizedVector{0, Any, Vector{Any}}, Tuple{}, Tuple{}, Tuple{Vector{DataInterpolations.ConstantInterpolation{ReadOnlyArrays.ReadOnlyVector{Float64, Vector{Float64}}, ReadOnlyArrays.ReadOnlyVector{Int64, Vector{Int64}}, Vector{Float64}, Float64}}}, RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(:ˍ₋out, :ˍ₋arg1), ModelingToolkit.var"#_RGF_ModTag", ModelingToolkit.var"#_RGF_ModTag", (0x33b3bda3, 0xd089b82e, 0xfedcd84c, 0x4037599c, 0x2848c6f6), Nothing}, RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(:ˍ₋arg1,), ModelingToolkit.var"#_RGF_ModTag", ModelingToolkit.var"#_RGF_ModTag", (0xb8b89fef, 0x1b0590a0, 0x8801a652, 0xfeb6172b, 0xb8db857d), Nothing}}, SciMLBase.ODEFunction{true, SciMLBase.AutoSpecialize, ModelingToolkit.var"#f#781"{RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(:ˍ₋arg1, :ˍ₋arg2, :t), ModelingToolkit.var"#_RGF_ModTag", ModelingToolkit.var"#_RGF_ModTag", (0xf484e4ce, 0x5b83a833, 0x33551c77, 0x7c352bb5, 0xdac54478), Nothing}, RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(:ˍ₋out, :ˍ₋arg1, :ˍ₋arg2, :t), ModelingToolkit.var"#_RGF_ModTag", ModelingToolkit.var"#_RGF_ModTag", (0x3c38bd43, 0x235c9731, 0xcd5da351, 0xf995747b, 0x31daffa0), Nothing}}, LinearAlgebra.UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, ModelingToolkit.ObservedFunctionCache{ModelingToolkit.ODESystem}, Nothing, ModelingToolkit.ODESystem, Nothing, Nothing}, @Kwargs{}, SciMLBase.StandardODEProblem}, oldbuf::ModelingToolkit.MTKParameters{StaticArraysCore.SizedVector{0, Float64, Vector{Float64}}, StaticArraysCore.SizedVector{0, Any, Vector{Any}}, Tuple{}, Tuple{}, Tuple{Vector{DataInterpolations.ConstantInterpolation{ReadOnlyArrays.ReadOnlyVector{Float64, Vector{Float64}}, ReadOnlyArrays.ReadOnlyVector{Int64, Vector{Int64}}, Vector{Float64}, Float64}}}, RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(:ˍ₋out, :ˍ₋arg1), ModelingToolkit.var"#_RGF_ModTag", ModelingToolkit.var"#_RGF_ModTag", (0x33b3bda3, 0xd089b82e, 0xfedcd84c, 0x4037599c, 0x2848c6f6), Nothing}, RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(:ˍ₋arg1,), ModelingToolkit.var"#_RGF_ModTag", ModelingToolkit.var"#_RGF_ModTag", (0xb8b89fef, 0x1b0590a0, 0x8801a652, 0xfeb6172b, 0xb8db857d), Nothing}}, vals::Dict{Any, Any}) @ parameter_buffer.jl:602
_updated_u0_p_symmap(prob::SciMLBase.ODEProblem{Nothing, Tuple{Float64, Float64}, true, ModelingToolkit.MTKParameters{StaticArraysCore.SizedVector{0, Float64, Vector{Float64}}, StaticArraysCore.SizedVector{0, Any, Vector{Any}}, Tuple{}, Tuple{}, Tuple{Vector{DataInterpolations.ConstantInterpolation{ReadOnlyArrays.ReadOnlyVector{Float64, Vector{Float64}}, ReadOnlyArrays.ReadOnlyVector{Int64, Vector{Int64}}, Vector{Float64}, Float64}}}, RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(:ˍ₋out, :ˍ₋arg1), ModelingToolkit.var"#_RGF_ModTag", ModelingToolkit.var"#_RGF_ModTag", (0x33b3bda3, 0xd089b82e, 0xfedcd84c, 0x4037599c, 0x2848c6f6), Nothing}, RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(:ˍ₋arg1,), ModelingToolkit.var"#_RGF_ModTag", ModelingToolkit.var"#_RGF_ModTag", (0xb8b89fef, 0x1b0590a0, 0x8801a652, 0xfeb6172b, 0xb8db857d), Nothing}}, SciMLBase.ODEFunction{true, SciMLBase.AutoSpecialize, ModelingToolkit.var"#f#781"{RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(:ˍ₋arg1, :ˍ₋arg2, :t), ModelingToolkit.var"#_RGF_ModTag", ModelingToolkit.var"#_RGF_ModTag", (0xf484e4ce, 0x5b83a833, 0x33551c77, 0x7c352bb5, 0xdac54478), Nothing}, RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(:ˍ₋out, :ˍ₋arg1, :ˍ₋arg2, :t), ModelingToolkit.var"#_RGF_ModTag", ModelingToolkit.var"#_RGF_ModTag", (0x3c38bd43, 0x235c9731, 0xcd5da351, 0xf995747b, 0x31daffa0), Nothing}}, LinearAlgebra.UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, ModelingToolkit.ObservedFunctionCache{ModelingToolkit.ODESystem}, Nothing, ModelingToolkit.ODESystem, Nothing, Nothing}, @Kwargs{}, SciMLBase.StandardODEProblem}, u0::Nothing, ::Val{false}, p::Dict{Any, Any}, ::Val{true}) @ remake.jl:603
_updated_u0_p_internal(prob::SciMLBase.ODEProblem{Nothing, Tuple{Float64, Float64}, true, ModelingToolkit.MTKParameters{StaticArraysCore.SizedVector{0, Float64, Vector{Float64}}, StaticArraysCore.SizedVector{0, Any, Vector{Any}}, Tuple{}, Tuple{}, Tuple{Vector{DataInterpolations.ConstantInterpolation{ReadOnlyArrays.ReadOnlyVector{Float64, Vector{Float64}}, ReadOnlyArrays.ReadOnlyVector{Int64, Vector{Int64}}, Vector{Float64}, Float64}}}, RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(:ˍ₋out, :ˍ₋arg1), ModelingToolkit.var"#_RGF_ModTag", ModelingToolkit.var"#_RGF_ModTag", (0x33b3bda3, 0xd089b82e, 0xfedcd84c, 0x4037599c, 0x2848c6f6), Nothing}, RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(:ˍ₋arg1,), ModelingToolkit.var"#_RGF_ModTag", ModelingToolkit.var"#_RGF_ModTag", (0xb8b89fef, 0x1b0590a0, 0x8801a652, 0xfeb6172b, 0xb8db857d), Nothing}}, SciMLBase.ODEFunction{true, SciMLBase.AutoSpecialize, ModelingToolkit.var"#f#781"{RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(:ˍ₋arg1, :ˍ₋arg2, :t), ModelingToolkit.var"#_RGF_ModTag", ModelingToolkit.var"#_RGF_ModTag", (0xf484e4ce, 0x5b83a833, 0x33551c77, 0x7c352bb5, 0xdac54478), Nothing}, RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(:ˍ₋out, :ˍ₋arg1, :ˍ₋arg2, :t), ModelingToolkit.var"#_RGF_ModTag", ModelingToolkit.var"#_RGF_ModTag", (0x3c38bd43, 0x235c9731, 0xcd5da351, 0xf995747b, 0x31daffa0), Nothing}}, LinearAlgebra.UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, ModelingToolkit.ObservedFunctionCache{ModelingToolkit.ODESystem}, Nothing, ModelingToolkit.ODESystem, Nothing, Nothing}, @Kwargs{}, SciMLBase.StandardODEProblem}, ::Missing, p::Vector{Pair{SymbolicUtils.BasicSymbolic{DataInterpolations.ConstantInterpolation{ReadOnlyArrays.ReadOnlyVector{Float64, Vector{Float64}}, ReadOnlyArrays.ReadOnlyVector{Int64, Vector{Int64}}, Vector{Float64}, Float64}}, DataInterpolations.ConstantInterpolation{ReadOnlyArrays.ReadOnlyVector{Float64, Vector{Float64}}, ReadOnlyArrays.ReadOnlyVector{Int64, Vector{Int64}}, Vector{Float64}, Float64}}}; interpret_symbolicmap::Bool, use_defaults::Bool) @ remake.jl:513
_updated_u0_p_internal @ remake.jl:502
#updated_u0_p#713 @ remake.jl:665
updated_u0_p @ remake.jl:646
#remake#676 @ remake.jl:121
remake @ remake.jl:109
AayushSabharwal commented 3 days ago

@cstjean your MWE works on the latest MTK/SciMLBase.

@acertain since you didn't provide an MWE, I ran the following:

julia> @variables x(t)
julia> @parameters p::Bool q::Bool r
julia> @mtkbuild sys = ODESystem(D(x) ~ x + r, t, [x], [p,q,r])
julia> prob = ODEProblem(sys, [x => 1.0], (0.0, 1.0), [p => true, q => false, r => 1.0])
julia> prob.p # contains a `BitVector`
MTKParameters{Vector{Float64}, Tuple{}, Tuple{BitVector}, Tuple{}}([1.0], (), (Bool[1, 0],), ())
julia> prob2 = remake(prob, p = [q => true])
julia> prob2.p # still contains a `BitVector` and has updated values
MTKParameters{Vector{Float64}, Tuple{}, Tuple{Vector{Bool}}, Tuple{}}([1.0], (), (Bool[1, 1],), ())

Which also works.