DifferentiationInterface

Build Status Coverage Code Style: Blue

PackageDocs
DifferentiationInterfaceStable Dev
DifferentiationInterfaceTestStable 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:

  • one-argument functions (allocating): f(x) = y
  • two-argument functions (mutating): f!(y, x) = nothing

Features

  • First- and second-order operators
  • In-place and out-of-place differentiation
  • Preparation mechanism (e.g. to create a config or tape)
  • Thorough validation on standard inputs and outputs (numbers, vectors, matrices)
  • Testing and benchmarking utilities accessible to users with DifferentiationInterfaceTest

Compatibility

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

BackendObject
ChainRulesCore.jlAutoChainRules(; ruleconfig)
Diffractor.jlAutoDiffractor()
Enzyme.jlAutoEnzyme(; mode=Enzyme.Forward), AutoEnzyme(; mode=Enzyme.Reverse)
FiniteDiff.jlAutoFiniteDiff()
FiniteDifferences.jlAutoFiniteDifferences(; fdm)
ForwardDiff.jlAutoForwardDiff()
PolyesterForwardDiff.jlAutoPolyesterForwardDiff(; chunksize)
ReverseDiff.jlAutoReverseDiff()
SparseDiffTools.jlAutoSparseForwardDiff(), AutoSparseFiniteDiff()
Tracker.jlAutoTracker()
Zygote.jlAutoZygote()

We also provide some experimental backends ourselves:

BackendObject
FastDifferentiation.jlAutoFastDifferentiation(), AutoSparseFastDifferentiation()
Symbolics.jlAutoSymbolics(), AutoSparseSymbolics()
Tapir.jlAutoTapir()

Installation

To install the stable version of the package, run the following code in a Julia REPL:

julia> using Pkg

julia> Pkg.add("DifferentiationInterface")

To install the development version, run this instead:

julia> using Pkg

julia> Pkg.add(
        url="https://github.com/gdalle/DifferentiationInterface.jl",
        subdir="DifferentiationInterface"
    )

Example

julia> import ForwardDiff

julia> using DifferentiationInterface

julia> backend = AutoForwardDiff();

julia> f(x) = sum(abs2, x);

julia> value_and_gradient(f, backend, [1., 2., 3.])
(14.0, [2.0, 4.0, 6.0])