-
Notifications
You must be signed in to change notification settings - Fork 17
Add new backends with DifferentiationInterface.jl #302
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
amontoison
wants to merge
2
commits into
main
Choose a base branch
from
di
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.
Open
Changes from all commits
Commits
Show all changes
2 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
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
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,212 @@ | ||
| struct DIADGradient{B, E} <: ADBackend | ||
| backend::B | ||
| prep::E | ||
| end | ||
|
|
||
| function DIADGradient( | ||
| nvar::Integer, | ||
| f, | ||
| ncon::Integer = 0, | ||
| c::Function = (args...) -> []; | ||
| x0::AbstractVector = rand(nvar), | ||
| backend = AutoReverseDiff(), | ||
| kwargs..., | ||
| ) | ||
| prep = DifferentiationInterface.prepare_gradient(f, backend, x0) | ||
| return DIADGradient(backend, prep) | ||
| end | ||
|
|
||
| function gradient(b::DIADGradient, f, x) | ||
| g = DifferentiationInterface.gradient(f, b.prep, b.backend, x) | ||
| return g | ||
| end | ||
|
|
||
| function gradient!(b::DIADGradient, g, f, x) | ||
| DifferentiationInterface.gradient!(f, g, b.prep, b.backend, x) | ||
| return g | ||
| end | ||
|
|
||
| struct DIADJprod{B, E} <: ADBackend | ||
| backend::B | ||
| prep::E | ||
| end | ||
|
|
||
| function DIADJprod( | ||
| nvar::Integer, | ||
| f, | ||
| ncon::Integer = 0, | ||
| c::Function = (args...) -> []; | ||
| x0::AbstractVector = rand(nvar), | ||
| backend = AutoReverseDiff(), | ||
| kwargs..., | ||
| ) | ||
| T = eltype(x0) | ||
| dy = similar(x0, ncon) | ||
| dx = ntuple(_ -> zero(T), nvar) | ||
| prep = DifferentiationInterface.prepare_pushforward(c, dy, backend, x0, dx) | ||
| return DIADJprod(backend, prep) | ||
| end | ||
|
|
||
| function Jprod!(b::DIADJprod, Jv, c, x, v, ::Val) | ||
| DifferentiationInterface.pushforward!(c, Jv, b.prep, b.backend, x, Tuple(v)) | ||
| return Jv | ||
| end | ||
|
|
||
| struct DIADJtprod{B, E} <: ADBackend | ||
| backend::B | ||
| prep::E | ||
| end | ||
|
|
||
| function DIADJtprod( | ||
| nvar::Integer, | ||
| f, | ||
| ncon::Integer = 0, | ||
| c::Function = (args...) -> []; | ||
| x0::AbstractVector = rand(nvar), | ||
| backend = AutoReverseDiff(), | ||
| kwargs..., | ||
| ) | ||
| T = eltype(x0) | ||
| dx = similar(x0, nvar) | ||
| dy = ntuple(_ -> zero(T), ncon) | ||
| prep = DifferentiationInterface.prepare_pullback(c, dx, backend, x0, dy) | ||
| return DIADJtprod(backend, prep) | ||
| end | ||
|
|
||
| function Jtprod!(b::DIADJtprod, Jtv, c, x, v, ::Val) | ||
| DifferentiationInterface.pullback!(c, Jtv, b.prep, b.backend, x, Tuple(v)) | ||
| return Jtv | ||
| end | ||
|
|
||
| struct DIADJacobian{B, E} <: ADBackend | ||
| backend::B | ||
| prep::E | ||
| end | ||
|
|
||
| function DIADJacobian( | ||
| nvar::Integer, | ||
| f, | ||
| ncon::Integer = 0, | ||
| c::Function = (args...) -> []; | ||
| x0::AbstractVector = rand(nvar), | ||
| backend = AutoForwardDiff(), | ||
| kwargs..., | ||
| ) | ||
| y = similar(x0, ncon) | ||
| prep = DifferentiationInterface.prepare_jacobian(c, y, backend, x0) | ||
| return DIADJacobian(backend, prep) | ||
| end | ||
|
|
||
| function jacobian(b::DIADJacobian, c, x) | ||
| J = DifferentiationInterface.jacobian(c, b.prep, b.backend, x) | ||
| return J | ||
| end | ||
|
|
||
| struct SparseDIADJacobian{B, E} <: ADBackend | ||
| backend::B | ||
| prep::E | ||
| end | ||
|
|
||
| function SparseDIADJacobian( | ||
| nvar::Integer, | ||
| f, | ||
| ncon::Integer = 0, | ||
| c::Function = (args...) -> []; | ||
| x0::AbstractVector = rand(nvar), | ||
| coloring_algorithm::AbstractColoringAlgorithm = GreedyColoringAlgorithm{:direct}( | ||
| postprocessing = true, | ||
| ), | ||
| detector::AbstractSparsityDetector = TracerSparsityDetector(), | ||
| backend = AutoForwardDiff(), | ||
| kwargs..., | ||
| ) | ||
| y = similar(x0, ncon) | ||
| sparse_backend = DifferentiationInterface.AutoSparse(backend, sparsity_detector=detector, coloring_algorithm=coloring_algorithm) | ||
| prep = DifferentiationInterface.prepare_jacobian(c, y, sparse_backend, x0) | ||
| return SparseDIADJacobian(sparse_backend, prep) | ||
| end | ||
|
|
||
| function jacobian(b::SparseDIADJacobian, c, x) | ||
| J = DifferentiationInterface.jacobian(c, b.prep, b.backend, x) | ||
| return J | ||
| end | ||
|
|
||
| struct DIADHvprod{B, E} <: ADBackend | ||
| backend::B | ||
| prep::E | ||
| end | ||
|
|
||
| function DIADHvprod( | ||
| nvar::Integer, | ||
| f, | ||
| ncon::Integer = 0, | ||
| c::Function = (args...) -> []; | ||
| x0::AbstractVector = rand(nvar), | ||
| backend = AutoReverseDiff(), | ||
| kwargs..., | ||
| ) | ||
| T = eltype(x0) | ||
| tx = ntuple(_ -> zero(T), nvar) | ||
| prep = DifferentiationInterface.prepare_hvp(f, backend, x0, tx) | ||
| return DIADHvprod(backend, prep) | ||
| end | ||
|
|
||
| function Hvprod!(b::DIADHvprod, Hv, f, x, v, ::Val) | ||
| DifferentiationInterface.hvp!(f, Hv, b.prep, b.backend, x, Tuple(v)) | ||
| return Hv | ||
| end | ||
|
|
||
| struct DIADHessian{B, E} <: ADBackend | ||
| backend::B | ||
| prep::E | ||
| end | ||
|
|
||
| function DIADHessian( | ||
| nvar::Integer, | ||
| f, | ||
| ncon::Integer = 0, | ||
| c::Function = (args...) -> []; | ||
| x0::AbstractVector = rand(nvar), | ||
| first_backend = AutoReverseDiff(), | ||
| second_backend = AutoForwardDiff(), | ||
| kwargs..., | ||
| ) | ||
| backend = DifferentiationInterface.SecondOrder(second_backend, first_backend) | ||
| prep = DifferentiationInterface.prepare_hessian(f, backend, x0) | ||
| return DIADHessian(backend, prep) | ||
| end | ||
|
|
||
| function hessian(b::DIADHessian, f, x) | ||
| H = DifferentiationInterface.hessian(f, b.prep, b.backend, x) | ||
| return H | ||
| end | ||
|
|
||
| struct SparseDIADHessian{B, E} <: ADBackend | ||
| backend::B | ||
| prep::E | ||
| end | ||
|
|
||
| function SparseDIADHessian( | ||
| nvar::Integer, | ||
| f, | ||
| ncon::Integer = 0, | ||
| c::Function = (args...) -> []; | ||
| x0::AbstractVector = rand(nvar), | ||
| coloring_algorithm::AbstractColoringAlgorithm = GreedyColoringAlgorithm{:substitution}( | ||
| postprocessing = true, | ||
| ), | ||
| detector::AbstractSparsityDetector = TracerSparsityDetector(), | ||
| first_backend = AutoReverseDiff(), | ||
| second_backend = AutoForwardDiff(), | ||
| kwargs..., | ||
| ) | ||
| backend = DifferentiationInterface.SecondOrder(second_backend, first_backend) | ||
| sparse_backend = DifferentiationInterface.AutoSparse(backend, sparsity_detector=detector, coloring_algorithm=coloring_algorithm) | ||
| prep = DifferentiationInterface.prepare_hessian(f, backend, x0) | ||
| return SparseDIADHessian(sparse_backend, prep) | ||
| end | ||
|
|
||
| function hessian(b::SparseDIADHessian, f, x) | ||
| H = DifferentiationInterface.hessian(f, b.prep, b.backend, x) | ||
| return H | ||
| 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
Oops, something went wrong.
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.
Why not just switch fully to the ADTypes specification? You're gonna run into trouble translating symbols into
AbstractADTypeobjectsThere 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.
And the symbols don't allow you to set parameters like