Skip to content

Commit 7fae13b

Browse files
Add Enzyme extension to make AbstractSensitivityAlgorithm inactive
Fixes SciMLSensitivity.jl#1225: When `sensealg` is passed to ODEProblem constructor, Enzyme would fail trying to differentiate through the sensitivity algorithm object. This adds an inactive_type rule for AbstractSensitivityAlgorithm to treat it as constant during Enzyme differentiation. This is the correct place for this fix since AbstractSensitivityAlgorithm is defined in SciMLBase, avoiding type piracy that would occur if the rule were defined in SciMLSensitivity. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent 820ddfb commit 7fae13b

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

Project.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ SymbolicIndexingInterface = "2efcf032-c050-4f8e-a9bb-153293bab1f5"
3737
ChainRules = "082447d4-558c-5d27-93f4-14fc19e9eca2"
3838
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
3939
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
40+
Enzyme = "7da242da-08ed-463a-9acd-ee780be4f1d9"
4041
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
4142
MLStyle = "d8e11817-5142-5d16-987a-aa16d5891078"
4243
Makie = "ee78f7c6-11fb-53f2-987a-cfe4a2b5a57a"
@@ -54,6 +55,7 @@ Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f"
5455
[extensions]
5556
SciMLBaseChainRulesCoreExt = "ChainRulesCore"
5657
SciMLBaseDistributionsExt = "Distributions"
58+
SciMLBaseEnzymeExt = "Enzyme"
5759
SciMLBaseForwardDiffExt = "ForwardDiff"
5860
SciMLBaseMLStyleExt = "MLStyle"
5961
SciMLBaseMakieExt = "Makie"
@@ -80,6 +82,7 @@ ConstructionBase = "1.5"
8082
Distributed = "1.10"
8183
Distributions = "0.25"
8284
DocStringExtensions = "0.9"
85+
Enzyme = "0.13"
8386
EnumX = "1"
8487
ForwardDiff = "0.10.36, 1"
8588
FunctionWrappersWrappers = "0.1.3"

ext/SciMLBaseEnzymeExt.jl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module SciMLBaseEnzymeExt
2+
3+
using SciMLBase
4+
import Enzyme: EnzymeRules
5+
6+
# Enzyme rules for SciMLBase abstract types
7+
#
8+
# Sensitivity algorithms define HOW to compute derivatives, not WHAT to differentiate.
9+
# They should be treated as inactive (constant) during Enzyme differentiation to prevent
10+
# errors when they are stored in problem structures that Enzyme differentiates through.
11+
#
12+
# This fixes issues like SciMLSensitivity.jl#1225 where passing `sensealg` to ODEProblem
13+
# constructor would fail with Enzyme.
14+
15+
# All sensitivity algorithm types should be inactive for Enzyme differentiation
16+
EnzymeRules.inactive_type(::Type{<:AbstractSensitivityAlgorithm}) = true
17+
18+
end

0 commit comments

Comments
 (0)