-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ReverseDiff, FiniteDiff and ChainRules backends (#9)
* Add ReverseDiff and ChainRules backends * Add Diffractor and FiniteDiff * Explanation
- Loading branch information
Showing
18 changed files
with
425 additions
and
160 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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,97 @@ | ||
module DifferentiationInterfaceFiniteDiffExt | ||
|
||
using DifferentiationInterface | ||
using DocStringExtensions | ||
using FiniteDiff | ||
using LinearAlgebra | ||
|
||
## Pushforward | ||
|
||
""" | ||
$(TYPEDSIGNATURES) | ||
""" | ||
function DifferentiationInterface.pushforward!( | ||
dy::Y, ::FiniteDiffBackend, f, x::X, dx::X | ||
) where {X<:Number,Y<:Number} | ||
new_dy = FiniteDiff.finite_difference_derivative(f, x) * dx | ||
return new_dy | ||
end | ||
|
||
""" | ||
$(TYPEDSIGNATURES) | ||
""" | ||
function DifferentiationInterface.pushforward!( | ||
dy::Y, ::FiniteDiffBackend, f, x::X, dx::X | ||
) where {X<:Number,Y<:AbstractArray} | ||
new_dy = FiniteDiff.finite_difference_derivative(f, x) | ||
dy .= new_dy .* dx | ||
return dy | ||
end | ||
|
||
""" | ||
$(TYPEDSIGNATURES) | ||
""" | ||
function DifferentiationInterface.pushforward!( | ||
dy::Y, ::FiniteDiffBackend, f, x::X, dx::X | ||
) where {X<:AbstractArray,Y<:Number} | ||
g = FiniteDiff.finite_difference_gradient(f, x) | ||
new_dy = dot(g, dx) | ||
return new_dy | ||
end | ||
|
||
""" | ||
$(TYPEDSIGNATURES) | ||
""" | ||
function DifferentiationInterface.pushforward!( | ||
dy::Y, ::FiniteDiffBackend, f, x::X, dx::X | ||
) where {X<:AbstractArray,Y<:AbstractArray} | ||
J = FiniteDiff.finite_difference_jacobian(f, x) | ||
mul!(dy, J, dx) | ||
return dy | ||
end | ||
|
||
## Pullback | ||
|
||
""" | ||
$(TYPEDSIGNATURES) | ||
""" | ||
function DifferentiationInterface.pullback!( | ||
dx::X, ::FiniteDiffBackend, f, x::X, dy::Y | ||
) where {X<:Real,Y<:Real} | ||
new_dx = dy * FiniteDiff.finite_difference_derivative(f, x) | ||
return new_dx | ||
end | ||
|
||
""" | ||
$(TYPEDSIGNATURES) | ||
""" | ||
function DifferentiationInterface.pullback!( | ||
dx::X, ::FiniteDiffBackend, f, x::X, dy::Y | ||
) where {X<:Real,Y<:AbstractArray} | ||
new_dx = dot(dy, FiniteDiff.finite_difference_derivative(f, x)) | ||
return new_dx | ||
end | ||
|
||
""" | ||
$(TYPEDSIGNATURES) | ||
""" | ||
function DifferentiationInterface.pullback!( | ||
dx::X, ::FiniteDiffBackend, f, x::X, dy::Y | ||
) where {X<:AbstractArray,Y<:Real} | ||
g = FiniteDiff.finite_difference_gradient(f, x) | ||
dx .= g .* dy | ||
return dx | ||
end | ||
|
||
""" | ||
$(TYPEDSIGNATURES) | ||
""" | ||
function DifferentiationInterface.pullback!( | ||
dx::X, ::FiniteDiffBackend, f, x::X, dy::Y | ||
) where {X<:AbstractArray,Y<:AbstractArray} | ||
J = FiniteDiff.finite_difference_jacobian(f, x) | ||
mul!(dx, transpose(J), dy) | ||
return dx | ||
end | ||
|
||
end |
This file contains 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 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,30 @@ | ||
module DifferentiationInterfaceReverseDiffExt | ||
|
||
using DifferentiationInterface | ||
using DocStringExtensions | ||
using ReverseDiff | ||
using LinearAlgebra | ||
|
||
""" | ||
$(TYPEDSIGNATURES) | ||
""" | ||
function DifferentiationInterface.pullback!( | ||
dx::X, ::ReverseDiffBackend, f, x::X, dy::Y | ||
) where {X<:AbstractArray,Y<:Real} | ||
ReverseDiff.gradient!(dx, f, x) | ||
dx .*= dy | ||
return dx | ||
end | ||
|
||
""" | ||
$(TYPEDSIGNATURES) | ||
""" | ||
function DifferentiationInterface.pullback!( | ||
dx::X, ::ReverseDiffBackend, f, x::X, dy::Y | ||
) where {X<:AbstractArray,Y<:AbstractArray} | ||
J = ReverseDiff.jacobian(f, x) | ||
mul!(dx, transpose(J), dy) | ||
return dx | ||
end | ||
|
||
end |
Oops, something went wrong.