Skip to content

Commit 2ed51bb

Browse files
Use SciMLBase parameter despecialization
Co-Authored-By: Chris Rackauckas <[email protected]>
1 parent dc26a82 commit 2ed51bb

19 files changed

Lines changed: 85 additions & 169 deletions

File tree

‎Project.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ REPL = "1"
112112
Reexport = "1"
113113
RuntimeGeneratedFunctions = "0.5.12"
114114
SCCNonlinearSolve = "1.13"
115-
SciMLBase = "3.19"
115+
SciMLBase = "3.46"
116116
SciMLPublic = "1.0.0"
117117
Serialization = "1"
118118
Setfield = "1"

‎docs/src/API/problems.md‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,15 @@ ModelingToolkit.varmap_to_vars
8282
```
8383

8484
The parameters of a split system are stored in a custom data structure called
85-
`MTKParameters`. `AutoSpecialize` ODE problems wrap it in `OpaqueMTKParameters` so solvers
86-
can reuse compiled code across parameter-buffer layouts. `FullSpecialize` problems keep the
87-
concrete `MTKParameters` object and avoid the wrapper's dynamic function barrier. These
88-
objects should only be interacted with through their defined public API.
85+
`MTKParameters`. `AutoSpecialize` problems wrap it in
86+
[`SciMLBase.DespecializedParameters`](@ref) so solvers can reuse compiled code across
87+
parameter-buffer layouts. `FullSpecialize` problems keep the concrete `MTKParameters`
88+
object and avoid the wrapper's dynamic function barrier. These objects should only be
89+
interacted with through their defined public API.
8990
SymbolicIndexingInterface.jl contains functionality useful for this purpose.
9091

9192
```@docs
9293
MTKParameters
93-
OpaqueMTKParameters
9494
ModelingToolkit.get_p
9595
```
9696

‎docs/src/basics/FAQ.md‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
## Why are my parameters some obscure object?
44

55
In ModelingToolkit.jl version 9, the parameter vector was replaced with a custom
6-
`MTKParameters` object. `AutoSpecialize` ODE problems expose an `OpaqueMTKParameters`
7-
wrapper around that object so solver compilation can be reused across parameter layouts.
8-
The internals of both types are intentionally undocumented and subject to change without a
9-
breaking release. This representation enables us to efficiently store and generate code for
10-
parameters of multiple types. To obtain parameter values use
6+
`MTKParameters` object. `AutoSpecialize` problems expose a
7+
[`SciMLBase.DespecializedParameters`](@ref) wrapper around that object so solver compilation
8+
can be reused across parameter layouts. The internals of `MTKParameters` are intentionally
9+
undocumented and subject to change without a breaking release. This representation enables
10+
us to efficiently store and generate code for parameters of multiple types. To obtain
11+
parameter values use
1112
[SymbolicIndexingInterface.jl](https://github.com/SciML/SymbolicIndexingInterface.jl/) or
1213
[SciMLStructures.jl](https://github.com/SciML/SciMLStructures.jl/). For example:
1314

@@ -19,8 +20,8 @@ getβ(prob)
1920
```
2021

2122
Indexes into these parameter objects take the form of `ParameterIndex` objects, which are
22-
similarly undocumented. The following behaviors can be relied on for both
23-
`MTKParameters` and `OpaqueMTKParameters`:
23+
similarly undocumented. The following behaviors can be relied on for both `MTKParameters`
24+
and `SciMLBase.DespecializedParameters`:
2425

2526
- It implements the SciMLStructures interface.
2627
- It can be queried for parameters using functions returned from
@@ -30,7 +31,7 @@ similarly undocumented. The following behaviors can be relied on for both
3031
- `setindex!` with a `ParameterIndex` can be used to set the value of a parameter with the
3132
given index.
3233
- The wrapped `MTKParameters` object is available as the `params` field of an
33-
`OpaqueMTKParameters` object.
34+
`SciMLBase.DespecializedParameters` object.
3435
- `parameter_index(sys, sym)` will return a `ParameterIndex` object if `sys` has been
3536
`complete`d (through `mtkcompile`, `complete` or `@mtkcompile`).
3637
- `copy` duplicates the parameter object, including the memory used by the underlying

