-
Notifications
You must be signed in to change notification settings - Fork 28
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
Draft
gdalle
wants to merge
6
commits into
main
Choose a base branch
from
gd/reac
base: main
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.
Draft
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
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
Some comments aren't visible on the classic Files Changed page.
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
12 changes: 12 additions & 0 deletions
12
...nInterface/ext/DifferentiationInterfaceReactantExt/DifferentiationInterfaceReactantExt.jl
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,12 @@ | ||
| module DifferentiationInterfaceReactantExt | ||
|
|
||
| using ADTypes: ADTypes, AutoReactant | ||
| import DifferentiationInterface as DI | ||
| using Reactant: @compile, to_rarray | ||
|
|
||
| DI.check_available(backend::AutoReactant) = DI.check_available(backend.mode) | ||
| DI.inplace_support(backend::AutoReactant) = DI.inplace_support(backend.mode) | ||
|
|
||
| include("onearg.jl") | ||
|
|
||
| end # module |
81 changes: 81 additions & 0 deletions
81
DifferentiationInterface/ext/DifferentiationInterfaceReactantExt/onearg.jl
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,81 @@ | ||
| 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) where {F} | ||
| _sig = DI.signature(f, rebackend, x; strict) | ||
| backend = rebackend.mode | ||
| xr = to_rarray(x) | ||
gdalle marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| gr = to_rarray(similar(x)) | ||
| _gradient(_xr) = DI.gradient(f, backend, _xr) | ||
| _gradient!(_gr, _xr) = copy!(_gr, DI.gradient(f, backend, _xr)) | ||
| _value_and_gradient(_xr) = DI.value_and_gradient(f, backend, _xr) | ||
| function _value_and_gradient!(_gr, _xr) | ||
| y, __gr = DI.value_and_gradient(f, backend, _xr) | ||
gdalle marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| copy!(_gr, __gr) | ||
| return y, _gr | ||
| end | ||
| compiled_gradient = @compile _gradient(xr) | ||
| compiled_gradient! = @compile _gradient!(gr, xr) | ||
| compiled_value_and_gradient = @compile _value_and_gradient(xr) | ||
| compiled_value_and_gradient! = @compile _value_and_gradient!(gr, xr) | ||
| 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 | ||
| ) where {F} | ||
| DI.check_prep(f, prep, rebackend, x) | ||
| (; xr, compiled_gradient) = prep | ||
| copy!(xr, x) | ||
| gr = compiled_gradient(xr) | ||
| g = convert(typeof(x), gr) | ||
| return g | ||
| end | ||
|
|
||
| function DI.value_and_gradient( | ||
| f::F, prep::ReactantGradientPrep, rebackend::AutoReactant, x | ||
| ) where {F} | ||
| DI.check_prep(f, prep, rebackend, x) | ||
| (; xr, compiled_value_and_gradient) = prep | ||
| copy!(xr, x) | ||
| yr, gr = compiled_value_and_gradient(xr) | ||
| y = convert(eltype(x), yr) | ||
| g = convert(typeof(x), gr) | ||
| return y, g | ||
| end | ||
|
|
||
| function DI.gradient!( | ||
| f::F, grad, prep::ReactantGradientPrep, rebackend::AutoReactant, x | ||
| ) where {F} | ||
| DI.check_prep(f, prep, rebackend, x) | ||
| (; xr, gr, compiled_gradient!) = prep | ||
| copy!(xr, x) | ||
| compiled_gradient!(gr, xr) | ||
| return copy!(grad, gr) | ||
| end | ||
|
|
||
| function DI.value_and_gradient!( | ||
| f::F, grad, prep::ReactantGradientPrep, rebackend::AutoReactant, x | ||
| ) where {F} | ||
| DI.check_prep(f, prep, rebackend, x) | ||
| (; xr, gr, compiled_value_and_gradient!) = prep | ||
| copy!(xr, x) | ||
| yr, gr = compiled_value_and_gradient!(gr, xr) | ||
| y = convert(eltype(x), yr) | ||
| return y, copy!(grad, gr) | ||
| 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
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 @@ | ||
| using Pkg | ||
| Pkg.add("Reactant") | ||
|
|
||
| using DifferentiationInterface | ||
| using DifferentiationInterfaceTest | ||
| using Reactant | ||
|
|
||
| backend = AutoReactant() | ||
|
|
||
| test_differentiation( | ||
| backend, DifferentiationInterfaceTest.default_scenarios(); | ||
| excluded = vcat(SECOND_ORDER, :jacobian, :derivative, :pushforward, :pullback), | ||
| logging = true | ||
| ) |
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.
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.
can we have different prep objects for each of the compiled variants. Reason being that one may compile whereas the other may not.
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.
I'm not sure what you mean here. You want to explore all 2^4 combinations of compiled/non-compiled operator variants?
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.
I mean that we should have distinct
perhaps they can be templated or anything else, but they should be distinct (and therefore prepare should only compile for the one that will be used)
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.
That is not compatible with DI's API. A preparation result must allow calling all four variants of an operator.
Would it speed things up if the compiled versions built off one another?
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.
Nope, and rip that seems like a design limitation of DI.
Is it something that's fixable in time?
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.
Just because you don't like it doesn't automatically mean it is a design limitation, it could also just be a design decision you disagree with 😉 In fact, it is completely consistent with the way most backends work, otherwise I would have done it differently. And it is rather convenient for users having 1 preparation for 4 variants.
It is not fixable without a breaking release, which I'm fairly reluctant to do given that we have 1000 indirect dependents.
Even if we did introduce variants like
prepare_gradient!orprepare_value_and_gradient, which we can do fairly easily, the basicprepare_gradientwould still need to prepare for all 4 variants.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.
See #685 to continue this design discussion (I opened this issue a year ago ^^)