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

Enzyme support #85

Draft
wants to merge 10 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 3 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
43 changes: 24 additions & 19 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,31 +1,15 @@
name = "AbstractDifferentiation"
uuid = "c29ec348-61ec-40c8-8164-b8c60e9d9f3d"
authors = ["Mohamed Tarek <[email protected]> and contributors"]
version = "0.5.2"
version = "0.6.0"
mohamed82008 marked this conversation as resolved.
Show resolved Hide resolved

[deps]
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
Enzyme = "7da242da-08ed-463a-9acd-ee780be4f1d9"
ExprTools = "e2ba6199-217a-4e67-a87a-7c52f15ade04"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Requires = "ae029012-a4dd-5104-9daa-d747884805df"

[weakdeps]
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
DiffResults = "163ba53b-c6d8-5494-b064-1a9d43ac40c5"
FiniteDifferences = "26cc04aa-876d-5657-8c51-4c34ba976000"
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
ReverseDiff = "37e2e3b7-166d-5795-8a7a-e32c996b4267"
Tracker = "9f7883ad-71c0-57eb-9f7f-b5c9e6d3789c"
Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f"

[extensions]
AbstractDifferentiationChainRulesCoreExt = "ChainRulesCore"
AbstractDifferentiationFiniteDifferencesExt = "FiniteDifferences"
AbstractDifferentiationForwardDiffExt = ["DiffResults", "ForwardDiff"]
AbstractDifferentiationReverseDiffExt = ["DiffResults", "ReverseDiff"]
AbstractDifferentiationTrackerExt = "Tracker"
AbstractDifferentiationZygoteExt = "Zygote"

[compat]
ChainRulesCore = "1"
DiffResults = "1"
Expand All @@ -37,6 +21,16 @@ ReverseDiff = "1"
Tracker = "0.2"
Zygote = "0.6"
julia = "1.6"
Enzyme = "0.11"

[extensions]
AbstractDifferentiationChainRulesCoreExt = "ChainRulesCore"
AbstractDifferentiationFiniteDifferencesExt = "FiniteDifferences"
AbstractDifferentiationForwardDiffExt = ["DiffResults", "ForwardDiff"]
AbstractDifferentiationReverseDiffExt = ["DiffResults", "ReverseDiff"]
AbstractDifferentiationTrackerExt = "Tracker"
AbstractDifferentiationZygoteExt = "Zygote"
AbstractDifferentiationEnzymeExt = "Enzyme"

[extras]
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
Expand All @@ -48,6 +42,17 @@ ReverseDiff = "37e2e3b7-166d-5795-8a7a-e32c996b4267"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Tracker = "9f7883ad-71c0-57eb-9f7f-b5c9e6d3789c"
Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f"
Enzyme = "7da242da-08ed-463a-9acd-ee780be4f1d9"

[targets]
test = ["Test", "ChainRulesCore", "DiffResults", "FiniteDifferences", "ForwardDiff", "Random", "ReverseDiff", "Tracker", "Zygote"]
test = ["Test", "ChainRulesCore", "DiffResults", "FiniteDifferences", "ForwardDiff", "Random", "ReverseDiff", "Tracker", "Zygote", "Enzyme"]

[weakdeps]
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
DiffResults = "163ba53b-c6d8-5494-b064-1a9d43ac40c5"
FiniteDifferences = "26cc04aa-876d-5657-8c51-4c34ba976000"
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
ReverseDiff = "37e2e3b7-166d-5795-8a7a-e32c996b4267"
Tracker = "9f7883ad-71c0-57eb-9f7f-b5c9e6d3789c"
Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f"
Enzyme = "7da242da-08ed-463a-9acd-ee780be4f1d9"
60 changes: 60 additions & 0 deletions ext/AbstractDifferentiationEnzymeExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
module AbstractDifferentiationEnzymeExt

if isdefined(Base, :get_extension)
import AbstractDifferentiation as AD
using Enzyme: Enzyme
else
import ..AbstractDifferentiation as AD
using ..Enzyme: Enzyme
end

AD.@primitive function jacobian(b::AD.EnzymeForwardBackend, f, x)
mohamed82008 marked this conversation as resolved.
Show resolved Hide resolved
val = f(x)
if val isa Real
mohamed82008 marked this conversation as resolved.
Show resolved Hide resolved
return adjoint.(AD.gradient(b, f, x))
else
if length(x) == 1 && length(val) == 1
# Enzyme.jacobian returns a vector of length 1 in this case
return (Matrix(adjoint(Enzyme.jacobian(Enzyme.Forward, f, x))),)
else
return (Enzyme.jacobian(Enzyme.Forward, f, x),)
end
end
end
function AD.jacobian(b::AD.EnzymeForwardBackend, f, x::Real)
return AD.derivative(b, f, x)
end
function AD.gradient(::AD.EnzymeForwardBackend, f, x::AbstractArray)
# Enzyme.gradient with Forward returns a tuple of the same length as the input
return ([Enzyme.gradient(Enzyme.Forward, f, x)...],)
mohamed82008 marked this conversation as resolved.
Show resolved Hide resolved
end
function AD.gradient(b::AD.EnzymeForwardBackend, f, x::Real)
return AD.derivative(b, f, x)
end
function AD.derivative(::AD.EnzymeForwardBackend, f, x::Number)
(Enzyme.autodiff(Enzyme.Forward, f, Enzyme.Duplicated(x, one(x)))[1],)
end

