Skip to content

Mark InitializationMetadata inactive for Enzyme - #5094

Merged
ChrisRackauckas merged 1 commit into
SciML:masterfrom
SebastianM-C:smc/inactive-initialization-metadata
Sep 8, 2026
Merged

ChrisRackauckas merged 1 commit into
SciML:masterfrom
SebastianM-C:smc/inactive-initialization-metadata

Conversation

@SebastianM-C

Copy link
Copy Markdown
Member

What

Adds EnzymeCore.EnzymeRules.inactive_type(::Type{<:InitializationMetadata}) = true
next to the existing AbstractSystem rule, plus a test.

Why

SciMLSensitivity's in-place EnzymeVJP passes the full ODEFunction to
Enzyme.autodiff as Duplicated(Void(f), shadow) and calls Enzyme.remake_zero!(shadow)
before every VJP. For an MTK-generated ODEFunction the shadow is dominated by
f.initialization_data.metadata::InitializationMetadata, because its
op/guesses maps are Dict{SymbolicT, SymbolicT} whose values Enzyme cannot
prove constant. Measured on a chain of 32 linear ODEs (D(x_i) = -k_i x_i + x_{i-1},
51 save points, Tsit5, abstol = reltol = 1e-8), Julia 1.12.7:

piece time
plain RHS call 5 ns
remake_zero! of the Void(f) shadow 188 µs
of which initialization_data.metadata 97 µs
one EnzymeVJP call 187 µs
GaussAdjoint(EnzymeVJP) gradient, full ODEFunction 259 ms
same, initialization_data stripped from the differentiated function 33 ms
GaussAdjoint(ReverseDiffVJP(true)) gradient, for reference 19 ms

Gradients agree to 1.7e-10 against forward sensitivities in all cases.

This also matters for SciMLSensitivity's automatic sensealg choice, which selects
GaussAdjoint(autojacvec = EnzymeVJP()) for in-place ODEs once
length(u0) + length(tunables) > 100: without this rule that default is
~14x slower than the ReverseDiffVJP(true) alternative on MTK problems.

Why this is safe

inactive_type states that no derivative information is stored in values of the
type. Every field is either construction-time data (op, guesses,
additional_initialization_eqs, flags) or a callable holding ParameterIndex
templates and setters (oop_reconstruct_u0_p, get_updated_u0,
set_initial_unknowns!). Derivatives w.r.t. u0, p and Initials flow through
those callables' arguments, which stay active, exactly as for the AbstractSystem
rule directly above (a System also carries defaults with numeric values).
Verified by re-running DyadModelOptimizer's Enzyme calibration tests that tune
Initial()s through remake + initialization with the rule applied: all 35 pass (test/calibrate/ad.jl, "SingleShooting with different problem types", which
includes "optimize initial u0 on ODE" and "mixed Initial and parameter tunables" across
FiniteDiff, ForwardDiff, Zygote and Enzyme), with Enzyme.make_zero(meta) === meta confirmed.

Reproduced on the current tips as well (SciMLSensitivity master 6fcf58d1, Enzyme main
c5f5b55d, ModelingToolkit v11.42.0 / ModelingToolkitBase v1.69.0, SciMLBase v3.53.1):

variant GaussAdjoint(EnzymeVJP) gradient, N = 32
full ODEFunction 255 ms
with this rule 60 ms
initialization_data stripped from the differentiated function 32 ms

The remaining gap is the shadow of initialization_data.initializeprob (a NonlinearProblem
with live arrays), which a follow-up on the SciMLSensitivity side could avoid by differentiating
f without its initialization_data in the VJP; the RHS never touches it either.

Written by Claude Code on Sebastian's behalf.

🤖 Generated with Claude Code

`InitializationMetadata` is rebuild-only data (operating point, guesses, extra
initialization equations, and the index-template reconstructors `remake` uses to
rebuild the initialization problem). The generated RHS never reads it and it
carries no derivative information, but Enzyme cannot prove that because of the
`Dict{SymbolicT, SymbolicT}` maps, so any `Duplicated` use of an MTK `ODEFunction`
allocates a shadow of the whole metadata and re-zeroes it on every call.

SciMLSensitivity's `EnzymeVJP` does exactly that once per adjoint RHS evaluation:
for a 32-state linear ODE the `remake_zero!` of that shadow costs ~100 µs against a
~5 ns RHS, making `GaussAdjoint(EnzymeVJP)` gradients 8x slower than they need to
be (259 ms -> 33 ms with the metadata excluded from the shadow).

Same treatment `AbstractSystem` already receives one line above.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@ChrisRackauckas
ChrisRackauckas merged commit 9b458b0 into SciML:master Sep 8, 2026
101 of 110 checks passed
ChrisRackauckas added a commit that referenced this pull request Sep 10, 2026
- Fixes to better support 32-bit builds (#5060)
- Add 32-bit InterfaceI CI lane via test_groups.toml (#5092)
- Mark `InitializationMetadata` inactive for Enzyme (#5094)
- Require Symbolics 7.39.1 (32-bit hash fix) (#5096)
- Qualify unbound_inputs @ref in the interactive simulation tutorial (#5099)



Agent-Harness: Claude Code
Agent-Model: claude-opus-5[1m]
Claude-Session: https://claude.ai/code/session_014FEzNTLFutCmTEAZ3zBg5R

Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
ChrisRackauckas added a commit that referenced this pull request Sep 10, 2026
- Keep the system tspan through SDE structural simplification (#5091)
- Fixes to better support 32-bit builds (#5060)
- Add 32-bit InterfaceI CI lane via test_groups.toml (#5092)
- Mark `InitializationMetadata` inactive for Enzyme (#5094)
- Require Symbolics 7.39.1 (32-bit hash fix) (#5096)
- Qualify unbound_inputs @ref in the interactive simulation tutorial (#5099)



Agent-Harness: Claude Code
Agent-Model: claude-opus-5[1m]
Claude-Session: https://claude.ai/code/session_014FEzNTLFutCmTEAZ3zBg5R

Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants