-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Return primal value in `pushforward!` and `pullback!` * Update tests * Allow BenchmarkCI workflow to write comments * Try removing PR write permission * Add `value_and_pushforward!` and `value_and_pullback!` functions * Add fallback for `pullback!` and `pushforward!`, restructure src * Rm developer.md * Skip broken tests * Fix tests and add DiffResults as trigger --------- Co-authored-by: Guillaume Dalle <[email protected]>
- Loading branch information
Showing
13 changed files
with
338 additions
and
202 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
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 |
---|---|---|
@@ -1,51 +1,69 @@ | ||
module DifferentiationInterfaceForwardDiffExt | ||
|
||
using DifferentiationInterface | ||
using DiffResults | ||
using DocStringExtensions | ||
using ForwardDiff | ||
using ForwardDiff: Dual, Tag, value, extract_derivative, extract_derivative! | ||
using LinearAlgebra | ||
|
||
function extract_value(::Type{T}, ydual) where {T} | ||
return value.(T, ydual) | ||
end | ||
|
||
""" | ||
$(TYPEDSIGNATURES) | ||
""" | ||
function DifferentiationInterface.pushforward!( | ||
dy::Y, ::ForwardDiffBackend, f, x::X, dx::X | ||
function DifferentiationInterface.value_and_pushforward!( | ||
_dy::Y, ::ForwardDiffBackend, f, x::X, dx::X | ||
) where {X<:Real,Y<:Real} | ||
new_dy = ForwardDiff.derivative(f, x) * dx | ||
return new_dy | ||
T = typeof(Tag(f, X)) | ||
xdual = Dual{T}(x, dx) | ||
ydual = f(xdual) | ||
y = extract_value(T, ydual) | ||
new_dy = extract_derivative(T, ydual) | ||
return y, new_dy | ||
end | ||
|
||
""" | ||
$(TYPEDSIGNATURES) | ||
""" | ||
function DifferentiationInterface.pushforward!( | ||
function DifferentiationInterface.value_and_pushforward!( | ||
dy::Y, ::ForwardDiffBackend, f, x::X, dx::X | ||
) where {X<:Real,Y<:AbstractArray} | ||
ForwardDiff.derivative!(dy, f, x) | ||
dy .*= dx | ||
return dy | ||
T = typeof(Tag(f, X)) | ||
xdual = Dual{T}(x, dx) | ||
ydual = f(xdual) | ||
y = extract_value(T, ydual) | ||
dy = extract_derivative!(T, dy, ydual) | ||
return y, dy | ||
end | ||
|
||
""" | ||
$(TYPEDSIGNATURES) | ||
""" | ||
function DifferentiationInterface.pushforward!( | ||
dy::Y, ::ForwardDiffBackend, f, x::X, dx::X | ||
function DifferentiationInterface.value_and_pushforward!( | ||
_dy::Y, ::ForwardDiffBackend, f, x::X, dx::X | ||
) where {X<:AbstractArray,Y<:Real} | ||
g = ForwardDiff.gradient(f, x) # TODO: replace with duals, n times too slow | ||
new_dy = dot(g, dx) | ||
return new_dy | ||
res = DiffResults.GradientResult(x) | ||
ForwardDiff.gradient!(res, f, x) | ||
y = DiffResults.value(res) | ||
new_dy = dot(DiffResults.gradient(res), dx) | ||
return y, new_dy | ||
end | ||
|
||
""" | ||
$(TYPEDSIGNATURES) | ||
""" | ||
function DifferentiationInterface.pushforward!( | ||
function DifferentiationInterface.value_and_pushforward!( | ||
dy::Y, ::ForwardDiffBackend, f, x::X, dx::X | ||
) where {X<:AbstractArray,Y<:AbstractArray} | ||
J = ForwardDiff.jacobian(f, x) # TODO: replace with duals, n times too slow | ||
res = DiffResults.JacobianResult(x) | ||
ForwardDiff.jacobian!(res, f, x) # TODO: replace with duals, n times too slow | ||
y = DiffResults.value(res) | ||
J = DiffResults.jacobian(res) | ||
mul!(dy, J, dx) | ||
return dy | ||
return y, dy | ||
end | ||
|
||
end | ||
end # module |
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 |
---|---|---|
@@ -1,30 +1,36 @@ | ||
module DifferentiationInterfaceReverseDiffExt | ||
|
||
using DifferentiationInterface | ||
using DiffResults | ||
using DocStringExtensions | ||
using ReverseDiff | ||
using LinearAlgebra | ||
|
||
""" | ||
$(TYPEDSIGNATURES) | ||
""" | ||
function DifferentiationInterface.pullback!( | ||
function DifferentiationInterface.value_and_pullback!( | ||
dx::X, ::ReverseDiffBackend, f, x::X, dy::Y | ||
) where {X<:AbstractArray,Y<:Real} | ||
ReverseDiff.gradient!(dx, f, x) | ||
dx .*= dy | ||
return dx | ||
res = DiffResults.GradientResult(x) | ||
ReverseDiff.gradient!(res, f, x) | ||
y = DiffResults.value(res) | ||
dx .= dy .* DiffResults.gradient(res) | ||
return y, dx | ||
end | ||
|
||
""" | ||
$(TYPEDSIGNATURES) | ||
""" | ||
function DifferentiationInterface.pullback!( | ||
function DifferentiationInterface.value_and_pullback!( | ||
dx::X, ::ReverseDiffBackend, f, x::X, dy::Y | ||
) where {X<:AbstractArray,Y<:AbstractArray} | ||
J = ReverseDiff.jacobian(f, x) | ||
res = DiffResults.JacobianResult(x) | ||
ReverseDiff.jacobian!(res, f, x) | ||
y = DiffResults.value(res) | ||
J = DiffResults.jacobian(res) | ||
mul!(dx, transpose(J), dy) | ||
return dx | ||
return y, dx | ||
end | ||
|
||
end | ||
end # module |
Oops, something went wrong.