gdalle / DifferentiationInterface.jl

An interface to various automatic differentiation backends in Julia.
https://gdalle.github.io/DifferentiationInterface.jl/DifferentiationInterface
MIT License
132 stars 9 forks source link
ad autodiff automatic-differentiation differentiation julia machine-learning

DifferentiationInterface Logo

DifferentiationInterface

Build Status Coverage Code Style: Blue DOI

Package Docs
DifferentiationInterface Stable Dev
DifferentiationInterfaceTest Stable Dev

An interface to various automatic differentiation (AD) backends in Julia.

Goal

This package provides a backend-agnostic syntax to differentiate functions of the following types:

Features

Compatibility

We support all of the backends defined by ADTypes.jl:

Note that in some cases, going through DifferentiationInterface.jl might be slower than a direct call to the backend's API. This is mostly true for Enzyme.jl, whose handling of activities and multiple arguments unlocks additional performance. We are working on this challenge, and welcome any suggestions or contributions. Meanwhile, if differentiation fails or takes too long, consider using Enzyme.jl directly.

Installation

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"
)

Example

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.

Citation

Please cite both DifferentiationInterface.jl and its inspiration AbstractDifferentiation.jl, using the provided CITATION.bib file.