Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

ChrisRackauckas-Claude
Copy link

Summary

Adds Enzyme extension to make VJP choice types inactive during differentiation, complementing the AbstractSensitivityAlgorithm rule in SciMLBase.

Problem

When sensealg=BacksolveAdjoint(autojacvec=EnzymeVJP()) is passed to ODEProblem constructor, Enzyme fails with:

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

This occurs because Enzyme tries to differentiate through VJP choice objects (like EnzymeVJP(), ZygoteVJP(), etc.) that are stored within the sensitivity algorithm.

See issue: #1225

Solution

This PR adds:

  1. Enzyme Extension: ext/SciMLSensitivityEnzymeExt.jl with inactive rule for VJP choice types
  2. Comprehensive Tests: test/enzyme_vjp_inactive.jl including the original failing case from Enzyme gradient fails when sensealg is defined in the ODEProblem rather than in the solve function #1225
# VJP choice types should be inactive since they configure computation methods
EnzymeRules.inactive_type(::Type{<:SciMLSensitivity.VJPChoice}) = true

Architecture

This PR handles VJP choice types defined in SciMLSensitivity, while the AbstractSensitivityAlgorithm inactive rule is properly handled in SciMLBase to avoid type piracy.

Complete solution requires both:

  • SciMLBase: AbstractSensitivityAlgorithm inactive rule
  • SciMLSensitivity: VJPChoice inactive rule (this PR)

Testing

Before/After

Before:

# This works
sol = solve(prob, alg; sensealg=BacksolveAdjoint(autojacvec=EnzymeVJP()))

# This fails with Enzyme error  
prob = ODEProblem(f, u0, tspan, p; sensealg=BacksolveAdjoint(autojacvec=EnzymeVJP()))

After:
Both cases work correctly with Enzyme differentiation.

🤖 Generated with Claude Code

ChrisRackauckas and others added 4 commits September 7, 2025 23:29
Adds proper Enzyme extension for SciMLSensitivity to make VJP choice
types inactive during differentiation. This complements the
AbstractSensitivityAlgorithm rule in SciMLBase.

Fixes SciMLSensitivity.jl#1225: When `sensealg=BacksolveAdjoint(autojacvec=EnzymeVJP())`
is passed to ODEProblem constructor, Enzyme would fail with "Error handling
recursive stores for String" because it tried to differentiate through the
VJP choice objects.

Changes:
- Add ext/SciMLSensitivityEnzymeExt.jl with VJPChoice inactive rule
- Add comprehensive tests in test/enzyme_vjp_inactive.jl
- Tests include original failing case from issue SciML#1225

This works in conjunction with SciMLBase PR that adds AbstractSensitivityAlgorithm
inactive rule to avoid type piracy.

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

Co-Authored-By: Claude <[email protected]>
This version includes the Enzyme extension for AbstractSensitivityAlgorithm
inactive_type rule, which is needed for this PR's VJP choice rules to
work together to fix SciMLSensitivity.jl#1225.

Corresponds to General registry PR #138113.

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

Co-Authored-By: Claude <[email protected]>
Run JuliaFormatter with SciMLStyle on the new files to fix format check errors.

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

Co-Authored-By: Claude <[email protected]>
Since SciMLSensitivity directly depends on Enzyme, use direct include
instead of extension for VJP choice inactive rules.

Changes:
- Remove ext/SciMLSensitivityEnzymeExt.jl
- Add src/enzyme_rules.jl with VJPChoice inactive rule
- Include in main SciMLSensitivity.jl module
- Remove extension registration from Project.toml

Note: This addresses the original "Error handling recursive stores for String"
error from SciML#1225, but there may be additional Enzyme compatibility issues
with array operations as noted in recent issue comments.

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

Co-Authored-By: Claude <[email protected]>
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