AD.@primitive function jacobian(::AD.EnzymeReverseBackend, f, x)
mohamed82008 marked this conversation as resolved.
Show resolved Hide resolved
val = f(x)
if val isa Real
return (adjoint(Enzyme.gradient(Enzyme.Reverse, f, x)),)
mohamed82008 marked this conversation as resolved.
Show resolved Hide resolved
else
if length(x) == 1 && length(val) == 1
# Enzyme.jacobian returns an adjoint vector of length 1 in this case
return (Matrix(Enzyme.jacobian(Enzyme.Reverse, f, x, Val(1))),)
else
return (Enzyme.jacobian(Enzyme.Reverse, f, x, Val(length(val))),)
end
end
end
function AD.gradient(::AD.EnzymeReverseBackend, f, x::AbstractArray)
mohamed82008 marked this conversation as resolved.
Show resolved Hide resolved
dx = similar(x)
Enzyme.gradient!(Enzyme.Reverse, dx, f, x)
mohamed82008 marked this conversation as resolved.
Show resolved Hide resolved
return (dx,)
end
function AD.derivative(::AD.EnzymeReverseBackend, f, x::Number)
(Enzyme.autodiff(Enzyme.Reverse, f, Enzyme.Active(x))[1][1],)
end

end # module
1 change: 1 addition & 0 deletions src/AbstractDifferentiation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,7 @@ end
@require FiniteDifferences = "26cc04aa-876d-5657-8c51-4c34ba976000" include("../ext/AbstractDifferentiationFiniteDifferencesExt.jl")
@require Tracker = "9f7883ad-71c0-57eb-9f7f-b5c9e6d3789c" include("../ext/AbstractDifferentiationTrackerExt.jl")
@require Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f" include("../ext/AbstractDifferentiationZygoteExt.jl")
@require Enzyme = "7da242da-08ed-463a-9acd-ee780be4f1d9" include("../ext/AbstractDifferentiationEnzymeExt.jl")
end
end

Expand Down
20 changes: 20 additions & 0 deletions src/backends.jl
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,23 @@ It is a special case of [`ReverseRuleConfigBackend`](@ref).
To be able to use this backend, you have to load Zygote.
"""
function ZygoteBackend end

"""
EnzymeReverseBackend

AD backend that uses reverse mode of Enzyme.jl.

!!! note
To be able to use this backend, you have to load Enzyme.
Copy link
Member

Choose a reason for hiding this comment

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

Only true on Julia >= 1.9 I think?

"""
struct EnzymeReverseBackend <: AbstractReverseMode end

"""
EnzymeForwardBackend

AD backend that uses forward mode of Enzyme.jl.

!!! note
To be able to use this backend, you have to load Enzyme.
"""
struct EnzymeForwardBackend <: AbstractForwardMode end
47 changes: 47 additions & 0 deletions test/enzyme.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import AbstractDifferentiation as AD
using Test
using Enzyme

backends = [
"EnzymeForwardBackend" => AD.EnzymeForwardBackend(),
"EnzymeReverseBackend" => AD.EnzymeReverseBackend(),
]

@testset "$name" for (name, backend) in backends
if name == "EnzymeForwardBackend"
@test backend isa AD.AbstractForwardMode
else
@test backend isa AD.AbstractReverseMode
end

@testset "Derivative" begin
test_derivatives(backend, multiple_inputs = false)
mohamed82008 marked this conversation as resolved.
Show resolved Hide resolved
end
@testset "Gradient" begin
test_gradients(backend, multiple_inputs = false)
mohamed82008 marked this conversation as resolved.
Show resolved Hide resolved
end
@testset "Jacobian" begin
test_jacobians(backend, multiple_inputs = false)
mohamed82008 marked this conversation as resolved.
Show resolved Hide resolved
end
# @testset "Hessian" begin
# test_hessians(backend, multiple_inputs = false)
# end
@testset "jvp" begin
test_jvp(backend; multiple_inputs = false, vaugmented=true)
mohamed82008 marked this conversation as resolved.
Show resolved Hide resolved
end
@testset "j′vp" begin
test_j′vp(backend, multiple_inputs = false)
mohamed82008 marked this conversation as resolved.
Show resolved Hide resolved
end
@testset "Lazy Derivative" begin
test_lazy_derivatives(backend, multiple_inputs = false)
mohamed82008 marked this conversation as resolved.
Show resolved Hide resolved
end
@testset "Lazy Gradient" begin
test_lazy_gradients(backend, multiple_inputs = false)
mohamed82008 marked this conversation as resolved.
Show resolved Hide resolved
end
@testset "Lazy Jacobian" begin
test_lazy_jacobians(backend; multiple_inputs = false, vaugmented=true)
mohamed82008 marked this conversation as resolved.
Show resolved Hide resolved
end
# @testset "Lazy Hessian" begin
# test_lazy_hessians(backend, multiple_inputs = false)
# end
end
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ using Test
include("finitedifferences.jl")
include("tracker.jl")
include("ruleconfig.jl")
include("enzyme.jl")
end