API reference
DifferentiationInterface.DifferentiationInterface
— ModuleDifferentiationInterface
An interface to various automatic differentiation backends in Julia.
Exports
AutoFastDifferentiation
SecondOrder
available
derivative
gradient
gradient!
gradient_and_hessian_vector_product
gradient_and_hessian_vector_product!
hessian
hessian!
hessian_vector_product
hessian_vector_product!
jacobian
jacobian!
multiderivative
multiderivative!
prepare_derivative
prepare_gradient
prepare_hessian
prepare_hessian_vector_product
prepare_jacobian
prepare_multiderivative
prepare_pullback
prepare_pushforward
prepare_second_derivative
pullback
pullback!
pushforward
pushforward!
second_derivative
supports_hessian
supports_mutation
value_and_derivative
value_and_gradient
value_and_gradient!
value_and_jacobian
value_and_jacobian!
value_and_multiderivative
value_and_multiderivative!
value_and_pullback
value_and_pullback!
value_and_pushforward
value_and_pushforward!
value_derivative_and_second_derivative
value_gradient_and_hessian
value_gradient_and_hessian!
Derivative
DifferentiationInterface.derivative
— Methodderivative(backend, f, x, [extras]) -> der
Compute the derivative der = f'(x)
of a scalar-to-scalar function.
DifferentiationInterface.value_and_derivative
— Methodvalue_and_derivative(backend, f, x, [extras]) -> (y, der)
Compute the primal value y = f(x)
and the derivative der = f'(x)
of a scalar-to-scalar function.
Multiderivative
DifferentiationInterface.multiderivative!
— Methodmultiderivative!(multider, backend, f, x, [extras]) -> multider
Compute the (array-valued) derivative multider = f'(x)
of a scalar-to-array function, overwriting multider
.
DifferentiationInterface.multiderivative
— Methodmultiderivative(backend, f, x, [extras]) -> multider
Compute the (array-valued) derivative multider = f'(x)
of a scalar-to-array function.
DifferentiationInterface.value_and_multiderivative!
— Methodvalue_and_multiderivative!(multider, backend, f, x, [extras]) -> (y, multider)
-value_and_multiderivative!(y, multider, backend, f!, x, [extras]) -> (y, multider)
Compute the primal value y = f(x)
and the (array-valued) derivative multider = f'(x)
of a scalar-to-array function, overwriting multider
.
DifferentiationInterface.value_and_multiderivative
— Methodvalue_and_multiderivative(backend, f, x, [extras]) -> (y, multider)
Compute the primal value y = f(x)
and the (array-valued) derivative multider = f'(x)
of a scalar-to-array function.
Gradient
DifferentiationInterface.gradient!
— Methodgradient!(grad, backend, f, x, [extras]) -> grad
Compute the gradient grad = ∇f(x)
of an array-to-scalar function, overwriting grad
.
DifferentiationInterface.gradient
— Methodgradient(backend, f, x, [extras]) -> grad
Compute the gradient grad = ∇f(x)
of an array-to-scalar function.
DifferentiationInterface.value_and_gradient!
— Methodvalue_and_gradient!(grad, backend, f, x, [extras]) -> (y, grad)
Compute the primal value y = f(x)
and the gradient grad = ∇f(x)
of an array-to-scalar function, overwriting grad
.
DifferentiationInterface.value_and_gradient
— Methodvalue_and_gradient(backend, f, x, [extras]) -> (y, grad)
Compute the primal value y = f(x)
and the gradient grad = ∇f(x)
of an array-to-scalar function.
Jacobian
DifferentiationInterface.jacobian!
— Methodjacobian!(jac, backend, f, x, [extras]) -> jac
Compute the Jacobian matrix jac = ∂f(x)
of an array-to-array function, overwriting jac
.
Notes
Regardless of the shape of x
and y
, if x
has length n
and y
has length m
, then jac
is expected to be a m × n
matrix. This function acts as if the input and output had been flattened with vec
.
DifferentiationInterface.jacobian
— Methodjacobian(backend, f, x, [extras]) -> jac
Compute the Jacobian matrix jac = ∂f(x)
of an array-to-array function.
Notes
Regardless of the shape of x
and y
, if x
has length n
and y
has length m
, then jac
is expected to be a m × n
matrix. This function acts as if the input and output had been flattened with vec
.
DifferentiationInterface.value_and_jacobian!
— Methodvalue_and_jacobian!(jac, backend, f, x, [extras]) -> (y, jac)
-value_and_jacobian!(y, jac, backend, f!, x, [extras]) -> (y, jac)
Compute the primal value y = f(x)
and the Jacobian matrix jac = ∂f(x)
of an array-to-array function, overwriting jac
.
Notes
Regardless of the shape of x
and y
, if x
has length n
and y
has length m
, then jac
is expected to be a m × n
matrix. This function acts as if the input and output had been flattened with vec
.
DifferentiationInterface.value_and_jacobian
— Methodvalue_and_jacobian(backend, f, x, [extras]) -> (y, jac)
Compute the primal value y = f(x)
and the Jacobian matrix jac = ∂f(x)
of an array-to-array function.
Notes
Regardless of the shape of x
and y
, if x
has length n
and y
has length m
, then jac
is expected to be a m × n
matrix. This function acts as if the input and output had been flattened with vec
.
Second derivative
DifferentiationInterface.second_derivative
— Methodsecond_derivative(backend, f, x, [extras]) -> derder
Compute the second derivative derder = f''(x)
of a scalar-to-scalar function.
DifferentiationInterface.value_derivative_and_second_derivative
— Methodvalue_derivative_and_second_derivative(backend, f, x, [extras]) -> (y, der, derder)
Compute the primal value y = f(x)
, the derivative der = f'(x)
and the second derivative derder = f''(x)
of a scalar-to-scalar function.
Hessian
DifferentiationInterface.hessian!
— Methodhessian!(hess, backend, f, x, [extras]) -> hess
Compute the Hessian hess = ∇²f(x)
of an array-to-scalar function, overwriting hess
.
Notes
Regardless of the shape of x
, if x
has length n
, then hess
is expected to be a n × n
matrix. This function acts as if the input had been flattened with vec
.
DifferentiationInterface.hessian
— Methodhessian(backend, f, x, [extras]) -> hess
Compute the Hessian hess = ∇²f(x)
of an array-to-scalar function.
Notes
Regardless of the shape of x
, if x
has length n
, then hess
is expected to be a n × n
matrix. This function acts as if the input had been flattened with vec
.
DifferentiationInterface.value_gradient_and_hessian!
— Methodvalue_gradient_and_hessian!(grad, hess, backend, f, x, [extras]) -> (y, grad, hess)
Compute the primal value y = f(x)
, the gradient grad = ∇f(x)
and the Hessian hess = ∇²f(x)
of an array-to-scalar function, overwriting grad
and hess
.
Notes
Regardless of the shape of x
, if x
has length n
, then hess
is expected to be a n × n
matrix. This function acts as if the input had been flattened with vec
.
DifferentiationInterface.value_gradient_and_hessian
— Methodvalue_gradient_and_hessian(backend, f, x, [extras]) -> (y, grad, hess)
Compute the primal value y = f(x)
, the gradient grad = ∇f(x)
and the Hessian hess = ∇²f(x)
of an array-to-scalar function, overwriting grad
and hess
.
Notes
Regardless of the shape of x
, if x
has length n
, then hess
is expected to be a n × n
matrix. This function acts as if the input had been flattened with vec
.
Pushforward (JVP)
DifferentiationInterface.pushforward!
— Methodpushforward!(dy, backend, f, x, dx, [extras]) -> dy
Compute the Jacobian-vector product dy = ∂f(x) * dx
, overwriting dy
if possible.
DifferentiationInterface.pushforward
— Methodpushforward(backend, f, x, dx, [extras]) -> dy
Compute the Jacobian-vector product dy = ∂f(x) * dx
.
DifferentiationInterface.value_and_pushforward!
— Methodvalue_and_pushforward!(dy, backend, f, x, dx, [extras]) -> (y, dy)
-value_and_pushforward!(y, dy, backend, f!, x, dx, [extras]) -> (y, dy)
Compute the primal value y = f(x)
and the Jacobian-vector product dy = ∂f(x) * dx
, overwriting dy
if possible.
This is the only required implementation for a forward mode backend.
DifferentiationInterface.value_and_pushforward
— Methodvalue_and_pushforward(backend, f, x, dx, [extras]) -> (y, dy)
Compute the primal value y = f(x)
and the Jacobian-vector product dy = ∂f(x) * dx
.
Pullback (JVP)
DifferentiationInterface.pullback!
— Methodpullback!(dx, backend, f, x, dy, [extras]) -> dx
Compute the vector-Jacobian product dx = ∂f(x)' * dy
, overwriting dx
if possible.
DifferentiationInterface.pullback
— Methodpullback(backend, f, x, dy, [extras]) -> dx
Compute the vector-Jacobian product dx = ∂f(x)' * dy
.
DifferentiationInterface.value_and_pullback!
— Methodvalue_and_pullback!(dx, backend, f, x, dy, [extras]) -> (y, dx)
-value_and_pullback!(y, dx, backend, f!, x, dy, [extras]) -> (y, dx)
Compute the primal value y = f(x)
and the vector-Jacobian product dx = ∂f(x)' * dy
, overwriting dx
if possible.
This is the only required implementation for a reverse mode backend.
DifferentiationInterface.value_and_pullback
— Methodvalue_and_pullback(backend, f, x, dy, [extras]) -> (y, dx)
Compute the primal value y = f(x)
and the vector-Jacobian product dx = ∂f(x)' * dy
.
Hessian-vector product (HVP)
DifferentiationInterface.gradient_and_hessian_vector_product!
— Methodgradient_and_hessian_vector_product!(grad, backend, hvp, backend, f, x, v, [extras]) -> (grad, hvp)
Compute the gradient grad = ∇f(x)
and the Hessian-vector product hvp = ∇²f(x) * v
of an array-to-scalar function, overwriting grad
and hvp
.
DifferentiationInterface.gradient_and_hessian_vector_product
— Methodgradient_and_hessian_vector_product(backend, f, x, v, [extras]) -> (grad, hvp)
Compute the gradient grad = ∇f(x)
and the Hessian-vector product hvp = ∇²f(x) * v
of an array-to-scalar function.
DifferentiationInterface.hessian_vector_product!
— Methodhessian_vector_product!(hvp, backend, f, x, v, [extras]) -> hvp
Compute the Hessian-vector product hvp = ∇²f(x) * v
of an array-to-scalar function, overwriting hvp
.
DifferentiationInterface.hessian_vector_product
— Methodhessian_vector_product(backend, f, x, v, [extras]) -> hvp
Compute the Hessian-vector product hvp = ∇²f(x) * v
of an array-to-scalar function.
Preparation
DifferentiationInterface.prepare_derivative
— Methodprepare_derivative(backend, f, x) -> extras
Create an extras
object that can be given to derivative operators.
DifferentiationInterface.prepare_gradient
— Methodprepare_gradient(backend, f, x) -> extras
Create an extras
object that can be given to gradient operators.
DifferentiationInterface.prepare_hessian
— Methodprepare_hessian(backend, f, x) -> extras
Create an extras
object that can be given to Hessian operators.
DifferentiationInterface.prepare_hessian_vector_product
— Methodprepare_hessian_vector_product(backend, f, x) -> extras
Create an extras
object that can be given to Hessian-vector product operators.
DifferentiationInterface.prepare_jacobian
— Methodprepare_jacobian(backend, f, x) -> extras
-prepare_jacobian(backend, f!, x, y) -> extras
Create an extras
object that can be given to Jacobian operators.
DifferentiationInterface.prepare_multiderivative
— Methodprepare_multiderivative(backend, f, x) -> extras
-prepare_multiderivative(backend, f!, x, y) -> extras
Create an extras
object that can be given to multiderivative operators.
DifferentiationInterface.prepare_pullback
— Methodprepare_pullback(backend, f, x) -> extras
-prepare_pullback(backend, f!, x, y) -> extras
Create an extras
object that can be given to pullback operators.
DifferentiationInterface.prepare_pushforward
— Methodprepare_pushforward(backend, f, x) -> extras
-prepare_pushforward(backend, f!, x, y) -> extras
Create an extras
object that can be given to pushforward operators.
DifferentiationInterface.prepare_second_derivative
— Methodprepare_second_derivative(backend, f, x) -> extras
Create an extras
object that can be given to second derivative operators.
Backend queries
DifferentiationInterface.available
— Methodavailable(backend)
Check whether backend
is available by trying a scalar-to-scalar derivative. Might take a while due to compilation time.
DifferentiationInterface.supports_hessian
— Methodsupports_hessian(backend)
Check whether backend
supports second order differentiation by trying a hessian. Might take a while due to compilation time.
DifferentiationInterface.supports_mutation
— Methodsupports_mutation(backend)
Check whether backend
supports differentiation of mutating functions by trying a jacobian. Might take a while due to compilation time.
Internals
This is not part of the public API.
DifferentiationInterface.AutoZeroForward
— TypeAutoZeroForward <: ADTypes.AbstractForwardMode
Trivial backend that sets all derivatives to zero. Used in testing and benchmarking.
DifferentiationInterface.AutoZeroReverse
— TypeAutoZeroReverse <: ADTypes.AbstractReverseMode
Trivial backend that sets all derivatives to zero. Used in testing and benchmarking.
DifferentiationInterface.ForwardMode
— TypeForwardMode
Trait identifying forward mode (and finite differences) first-order AD backends.
DifferentiationInterface.MutationNotSupported
— TypeMutationNotSupported
Trait identifying backends that do not support mutating functions f!(y, x)
.
DifferentiationInterface.MutationSupported
— TypeMutationSupported
Trait identifying backends that support mutating functions f!(y, x)
.
DifferentiationInterface.ReverseMode
— TypeReverseMode
Trait identifying reverse mode first-order AD backends.
DifferentiationInterface.SymbolicMode
— TypeSymbolicMode
Trait identifying symbolic first-order AD backends. Their fallback structure is different from the rest.
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.
DifferentiationInterface.mode
— Functionmode(backend)
Return the AD mode of a backend in a statically predictable way.
This function must be overloaded for backends that support both forward and reverse.
We classify finite differences as a forward mode.
DifferentiationInterface.mutation_behavior
— Methodmutation_behavior(backend)
Return the mutation behavior of a backend in a statically predictable way.
Note
This is different from supports_mutation
, which performs an actual call to jacobian!
.
Testing
This is not part of the public API.
DifferentiationInterface.DifferentiationTest
— ModuleDifferentiationInterface.DifferentiationTest
Testing utilities for DifferentiationInterface
.
DifferentiationInterface.DifferentiationTest.BenchmarkData
— TypeBenchmarkData
Fields
backend::Vector
operator::Vector
func::Vector
mutating::Vector
input_type::Vector
output_type::Vector
input_size::Vector
output_size::Vector
samples::Vector
time::Vector
bytes::Vector
allocs::Vector
compile_fraction::Vector
gc_fraction::Vector
evals::Vector
DifferentiationInterface.DifferentiationTest.Scenario
— TypeScenario
Store a testing scenario composed of a function and its input + output + tangents.
Fields
f::Any
: functionmutating::Bool
: mutationx::Union{Number, AbstractArray}
: inputy::Union{Number, AbstractArray}
: outputdx::Union{Number, AbstractArray}
: pushforward seeddy::Union{Number, AbstractArray}
: pullback seed
DifferentiationInterface.DifferentiationTest.backend_string
— Methodbackend_string(backend)
Return a shorter string than the full object printing from ADTypes.jl. Might be ambiguous.
DifferentiationInterface.DifferentiationTest.test_operators
— Functiontest_operators(
+API reference · DifferentiationInterface.jl API reference
DifferentiationInterface.DifferentiationInterface
— ModuleDifferentiationInterface
An interface to various automatic differentiation backends in Julia.
Exports
AutoFastDifferentiation
SecondOrder
available
derivative
gradient
gradient!
gradient_and_hessian_vector_product
gradient_and_hessian_vector_product!
hessian
hessian!
hessian_vector_product
hessian_vector_product!
jacobian
jacobian!
multiderivative
multiderivative!
prepare_derivative
prepare_gradient
prepare_hessian
prepare_hessian_vector_product
prepare_jacobian
prepare_multiderivative
prepare_pullback
prepare_pushforward
prepare_second_derivative
pullback
pullback!
pushforward
pushforward!
second_derivative
supports_hessian
supports_mutation
value_and_derivative
value_and_gradient
value_and_gradient!
value_and_jacobian
value_and_jacobian!
value_and_multiderivative
value_and_multiderivative!
value_and_pullback
value_and_pullback!
value_and_pushforward
value_and_pushforward!
value_derivative_and_second_derivative
value_gradient_and_hessian
value_gradient_and_hessian!
sourceDerivative
DifferentiationInterface.derivative
— Methodderivative(backend, f, x, [extras]) -> der
Compute the derivative der = f'(x)
of a scalar-to-scalar function.
sourceDifferentiationInterface.value_and_derivative
— Methodvalue_and_derivative(backend, f, x, [extras]) -> (y, der)
Compute the primal value y = f(x)
and the derivative der = f'(x)
of a scalar-to-scalar function.
sourceMultiderivative
DifferentiationInterface.multiderivative!
— Methodmultiderivative!(multider, backend, f, x, [extras]) -> multider
Compute the (array-valued) derivative multider = f'(x)
of a scalar-to-array function, overwriting multider
.
sourceDifferentiationInterface.multiderivative
— Methodmultiderivative(backend, f, x, [extras]) -> multider
Compute the (array-valued) derivative multider = f'(x)
of a scalar-to-array function.
sourceDifferentiationInterface.value_and_multiderivative!
— Methodvalue_and_multiderivative!(multider, backend, f, x, [extras]) -> (y, multider)
+value_and_multiderivative!(y, multider, backend, f!, x, [extras]) -> (y, multider)
Compute the primal value y = f(x)
and the (array-valued) derivative multider = f'(x)
of a scalar-to-array function, overwriting multider
.
sourceDifferentiationInterface.value_and_multiderivative
— Methodvalue_and_multiderivative(backend, f, x, [extras]) -> (y, multider)
Compute the primal value y = f(x)
and the (array-valued) derivative multider = f'(x)
of a scalar-to-array function.
sourceGradient
DifferentiationInterface.gradient!
— Methodgradient!(grad, backend, f, x, [extras]) -> grad
Compute the gradient grad = ∇f(x)
of an array-to-scalar function, overwriting grad
.
sourceDifferentiationInterface.gradient
— Methodgradient(backend, f, x, [extras]) -> grad
Compute the gradient grad = ∇f(x)
of an array-to-scalar function.
sourceDifferentiationInterface.value_and_gradient!
— Methodvalue_and_gradient!(grad, backend, f, x, [extras]) -> (y, grad)
Compute the primal value y = f(x)
and the gradient grad = ∇f(x)
of an array-to-scalar function, overwriting grad
.
sourceDifferentiationInterface.value_and_gradient
— Methodvalue_and_gradient(backend, f, x, [extras]) -> (y, grad)
Compute the primal value y = f(x)
and the gradient grad = ∇f(x)
of an array-to-scalar function.
sourceJacobian
DifferentiationInterface.jacobian!
— Methodjacobian!(jac, backend, f, x, [extras]) -> jac
Compute the Jacobian matrix jac = ∂f(x)
of an array-to-array function, overwriting jac
.
Notes
Regardless of the shape of x
and y
, if x
has length n
and y
has length m
, then jac
is expected to be a m × n
matrix. This function acts as if the input and output had been flattened with vec
.
sourceDifferentiationInterface.jacobian
— Methodjacobian(backend, f, x, [extras]) -> jac
Compute the Jacobian matrix jac = ∂f(x)
of an array-to-array function.
Notes
Regardless of the shape of x
and y
, if x
has length n
and y
has length m
, then jac
is expected to be a m × n
matrix. This function acts as if the input and output had been flattened with vec
.
sourceDifferentiationInterface.value_and_jacobian!
— Methodvalue_and_jacobian!(jac, backend, f, x, [extras]) -> (y, jac)
+value_and_jacobian!(y, jac, backend, f!, x, [extras]) -> (y, jac)
Compute the primal value y = f(x)
and the Jacobian matrix jac = ∂f(x)
of an array-to-array function, overwriting jac
.
Notes
Regardless of the shape of x
and y
, if x
has length n
and y
has length m
, then jac
is expected to be a m × n
matrix. This function acts as if the input and output had been flattened with vec
.
sourceDifferentiationInterface.value_and_jacobian
— Methodvalue_and_jacobian(backend, f, x, [extras]) -> (y, jac)
Compute the primal value y = f(x)
and the Jacobian matrix jac = ∂f(x)
of an array-to-array function.
Notes
Regardless of the shape of x
and y
, if x
has length n
and y
has length m
, then jac
is expected to be a m × n
matrix. This function acts as if the input and output had been flattened with vec
.
sourceSecond derivative
DifferentiationInterface.second_derivative
— Methodsecond_derivative(backend, f, x, [extras]) -> derder
Compute the second derivative derder = f''(x)
of a scalar-to-scalar function.
sourceDifferentiationInterface.value_derivative_and_second_derivative
— Methodvalue_derivative_and_second_derivative(backend, f, x, [extras]) -> (y, der, derder)
Compute the primal value y = f(x)
, the derivative der = f'(x)
and the second derivative derder = f''(x)
of a scalar-to-scalar function.
sourceHessian
DifferentiationInterface.hessian!
— Methodhessian!(hess, backend, f, x, [extras]) -> hess
Compute the Hessian hess = ∇²f(x)
of an array-to-scalar function, overwriting hess
.
Notes
Regardless of the shape of x
, if x
has length n
, then hess
is expected to be a n × n
matrix. This function acts as if the input had been flattened with vec
.
sourceDifferentiationInterface.hessian
— Methodhessian(backend, f, x, [extras]) -> hess
Compute the Hessian hess = ∇²f(x)
of an array-to-scalar function.
Notes
Regardless of the shape of x
, if x
has length n
, then hess
is expected to be a n × n
matrix. This function acts as if the input had been flattened with vec
.
sourceDifferentiationInterface.value_gradient_and_hessian!
— Methodvalue_gradient_and_hessian!(grad, hess, backend, f, x, [extras]) -> (y, grad, hess)
Compute the primal value y = f(x)
, the gradient grad = ∇f(x)
and the Hessian hess = ∇²f(x)
of an array-to-scalar function, overwriting grad
and hess
.
Notes
Regardless of the shape of x
, if x
has length n
, then hess
is expected to be a n × n
matrix. This function acts as if the input had been flattened with vec
.
sourceDifferentiationInterface.value_gradient_and_hessian
— Methodvalue_gradient_and_hessian(backend, f, x, [extras]) -> (y, grad, hess)
Compute the primal value y = f(x)
, the gradient grad = ∇f(x)
and the Hessian hess = ∇²f(x)
of an array-to-scalar function, overwriting grad
and hess
.
Notes
Regardless of the shape of x
, if x
has length n
, then hess
is expected to be a n × n
matrix. This function acts as if the input had been flattened with vec
.
sourcePushforward (JVP)
DifferentiationInterface.pushforward!
— Methodpushforward!(dy, backend, f, x, dx, [extras]) -> dy
Compute the Jacobian-vector product dy = ∂f(x) * dx
, overwriting dy
if possible.
sourceDifferentiationInterface.pushforward
— Methodpushforward(backend, f, x, dx, [extras]) -> dy
Compute the Jacobian-vector product dy = ∂f(x) * dx
.
sourceDifferentiationInterface.value_and_pushforward!
— Methodvalue_and_pushforward!(dy, backend, f, x, dx, [extras]) -> (y, dy)
+value_and_pushforward!(y, dy, backend, f!, x, dx, [extras]) -> (y, dy)
Compute the primal value y = f(x)
and the Jacobian-vector product dy = ∂f(x) * dx
, overwriting dy
if possible.
Interface requirement This is the only required implementation for a forward mode backend.
sourceDifferentiationInterface.value_and_pushforward
— Methodvalue_and_pushforward(backend, f, x, dx, [extras]) -> (y, dy)
Compute the primal value y = f(x)
and the Jacobian-vector product dy = ∂f(x) * dx
.
sourcePullback (JVP)
DifferentiationInterface.pullback!
— Methodpullback!(dx, backend, f, x, dy, [extras]) -> dx
Compute the vector-Jacobian product dx = ∂f(x)' * dy
, overwriting dx
if possible.
sourceDifferentiationInterface.pullback
— Methodpullback(backend, f, x, dy, [extras]) -> dx
Compute the vector-Jacobian product dx = ∂f(x)' * dy
.
sourceDifferentiationInterface.value_and_pullback!
— Methodvalue_and_pullback!(dx, backend, f, x, dy, [extras]) -> (y, dx)
+value_and_pullback!(y, dx, backend, f!, x, dy, [extras]) -> (y, dx)
Compute the primal value y = f(x)
and the vector-Jacobian product dx = ∂f(x)' * dy
, overwriting dx
if possible.
Interface requirement This is the only required implementation for a reverse mode backend.
sourceDifferentiationInterface.value_and_pullback
— Methodvalue_and_pullback(backend, f, x, dy, [extras]) -> (y, dx)
Compute the primal value y = f(x)
and the vector-Jacobian product dx = ∂f(x)' * dy
.
sourceHessian-vector product (HVP)
DifferentiationInterface.gradient_and_hessian_vector_product!
— Methodgradient_and_hessian_vector_product!(grad, backend, hvp, backend, f, x, v, [extras]) -> (grad, hvp)
Compute the gradient grad = ∇f(x)
and the Hessian-vector product hvp = ∇²f(x) * v
of an array-to-scalar function, overwriting grad
and hvp
.
sourceDifferentiationInterface.gradient_and_hessian_vector_product
— Methodgradient_and_hessian_vector_product(backend, f, x, v, [extras]) -> (grad, hvp)
Compute the gradient grad = ∇f(x)
and the Hessian-vector product hvp = ∇²f(x) * v
of an array-to-scalar function.
sourceDifferentiationInterface.hessian_vector_product!
— Methodhessian_vector_product!(hvp, backend, f, x, v, [extras]) -> hvp
Compute the Hessian-vector product hvp = ∇²f(x) * v
of an array-to-scalar function, overwriting hvp
.
sourceDifferentiationInterface.hessian_vector_product
— Methodhessian_vector_product(backend, f, x, v, [extras]) -> hvp
Compute the Hessian-vector product hvp = ∇²f(x) * v
of an array-to-scalar function.
sourcePreparation
DifferentiationInterface.prepare_derivative
— Methodprepare_derivative(backend, f, x) -> extras
Create an extras
object that can be given to derivative operators.
sourceDifferentiationInterface.prepare_gradient
— Methodprepare_gradient(backend, f, x) -> extras
Create an extras
object that can be given to gradient operators.
sourceDifferentiationInterface.prepare_hessian
— Methodprepare_hessian(backend, f, x) -> extras
Create an extras
object that can be given to Hessian operators.
sourceDifferentiationInterface.prepare_hessian_vector_product
— Methodprepare_hessian_vector_product(backend, f, x) -> extras
Create an extras
object that can be given to Hessian-vector product operators.
sourceDifferentiationInterface.prepare_jacobian
— Methodprepare_jacobian(backend, f, x) -> extras
+prepare_jacobian(backend, f!, x, y) -> extras
Create an extras
object that can be given to Jacobian operators.
sourceDifferentiationInterface.prepare_multiderivative
— Methodprepare_multiderivative(backend, f, x) -> extras
+prepare_multiderivative(backend, f!, x, y) -> extras
Create an extras
object that can be given to multiderivative operators.
sourceDifferentiationInterface.prepare_pullback
— Methodprepare_pullback(backend, f, x) -> extras
+prepare_pullback(backend, f!, x, y) -> extras
Create an extras
object that can be given to pullback operators.
sourceDifferentiationInterface.prepare_pushforward
— Methodprepare_pushforward(backend, f, x) -> extras
+prepare_pushforward(backend, f!, x, y) -> extras
Create an extras
object that can be given to pushforward operators.
sourceDifferentiationInterface.prepare_second_derivative
— Methodprepare_second_derivative(backend, f, x) -> extras
Create an extras
object that can be given to second derivative operators.
sourceBackend queries
DifferentiationInterface.available
— Methodavailable(backend)
Check whether backend
is available by trying a scalar-to-scalar derivative. Might take a while due to compilation time.
sourceDifferentiationInterface.supports_hessian
— Methodsupports_hessian(backend)
Check whether backend
supports second order differentiation by trying a hessian. Might take a while due to compilation time.
sourceDifferentiationInterface.supports_mutation
— Methodsupports_mutation(backend)
Check whether backend
supports differentiation of mutating functions by trying a jacobian. Might take a while due to compilation time.
sourceInternals
This is not part of the public API.
DifferentiationInterface.ForwardMode
— TypeForwardMode
Trait identifying forward mode (and finite differences) first-order AD backends.
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.ReverseMode
— TypeReverseMode
Trait identifying reverse mode first-order AD backends.
sourceDifferentiationInterface.SymbolicMode
— TypeSymbolicMode
Trait identifying symbolic first-order AD backends. Their fallback structure is different from the rest.
sourceDifferentiationInterface.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
— Functionmode(backend)
Return the AD mode of a backend in a statically predictable way.
This function must be overloaded for backends that support both forward and reverse.
We classify finite differences as a forward mode.
sourceDifferentiationInterface.mutation_behavior
— Methodmutation_behavior(backend)
Return the mutation behavior of a backend in a statically predictable way.
Note
This is different from supports_mutation
, which performs an actual call to jacobian!
.
sourceTesting
This is not part of the public API.
DifferentiationInterface.DifferentiationTest
— ModuleDifferentiationInterface.DifferentiationTest
Testing utilities for DifferentiationInterface
.
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.BenchmarkData
— TypeBenchmarkData
Fields
backend::Vector
operator::Vector
func::Vector
mutating::Vector
input_type::Vector
output_type::Vector
input_size::Vector
output_size::Vector
samples::Vector
time::Vector
bytes::Vector
allocs::Vector
compile_fraction::Vector
gc_fraction::Vector
evals::Vector
sourceDifferentiationInterface.DifferentiationTest.Scenario
— TypeScenario
Store a testing scenario composed of a function and its input + output + tangents.
Fields
f::Any
: function
mutating::Bool
: mutation
x::Union{Number, AbstractArray}
: input
y::Union{Number, AbstractArray}
: output
dx::Union{Number, AbstractArray}
: pushforward seed
dy::Union{Number, AbstractArray}
: pullback seed
sourceDifferentiationInterface.DifferentiationTest.backend_string
— Methodbackend_string(backend)
Return a shorter string than the full object printing from ADTypes.jl. Might be ambiguous.
sourceDifferentiationInterface.DifferentiationTest.test_operators
— Functiontest_operators(
backends, [operators, scenarios];
correctness, type_stability, benchmark, allocations,
input_type, output_type,
first_order, second_order, allocating, mutating,
excluded,
-)
Cross-test a list of backends
for a list of operators
on a list of scenarios.
Return nothing
, except when benchmark=true
.
Default arguments
operators
: defaults to all of themscenarios
: defaults to a set of default scenarios
Keyword arguments
correctness=true
: whether to compare the differentiation results with those given by ForwardDiff.jltype_stability=true
: whether to check type stability with JET.jlcall_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-freeinput_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_operators
— Methodtest_operators(
+)
Cross-test a list of backends
for a list of operators
on a list of scenarios.
Return nothing
, except when benchmark=true
.
Default arguments
operators
: defaults to all of themscenarios
: defaults to a set of default scenarios
Keyword arguments
correctness=true
: whether to compare the differentiation results with those given by ForwardDiff.jltype_stability=true
: whether to check type stability with JET.jlcall_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-freeinput_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_operators
— Methodtest_operators(
backend::ADTypes.AbstractADType,
args...;
kwargs...
) -> Union{Nothing, BenchmarkData}
-
Shortcut for a single backend.
sourceSettings
This document was generated with Documenter.jl version 1.3.0 on Wednesday 20 March 2024. Using Julia version 1.10.2.
+
Settings
This document was generated with Documenter.jl version 1.3.0 on Thursday 21 March 2024. Using Julia version 1.10.2.