Skip to content
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

Implement backend for Tracker #44

Merged
merged 7 commits into from
Feb 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
ReverseDiff = "37e2e3b7-166d-5795-8a7a-e32c996b4267"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Tracker = "9f7883ad-71c0-57eb-9f7f-b5c9e6d3789c"
Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f"

[targets]
test = ["Test", "FiniteDifferences", "ForwardDiff", "Random", "ReverseDiff", "Zygote"]
test = ["Test", "FiniteDifferences", "ForwardDiff", "Random", "ReverseDiff", "Tracker", "Zygote"]
1 change: 1 addition & 0 deletions src/AbstractDifferentiation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,7 @@ 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")
end

end
33 changes: 33 additions & 0 deletions src/tracker.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using .Tracker: Tracker

"""
TrackerBackend

AD backend that uses reverse mode with Tracker.jl.
"""
struct TrackerBackend <: AbstractReverseMode end

function second_lowest(::TrackerBackend)
return throw(ArgumentError("Tracker backend does not support nested differentiation."))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the best way I could think of to signal that Tracker should not be used for higher order AD.

end

@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 derivative(ba::TrackerBackend, f, xs::Number...)
return Tracker.data.(Tracker.gradient(f, xs...))
end

function gradient(ba::TrackerBackend, f, xs::AbstractVector...)
return Tracker.data.(Tracker.gradient(f, xs...))
end
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ using Test
include("forwarddiff.jl")
include("reversediff.jl")
include("finitedifferences.jl")
include("tracker.jl")
end
37 changes: 37 additions & 0 deletions test/tracker.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using AbstractDifferentiation
using Test
using Tracker

@testset "TrackerBackend" begin
backends = [@inferred(AD.TrackerBackend())]
@testset for backend in backends
@testset "errors when nested" begin
@test_throws ArgumentError AD.second_lowest(backend)
@test_throws ArgumentError AD.hessian(backend, sum, randn(3))
end
@testset "Derivative" begin
test_derivatives(backend)
end
@testset "Gradient" begin
test_gradients(backend)
end
@testset "Jacobian" begin
test_jacobians(backend)
end
@testset "jvp" begin
test_jvp(backend)
end
@testset "j′vp" begin
test_j′vp(backend)
end
@testset "Lazy Derivative" begin
test_lazy_derivatives(backend)
end
@testset "Lazy Gradient" begin
test_lazy_gradients(backend)
end
@testset "Lazy Jacobian" begin
test_lazy_jacobians(backend)
end
end
end