Repository navigation
Add adtype passthrough to OptimizationFunction/OptimizationProblem constructors - #5110
Conversation
|
Fixed the CI failure:
Verified locally on Julia 1.12.4: Fix by an AI agent (Devin CLI 3000.10.21, model: SWE-2 High; local session, no shareable URL). |
`OptimizationFunction{iip}(sys, ...)` hardcoded `ad = SciMLBase.NoAD()`,
so there was no way to select an AD backend when building an optimization
problem from a `System`. Add an `adtype` keyword (default `NoAD()`) to the
`OptimizationFunction` constructors, a positional `OptimizationFunction{iip}
(sys, adtype)` method matching the SciMLBase signature, and let `adtype`
flow through `OptimizationProblem(sys, op; adtype = ...)` to the function
constructor like `grad`/`hess`. `Optimization.jl`'s `instantiate_function`
dispatches on `f.adtype`, so this selects solver-side AD while `grad`/`hess`
keep controlling symbolic derivative generation independently.
Promotes ADTypes from a test-only extra to a real dependency of
ModelingToolkitBase (it is already unconditionally loaded via SciMLBase and
has no dependencies of its own) so the `adtype` annotation can use the
public `ADTypes.AbstractADType` spelling.
Motivated by SciML/NeuralPDE.jl#1161
Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Agent-Harness: Devin CLI 3000.10.21
Agent-Model: SWE-2 High
Agent-Session: local Devin CLI session (no shareable URL)
`Pkg.test` requires `[targets] test` entries to resolve through `[extras]`/`[weakdeps]`; since ADTypes moved to `[deps]` it must not remain in the test target, or test-env resolution fails before any tests run. Co-Authored-By: Chris Rackauckas <[email protected]> Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> Agent-Harness: Devin CLI 3000.10.21 Agent-Model: SWE-2 High Agent-Session: local Devin CLI session (no shareable URL)
6dc3d1d to
a127ba9
Compare
|
Rebased onto master ( Two files conflicted. Both were resolved by keeping both features rather than choosing between them, so the approved behaviour of this PR is unchanged and master's
Verification performed locally: all three touched Julia files parse ( Since the approval predates this rebase, the combined-kwarg resolution is worth a second look before merge. 🤖 Posted by an AI agent — harness: Claude Code, model: claude-opus-5[1m] |
Summary
SciMLBase.OptimizationFunction{iip}(sys::System, ...)hardcodedad = SciMLBase.NoAD(), so there was no way to select an AD backend when building anOptimizationProblemfrom aSystem— users always had to rely on whatever the solver picked. Optimization.jl'sinstantiate_functiondispatches onf.adtype, so this is the knob that selects solver-side AD.This PR plumbs an
adtypekeyword through the MTKBase constructors:OptimizationFunction(sys, AutoForwardDiff())— positional, matching theSciMLBase.OptimizationFunction(f, adtype)signature — andOptimizationFunction{iip}(sys; adtype = AutoEnzyme())both setf.adtype.OptimizationProblem(sys, op; adtype = AutoForwardDiff())forwards it throughprocess_SciMLProblemto theOptimizationFunctionconstructor, the same waygrad/hess/cons_j/cons_halready flow through.SciMLBase.NoAD(), so existing behavior is unchanged.adtype(runtime AD selection) is independent ofgrad/hess/cons_j/cons_h(symbolic derivative generation); both can be combined.expression = Val{true}codegen path works as well: the adtype object is embedded in the generatedletblock (ad = AutoForwardDiff()), verified below.To keep the type annotation on
adtypea public spelling (SciMLBase.AbstractADTypeis not public in SciMLBase and would fail the ExplicitImports QA check), ADTypes is promoted from a test-only[extras]entry to a real dependency of ModelingToolkitBase. It is a zero-dependency stub package that is already unconditionally loaded via SciMLBase, and it already had a[compat]bound (1.14.0), so this adds no effective weight.Motivated by SciML/NeuralPDE.jl#1161 (NeuralPDE needs to control the AD backend used for its optimization problems).
Test plan
New
@testset "adtype passthrough"inlib/ModelingToolkitBase/test/optimization/optimizationsystem.jlcovers:OptimizationFunction(sys, AutoForwardDiff())→f.adtype isa AutoForwardDiffOptimizationFunction{true}(sys; adtype = AutoEnzyme())→f.adtype isa AutoEnzymeOptimizationFunction(sys, AutoForwardDiff(); grad = true, hess = true)→ adtype stored and symbolic grad/hess still generatedNoAD()for bothOptimizationFunctionandOptimizationProblemOptimizationProblem(sys, op; adtype = AutoForwardDiff())→prob.f.adtype isa AutoForwardDiffsolve(prob, Optim.BFGS())on the Rosenbrock-like system using the selected ForwardDiff backend (ForwardDiff and ADTypes added to the optimization test env)Run (
cd lib/ModelingToolkitBase/test/optimization && julia --project=. optimizationsystem.jl, Julia 1.12.4):Full
optimizationsystem.jlfile: all testsets pass (exit code 0; the pre-existing@test_brokenin "time dependent var" is unchanged).Additionally verified locally:
expression = Val{true}:eval(OptimizationFunction{true}(sys; expression = Val{true}, adtype = AutoForwardDiff())).adtype isa AutoForwardDiff→truecheck_all_qualified_accesses_are_public,check_no_implicit_imports,check_no_stale_explicit_imports,check_all_explicit_imports_are_public): zeroADTypes-related violations —SciMLBase.AbstractADTypewould have been flagged,ADTypes.AbstractADTypeis not.Runic.main --inplaceon all changed files);typosclean.Not run locally: the full
qa/aqua.jlfile (JET) and the docs build — the change touches docstrings only via the existingfunction_docstring/problem_docstringmachinery, which was exercised by precompiling the package, but CI should confirm.🤖 Generated with Devin (harness: Devin CLI 3000.10.21, model: SWE-2 High, session: local Devin CLI session — no shareable URL)