Skip to content

Commit 8cf2552

Browse files
DiffEqBaseEnzymeExt: tighten sensealg inner type to exclude SensitivityADPassThrough (#3681)
PR #3678 loosened the `augmented_primal`/`reverse` rules from `Const{Nothing}|Const{<:AbstractSensitivityAlgorithm}` to a plain `Enzyme.Annotation` and added a runtime check throwing on anything other than `nothing`/`AbstractSensitivityAlgorithm`. That broke the existing `Direct Differentiation of Explicit ODE Solve` AD test (test/ad/ad_tests.jl:583), which passes `sensealg = SensitivityADPassThrough()` to deliberately bypass adjoint machinery and do direct AD through `solve_up`. Pre-#3678 the rule simply did not dispatch for `Const{SensitivityADPassThrough}` (since `SensitivityADPassThrough <: AbstractDEAlgorithm`, not `<: AbstractSensitivityAlgorithm`), so Enzyme fell through to direct AD. Post-#3678 the rule swallowed it and immediately raised. Restore the original dispatch behavior while keeping #3678's wider outer-annotation match: constrain the *inner* sensealg value type to `Union{Nothing, AbstractSensitivityAlgorithm}` and drop the redundant runtime ArgumentError check. The outer `Enzyme.Annotation` (Const, Duplicated, MixedDuplicated, Active) is still freely accepted, which preserves the original motivation of #3678 (tolerate `set_runtime_activity(Reverse)` promoting a sensealg const to `Duplicated{GaussAdjoint{...}}` before the rule matches). Bumps DiffEqBase to 7.5.3. Co-authored-by: ChrisRackauckas-Claude <accounts@chrisrackauckas.com>
1 parent 66b4561 commit 8cf2552

2 files changed

Lines changed: 9 additions & 10 deletions

File tree

lib/DiffEqBase/Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "DiffEqBase"
22
uuid = "2b5f629d-d688-5b77-993f-72d75c75574e"
33
authors = ["Chris Rackauckas <accounts@chrisrackauckas.com>"]
4-
version = "7.5.2"
4+
version = "7.5.3"
55

66
[deps]
77
ArrayInterface = "4fba245c-0d91-5ea0-9b3e-6abc04ee57a9"

lib/DiffEqBase/ext/DiffEqBaseEnzymeExt.jl

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,18 @@ module DiffEqBaseEnzymeExt
3434
# can still promote module-level `const _MTK_SENSEALG = GaussAdjoint(...)`
3535
# references — or any sensealg captured in an `ODEProblem` field — to
3636
# `Duplicated{<:AbstractSensitivityAlgorithm}` before this rule is matched.
37-
# Accept any `Enzyme.Annotation` (Const / Duplicated / MixedDuplicated /
38-
# Active) here and read only `sensealg.val`; the shadow has no meaning.
37+
# Accept any `Enzyme.Annotation` wrapping a sensealg value (Const /
38+
# Duplicated / MixedDuplicated / Active) and read only `sensealg.val`;
39+
# the shadow has no meaning. The inner-type constraint keeps
40+
# `SensitivityADPassThrough` (which is `<:AbstractDEAlgorithm`, not
41+
# `<:AbstractSensitivityAlgorithm`) off this rule so that case still
42+
# falls through to direct AD of `solve_up`.
3943
function Enzyme.EnzymeRules.augmented_primal(
4044
config::Enzyme.EnzymeRules.RevConfigWidth{1},
4145
func::Const{typeof(DiffEqBase.solve_up)}, RTA::Union{Type{Duplicated{RT}}, Type{MixedDuplicated{RT}}}, prob,
42-
sensealg::Enzyme.Annotation,
46+
sensealg::Enzyme.Annotation{<:Union{Nothing, DiffEqBase.AbstractSensitivityAlgorithm}},
4347
u0, p, args...; kwargs...
4448
) where {RT}
45-
sensealg.val isa Union{Nothing, DiffEqBase.AbstractSensitivityAlgorithm} ||
46-
throw(ArgumentError(
47-
"DiffEqBaseEnzymeExt: `sensealg` must be `nothing` or " *
48-
"`<:AbstractSensitivityAlgorithm`, got $(typeof(sensealg.val))",
49-
))
5049

5150
res = DiffEqBase._solve_adjoint(
5251
copy_or_reuse(config, prob.val, 2), copy_or_reuse(config, sensealg.val, 3),
@@ -77,7 +76,7 @@ module DiffEqBaseEnzymeExt
7776
function Enzyme.EnzymeRules.reverse(
7877
config::Enzyme.EnzymeRules.RevConfigWidth{1},
7978
func::Const{typeof(DiffEqBase.solve_up)}, ::Union{Type{Duplicated{RT}}, Type{MixedDuplicated{RT}}}, tape, prob,
80-
sensealg::Enzyme.Annotation,
79+
sensealg::Enzyme.Annotation{<:Union{Nothing, DiffEqBase.AbstractSensitivityAlgorithm}},
8180
u0, p, args...; kwargs...
8281
) where {RT}
8382

0 commit comments

Comments
 (0)