-
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 5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
module FiniteDifferencesExt | ||
|
||
using AbstractDifferentiation: AbstractDifferentiation, EXTENSIONS_SUPPORTED, FiniteDifferencesBackend | ||
if EXTENSIONS_SUPPORTED | ||
using FiniteDifferences: FiniteDifferences | ||
else | ||
using ..FiniteDifferences: FiniteDifferences | ||
end | ||
|
||
const AD = AbstractDifferentiation | ||
|
||
""" | ||
FiniteDifferencesBackend(method=FiniteDifferences.central_fdm(5, 1)) | ||
|
||
Create an AD backend that uses forward mode with FiniteDifferences.jl. | ||
""" | ||
AD.FiniteDifferencesBackend() = FiniteDifferencesBackend(FiniteDifferences.central_fdm(5, 1)) | ||
|
||
AD.@primitive function jacobian(ba::FiniteDifferencesBackend, f, xs...) | ||
return FiniteDifferences.jacobian(ba.method, f, xs...) | ||
end | ||
|
||
function AD.pushforward_function(ba::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::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
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,41 @@ | ||
module TrackerExt | ||
|
||
using AbstractDifferentiation: AbstractDifferentiation, EXTENSIONS_SUPPORTED, TrackerBackend | ||
if EXTENSIONS_SUPPORTED | ||
using Tracker: Tracker | ||
else | ||
using ..Tracker: Tracker | ||
end | ||
|
||
const AD = AbstractDifferentiation | ||
|
||
function AD.second_lowest(::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::TrackerBackend, f, xs...) | ||
value, back = Tracker.forward(f, xs...) | ||
function pullback(ws) | ||
if ws isa Tuple && !(value isa Tuple) | ||
@assert length(ws) == 1 | ||
map(Tracker.data, back(ws[1])) | ||
else | ||
map(Tracker.data, back(ws)) | ||
end | ||
end | ||
return pullback | ||
end | ||
|
||
function AD.derivative(ba::TrackerBackend, f, xs::Number...) | ||
return Tracker.data.(Tracker.gradient(f, xs...)) | ||
end | ||
|
||
function AD.gradient(ba::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,15 @@ | ||
module ZygoteExt | ||
|
||
using AbstractDifferentiation: AbstractDifferentiation, EXTENSIONS_SUPPORTED, ReverseRuleConfigBackend | ||
|
||
if EXTENSIONS_SUPPORTED | ||
using Zygote: Zygote | ||
else | ||
using ..Zygote: Zygote | ||
end | ||
|
||
@static if isdefined(AbstractDifferentiation, :ZygoteBackend) && isdefined(Zygote, :ZygoteRuleConfig) | ||
AbstractDifferentiation.ZygoteBackend() = ReverseRuleConfigBackend(Zygote.ZygoteRuleConfig()) | ||
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,11 +1,10 @@ | ||
module AbstractDifferentiation | ||
|
||
using LinearAlgebra, ExprTools, Requires, Compat | ||
using LinearAlgebra, ExprTools | ||
using ChainRulesCore: ChainRulesCore | ||
|
||
export AD | ||
|
||
const AD = AbstractDifferentiation | ||
if VERSION < v"1.1.0-DEV.792" | ||
using Compat: eachcol | ||
end | ||
|
||
abstract type AbstractBackend end | ||
abstract type AbstractFiniteDifference <: AbstractBackend end | ||
|
@@ -645,14 +644,21 @@ end | |
@inline asarray(x::AbstractArray) = x | ||
|
||
include("ruleconfig.jl") | ||
include("backends.jl") | ||
|
||
# TODO: Replace with proper version | ||
const EXTENSIONS_SUPPORTED = isdefined(Base, :get_extension) | ||
if !EXTENSIONS_SUPPORTED | ||
using Requires: @require | ||
end | ||
function __init__() | ||
@require ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210" include("forwarddiff.jl") | ||
@require ReverseDiff = "37e2e3b7-166d-5795-8a7a-e32c996b4267" include("reversediff.jl") | ||
@require FiniteDifferences = "26cc04aa-876d-5657-8c51-4c34ba976000" include("finitedifferences.jl") | ||
@require Tracker = "9f7883ad-71c0-57eb-9f7f-b5c9e6d3789c" include("tracker.jl") | ||
@require Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f" begin | ||
@static if !EXTENSIONS_SUPPORTED | ||
@require ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210" include("../ext/ForwardDiffExt.jl") | ||
@require ReverseDiff = "37e2e3b7-166d-5795-8a7a-e32c996b4267" include("../ext/ReverseDiffExt.jl") | ||
@require FiniteDifferences = "26cc04aa-876d-5657-8c51-4c34ba976000" include("../ext/FiniteDifferencesExt.jl") | ||
@require Tracker = "9f7883ad-71c0-57eb-9f7f-b5c9e6d3789c" include("../ext/TrackerExt.jl") | ||
@static if VERSION >= v"1.6" | ||
ZygoteBackend() = ReverseRuleConfigBackend(Zygote.ZygoteRuleConfig()) | ||
@require Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f" include("../ext/ZygoteExt.jl") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why is this Zygote one versioned off? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For simplicity, just bump to LTS? |
||
end | ||
end | ||
end | ||
|
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 move this out of the init definition
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 tried many different versions and this was the (only?) one that seemed to work. But I'll give it another shot.
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.
https://github.com/SciML/RecursiveArrayTools.jl/blob/master/src/RecursiveArrayTools.jl#L45-L50 it works here?
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.
Ah, I think the problem was that I had put
using Requires
in the same check (outside of ìnit) which does not work. Having toif
statements seems to work though.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.
Actually, I just managed to reproduce the problem: tpapp/LogDensityProblemsAD.jl@e4194f8 failed with https://github.com/tpapp/LogDensityProblemsAD.jl/actions/runs/4226767173/jobs/7340561574