Repository navigation
Mark InitializationMetadata inactive for Enzyme - #5094
Merged
ChrisRackauckas merged 1 commit intoSep 8, 2026
Merged
ChrisRackauckas merged 1 commit into
ChrisRackauckas merged 1 commit into
Conversation
`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
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Adds
EnzymeCore.EnzymeRules.inactive_type(::Type{<:InitializationMetadata}) = truenext to the existing
AbstractSystemrule, plus a test.Why
SciMLSensitivity's in-placeEnzymeVJPpasses the fullODEFunctiontoEnzyme.autodiffasDuplicated(Void(f), shadow)and callsEnzyme.remake_zero!(shadow)before every VJP. For an MTK-generated
ODEFunctionthe shadow is dominated byf.initialization_data.metadata::InitializationMetadata, because itsop/guessesmaps areDict{SymbolicT, SymbolicT}whose values Enzyme cannotprove 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:
remake_zero!of theVoid(f)shadowinitialization_data.metadataEnzymeVJPcallGaussAdjoint(EnzymeVJP)gradient, fullODEFunctioninitialization_datastripped from the differentiated functionGaussAdjoint(ReverseDiffVJP(true))gradient, for referenceGradients agree to 1.7e-10 against forward sensitivities in all cases.
This also matters for SciMLSensitivity's automatic
sensealgchoice, which selectsGaussAdjoint(autojacvec = EnzymeVJP())for in-place ODEs oncelength(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_typestates that no derivative information is stored in values of thetype. Every field is either construction-time data (
op,guesses,additional_initialization_eqs, flags) or a callable holdingParameterIndextemplates and setters (
oop_reconstruct_u0_p,get_updated_u0,set_initial_unknowns!). Derivatives w.r.t.u0,pandInitials flow throughthose callables' arguments, which stay active, exactly as for the
AbstractSystemrule directly above (a
Systemalso carries defaults with numeric values).Verified by re-running DyadModelOptimizer's Enzyme calibration tests that tune
Initial()s throughremake+ initialization with the rule applied: all 35 pass (test/calibrate/ad.jl, "SingleShooting with different problem types", whichincludes "optimize initial u0 on ODE" and "mixed Initial and parameter tunables" across
FiniteDiff, ForwardDiff, Zygote and Enzyme), with
Enzyme.make_zero(meta) === metaconfirmed.Reproduced on the current tips as well (SciMLSensitivity master
6fcf58d1, Enzyme mainc5f5b55d, ModelingToolkit v11.42.0 / ModelingToolkitBase v1.69.0, SciMLBase v3.53.1):GaussAdjoint(EnzymeVJP)gradient, N = 32ODEFunctioninitialization_datastripped from the differentiated functionThe remaining gap is the shadow of
initialization_data.initializeprob(aNonlinearProblemwith live arrays), which a follow-up on the SciMLSensitivity side could avoid by differentiating
fwithout itsinitialization_datain the VJP; the RHS never touches it either.Written by Claude Code on Sebastian's behalf.
🤖 Generated with Claude Code