‎lib/ModelingToolkitBase/Project.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ ReferenceTests = "0.10"
174174
RuntimeGeneratedFunctions = "0.5.12"
175175
SCCNonlinearSolve = "1.13"
176176
SafeTestsets = "0.1"
177-
SciMLBase = "3.44"
177+
SciMLBase = "3.46"
178178
SciMLPublic = "1.0.0"
179179
SciMLStructures = "1.7"
180180
Serialization = "1"

‎lib/ModelingToolkitBase/src/ModelingToolkitBase.jl‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ export Shift, ShiftIndex
371371
export Sample, Hold, SampleTime
372372
export Clock, SolverStepClock, TimeDomain
373373

374-
export MTKParameters, OpaqueMTKParameters, reorder_dimension_by_tunables!,
374+
export MTKParameters, reorder_dimension_by_tunables!,
375375
reorder_dimension_by_tunables
376376

377377
export HomotopyContinuationProblem

‎lib/ModelingToolkitBase/src/modelingtoolkitize/common.jl‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ function define_params(p::MTKParameters, t, names = nothing)
219219
end
220220
end
221221

222-
define_params(p::OpaqueMTKParameters, t, names = nothing) =
222+
define_params(p::SciMLBase.DespecializedParameters, t, names = nothing) =
223223
define_params(p.params, t, names)
224224

