-
Notifications
You must be signed in to change notification settings - Fork 27
feat: add gradient with AutoReactant #918
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| module DifferentiationInterfaceReactantExt | ||
|
|
||
| using ADTypes: ADTypes, AutoReactant | ||
| import DifferentiationInterface as DI | ||
| using Reactant: @compile, ConcreteRArray, ConcreteRNumber, to_rarray | ||
|
|
||
| DI.check_available(backend::AutoReactant) = DI.check_available(backend.mode) | ||
| DI.inplace_support(backend::AutoReactant) = DI.inplace_support(backend.mode) | ||
|
|
||
| include("utils.jl") | ||
| include("onearg.jl") | ||
|
|
||
| end # module |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| struct ReactantGradientPrep{SIG, XR, GR, CG, CG!, CVG, CVG!} <: DI.GradientPrep{SIG} | ||
| _sig::Val{SIG} | ||
| xr::XR | ||
| gr::GR | ||
| compiled_gradient::CG | ||
| compiled_gradient!::CG! | ||
| compiled_value_and_gradient::CVG | ||
| compiled_value_and_gradient!::CVG! | ||
| end | ||
|
|
||
| function DI.prepare_gradient_nokwarg( | ||
| strict::Val, f::F, rebackend::AutoReactant, x, contexts::Vararg{DI.Context, C} | ||
| ) where {F, C} | ||
| _sig = DI.signature(f, rebackend, x; strict) | ||
| backend = rebackend.mode | ||
| xr = to_reac(x) | ||
| gr = to_reac(similar(x)) | ||
| contextsr = map(to_reac, contexts) | ||
| compiled_gradient = @compile DI.gradient(f, backend, xr, contextsr...) | ||
| compiled_gradient! = @compile DI.gradient!(f, gr, backend, xr, contextsr...) | ||
| compiled_value_and_gradient = @compile DI.value_and_gradient(f, backend, xr, contextsr...) | ||
| compiled_value_and_gradient! = @compile DI.value_and_gradient!(f, gr, backend, xr, contextsr...) | ||
| return ReactantGradientPrep( | ||
| _sig, | ||
| xr, | ||
| gr, | ||
| compiled_gradient, | ||
| compiled_gradient!, | ||
| compiled_value_and_gradient, | ||
| compiled_value_and_gradient!, | ||
| ) | ||
| end | ||
|
|
||
| function DI.gradient( | ||
| f::F, prep::ReactantGradientPrep, rebackend::AutoReactant, x, contexts::Vararg{DI.Context, C} | ||
| ) where {F, C} | ||
| DI.check_prep(f, prep, rebackend, x) | ||
| backend = rebackend.mode | ||
| (; xr, compiled_gradient) = prep | ||
| copyto!(xr, x) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should only do this if x is not a reactantarray |
||
| contextsr = map(to_reac, contexts) | ||
| gr = compiled_gradient(f, backend, xr, contextsr...) | ||
| return gr | ||
| end | ||
|
|
||
| function DI.value_and_gradient( | ||
| f::F, prep::ReactantGradientPrep, rebackend::AutoReactant, x, contexts::Vararg{DI.Context, C} | ||
| ) where {F, C} | ||
| DI.check_prep(f, prep, rebackend, x) | ||
| backend = rebackend.mode | ||
| (; xr, compiled_value_and_gradient) = prep | ||
| copyto!(xr, x) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment here |
||
| contextsr = map(to_reac, contexts) | ||
| yr, gr = compiled_value_and_gradient(f, backend, xr, contextsr...) | ||
| return yr, gr | ||
| end | ||
|
|
||
| function DI.gradient!( | ||
| f::F, grad, prep::ReactantGradientPrep, rebackend::AutoReactant, x, contexts::Vararg{DI.Context, C} | ||
| ) where {F, C} | ||
| DI.check_prep(f, prep, rebackend, x) | ||
| backend = rebackend.mode | ||
| (; xr, gr, compiled_gradient!) = prep | ||
| copyto!(xr, x) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Etc |
||
| contextsr = map(to_reac, contexts) | ||
| compiled_gradient!(f, gr, backend, xr, contextsr...) | ||
| return copyto!(grad, gr) | ||
| end | ||
|
|
||
| function DI.value_and_gradient!( | ||
| f::F, grad, prep::ReactantGradientPrep, rebackend::AutoReactant, x, contexts::Vararg{DI.Context, C} | ||
| ) where {F, C} | ||
| DI.check_prep(f, prep, rebackend, x) | ||
| backend = rebackend.mode | ||
| (; xr, gr, compiled_value_and_gradient!) = prep | ||
| copyto!(xr, x) | ||
| contextsr = map(to_reac, contexts) | ||
| yr, gr = compiled_value_and_gradient!(f, gr, backend, xr, contextsr...) | ||
| return yr, copyto!(grad, gr) | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| to_reac(x::AbstractArray) = to_rarray(x) | ||
| to_reac(x::ConcreteRArray) = x | ||
| to_reac(x::Number) = ConcreteRNumber(x) | ||
| to_reac(x::ConcreteRNumber) = x | ||
|
|
||
| to_reac(c::DI.Constant) = DI.Constant(to_reac(DI.unwrap(c))) | ||
| to_reac(c::DI.Cache) = DI.Cache(to_reac(DI.unwrap(c))) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| using Pkg | ||
| Pkg.add(url = "https://github.com/EnzymeAD/Enzyme.jl") | ||
| Pkg.add("Reactant") | ||
|
|
||
| using DifferentiationInterface | ||
| using DifferentiationInterfaceTest | ||
| using Reactant | ||
| using Test | ||
|
|
||
| backend = AutoReactant() | ||
|
|
||
| @test check_available(backend) | ||
| @test check_inplace(backend) | ||
|
|
||
| test_differentiation( | ||
| backend, DifferentiationInterfaceTest.default_scenarios(; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add a test that the prep contains no data except the compiled fn if compiled for a reactant array |
||
| include_constantified = true, include_cachified = false | ||
| ); | ||
| excluded = vcat(SECOND_ORDER, :jacobian, :derivative, :pushforward, :pullback), | ||
| logging = false | ||
| ) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We shouldn't save anything as a prep argument if a reactant array, I would keep this as if reactant array then xr is nothing otherwise to_rarray(x)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds reasonable