API reference
DifferentiationInterface.DifferentiationInterface
— ModuleDifferentiationInterface
An interface to various automatic differentiation backends in Julia.
Exports
AutoFastDifferentiation
SecondOrder
check_available
check_hessian
check_mutation
derivative
derivative!!
gradient
gradient!!
hessian
hvp
jacobian
jacobian!!
prepare_derivative
prepare_gradient
prepare_hessian
prepare_hvp
prepare_jacobian
prepare_pullback
prepare_pushforward
prepare_second_derivative
pullback
pullback!!
pushforward
pushforward!!
second_derivative
value_and_derivative
value_and_derivative!!
value_and_gradient
value_and_gradient!!
value_and_jacobian
value_and_jacobian!!
value_and_pullback
value_and_pullback!!
value_and_pushforward
value_and_pushforward!!
Derivative
DifferentiationInterface.derivative!!
— Methodderivative!!(f, der, backend, x, [extras]) -> der
DifferentiationInterface.derivative
— Methodderivative(f, backend, x, [extras]) -> der
DifferentiationInterface.value_and_derivative!!
— Methodvalue_and_derivative!!(f, der, backend, x, [extras]) -> (y, der)
DifferentiationInterface.value_and_derivative!!
— Methodvalue_and_derivative!!(f!, y, der, backend, x, [extras]) -> (y, der)
DifferentiationInterface.value_and_derivative
— Methodvalue_and_derivative(f, backend, x, [extras]) -> (y, der)
Gradient
DifferentiationInterface.gradient!!
— Methodgradient!!(f, grad, backend, x, [extras]) -> grad
DifferentiationInterface.gradient
— Methodgradient(f, backend, x, [extras]) -> grad
DifferentiationInterface.value_and_gradient!!
— Methodvalue_and_gradient!!(f, grad, backend, x, [extras]) -> (y, grad)
DifferentiationInterface.value_and_gradient
— Methodvalue_and_gradient(f, backend, x, [extras]) -> (y, grad)
Jacobian
DifferentiationInterface.jacobian!!
— Methodjacobian!!(f, jac, backend, x, [extras]) -> jac
DifferentiationInterface.jacobian
— Methodjacobian(f, backend, x, [extras]) -> jac
DifferentiationInterface.value_and_jacobian!!
— Methodvalue_and_jacobian!!(f!, y, jac, backend, x, [extras]) -> (y, jac)
DifferentiationInterface.value_and_jacobian!!
— Methodvalue_and_jacobian!!(f, jac, backend, x, [extras]) -> (y, jac)
DifferentiationInterface.value_and_jacobian
— Methodvalue_and_jacobian(f, backend, x, [extras]) -> (y, jac)
Second order
DifferentiationInterface.SecondOrder
— TypeSecondOrder
Combination of two backends for second-order differentiation.
Fields
outer::ADTypes.AbstractADType
: backend for the outer differentiationinner::ADTypes.AbstractADType
: backend for the inner differentiation
DifferentiationInterface.second_derivative
— Methodsecond_derivative(f, backend, x, [extras]) -> der2
DifferentiationInterface.hessian
— Methodhessian(f, backend, x, [extras]) -> hess
DifferentiationInterface.hvp
— Methodhvp(f, backend, x, v, [extras]) -> p
Primitives
DifferentiationInterface.pushforward!!
— Methodpushforward!!(f, dy, backend, x, dx, [extras]) -> (y, dy)
DifferentiationInterface.pushforward
— Methodpushforward(f, backend, x, dx, [extras]) -> (y, dy)
DifferentiationInterface.value_and_pushforward!!
— Methodvalue_and_pushforward!!(f, dy, backend, x, dx, [extras]) -> (y, dy)
DifferentiationInterface.value_and_pushforward!!
— Methodvalue_and_pushforward!!(f!, y, dy, backend, x, dx, [extras]) -> (y, dy)
Required primitive for forward mode backends to support mutating functions.
DifferentiationInterface.value_and_pushforward
— Methodvalue_and_pushforward(f, backend, x, dx, [extras]) -> (y, dy)
Required primitive for forward mode backends to support allocating functions.
DifferentiationInterface.pullback!!
— Methodpullback!!(f, dx, backend, x, dy, [extras]) -> dx
DifferentiationInterface.pullback
— Methodpullback(f, backend, x, dy, [extras]) -> dx
DifferentiationInterface.value_and_pullback!!
— Methodvalue_and_pullback!!(f, dx, backend, x, dy, [extras]) -> (y, dx)
DifferentiationInterface.value_and_pullback!!
— Methodvalue_and_pullback!!(f!, y, dx, backend, x, dy, [extras]) -> (y, dx)
Required primitive for reverse mode backends to support mutating functions.
DifferentiationInterface.value_and_pullback
— Methodvalue_and_pullback(f, backend, x, dy, [extras]) -> (y, dx)
Required primitive for reverse mode backends to support allocating functions.
Backend queries
DifferentiationInterface.check_available
— Methodcheck_available(backend)
Check whether backend
is available by trying a scalar-to-scalar derivative.
Might take a while due to compilation time.
DifferentiationInterface.check_hessian
— Methodcheck_hessian(backend)
Check whether backend
supports second order differentiation by trying a hessian.
Might take a while due to compilation time.
DifferentiationInterface.check_mutation
— Methodcheck_mutation(backend)
Check whether backend
supports differentiation of mutating functions by trying a jacobian.
Might take a while due to compilation time.
Preparation
DifferentiationInterface.prepare_derivative
— Methodprepare_derivative(f, backend, x) -> extras
-prepare_derivative(f!, backend, y, x) -> extras
Create an extras
object that can be given to derivative operators.
DifferentiationInterface.prepare_gradient
— Methodprepare_gradient(f, backend, x) -> extras
Create an extras
object that can be given to gradient operators.
DifferentiationInterface.prepare_hessian
— Methodprepare_hessian(f, backend, x) -> extras
Create an extras
object that can be given to Hessian operators.
DifferentiationInterface.prepare_hvp
— Methodprepare_hvp(f, backend, x) -> extras
Create an extras
object that can be given to Hessian-vector product operators.
DifferentiationInterface.prepare_jacobian
— Methodprepare_jacobian(f, backend, x) -> extras
-prepare_jacobian(f!, backend, y, x) -> extras
Create an extras
object that can be given to Jacobian operators.
DifferentiationInterface.prepare_pullback
— Methodprepare_pullback(f, backend, x) -> extras
-prepare_pullback(f!, backend, y, x) -> extras
Create an extras
object that can be given to pullback operators.
DifferentiationInterface.prepare_pushforward
— Methodprepare_pushforward(f, backend, x) -> extras
-prepare_pushforward(f!, backend, y, x) -> extras
Create an extras
object that can be given to pushforward operators.
DifferentiationInterface.prepare_second_derivative
— Methodprepare_second_derivative(f, backend, x) -> extras
-prepare_second_derivative(f!, backend, y, x) -> extras
Create an extras
object that can be given to second derivative operators.
Testing & benchmarking
DifferentiationInterface.DifferentiationTest
— ModuleDifferentiationInterface.DifferentiationTest
Testing utilities for DifferentiationInterface
.
DifferentiationInterface.DifferentiationTest.BenchmarkData
— TypeBenchmarkData
Ad-hoc storage type for differentiation benchmarking results. You can turn it into a DataFrame
as follows:
df = DataFrames.DataFrame(pairs(benchmark_data)...)
Fields
These are not part of the public API.
backend::Vector{String}
mode::Vector{Type}
operator::Vector{Function}
variant::Vector{Function}
func::Vector{String}
mutating::Vector{Bool}
input_type::Vector{Type}
output_type::Vector{Type}
input_size::Vector
output_size::Vector
samples::Vector{Int64}
time::Vector{Float64}
bytes::Vector{Float64}
allocs::Vector{Float64}
compile_fraction::Vector{Float64}
gc_fraction::Vector{Float64}
evals::Vector{Float64}
DifferentiationInterface.DifferentiationTest.Scenario
— TypeScenario{mutating}
Store a testing scenario composed of a function and its input + output + tangents.
Fields
f::Any
: functionx::Any
: inputy::Any
: outputdx::Any
: pushforward seeddy::Any
: pullback seed
DifferentiationInterface.DifferentiationTest.all_operators
— Methodall_operators()
List all operators that can be tested with test_differentiation
.
DifferentiationInterface.DifferentiationTest.default_scenarios
— Methoddefault_scenarios()
Create a vector of Scenario
s for testing differentiation.
DifferentiationInterface.DifferentiationTest.test_differentiation
— Functiontest_differentiation(backends, [operators, scenarios]; [kwargs...])
Cross-test a list of backends
for a list of operators
on a list of scenarios
, running a variety of different tests.
- If
benchmark
isfalse
, this returns aTestSet
object. - If
benchmark
istrue
, this returns aBenchmarkData
object, which is easy to turn into aDataFrame
.
Default arguments
operators::Vector{Function}
: the list[pushforward, pullback,derivative, gradient, jacobian, second_derivative, hvp, hessian]
scenarios::Vector{Scenario}
: the output ofdefault_scenarios()
Keyword arguments
Testing:
correctness=true
: whether to compare the differentiation results with those given by ForwardDiff.jlerror_free=false
: whether to run it once and see if it errorstype_stability=false
: whether to check type stability with JET.jl (@test_opt
)call_count=false
: whether to check that the function is called the right number of timesbenchmark=false
: whether to run and return a benchmark suite with Chairmarks.jlallocations=false
: whether to check that the benchmarks are allocation-freedetailed=false
: whether to print a detailed test set (by scenario) or condensed test set (by operator)
Filtering:
input_type=Any
: restrict scenario inputs to subtypes of thisoutput_type=Any
: restrict scenario outputs to subtypes of thisfirst_order=true
: consider first order operatorssecond_order=true
: consider second order operatorsallocating=true
: consider operators for allocating functionsmutating=true
: consider operators for mutating functionsexcluded=Symbol[]
: list of excluded operators
DifferentiationInterface.DifferentiationTest.test_differentiation
— Methodtest_differentiation(
+API reference · DifferentiationInterface.jl API reference
DifferentiationInterface.DifferentiationInterface
— ModuleDifferentiationInterface
An interface to various automatic differentiation backends in Julia.
Exports
AutoFastDifferentiation
SecondOrder
check_available
check_hessian
check_mutation
derivative
derivative!!
gradient
gradient!!
hessian
hvp
jacobian
jacobian!!
prepare_derivative
prepare_gradient
prepare_hessian
prepare_hvp
prepare_jacobian
prepare_pullback
prepare_pushforward
prepare_second_derivative
pullback
pullback!!
pushforward
pushforward!!
second_derivative
value_and_derivative
value_and_derivative!!
value_and_gradient
value_and_gradient!!
value_and_jacobian
value_and_jacobian!!
value_and_pullback
value_and_pullback!!
value_and_pushforward
value_and_pushforward!!
sourceDerivative
DifferentiationInterface.derivative!!
— Methodderivative!!(f, der, backend, x, [extras]) -> der
sourceDifferentiationInterface.derivative
— Methodderivative(f, backend, x, [extras]) -> der
sourceDifferentiationInterface.value_and_derivative!!
— Methodvalue_and_derivative!!(f, der, backend, x, [extras]) -> (y, der)
sourceDifferentiationInterface.value_and_derivative!!
— Methodvalue_and_derivative!!(f!, y, der, backend, x, [extras]) -> (y, der)
sourceDifferentiationInterface.value_and_derivative
— Methodvalue_and_derivative(f, backend, x, [extras]) -> (y, der)
sourceGradient
DifferentiationInterface.gradient!!
— Methodgradient!!(f, grad, backend, x, [extras]) -> grad
sourceDifferentiationInterface.gradient
— Methodgradient(f, backend, x, [extras]) -> grad
sourceDifferentiationInterface.value_and_gradient!!
— Methodvalue_and_gradient!!(f, grad, backend, x, [extras]) -> (y, grad)
sourceDifferentiationInterface.value_and_gradient
— Methodvalue_and_gradient(f, backend, x, [extras]) -> (y, grad)
sourceJacobian
DifferentiationInterface.jacobian!!
— Methodjacobian!!(f, jac, backend, x, [extras]) -> jac
sourceDifferentiationInterface.jacobian
— Methodjacobian(f, backend, x, [extras]) -> jac
sourceDifferentiationInterface.value_and_jacobian!!
— Methodvalue_and_jacobian!!(f!, y, jac, backend, x, [extras]) -> (y, jac)
sourceDifferentiationInterface.value_and_jacobian!!
— Methodvalue_and_jacobian!!(f, jac, backend, x, [extras]) -> (y, jac)
sourceDifferentiationInterface.value_and_jacobian
— Methodvalue_and_jacobian(f, backend, x, [extras]) -> (y, jac)
sourceSecond order
DifferentiationInterface.SecondOrder
— TypeSecondOrder
Combination of two backends for second-order differentiation.
Fields
outer::ADTypes.AbstractADType
: backend for the outer differentiation
inner::ADTypes.AbstractADType
: backend for the inner differentiation
sourceDifferentiationInterface.second_derivative
— Methodsecond_derivative(f, backend, x, [extras]) -> der2
sourceDifferentiationInterface.hessian
— Methodhessian(f, backend, x, [extras]) -> hess
sourceDifferentiationInterface.hvp
— Methodhvp(f, backend, x, v, [extras]) -> p
sourcePrimitives
DifferentiationInterface.pushforward!!
— Methodpushforward!!(f, dy, backend, x, dx, [extras]) -> (y, dy)
sourceDifferentiationInterface.pushforward
— Methodpushforward(f, backend, x, dx, [extras]) -> (y, dy)
sourceDifferentiationInterface.value_and_pushforward!!
— Methodvalue_and_pushforward!!(f, dy, backend, x, dx, [extras]) -> (y, dy)
sourceDifferentiationInterface.value_and_pushforward!!
— Methodvalue_and_pushforward!!(f!, y, dy, backend, x, dx, [extras]) -> (y, dy)
Info Required primitive for forward mode backends to support mutating functions.
sourceDifferentiationInterface.value_and_pushforward
— Methodvalue_and_pushforward(f, backend, x, dx, [extras]) -> (y, dy)
Info Required primitive for forward mode backends to support allocating functions.
sourceDifferentiationInterface.pullback!!
— Methodpullback!!(f, dx, backend, x, dy, [extras]) -> dx
sourceDifferentiationInterface.pullback
— Methodpullback(f, backend, x, dy, [extras]) -> dx
sourceDifferentiationInterface.value_and_pullback!!
— Methodvalue_and_pullback!!(f, dx, backend, x, dy, [extras]) -> (y, dx)
sourceDifferentiationInterface.value_and_pullback!!
— Methodvalue_and_pullback!!(f!, y, dx, backend, x, dy, [extras]) -> (y, dx)
Info Required primitive for reverse mode backends to support mutating functions.
sourceDifferentiationInterface.value_and_pullback
— Methodvalue_and_pullback(f, backend, x, dy, [extras]) -> (y, dx)
Info Required primitive for reverse mode backends to support allocating functions.
sourceBackend queries
DifferentiationInterface.check_available
— Methodcheck_available(backend)
Check whether backend
is available by trying a scalar-to-scalar derivative.
Warning Might take a while due to compilation time.
sourceDifferentiationInterface.check_hessian
— Methodcheck_hessian(backend)
Check whether backend
supports second order differentiation by trying a hessian.
Warning Might take a while due to compilation time.
sourceDifferentiationInterface.check_mutation
— Methodcheck_mutation(backend)
Check whether backend
supports differentiation of mutating functions by trying a jacobian.
Warning Might take a while due to compilation time.
sourcePreparation
DifferentiationInterface.prepare_derivative
— Methodprepare_derivative(f, backend, x) -> extras
+prepare_derivative(f!, backend, y, x) -> extras
Create an extras
object that can be given to derivative operators.
sourceDifferentiationInterface.prepare_gradient
— Methodprepare_gradient(f, backend, x) -> extras
Create an extras
object that can be given to gradient operators.
sourceDifferentiationInterface.prepare_hessian
— Methodprepare_hessian(f, backend, x) -> extras
Create an extras
object that can be given to Hessian operators.
sourceDifferentiationInterface.prepare_hvp
— Methodprepare_hvp(f, backend, x) -> extras
Create an extras
object that can be given to Hessian-vector product operators.
sourceDifferentiationInterface.prepare_jacobian
— Methodprepare_jacobian(f, backend, x) -> extras
+prepare_jacobian(f!, backend, y, x) -> extras
Create an extras
object that can be given to Jacobian operators.
sourceDifferentiationInterface.prepare_pullback
— Methodprepare_pullback(f, backend, x) -> extras
+prepare_pullback(f!, backend, y, x) -> extras
Create an extras
object that can be given to pullback operators.
sourceDifferentiationInterface.prepare_pushforward
— Methodprepare_pushforward(f, backend, x) -> extras
+prepare_pushforward(f!, backend, y, x) -> extras
Create an extras
object that can be given to pushforward operators.
sourceDifferentiationInterface.prepare_second_derivative
— Methodprepare_second_derivative(f, backend, x) -> extras
+prepare_second_derivative(f!, backend, y, x) -> extras
Create an extras
object that can be given to second derivative operators.
sourceTesting & benchmarking
DifferentiationInterface.DifferentiationTest
— ModuleDifferentiationInterface.DifferentiationTest
Testing utilities for DifferentiationInterface
.
sourceDifferentiationInterface.DifferentiationTest.BenchmarkData
— TypeBenchmarkData
Ad-hoc storage type for differentiation benchmarking results. You can turn it into a DataFrame
as follows:
df = DataFrames.DataFrame(pairs(benchmark_data)...)
Fields
These are not part of the public API.
backend::Vector{String}
mode::Vector{Type}
operator::Vector{Function}
variant::Vector{Function}
func::Vector{String}
mutating::Vector{Bool}
input_type::Vector{Type}
output_type::Vector{Type}
input_size::Vector
output_size::Vector
samples::Vector{Int64}
time::Vector{Float64}
bytes::Vector{Float64}
allocs::Vector{Float64}
compile_fraction::Vector{Float64}
gc_fraction::Vector{Float64}
evals::Vector{Float64}
sourceDifferentiationInterface.DifferentiationTest.Scenario
— TypeScenario{mutating}
Store a testing scenario composed of a function and its input + output + tangents.
Fields
f::Any
: function
x::Any
: input
y::Any
: output
dx::Any
: pushforward seed
dy::Any
: pullback seed
sourceDifferentiationInterface.DifferentiationTest.all_operators
— Methodall_operators()
List all operators that can be tested with test_differentiation
.
sourceDifferentiationInterface.DifferentiationTest.default_scenarios
— Methoddefault_scenarios()
Create a vector of Scenario
s for testing differentiation.
sourceDifferentiationInterface.DifferentiationTest.test_differentiation
— Functiontest_differentiation(backends, [operators, scenarios]; [kwargs...])
Cross-test a list of backends
for a list of operators
on a list of scenarios
, running a variety of different tests.
- If
benchmark
is false
, this returns a TestSet
object. - If
benchmark
is true
, this returns a BenchmarkData
object, which is easy to turn into a DataFrame
.
Default arguments
operators::Vector{Function}
: the list [pushforward, pullback,derivative, gradient, jacobian, second_derivative, hvp, hessian]
scenarios::Vector{Scenario}
: the output of default_scenarios()
Keyword arguments
Testing:
correctness=true
: whether to compare the differentiation results with those given by ForwardDiff.jlerror_free=false
: whether to run it once and see if it errorscall_count=false
: whether to check that the function is called the right number of timestype_stability=false
: whether to check type stability with JET.jl (@test_opt
)benchmark=false
: whether to run and return a benchmark suite with Chairmarks.jlallocations=false
: whether to check that the benchmarks are allocation-freedetailed=false
: whether to print a detailed test set (by scenario) or condensed test set (by operator)
Filtering:
input_type=Any
: restrict scenario inputs to subtypes of thisoutput_type=Any
: restrict scenario outputs to subtypes of thisfirst_order=true
: consider first order operatorssecond_order=true
: consider second order operatorsallocating=true
: consider operators for allocating functionsmutating=true
: consider operators for mutating functionsexcluded=Symbol[]
: list of excluded operators
sourceDifferentiationInterface.DifferentiationTest.test_differentiation
— Methodtest_differentiation(
backend::ADTypes.AbstractADType,
args...;
kwargs...
) -> Union{Test.FallbackTestSet, BenchmarkData, Test.DefaultTestSet}
-
Shortcut for a single backend.
sourceDifferentiationInterface.DifferentiationTest.weird_array_scenarios
— Methodweird_array_scenarios()
Create a vector of Scenario
s involving weird types for testing differentiation.
sourceInternals
This is not part of the public API.
DifferentiationInterface.basisarray
— Methodbasisarray(backend, a::AbstractArray, i::CartesianIndex)
Construct the i
-th stardard basis array in the vector space of a
with element type eltype(a)
.
Note
If an AD backend benefits from a more specialized basis array implementation, this function can be extended on the backend type.
sourceDifferentiationInterface.mode
— Methodmode(backend)
Return the AD mode of a backend in a statically predictable way.
The return value is a Type
object chosen among:
ADTypes.AbstractForwardMode
ADTypes.AbstractFiniteDifferencesMode
ADTypes.AbstractReverseMode
ADTypes.AbstractSymbolicDifferentiationMode
This function exists because there are backends (like Enzyme) that can support both forward and reverse mode, which means their ADTypes.jl object does not subtype either.
sourceDifferentiationInterface.supports_mutation
— Methodsupports_mutation(backend)
Return MutationSupported
or MutationNotSupported
in a statically predictable way.
sourceDifferentiationInterface.supports_pullback
— Methodsupports_pullback(backend)
Return PullbackSupported
or PullbackNotSupported
in a statically predictable way.
sourceDifferentiationInterface.supports_pushforward
— Methodsupports_pushforward(backend)
Return PushforwardSupported
or PushforwardNotSupported
in a statically predictable way.
sourceDifferentiationInterface.ForwardOverForward
— TypeForwardOverForward
Traits identifying second-order backends that compute HVPs in forward over forward mode (inefficient).
sourceDifferentiationInterface.ForwardOverReverse
— TypeForwardOverReverse
Traits identifying second-order backends that compute HVPs in forward over reverse mode.
sourceDifferentiationInterface.MutationNotSupported
— TypeMutationNotSupported
Trait identifying backends that do not support mutating functions f!(y, x)
.
sourceDifferentiationInterface.MutationSupported
— TypeMutationSupported
Trait identifying backends that support mutating functions f!(y, x)
.
sourceDifferentiationInterface.PullbackNotSupported
— TypePullbackNotSupported
Trait identifying backends that do not support efficient pullbacks.
sourceDifferentiationInterface.PullbackSupported
— TypePullbackSupported
Trait identifying backends that support efficient pullbacks.
sourceDifferentiationInterface.PushforwardNotSupported
— TypePushforwardNotSupported
Trait identifying backends that do not support efficient pushforwards.
sourceDifferentiationInterface.PushforwardSupported
— TypePushforwardSupported
Trait identifying backends that support efficient pushforwards.
sourceDifferentiationInterface.ReverseOverForward
— TypeReverseOverForward
Traits identifying second-order backends that compute HVPs in reverse over forward mode.
sourceDifferentiationInterface.ReverseOverReverse
— TypeReverseOverReverse
Traits identifying second-order backends that compute HVPs in reverse over reverse mode.
sourceDifferentiationInterface.DifferentiationTest.AutoZeroForward
— TypeAutoZeroForward <: ADTypes.AbstractForwardMode
Trivial backend that sets all derivatives to zero. Used in testing and benchmarking.
sourceDifferentiationInterface.DifferentiationTest.AutoZeroReverse
— TypeAutoZeroReverse <: ADTypes.AbstractReverseMode
Trivial backend that sets all derivatives to zero. Used in testing and benchmarking.
sourceDifferentiationInterface.DifferentiationTest.backend_string
— Methodbackend_string(backend)
Return a shorter string than the full object printing from ADTypes.jl. Might be ambiguous.
sourceSettings
This document was generated with Documenter.jl version 1.3.0 on Sunday 24 March 2024. Using Julia version 1.10.2.
+
Settings
This document was generated with Documenter.jl version 1.3.0 on Sunday 24 March 2024. Using Julia version 1.10.2.