-
Notifications
You must be signed in to change notification settings - Fork 18
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
Use weak dependencies if supported #68
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
154769c
Reorganize files
devmotion b659ca2
Use weak dependencies and remove `AD` alias
devmotion 87694f3
Bump version
devmotion db4af2d
Load `only` on Julia < 1.4
devmotion ec29a2d
Use `Compat.eachcol`
devmotion 5403dd2
Rename extensions
devmotion a83e71c
Move check out of `__init__`
devmotion 6e6e307
Merge branch 'dw/weakdeps' of ssh://github.com/JuliaDiff/AbstractDiff…
devmotion 6fe27db
Move ChainRulesCore support in extension and drop Julia < 1.6
devmotion 6dccd17
Simplify code using features of Julia >= 1.6
devmotion 7fb49be
Merge branch 'master' into dw/weakdeps
devmotion 9e77312
Update CI
devmotion d42dbe6
Fix include statements
devmotion 90fcbae
More fixes
devmotion 8378e26
Additional fixes
devmotion 9db3b75
Simplify Zygote extension
devmotion 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 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,13 @@ | ||
module AbstractDifferentiationChainRulesCoreExt | ||
|
||
import AbstractDifferentiation as AD | ||
using ChainRulesCore: ChainRulesCore | ||
|
||
AD.@primitive function pullback_function(ba::AD.ReverseRuleConfigBackend, f, xs...) | ||
_, back = ChainRulesCore.rrule_via_ad(ba.ruleconfig, f, xs...) | ||
pullback(vs) = Base.tail(back(vs)) | ||
pullback(vs::Tuple{Any}) = Base.tail(back(first(vs))) | ||
return pullback | ||
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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
module AbstractDifferentiationFiniteDifferencesExt | ||
|
||
import AbstractDifferentiation as AD | ||
if AD.EXTENSIONS_SUPPORTED | ||
using FiniteDifferences: FiniteDifferences | ||
else | ||
using ..FiniteDifferences: FiniteDifferences | ||
end | ||
|
||
""" | ||
FiniteDifferencesBackend(method=FiniteDifferences.central_fdm(5, 1)) | ||
|
||
Create an AD backend that uses forward mode with FiniteDifferences.jl. | ||
""" | ||
AD.FiniteDifferencesBackend() = AD.FiniteDifferencesBackend(FiniteDifferences.central_fdm(5, 1)) | ||
|
||
AD.@primitive function jacobian(ba::AD.FiniteDifferencesBackend, f, xs...) | ||
return FiniteDifferences.jacobian(ba.method, f, xs...) | ||
end | ||
|
||
function AD.pushforward_function(ba::AD.FiniteDifferencesBackend, f, xs...) | ||
return function pushforward(vs) | ||
ws = FiniteDifferences.jvp(ba.method, f, tuple.(xs, vs)...) | ||
return length(xs) == 1 ? (ws,) : ws | ||
end | ||
end | ||
|
||
function AD.pullback_function(ba::AD.FiniteDifferencesBackend, f, xs...) | ||
function pullback(vs) | ||
return FiniteDifferences.j′vp(ba.method, f, vs, xs...) | ||
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
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,64 @@ | ||
module AbstractDifferentiationReverseDiffExt | ||
|
||
import AbstractDifferentiation as AD | ||
if AD.EXTENSIONS_SUPPORTED | ||
using DiffResults: DiffResults | ||
using ReverseDiff: ReverseDiff | ||
else | ||
using ..DiffResults: DiffResults | ||
using ..ReverseDiff: ReverseDiff | ||
end | ||
|
||
AD.primal_value(x::ReverseDiff.TrackedReal) = ReverseDiff.value(x) | ||
AD.primal_value(x::AbstractArray{<:ReverseDiff.TrackedReal}) = ReverseDiff.value.(x) | ||
AD.primal_value(x::ReverseDiff.TrackedArray) = ReverseDiff.value(x) | ||
|
||
AD.@primitive function jacobian(ba::AD.ReverseDiffBackend, f, xs...) | ||
xs_arr = map(AD.asarray, xs) | ||
tape = ReverseDiff.JacobianTape(xs_arr) do (xs_arr...) | ||
xs_new = map(xs, xs_arr) do x, x_arr | ||
return x isa Number ? only(x_arr) : x_arr | ||
end | ||
return AD.asarray(f(xs_new...)) | ||
end | ||
results = ReverseDiff.jacobian!(tape, xs_arr) | ||
return map(xs, results) do x, result | ||
return x isa Number ? vec(result) : result | ||
end | ||
end | ||
function AD.jacobian(::AD.ReverseDiffBackend, f, xs::AbstractArray...) | ||
return ReverseDiff.jacobian(AD.asarray ∘ f, xs) | ||
end | ||
|
||
function AD.derivative(::AD.ReverseDiffBackend, f, xs::Number...) | ||
tape = ReverseDiff.InstructionTape() | ||
xs_tracked = ReverseDiff.TrackedReal.(xs, zero.(xs), Ref(tape)) | ||
y_tracked = f(xs_tracked...) | ||
ReverseDiff.seed!(y_tracked) | ||
ReverseDiff.reverse_pass!(tape) | ||
return ReverseDiff.deriv.(xs_tracked) | ||
end | ||
|
||
function AD.gradient(::AD.ReverseDiffBackend, f, xs::AbstractArray...) | ||
return ReverseDiff.gradient(f, xs) | ||
end | ||
|
||
function AD.hessian(::AD.ReverseDiffBackend, f, x::AbstractArray) | ||
return (ReverseDiff.hessian(f, x),) | ||
end | ||
|
||
function AD.value_and_gradient(::AD.ReverseDiffBackend, f, x::AbstractArray) | ||
result = DiffResults.GradientResult(x) | ||
cfg = ReverseDiff.GradientConfig(x) | ||
ReverseDiff.gradient!(result, f, x, cfg) | ||
return DiffResults.value(result), (DiffResults.derivative(result),) | ||
end | ||
|
||
function AD.value_and_hessian(::AD.ReverseDiffBackend, f, x) | ||
result = DiffResults.HessianResult(x) | ||
cfg = ReverseDiff.HessianConfig(result, x) | ||
ReverseDiff.hessian!(result, f, x, cfg) | ||
return DiffResults.value(result), (DiffResults.hessian(result),) | ||
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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
module AbstractDifferentiationTrackerExt | ||
|
||
import AbstractDifferentiation as AD | ||
if AD.EXTENSIONS_SUPPORTED | ||
using Tracker: Tracker | ||
else | ||
using ..Tracker: Tracker | ||
end | ||
|
||
function AD.second_lowest(::AD.TrackerBackend) | ||
return throw(ArgumentError("Tracker backend does not support nested differentiation.")) | ||
end | ||
|
||
AD.primal_value(x::Tracker.TrackedReal) = Tracker.data(x) | ||
AD.primal_value(x::Tracker.TrackedArray) = Tracker.data(x) | ||
AD.primal_value(x::AbstractArray{<:Tracker.TrackedReal}) = Tracker.data.(x) | ||
|
||
AD.@primitive function pullback_function(ba::AD.TrackerBackend, f, xs...) | ||
value, back = Tracker.forward(f, xs...) | ||
function pullback(ws) | ||
if ws isa Tuple && !(value isa Tuple) | ||
map(Tracker.data, back(only(ws))) | ||
else | ||
map(Tracker.data, back(ws)) | ||
end | ||
end | ||
return pullback | ||
end | ||
|
||
function AD.derivative(::AD.TrackerBackend, f, xs::Number...) | ||
return Tracker.data.(Tracker.gradient(f, xs...)) | ||
end | ||
|
||
function AD.gradient(::AD.TrackerBackend, f, xs::AbstractVector...) | ||
return Tracker.data.(Tracker.gradient(f, xs...)) | ||
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
module AbstractDifferentiationZygoteExt | ||
|
||
import AbstractDifferentiation as AD | ||
if AD.EXTENSIONS_SUPPORTED | ||
using Zygote: Zygote | ||
else | ||
using ..Zygote: Zygote | ||
end | ||
|
||
AD.ZygoteBackend() = AD.ReverseRuleConfigBackend(Zygote.ZygoteRuleConfig()) | ||
|
||
end # module |
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.
This is not being imported using the
..ChainRulesCore
that's needed for RequiresThere 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.
It's not loaded with Requires.