225225
"""
@@ -236,7 +236,7 @@ function to_paramvec(p::MTKParameters)
236236
return reduce(vcat, collect(p); init = [])
237237
end
238238

239-
to_paramvec(p::OpaqueMTKParameters) = to_paramvec(p.params)
239+
to_paramvec(p::SciMLBase.DespecializedParameters) = to_paramvec(p.params)
240240

241241
"""
242242
$(TYPEDSIGNATURES)
@@ -395,7 +395,7 @@ function defaults_from_u0_p(prob, vars, paramobj, paramvec)
395395
if !(p isa Union{SciMLBase.NullParameters, Nothing})
396396
if p isa Union{NamedTuple, AbstractDict}
397397
merge!(defaults, Dict(v => p[k] for (k, v) in pairs(paramobj)))
398-
elseif p isa Union{MTKParameters, OpaqueMTKParameters}
398+
elseif p isa Union{MTKParameters, SciMLBase.DespecializedParameters}
399399
p = _unwrap_mtk_parameters(p)
400400
pvals = [
401401
p.tunable; reduce(vcat, p.discrete; init = []);

‎lib/ModelingToolkitBase/src/problems/jumpproblem.jl‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ end
189189

190190
function updateparams!(
191191
ratemap::JumpSysMajParamMapper{U, V, W},
192-
params::OpaqueMTKParameters
192+
params::SciMLBase.DespecializedParameters
193193
) where {U <: AbstractArray, V <: AbstractArray, W}
194194
return updateparams!(ratemap, params.params)
195195
end

‎lib/ModelingToolkitBase/src/problems/odeproblem.jl‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,6 @@ Base.@nospecializeinfer function _ode_problem(
144144
sys; expression, callback, eval_expression, eval_module, op, _skip_events, tspan, kwargs...
145145
)
146146

147-
if spec === SciMLBase.AutoSpecialize && p isa MTKParameters
148-
p = OpaqueMTKParameters(p)
149-
end
150-
151147
ptype = getmetadata(sys, ProblemTypeCtx, StandardODEProblem())
152148
args = (; f, u0, tspan, p, ptype)
153149
return maybe_codegen_scimlproblem(expression, ODEProblem{_iip}, args; kwargs...)

‎lib/ModelingToolkitBase/src/systems/codegen_utils.jl‎

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,18 +1012,6 @@ end
10121012
# function internals, which improves `juliac` trimmability.
10131013
Base.deepcopy_internal(gfw::GeneratedFunctionWrapper, ::IdDict) = gfw
10141014

1015-
Base.@noinline _invoke_with_unwrapped_parameters(f, a, b) = f(a, b)
1016-
Base.@noinline _invoke_with_unwrapped_parameters(f, a, b, c) = f(a, b, c)
1017-
Base.@noinline _invoke_with_unwrapped_parameters(f, a, b, c, d) = f(a, b, c, d)
1018-
Base.@noinline _invoke_with_unwrapped_parameters(f, args...) = f(args...)
1019-
1020-
Base.@inline @generated function _call_with_unwrapped_parameters(
1021-
f, args::Tuple{Vararg{Any, N}}, p::OpaqueMTKParameters, ::Val{PIdx}
1022-
) where {N, PIdx}
1023-
call_args = [i == PIdx ? :(p.params) : :(args[$i]) for i in 1:N]
1024-
return :(_invoke_with_unwrapped_parameters(f, $(call_args...)))
1025-
end
1026-
10271015
function GeneratedFunctionWrapper{P}(::Type{Val{true}}, foop, fiip; kwargs...) where {P}
10281016
return :($(GeneratedFunctionWrapper{_gfw_params_type(P)})($foop, $fiip))
10291017
end
@@ -1041,8 +1029,8 @@ end
10411029
function (gfw::GeneratedFunctionWrapper{Tuple{PIdx, NArgs, Split}})(args::Vararg{Any, NArgs}) where {PIdx, NArgs, Split}
10421030
# non-split systems just call it as-is
10431031
Split || return gfw.f_oop(args...)
1044-
if args[PIdx] isa OpaqueMTKParameters
1045-
return _call_with_unwrapped_parameters(
1032+
if args[PIdx] isa SciMLBase.DespecializedParameters
1033+
return SciMLBase.invoke_with_despecialized_parameters(
10461034
gfw.f_oop, args, args[PIdx], Val(PIdx)
10471035
)
10481036
end
@@ -1063,8 +1051,8 @@ function (gfw::GeneratedFunctionWrapper{Tuple{PIdx, NArgs, Split}})(args::Vararg
10631051
throw(MethodError(gfw, args))
10641052
end
10651053
Split || return gfw.f_iip(args...)
1066-
if args[PIdx + 1] isa OpaqueMTKParameters
1067-
return _call_with_unwrapped_parameters(
1054+
if args[PIdx + 1] isa SciMLBase.DespecializedParameters
1055+
return SciMLBase.invoke_with_despecialized_parameters(
10681056
gfw.f_iip, args, args[PIdx + 1], Val(PIdx + 1)
10691057
)
10701058
end

‎lib/ModelingToolkitBase/src/systems/nonlinear/initializesystem.jl‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -714,7 +714,9 @@ end
714714
function promote_type_with_nothing(::Type{T}, p::MTKParameters) where {T}
715715
return promote_type_with_nothing(promote_type_with_nothing(T, p.tunable), p.initials)
716716
end
717-
function promote_type_with_nothing(::Type{T}, p::OpaqueMTKParameters) where {T}
717+
function promote_type_with_nothing(
718+
::Type{T}, p::SciMLBase.DespecializedParameters
719+
) where {T}
718720
return promote_type_with_nothing(T, p.params)
719721
end
720722

@@ -743,8 +745,8 @@ function promote_with_nothing(::Type{T}, p::MTKParameters) where {T}
743745
end
744746
return p
745747
end
746-
function promote_with_nothing(::Type{T}, p::OpaqueMTKParameters) where {T}
747-
return OpaqueMTKParameters(promote_with_nothing(T, p.params))
748+
function promote_with_nothing(::Type{T}, p::SciMLBase.DespecializedParameters) where {T}
749+
return SciMLBase.DespecializedParameters(promote_with_nothing(T, p.params))
748750
end
749751

750752
function promote_u0_p(u0, p, t0)

0 commit comments

Comments
 (0)