Package | Docs |
---|---|
DifferentiationInterface | |
DifferentiationInterfaceTest |
An interface to various automatic differentiation (AD) backends in Julia.
This package provides a backend-agnostic syntax to differentiate functions of the following types:
- one-argument functions (allocating):
f(x) = y
- two-argument functions (mutating):
f!(y, x) = nothing
- First- and second-order operators (gradients, Jacobians, Hessians and more)
- In-place and out-of-place differentiation
- Preparation mechanism (e.g. to create a config or tape)
- Built-in sparsity handling
- Thorough validation on standard inputs and outputs (numbers, vectors, matrices)
- Testing and benchmarking utilities accessible to users with DifferentiationInterfaceTest
We support all of the backends defined by ADTypes.jl v1.0:
- ChainRulesCore.jl
- Diffractor.jl
- Enzyme.jl
- FastDifferentiation.jl
- FiniteDiff.jl
- FiniteDifferences.jl
- ForwardDiff.jl
- PolyesterForwardDiff.jl
- ReverseDiff.jl
- Symbolics.jl
- Tapir.jl
- Tracker.jl
- Zygote.jl
To install the stable version of the package, run the following code in a Julia REPL:
using Pkg
Pkg.add("DifferentiationInterface")
To install the development version, run this instead:
using Pkg
Pkg.add(
url="https://github.com/gdalle/DifferentiationInterface.jl",
subdir="DifferentiationInterface"
)
using DifferentiationInterface
import ForwardDiff, Enzyme, Zygote # AD backends you want to use
f(x) = sum(abs2, x)
x = [1.0, 2.0]
value_and_gradient(f, AutoForwardDiff(), x) # returns (5.0, [2.0, 4.0]) with ForwardDiff.jl
value_and_gradient(f, AutoEnzyme(), x) # returns (5.0, [2.0, 4.0]) with Enzyme.jl
value_and_gradient(f, AutoZygote(), x) # returns (5.0, [2.0, 4.0]) with Zygote.jl
To improve your performance by up to several orders of magnitude compared to this example, take a look at the DifferentiationInterface tutorial and its section on operator preparation.
- AbstractDifferentiation.jl is the original inspiration for DifferentiationInterface.jl.
- AutoDiffOperators.jl is an attempt to bridge ADTypes.jl with AbstractDifferentiation.jl.