-
-
Notifications
You must be signed in to change notification settings - Fork 79
Add Enzyme inactive rules for VJP choice types #1272
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ChrisRackauckas-Claude
wants to merge
5
commits into
SciML:master
Choose a base branch
from
ChrisRackauckas-Claude:enzyme-vjp-inactive-rules
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
02bc14d
Add Enzyme inactive rules for VJP choice types
ChrisRackauckas ae6c3ec
Bump SciMLBase requirement to v2.117.0
ChrisRackauckas 94be61e
Apply SciMLStyle formatting
ChrisRackauckas e1254e0
Replace extension with direct Enzyme rules file
ChrisRackauckas 57a0b60
Update Project.toml
ChrisRackauckas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| # Enzyme rules for VJP choice types defined in SciMLSensitivity | ||
| # | ||
| # VJP choice types configure how jacobian-vector products are computed within | ||
| # sensitivity algorithms. They should be treated as inactive (constant) during | ||
| # Enzyme differentiation to prevent errors when they are stored in problem | ||
| # structures or other data that Enzyme differentiates through. | ||
| # | ||
| # Note: AbstractSensitivityAlgorithm inactive rule is handled in SciMLBase | ||
| # to avoid type piracy. | ||
|
|
||
| import Enzyme: EnzymeRules | ||
|
|
||
| # VJP choice types should be inactive since they configure computation methods | ||
| EnzymeRules.inactive_type(::Type{<:VJPChoice}) = true |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| using Test, SciMLSensitivity, Enzyme, OrdinaryDiffEq | ||
|
|
||
| # Test that VJP choice types are treated as inactive by Enzyme | ||
| # The AbstractSensitivityAlgorithm inactive rule is handled in SciMLBase | ||
| # This addresses issue #1225 where sensealg in ODEProblem constructor would fail | ||
|
|
||
| @testset "Enzyme VJP Choice Inactive Types" begin | ||
|
|
||
| # Test 1: Basic test that VJP objects can be stored in data structures during Enzyme differentiation | ||
| @testset "VJP types in data structures" begin | ||
| vjp = EnzymeVJP() | ||
|
|
||
| function test_func(x) | ||
| # Store the VJP in a data structure (this would fail without inactive rules) | ||
| data = (value = x[1] + x[2], vjp = vjp) | ||
| return data.value * 2.0 | ||
| end | ||
|
|
||
| x = [1.0, 2.0] | ||
| dx = Enzyme.make_zero(x) | ||
|
|
||
| # This should not throw an error | ||
| @test_nowarn Enzyme.autodiff(Enzyme.Reverse, test_func, Enzyme.Active, Enzyme.Duplicated(x, dx)) | ||
| @test dx ≈ [2.0, 2.0] | ||
| end | ||
|
|
||
| # Test 2: Test different VJP choice types are inactive | ||
| @testset "Different VJP types inactive" begin | ||
| vjp_types = [EnzymeVJP(), ZygoteVJP(), ReverseDiffVJP(), TrackerVJP()] | ||
|
|
||
| for vjp in vjp_types | ||
| function test_func(x) | ||
| data = (value = x[1] * x[2], vjp = vjp) | ||
| return data.value + 1.0 | ||
| end | ||
|
|
||
| x = [2.0, 3.0] | ||
| dx = Enzyme.make_zero(x) | ||
|
|
||
| @test_nowarn Enzyme.autodiff(Enzyme.Reverse, test_func, Enzyme.Active, Enzyme.Duplicated(x, dx)) | ||
| end | ||
| end | ||
|
|
||
| # Test 3: Test sensitivity algorithms with VJP choices (integration test) | ||
| # Note: This test also depends on SciMLBase having AbstractSensitivityAlgorithm as inactive | ||
| @testset "Sensitivity algorithms with VJP choices" begin | ||
| function f(du, u, p, t) | ||
| du[1] = -p[1] * u[1] | ||
| du[2] = p[2] * u[2] | ||
| end | ||
|
|
||
| function loss_func(p) | ||
| u0 = [1.0, 2.0] | ||
| # Both VJP choice and sensitivity algorithm should be inactive | ||
| prob = ODEProblem( | ||
| f, u0, (0.0, 0.1), p, sensealg = BacksolveAdjoint(autojacvec = EnzymeVJP())) | ||
| sol = solve(prob, Tsit5()) | ||
| return sol.u[end][1] + sol.u[end][2] | ||
| end | ||
|
|
||
| p = [0.5, 1.5] | ||
| dp = Enzyme.make_zero(p) | ||
|
|
||
| # This should not throw the "Error handling recursive stores for String" error | ||
| # This is the original failing case from issue #1225 | ||
| @test_nowarn Enzyme.autodiff(Enzyme.Reverse, loss_func, Enzyme.Active, Enzyme.Duplicated(p, dp)) | ||
|
|
||
| # Verify the gradient is computed (non-zero and finite) | ||
| @test all(isfinite, dp) | ||
| @test any(x -> abs(x) > 1e-10, dp) # At least one component should be non-trivial | ||
| end | ||
| end |
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.