Skip to content

Conversation

@ChrisRackauckas-Claude
Copy link

Summary

Adds an Enzyme extension to SciMLBase to make AbstractSensitivityAlgorithm inactive during Enzyme differentiation.

Problem

Currently, when sensitivity algorithms are passed to ODEProblem constructors (e.g., ODEProblem(f, u0, tspan, p; sensealg=BacksolveAdjoint(autojacvec=EnzymeVJP()))), Enzyme fails with:

ERROR: Error handling recursive stores for String which has a fieldcount of 0

This occurs because Enzyme tries to differentiate through the sensitivity algorithm object, which contains complex structures that Enzyme cannot handle.

See issue: SciML/SciMLSensitivity.jl#1225

Solution

This PR adds a proper Enzyme extension to SciMLBase with an inactive_type rule:

# All sensitivity algorithm types should be inactive for Enzyme differentiation
EnzymeRules.inactive_type(::Type{<:AbstractSensitivityAlgorithm}) = true

This is the correct location for this fix because:

  1. AbstractSensitivityAlgorithm is defined in SciMLBase
  2. Putting the rule here avoids type piracy that would occur if defined in SciMLSensitivity
  3. Makes the rule available to all packages that use sensitivity algorithms

Changes

  • Added ext/SciMLBaseEnzymeExt.jl with the inactive type rule
  • Added Enzyme as a weak dependency in Project.toml
  • Added extension registration in Project.toml
  • Added Enzyme compatibility constraint

Testing

The fix resolves the original issue where:

  • solve(prob, alg; sensealg=BacksolveAdjoint(...)) works
  • ODEProblem(f, u0, tspan, p; sensealg=BacksolveAdjoint(...)) fails

After this change, both work correctly with Enzyme differentiation.

🤖 Generated with Claude Code

ChrisRackauckas and others added 2 commits September 7, 2025 22:47
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]>
EnzymeRules is the correct package to import for defining inactive_type rules.
Updated Project.toml to use EnzymeRules as weakdep instead of Enzyme.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
@ChrisRackauckas ChrisRackauckas merged commit 5744f8f into SciML:master Sep 8, 2025
49 of 63 checks passed
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