DiffEqBaseEnzymeExt: accept any sensealg Annotation (unblocks SciMLSensitivity #1456)#3679
Draft
ChrisRackauckas-Claude wants to merge 1 commit into
Draft
Conversation
`sensealg` is a configuration token whose tangent has no semantic
meaning, so its annotation should not constrain rule dispatch. But
Enzyme's `set_runtime_activity(Reverse)` mode can promote a
module-level `const _SENSEALG = GaussAdjoint(...)` reference (or any
sensealg captured in an `ODEProblem` field) to
`Duplicated{<:AbstractSensitivityAlgorithm}` before the rule is
matched. The previous `sensealg::Union{Const{Nothing}, Const{<:AbstractSensitivityAlgorithm}}`
signature then failed to dispatch and Enzyme fell back to native
reverse, producing a `MethodError`.
Accept any `Enzyme.Annotation` here and read only `sensealg.val`; the
shadow has no semantic meaning. Defensive type check inside the body
guards against accidental misuse.
Unblocks the `@test_broken` Enzyme block in SciML/SciMLSensitivity.jl
`test/mtk.jl` (SciML#1456).
Co-Authored-By: Chris Rackauckas <accounts@chrisrackauckas.com>
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.
Summary
DiffEqBaseEnzymeExt.augmented_primal/reverserules onDiffEqBase.solve_upwere signed forsensealg::Union{Const{Nothing}, Const{<:AbstractSensitivityAlgorithm}}. That signature was too narrow: underEnzyme.set_runtime_activity(Reverse), Enzyme can promote a module-levelconst _MTK_SENSEALG = GaussAdjoint(...)reference — or any sensealg captured in anODEProblemfield — toDuplicated{<:AbstractSensitivityAlgorithm}before the rule is matched. The rule then failed to dispatch and Enzyme fell back to native reverse, producing aMethodError.This loosens both rules to accept any
Enzyme.Annotationonsensealg(Const / Duplicated / MixedDuplicated / Active) and reads onlysensealg.valinside; the shadow onsensealghas no semantic meaning because anAbstractSensitivityAlgorithmis a configuration token, not a derivative-carrying value.Why this is safe
SciMLBasealready declaresEnzymeRules.inactive_type(::Type{<:AbstractSensitivityAlgorithm}) = true, expressing the same semantic. The narrowConst-only signature was an unnecessary additional restriction. The body of both rules now skipssensealg's slot when accumulating cotangents, regardless of which annotation it arrived under.Reproducer
The
@test_brokenEnzyme block inSciML/SciMLSensitivity.jltest/mtk.jlcallssolve(prob, Rodas5P(); sensealg = _MTK_SENSEALG, ...)underEnzyme.autodiff(set_runtime_activity(Reverse), ..., Duplicated(prob, diprob)). Before this PR, it failed withMethodErroron thesolve_uprule'ssensealgsignature.After this PR, the rule dispatches and execution proceeds into
_solve_adjoint. It then hits a separate downstreamMethodErrorinside Enzyme'sruntime_generic_augfwdtrying to constructMixedDuplicated(::ODESolution, ...)without the requiredBase.RefValue{T1}second argument — the MTK runtime-activity wrapping issue tracked in SciMLSensitivity.jl#1359 / EnzymeAD/Enzyme.jl#3117. That is out of scope here, so the@test_brokenin #1456 stays broken pending the upstream Enzyme fix.Bumps
DiffEqBase7.5.1 → 7.5.2.Test plan
solve_upEnzyme rule still dispatches under plainReverse(sanity, all existing tests)MethodErrormoves from rule dispatch to downstream MTK wrapping (documented above)Linked: SciML/SciMLSensitivity.jl#1456