API
Types and constants
EnzymeCore.ABI
EnzymeCore.Active
EnzymeCore.Annotation
EnzymeCore.BatchDuplicated
EnzymeCore.BatchDuplicatedNoNeed
EnzymeCore.Const
EnzymeCore.Duplicated
EnzymeCore.DuplicatedNoNeed
EnzymeCore.FFIABI
EnzymeCore.ForwardMode
EnzymeCore.InlineABI
EnzymeCore.Mode
EnzymeCore.ReverseMode
EnzymeCore.ReverseModeSplit
EnzymeCore.EnzymeRules.AugmentedReturn
EnzymeCore.EnzymeRules.Config
EnzymeTestUtils.ExprAndMsg
Functions and macros
Enzyme.gradient
Enzyme.gradient
Enzyme.gradient
Enzyme.gradient!
Enzyme.jacobian
Enzyme.jacobian
EnzymeCore.autodiff
EnzymeCore.autodiff
EnzymeCore.autodiff
EnzymeCore.autodiff
EnzymeCore.autodiff_deferred
EnzymeCore.autodiff_deferred
EnzymeCore.autodiff_deferred
EnzymeCore.autodiff_deferred_thunk
EnzymeCore.autodiff_thunk
EnzymeCore.autodiff_thunk
EnzymeCore.compiler_job_from_backend
EnzymeCore.make_zero
EnzymeCore.make_zero
EnzymeCore.EnzymeRules.augmented_primal
EnzymeCore.EnzymeRules.forward
EnzymeCore.EnzymeRules.inactive
EnzymeCore.EnzymeRules.inactive_noinl
EnzymeCore.EnzymeRules.inactive_type
EnzymeCore.EnzymeRules.reverse
EnzymeTestUtils.@test_msg
EnzymeTestUtils.are_activities_compatible
EnzymeTestUtils.test_forward
EnzymeTestUtils.test_reverse
Enzyme.API.runtimeActivity
Enzyme.API.runtimeActivity!
Documentation
Enzyme.gradient!
— Methodgradient!(::ReverseMode, dx, f, x)
Compute the gradient of an array-input function f
using reverse mode, storing the derivative result in an existing array dx
.
Example:
f(x) = x[1]*x[2]
+API · Enzyme.jl API
Types and constants
EnzymeCore.ABI
EnzymeCore.Active
EnzymeCore.Annotation
EnzymeCore.BatchDuplicated
EnzymeCore.BatchDuplicatedNoNeed
EnzymeCore.Const
EnzymeCore.Duplicated
EnzymeCore.DuplicatedNoNeed
EnzymeCore.FFIABI
EnzymeCore.ForwardMode
EnzymeCore.InlineABI
EnzymeCore.Mode
EnzymeCore.ReverseMode
EnzymeCore.ReverseModeSplit
EnzymeCore.EnzymeRules.AugmentedReturn
EnzymeCore.EnzymeRules.Config
EnzymeTestUtils.ExprAndMsg
Functions and macros
Enzyme.gradient
Enzyme.gradient
Enzyme.gradient
Enzyme.gradient!
Enzyme.jacobian
Enzyme.jacobian
EnzymeCore.autodiff
EnzymeCore.autodiff
EnzymeCore.autodiff
EnzymeCore.autodiff
EnzymeCore.autodiff_deferred
EnzymeCore.autodiff_deferred
EnzymeCore.autodiff_deferred
EnzymeCore.autodiff_deferred_thunk
EnzymeCore.autodiff_thunk
EnzymeCore.autodiff_thunk
EnzymeCore.compiler_job_from_backend
EnzymeCore.make_zero
EnzymeCore.make_zero
EnzymeCore.EnzymeRules.augmented_primal
EnzymeCore.EnzymeRules.forward
EnzymeCore.EnzymeRules.inactive
EnzymeCore.EnzymeRules.inactive_noinl
EnzymeCore.EnzymeRules.inactive_type
EnzymeCore.EnzymeRules.reverse
EnzymeTestUtils.@test_msg
EnzymeTestUtils.are_activities_compatible
EnzymeTestUtils.test_forward
EnzymeTestUtils.test_reverse
Enzyme.API.runtimeActivity
Enzyme.API.runtimeActivity!
Documentation
Enzyme.gradient!
— Methodgradient!(::ReverseMode, dx, f, x)
Compute the gradient of an array-input function f
using reverse mode, storing the derivative result in an existing array dx
.
Example:
f(x) = x[1]*x[2]
dx = [0.0, 0.0]
gradient!(Reverse, dx, f, [2.0, 3.0])
@@ -8,13 +8,13 @@
2-element Vector{Float64}:
3.0
- 2.0
sourceEnzyme.gradient
— Methodgradient(::ForwardMode, f, x; shadow=onehot(x))
Compute the gradient of an array-input function f
using forward mode. The optional keyword argument shadow
is a vector of one-hot vectors of type x
which are used to forward-propagate into the return. For performance reasons, this should be computed once, outside the call to gradient
, rather than within this call.
Example:
f(x) = x[1]*x[2]
+ 2.0
sourceEnzyme.gradient
— Methodgradient(::ForwardMode, f, x; shadow=onehot(x))
Compute the gradient of an array-input function f
using forward mode. The optional keyword argument shadow
is a vector of one-hot vectors of type x
which are used to forward-propagate into the return. For performance reasons, this should be computed once, outside the call to gradient
, rather than within this call.
Example:
f(x) = x[1]*x[2]
grad = gradient(Forward, f, [2.0, 3.0])
# output
-(3.0, 2.0)
sourceEnzyme.gradient
— Methodgradient(::ReverseMode, f, x)
Compute the gradient of an array-input function f
using reverse mode. This will allocate and return new array with the gradient result.
Example:
f(x) = x[1]*x[2]
+(3.0, 2.0)
sourceEnzyme.gradient
— Methodgradient(::ReverseMode, f, x)
Compute the gradient of an array-input function f
using reverse mode. This will allocate and return new array with the gradient result.
Example:
f(x) = x[1]*x[2]
grad = gradient(Reverse, f, [2.0, 3.0])
@@ -22,13 +22,13 @@
2-element Vector{Float64}:
3.0
- 2.0
sourceEnzyme.gradient
— Methodgradient(::ForwardMode, f, x, ::Val{chunk}; shadow=onehot(x))
Compute the gradient of an array-input function f
using vector forward mode. Like gradient
, except it uses a chunk size of chunk
to compute chunk
derivatives in a single call.
Example:
f(x) = x[1]*x[2]
+ 2.0
sourceEnzyme.gradient
— Methodgradient(::ForwardMode, f, x, ::Val{chunk}; shadow=onehot(x))
Compute the gradient of an array-input function f
using vector forward mode. Like gradient
, except it uses a chunk size of chunk
to compute chunk
derivatives in a single call.
Example:
f(x) = x[1]*x[2]
grad = gradient(Forward, f, [2.0, 3.0], Val(2))
# output
-(3.0, 2.0)
sourceEnzyme.jacobian
— Methodjacobian(::ForwardMode, f, x; shadow=onehot(x))
+(3.0, 2.0)
sourceEnzyme.jacobian
— Methodjacobian(::ForwardMode, f, x; shadow=onehot(x))
jacobian(::ForwardMode, f, x, ::Val{chunk}; shadow=onehot(x))
Compute the jacobian of an array-input function f
using (potentially vector) forward mode. This is a simple rename of the gradient
function, and all relevant arguments apply here.
Example:
f(x) = [x[1]*x[2], x[2]]
grad = jacobian(Forward, f, [2.0, 3.0])
@@ -37,7 +37,7 @@
2×2 Matrix{Float64}:
3.0 2.0
- 0.0 1.0
sourceEnzyme.jacobian
— Methodjacobian(::ReverseMode, f, x, ::Val{num_outs}, ::Val{chunk})
Compute the jacobian of an array-input function f
using (potentially vector) reverse mode. The chunk
argument denotes the chunk size to use and num_outs
denotes the number of outputs f
will return in an array.
Example:
f(x) = [x[1]*x[2], x[2]]
+ 0.0 1.0
sourceEnzyme.jacobian
— Methodjacobian(::ReverseMode, f, x, ::Val{num_outs}, ::Val{chunk})
Compute the jacobian of an array-input function f
using (potentially vector) reverse mode. The chunk
argument denotes the chunk size to use and num_outs
denotes the number of outputs f
will return in an array.
Example:
f(x) = [x[1]*x[2], x[2]]
grad = jacobian(Reverse, f, [2.0, 3.0], Val(2))
@@ -45,7 +45,7 @@
2×2 Matrix{Float64}:
3.0 2.0
- 0.0 1.0
sourceEnzymeCore.autodiff
— Methodautodiff(::ForwardMode, f, Activity, args::Vararg{Annotation, Nargs})
Auto-differentiate function f
at arguments args
using forward mode.
args
may be numbers, arrays, structs of numbers, structs of arrays and so on. Enzyme will only differentiate in respect to arguments that are wrapped in a Duplicated
or similar argument. Unlike reverse mode in autodiff
, Active
arguments are not allowed here, since all derivative results of immutable objects will be returned and should instead use Duplicated
or variants like DuplicatedNoNeed
.
Activity
is the Activity of the return value, it may be:
Const
if the return is not to be differentiated with respect toDuplicated
, if the return is being differentiated with respect to and both the original value and the derivative return are desiredDuplicatedNoNeed
, if the return is being differentiated with respect to and only the derivative return is desired.BatchDuplicated
, like Duplicated
, but computing multiple derivatives at once. All batch sizes must be the same for all arguments.BatchDuplicatedNoNeed
, like DuplicatedNoNeed
, but computing multiple derivatives at one. All batch sizes must be the same for all arguments.
Example returning both original return and derivative:
f(x) = x*x
+ 0.0 1.0
sourceEnzymeCore.autodiff
— Methodautodiff(::ForwardMode, f, Activity, args::Vararg{Annotation, Nargs})
Auto-differentiate function f
at arguments args
using forward mode.
args
may be numbers, arrays, structs of numbers, structs of arrays and so on. Enzyme will only differentiate in respect to arguments that are wrapped in a Duplicated
or similar argument. Unlike reverse mode in autodiff
, Active
arguments are not allowed here, since all derivative results of immutable objects will be returned and should instead use Duplicated
or variants like DuplicatedNoNeed
.
Activity
is the Activity of the return value, it may be:
Const
if the return is not to be differentiated with respect toDuplicated
, if the return is being differentiated with respect to and both the original value and the derivative return are desiredDuplicatedNoNeed
, if the return is being differentiated with respect to and only the derivative return is desired.BatchDuplicated
, like Duplicated
, but computing multiple derivatives at once. All batch sizes must be the same for all arguments.BatchDuplicatedNoNeed
, like DuplicatedNoNeed
, but computing multiple derivatives at one. All batch sizes must be the same for all arguments.
Example returning both original return and derivative:
f(x) = x*x
res, ∂f_∂x = autodiff(Forward, f, Duplicated, Duplicated(3.14, 1.0))
# output
@@ -55,7 +55,7 @@
# output
-(6.28,)
sourceEnzymeCore.autodiff
— Methodautodiff(::ReverseMode, f, Activity, args::Vararg{Annotation, Nargs})
Auto-differentiate function f
at arguments args
using reverse mode.
Limitations:
f
may only return a Real
(of a built-in/primitive type) or nothing
, not an array, struct, BigFloat
, etc. To handle vector-valued return types, use a mutating f!
that returns nothing
and stores it's return value in one of the arguments, which must be wrapped in a Duplicated
.
args
may be numbers, arrays, structs of numbers, structs of arrays and so on. Enzyme will only differentiate in respect to arguments that are wrapped in an Active
(for arguments whose derivative result must be returned rather than mutated in place, such as primitive types and structs thereof) or Duplicated
(for mutable arguments like arrays, Ref
s and structs thereof).
Activity
is the Activity of the return value, it may be Const
or Active
.
Example:
a = 4.2
+(6.28,)
sourceEnzymeCore.autodiff
— Methodautodiff(::ReverseMode, f, Activity, args::Vararg{Annotation, Nargs})
Auto-differentiate function f
at arguments args
using reverse mode.
Limitations:
f
may only return a Real
(of a built-in/primitive type) or nothing
, not an array, struct, BigFloat
, etc. To handle vector-valued return types, use a mutating f!
that returns nothing
and stores it's return value in one of the arguments, which must be wrapped in a Duplicated
.
args
may be numbers, arrays, structs of numbers, structs of arrays and so on. Enzyme will only differentiate in respect to arguments that are wrapped in an Active
(for arguments whose derivative result must be returned rather than mutated in place, such as primitive types and structs thereof) or Duplicated
(for mutable arguments like arrays, Ref
s and structs thereof).
Activity
is the Activity of the return value, it may be Const
or Active
.
Example:
a = 4.2
b = [2.2, 3.3]; ∂f_∂b = zero(b)
c = 55; d = 9
@@ -68,7 +68,7 @@
# output
-((6.0,), 9.0)
Note Enzyme gradients with respect to integer values are zero. Active
will automatically convert plain integers to floating point values, but cannot do so for integer values in tuples and structs.
sourceEnzymeCore.autodiff
— Methodautodiff(mode::Mode, f, args::Vararg{Annotation, Nargs})
Like autodiff
but will try to guess the activity of the return value.
sourceEnzymeCore.autodiff
— Methodautodiff(mode::Mode, f, ::Type{A}, args::Vararg{Annotation, Nargs})
Like autodiff
but will try to extend f to an annotation, if needed.
sourceEnzymeCore.autodiff_deferred
— Methodautodiff_deferred(::ForwardMode, f, Activity, args::Vararg{Annotation, Nargs})
Same as autodiff(::ForwardMode, f, Activity, args)
but uses deferred compilation to support usage in GPU code, as well as high-order differentiation.
sourceEnzymeCore.autodiff_deferred
— Methodautodiff_deferred(mode::Mode, f, ::Type{A}, args::Vararg{Annotation, Nargs})
Like autodiff_deferred
but will try to extend f to an annotation, if needed.
sourceEnzymeCore.autodiff_deferred
— Methodautodiff_deferred(::ReverseMode, f, Activity, args::Vararg{Annotation, Nargs})
Same as autodiff
but uses deferred compilation to support usage in GPU code, as well as high-order differentiation.
sourceEnzymeCore.autodiff_deferred_thunk
— Methodautodiff_deferred_thunk(::ReverseModeSplit, ftype, Activity, argtypes::Vararg{Type{<:Annotation}, Nargs})
Provide the split forward and reverse pass functions for annotated function type ftype when called with args of type argtypes
when using reverse mode.
Activity
is the Activity of the return value, it may be Const
, Active
, or Duplicated
(or its variants DuplicatedNoNeed
, BatchDuplicated
, and BatchDuplicatedNoNeed
).
The forward function will return a tape, the primal (or nothing if not requested), and the shadow (or nothing if not a Duplicated
variant), and tapes the corresponding type arguements provided.
The reverse function will return the derivative of Active
arguments, updating the Duplicated
arguments in place. The same arguments to the forward pass should be provided, followed by the adjoint of the return (if the return is active), and finally the tape from the forward pass.
Example:
+((6.0,), 9.0)
Note Enzyme gradients with respect to integer values are zero. Active
will automatically convert plain integers to floating point values, but cannot do so for integer values in tuples and structs.
sourceEnzymeCore.autodiff
— Methodautodiff(mode::Mode, f, args::Vararg{Annotation, Nargs})
Like autodiff
but will try to guess the activity of the return value.
sourceEnzymeCore.autodiff
— Methodautodiff(mode::Mode, f, ::Type{A}, args::Vararg{Annotation, Nargs})
Like autodiff
but will try to extend f to an annotation, if needed.
sourceEnzymeCore.autodiff_deferred
— Methodautodiff_deferred(::ForwardMode, f, Activity, args::Vararg{Annotation, Nargs})
Same as autodiff(::ForwardMode, f, Activity, args)
but uses deferred compilation to support usage in GPU code, as well as high-order differentiation.
sourceEnzymeCore.autodiff_deferred
— Methodautodiff_deferred(mode::Mode, f, ::Type{A}, args::Vararg{Annotation, Nargs})
Like autodiff_deferred
but will try to extend f to an annotation, if needed.
sourceEnzymeCore.autodiff_deferred
— Methodautodiff_deferred(::ReverseMode, f, Activity, args::Vararg{Annotation, Nargs})
Same as autodiff
but uses deferred compilation to support usage in GPU code, as well as high-order differentiation.
sourceEnzymeCore.autodiff_deferred_thunk
— Methodautodiff_deferred_thunk(::ReverseModeSplit, ftype, Activity, argtypes::Vararg{Type{<:Annotation}, Nargs})
Provide the split forward and reverse pass functions for annotated function type ftype when called with args of type argtypes
when using reverse mode.
Activity
is the Activity of the return value, it may be Const
, Active
, or Duplicated
(or its variants DuplicatedNoNeed
, BatchDuplicated
, and BatchDuplicatedNoNeed
).
The forward function will return a tape, the primal (or nothing if not requested), and the shadow (or nothing if not a Duplicated
variant), and tapes the corresponding type arguements provided.
The reverse function will return the derivative of Active
arguments, updating the Duplicated
arguments in place. The same arguments to the forward pass should be provided, followed by the adjoint of the return (if the return is active), and finally the tape from the forward pass.
Example:
A = [2.2]; ∂A = zero(A)
v = 3.3
@@ -88,7 +88,7 @@
# output
-(7.26, 2.2, [3.3])
sourceEnzymeCore.autodiff_thunk
— Methodautodiff_thunk(::ForwardMode, ftype, Activity, argtypes::Vararg{Type{<:Annotation}, Nargs})
Provide the thunk forward mode function for annotated function type ftype when called with args of type argtypes
.
Activity
is the Activity of the return value, it may be Const
or Duplicated
(or its variants DuplicatedNoNeed
, BatchDuplicated
, andBatchDuplicatedNoNeed
).
The forward function will return the primal (if requested) and the shadow (or nothing if not a Duplicated
variant).
Example returning both original return and derivative:
a = 4.2
+(7.26, 2.2, [3.3])
sourceEnzymeCore.autodiff_thunk
— Methodautodiff_thunk(::ForwardMode, ftype, Activity, argtypes::Vararg{Type{<:Annotation}, Nargs})
Provide the thunk forward mode function for annotated function type ftype when called with args of type argtypes
.
Activity
is the Activity of the return value, it may be Const
or Duplicated
(or its variants DuplicatedNoNeed
, BatchDuplicated
, andBatchDuplicatedNoNeed
).
The forward function will return the primal (if requested) and the shadow (or nothing if not a Duplicated
variant).
Example returning both original return and derivative:
a = 4.2
b = [2.2, 3.3]; ∂f_∂b = zero(b)
c = 55; d = 9
@@ -108,7 +108,7 @@
# output
-(6.28,)
sourceEnzymeCore.autodiff_thunk
— Methodautodiff_thunk(::ReverseModeSplit, ftype, Activity, argtypes::Vararg{Type{<:Annotation}, Nargs})
Provide the split forward and reverse pass functions for annotated function type ftype when called with args of type argtypes
when using reverse mode.
Activity
is the Activity of the return value, it may be Const
, Active
, or Duplicated
(or its variants DuplicatedNoNeed
, BatchDuplicated
, and BatchDuplicatedNoNeed
).
The forward function will return a tape, the primal (or nothing if not requested), and the shadow (or nothing if not a Duplicated
variant), and tapes the corresponding type arguements provided.
The reverse function will return the derivative of Active
arguments, updating the Duplicated
arguments in place. The same arguments to the forward pass should be provided, followed by the adjoint of the return (if the return is active), and finally the tape from the forward pass.
Example:
+(6.28,)
sourceEnzymeCore.autodiff_thunk
— Methodautodiff_thunk(::ReverseModeSplit, ftype, Activity, argtypes::Vararg{Type{<:Annotation}, Nargs})
Provide the split forward and reverse pass functions for annotated function type ftype when called with args of type argtypes
when using reverse mode.
Activity
is the Activity of the return value, it may be Const
, Active
, or Duplicated
(or its variants DuplicatedNoNeed
, BatchDuplicated
, and BatchDuplicatedNoNeed
).
The forward function will return a tape, the primal (or nothing if not requested), and the shadow (or nothing if not a Duplicated
variant), and tapes the corresponding type arguements provided.
The reverse function will return the derivative of Active
arguments, updating the Duplicated
arguments in place. The same arguments to the forward pass should be provided, followed by the adjoint of the return (if the return is active), and finally the tape from the forward pass.
Example:
A = [2.2]; ∂A = zero(A)
v = 3.3
@@ -127,17 +127,17 @@
# output
-(7.26, 2.2, [3.3])
sourceEnzymeCore.ABI
— Typeabstract type ABI
Abstract type for what ABI will be used.
sourceEnzymeCore.Active
— TypeActive(x)
Mark a function argument x
of autodiff
as active, Enzyme will auto-differentiate in respect Active
arguments.
Note Enzyme gradients with respect to integer values are zero. Active
will automatically convert plain integers to floating point values, but cannot do so for integer values in tuples and structs.
sourceEnzymeCore.Annotation
— Typeabstract type Annotation{T}
Abstract type for autodiff
function argument wrappers like Const
, Active
and Duplicated
.
sourceEnzymeCore.BatchDuplicated
— TypeBatchDuplicated(x, ∂f_∂xs)
Like Duplicated
, except contains several shadows to compute derivatives for all at once. Argument ∂f_∂xs
should be a tuple of the several values of type x
.
sourceEnzymeCore.BatchDuplicatedNoNeed
— TypeBatchDuplicatedNoNeed(x, ∂f_∂xs)
Like DuplicatedNoNeed
, except contains several shadows to compute derivatives for all at once. Argument ∂f_∂xs
should be a tuple of the several values of type x
.
sourceEnzymeCore.Const
— TypeConst(x)
Mark a function argument x
of autodiff
as constant, Enzyme will not auto-differentiate in respect Const
arguments.
sourceEnzymeCore.Duplicated
— TypeDuplicated(x, ∂f_∂x)
Mark a function argument x
of autodiff
as duplicated, Enzyme will auto-differentiate in respect to such arguments, with dx
acting as an accumulator for gradients (so $\partial f / \partial x$ will be added to) ∂f_∂x
.
sourceEnzymeCore.DuplicatedNoNeed
— TypeDuplicatedNoNeed(x, ∂f_∂x)
Like Duplicated
, except also specifies that Enzyme may avoid computing the original result and only compute the derivative values.
sourceEnzymeCore.FFIABI
— Typestruct FFIABI <: ABI
Foreign function call ABI. JIT the differentiated function, then inttoptr call the address.
sourceEnzymeCore.ForwardMode
— Typestruct Forward <: Mode
Forward mode differentiation
sourceEnzymeCore.InlineABI
— Typestruct InlineABI <: ABI
Inlining function call ABI.
sourceEnzymeCore.Mode
— Typeabstract type Mode
Abstract type for what differentiation mode will be used.
sourceEnzymeCore.ReverseMode
— Typestruct ReverseMode{ReturnPrimal,ABI,Holomorphic} <: Mode{ABI}
Reverse mode differentiation.
ReturnPrimal
: Should Enzyme return the primal return value from the augmented-forward.ABI
: What runtime ABI to useHolomorphic
: Whether the complex result function is holomorphic and we should compute d/dz
sourceEnzymeCore.ReverseModeSplit
— Typestruct ReverseModeSplit{ReturnPrimal,ReturnShadow,Width,ModifiedBetween,ABI} <: Mode{ABI}
Reverse mode differentiation.
ReturnPrimal
: Should Enzyme return the primal return value from the augmented-forward.ReturnShadow
: Should Enzyme return the shadow return value from the augmented-forward.Width
: Batch Size (0 if to be automatically derived)ModifiedBetween
: Tuple of each argument's modified between state (true if to be automatically derived).
sourceEnzymeCore.compiler_job_from_backend
— Functioncompiler_job_from_backend(::KernelAbstractions.Backend, F::Type, TT:Type)::GPUCompiler.CompilerJob
Returns a GPUCompiler CompilerJob from a backend as specified by the first argument to the function.
For example, in CUDA one would do:
function EnzymeCore.compiler_job_from_backend(::CUDABackend, @nospecialize(F::Type), @nospecialize(TT::Type))
+(7.26, 2.2, [3.3])
sourceEnzymeCore.ABI
— Typeabstract type ABI
Abstract type for what ABI will be used.
sourceEnzymeCore.Active
— TypeActive(x)
Mark a function argument x
of autodiff
as active, Enzyme will auto-differentiate in respect Active
arguments.
Note Enzyme gradients with respect to integer values are zero. Active
will automatically convert plain integers to floating point values, but cannot do so for integer values in tuples and structs.
sourceEnzymeCore.Annotation
— Typeabstract type Annotation{T}
Abstract type for autodiff
function argument wrappers like Const
, Active
and Duplicated
.
sourceEnzymeCore.BatchDuplicated
— TypeBatchDuplicated(x, ∂f_∂xs)
Like Duplicated
, except contains several shadows to compute derivatives for all at once. Argument ∂f_∂xs
should be a tuple of the several values of type x
.
sourceEnzymeCore.BatchDuplicatedNoNeed
— TypeBatchDuplicatedNoNeed(x, ∂f_∂xs)
Like DuplicatedNoNeed
, except contains several shadows to compute derivatives for all at once. Argument ∂f_∂xs
should be a tuple of the several values of type x
.
sourceEnzymeCore.Const
— TypeConst(x)
Mark a function argument x
of autodiff
as constant, Enzyme will not auto-differentiate in respect Const
arguments.
sourceEnzymeCore.Duplicated
— TypeDuplicated(x, ∂f_∂x)
Mark a function argument x
of autodiff
as duplicated, Enzyme will auto-differentiate in respect to such arguments, with dx
acting as an accumulator for gradients (so $\partial f / \partial x$ will be added to) ∂f_∂x
.
sourceEnzymeCore.DuplicatedNoNeed
— TypeDuplicatedNoNeed(x, ∂f_∂x)
Like Duplicated
, except also specifies that Enzyme may avoid computing the original result and only compute the derivative values.
sourceEnzymeCore.FFIABI
— Typestruct FFIABI <: ABI
Foreign function call ABI. JIT the differentiated function, then inttoptr call the address.
sourceEnzymeCore.ForwardMode
— Typestruct Forward <: Mode
Forward mode differentiation
sourceEnzymeCore.InlineABI
— Typestruct InlineABI <: ABI
Inlining function call ABI.
sourceEnzymeCore.Mode
— Typeabstract type Mode
Abstract type for what differentiation mode will be used.
sourceEnzymeCore.ReverseMode
— Typestruct ReverseMode{ReturnPrimal,ABI,Holomorphic} <: Mode{ABI}
Reverse mode differentiation.
ReturnPrimal
: Should Enzyme return the primal return value from the augmented-forward.ABI
: What runtime ABI to useHolomorphic
: Whether the complex result function is holomorphic and we should compute d/dz
sourceEnzymeCore.ReverseModeSplit
— Typestruct ReverseModeSplit{ReturnPrimal,ReturnShadow,Width,ModifiedBetween,ABI} <: Mode{ABI}
Reverse mode differentiation.
ReturnPrimal
: Should Enzyme return the primal return value from the augmented-forward.ReturnShadow
: Should Enzyme return the shadow return value from the augmented-forward.Width
: Batch Size (0 if to be automatically derived)ModifiedBetween
: Tuple of each argument's modified between state (true if to be automatically derived).
sourceEnzymeCore.compiler_job_from_backend
— Functioncompiler_job_from_backend(::KernelAbstractions.Backend, F::Type, TT:Type)::GPUCompiler.CompilerJob
Returns a GPUCompiler CompilerJob from a backend as specified by the first argument to the function.
For example, in CUDA one would do:
function EnzymeCore.compiler_job_from_backend(::CUDABackend, @nospecialize(F::Type), @nospecialize(TT::Type))
mi = GPUCompiler.methodinstance(F, TT)
return GPUCompiler.CompilerJob(mi, CUDA.compiler_config(CUDA.device()))
-end
sourceEnzymeCore.make_zero
— Functionmake_zero(::Type{T}, seen::IdDict, prev::T, ::Val{copy_if_inactive}=Val(false))::T
+end
sourceEnzymeCore.make_zero
— Functionmake_zero(::Type{T}, seen::IdDict, prev::T, ::Val{copy_if_inactive}=Val(false))::T
Recursively make a zero'd copy of the value `prev` of type `T`. The argument `copy_if_inactive` specifies
-what to do if the type `T` is guaranteed to be inactive, use the primal (the default) or still copy the value.
sourceEnzymeCore.make_zero
— Methodmake_zero(prev::T)
+what to do if the type `T` is guaranteed to be inactive, use the primal (the default) or still copy the value.
sourceEnzymeCore.make_zero
— Methodmake_zero(prev::T)
-Helper function to recursively make zero.
sourceEnzymeCore.EnzymeRules.AugmentedReturn
— TypeAugmentedReturn(primal, shadow, tape)
Augment the primal return value of a function with its shadow, as well as any additional information needed to correctly compute the reverse pass, stored in tape
.
Unless specified by the config that a variable is not overwritten, rules must assume any arrays/data structures/etc are overwritten between the forward and the reverse pass. Any floats or variables passed by value are always preserved as is (as are the arrays themselves, just not necessarily the values in the array).
See also augmented_primal
.
sourceEnzymeCore.EnzymeRules.Config
— TypeConfig{NeedsPrimal, NeedsShadow, Width, Overwritten}
-ConfigWidth{Width} = Config{<:Any,<:Any, Width}
Configuration type to dispatch on in custom reverse rules (see augmented_primal
and reverse
).
NeedsPrimal
and NeedsShadow
: boolean values specifying whether the primal and shadow (resp.) should be returned. Width
: an integer that specifies the number of adjoints/shadows simultaneously being propagated.Overwritten
: a tuple of booleans of whether each argument (including the function itself) is modified between the forward and reverse pass (true if potentially modified between).
Getters for the four type parameters are provided by needs_primal
, needs_shadow
, width
, and overwritten
.
sourceEnzymeCore.EnzymeRules.augmented_primal
— Functionaugmented_primal(::Config, func::Annotation{typeof(f)}, RT::Type{<:Annotation}, args::Annotation...)
Must return an AugmentedReturn
type.
- The primal must be the same type of the original return if
needs_primal(config)
, otherwise nothing. - The shadow must be nothing if needs_shadow(config) is false. If width is 1, the shadow should be the same type of the original return. If the width is greater than 1, the shadow should be NTuple{original return, width}.
- The tape can be any type (including Nothing) and is preserved for the reverse call.
sourceEnzymeCore.EnzymeRules.forward
— Functionforward(func::Annotation{typeof(f)}, RT::Type{<:Annotation}, args::Annotation...)
Calculate the forward derivative. The first argument func
is the callable for which the rule applies to. Either wrapped in a Const
), or a Duplicated
if it is a closure. The second argument is the return type annotation, and all other arguments are the annotated function arguments.
sourceEnzymeCore.EnzymeRules.inactive
— Functioninactive(func::typeof(f), args...)
Mark a particular function as always being inactive in both its return result and the function call itself.
sourceEnzymeCore.EnzymeRules.inactive_noinl
— Functioninactive_noinl(func::typeof(f), args...)
Mark a particular function as always being inactive in both its return result and the function call itself, but do not prevent inlining of the function.
sourceEnzymeCore.EnzymeRules.inactive_type
— Methodinactive_type(::Type{Ty})
Mark a particular type Ty
as always being inactive.
sourceEnzymeCore.EnzymeRules.reverse
— Functionreverse(::Config, func::Annotation{typeof(f)}, dret::Active, tape, args::Annotation...)
-reverse(::Config, func::Annotation{typeof(f)}, ::Type{<:Annotation), tape, args::Annotation...)
Takes gradient of derivative, activity annotation, and tape. If there is an active return dret is passed as Active{T} with the derivative of the active return val. Otherwise dret is passed as Type{Duplicated{T}}, etc.
sourceEnzymeTestUtils.ExprAndMsg
— TypeA cunning hack to carry extra message along with the original expression in a test
sourceEnzymeTestUtils.@test_msg
— Macro@test_msg msg condion kws...
This is per Test.@test condion kws...
except that if it fails it also prints the msg
. If msg==""
then this is just like @test
, nothing is printed
Examles
julia> @test_msg "It is required that the total is under 10" sum(1:1000) < 10;
+Helper function to recursively make zero.
sourceEnzymeCore.EnzymeRules.AugmentedReturn
— TypeAugmentedReturn(primal, shadow, tape)
Augment the primal return value of a function with its shadow, as well as any additional information needed to correctly compute the reverse pass, stored in tape
.
Unless specified by the config that a variable is not overwritten, rules must assume any arrays/data structures/etc are overwritten between the forward and the reverse pass. Any floats or variables passed by value are always preserved as is (as are the arrays themselves, just not necessarily the values in the array).
See also augmented_primal
.
sourceEnzymeCore.EnzymeRules.Config
— TypeConfig{NeedsPrimal, NeedsShadow, Width, Overwritten}
+ConfigWidth{Width} = Config{<:Any,<:Any, Width}
Configuration type to dispatch on in custom reverse rules (see augmented_primal
and reverse
).
NeedsPrimal
and NeedsShadow
: boolean values specifying whether the primal and shadow (resp.) should be returned. Width
: an integer that specifies the number of adjoints/shadows simultaneously being propagated.Overwritten
: a tuple of booleans of whether each argument (including the function itself) is modified between the forward and reverse pass (true if potentially modified between).
Getters for the four type parameters are provided by needs_primal
, needs_shadow
, width
, and overwritten
.
sourceEnzymeCore.EnzymeRules.augmented_primal
— Functionaugmented_primal(::Config, func::Annotation{typeof(f)}, RT::Type{<:Annotation}, args::Annotation...)
Must return an AugmentedReturn
type.
- The primal must be the same type of the original return if
needs_primal(config)
, otherwise nothing. - The shadow must be nothing if needs_shadow(config) is false. If width is 1, the shadow should be the same type of the original return. If the width is greater than 1, the shadow should be NTuple{original return, width}.
- The tape can be any type (including Nothing) and is preserved for the reverse call.
sourceEnzymeCore.EnzymeRules.forward
— Functionforward(func::Annotation{typeof(f)}, RT::Type{<:Annotation}, args::Annotation...)
Calculate the forward derivative. The first argument func
is the callable for which the rule applies to. Either wrapped in a Const
), or a Duplicated
if it is a closure. The second argument is the return type annotation, and all other arguments are the annotated function arguments.
sourceEnzymeCore.EnzymeRules.inactive
— Functioninactive(func::typeof(f), args...)
Mark a particular function as always being inactive in both its return result and the function call itself.
sourceEnzymeCore.EnzymeRules.inactive_noinl
— Functioninactive_noinl(func::typeof(f), args...)
Mark a particular function as always being inactive in both its return result and the function call itself, but do not prevent inlining of the function.
sourceEnzymeCore.EnzymeRules.inactive_type
— Methodinactive_type(::Type{Ty})
Mark a particular type Ty
as always being inactive.
sourceEnzymeCore.EnzymeRules.reverse
— Functionreverse(::Config, func::Annotation{typeof(f)}, dret::Active, tape, args::Annotation...)
+reverse(::Config, func::Annotation{typeof(f)}, ::Type{<:Annotation), tape, args::Annotation...)
Takes gradient of derivative, activity annotation, and tape. If there is an active return dret is passed as Active{T} with the derivative of the active return val. Otherwise dret is passed as Type{Duplicated{T}}, etc.
sourceEnzymeTestUtils.ExprAndMsg
— TypeA cunning hack to carry extra message along with the original expression in a test
sourceEnzymeTestUtils.@test_msg
— Macro@test_msg msg condion kws...
This is per Test.@test condion kws...
except that if it fails it also prints the msg
. If msg==""
then this is just like @test
, nothing is printed
Examles
julia> @test_msg "It is required that the total is under 10" sum(1:1000) < 10;
Test Failed at REPL[1]:1
Expression: sum(1:1000) < 10
Problem: It is required that the total is under 10
@@ -159,7 +159,7 @@
Test Failed at REPL[153]:1
Expression: sum(1:1000) < 10
Evaluated: 500500 < 10
- ERROR: There was an error during testing
sourceEnzymeTestUtils.are_activities_compatible
— Methodare_activities_compatible(Tret, activities...) -> Bool
Return true
if return activity type Tret
and activity types activities
are compatible.
sourceEnzymeTestUtils.test_forward
— Methodtest_forward(f, Activity, args...; kwargs...)
Test Enzyme.autodiff
of f
in Forward
-mode against finite differences.
f
has all constraints of the same argument passed to Enzyme.autodiff
, with several additional constraints:
- If it mutates one of its arguments, it must return that argument.
Arguments
Activity
: the activity of the return value of f
args
: Each entry is either an argument to f
, an activity type accepted by autodiff
, or a tuple of the form (arg, Activity)
, where Activity
is the activity type of arg
. If the activity type specified requires a tangent, a random tangent will be automatically generated.
Keywords
fdm=FiniteDifferences.central_fdm(5, 1)
: The finite differences method to use.fkwargs
: Keyword arguments to pass to f
.rtol
: Relative tolerance for isapprox
.atol
: Absolute tolerance for isapprox
.testset_name
: Name to use for a testset in which all tests are evaluated.
Examples
Here we test a rule for a function of scalars. Because we don't provide an activity annotation for y
, it is assumed to be Const
.
using Enzyme, EnzymeTestUtils
+ ERROR: There was an error during testing
sourceEnzymeTestUtils.are_activities_compatible
— Methodare_activities_compatible(Tret, activities...) -> Bool
Return true
if return activity type Tret
and activity types activities
are compatible.
sourceEnzymeTestUtils.test_forward
— Methodtest_forward(f, Activity, args...; kwargs...)
Test Enzyme.autodiff
of f
in Forward
-mode against finite differences.
f
has all constraints of the same argument passed to Enzyme.autodiff
, with several additional constraints:
- If it mutates one of its arguments, it must return that argument.
Arguments
Activity
: the activity of the return value of f
args
: Each entry is either an argument to f
, an activity type accepted by autodiff
, or a tuple of the form (arg, Activity)
, where Activity
is the activity type of arg
. If the activity type specified requires a tangent, a random tangent will be automatically generated.
Keywords
fdm=FiniteDifferences.central_fdm(5, 1)
: The finite differences method to use.fkwargs
: Keyword arguments to pass to f
.rtol
: Relative tolerance for isapprox
.atol
: Absolute tolerance for isapprox
.testset_name
: Name to use for a testset in which all tests are evaluated.
Examples
Here we test a rule for a function of scalars. Because we don't provide an activity annotation for y
, it is assumed to be Const
.
using Enzyme, EnzymeTestUtils
x, y = randn(2)
for Tret in (Const, Duplicated, DuplicatedNoNeed), Tx in (Const, Duplicated)
@@ -171,7 +171,7 @@
Ty in (Const, BatchDuplicated)
test_forward(*, Tret, (x, Tx), (y, Ty))
-end
sourceEnzymeTestUtils.test_reverse
— Methodtest_reverse(f, Activity, args...; kwargs...)
Test Enzyme.autodiff_thunk
of f
in ReverseSplitWithPrimal
-mode against finite differences.
f
has all constraints of the same argument passed to Enzyme.autodiff_thunk
, with several additional constraints:
- If it mutates one of its arguments, it must not also return that argument.
- If the return value is a struct, then all floating point numbers contained in the struct or its fields must be in arrays.
Arguments
Activity
: the activity of the return value of f
.args
: Each entry is either an argument to f
, an activity type accepted by autodiff
, or a tuple of the form (arg, Activity)
, where Activity
is the activity type of arg
. If the activity type specified requires a shadow, one will be automatically generated.
Keywords
fdm=FiniteDifferences.central_fdm(5, 1)
: The finite differences method to use.fkwargs
: Keyword arguments to pass to f
.rtol
: Relative tolerance for isapprox
.atol
: Absolute tolerance for isapprox
.testset_name
: Name to use for a testset in which all tests are evaluated.
Examples
Here we test a rule for a function of scalars. Because we don't provide an activity annotation for y
, it is assumed to be Const
.
using Enzyme, EnzymeTestUtils
+end
sourceEnzymeTestUtils.test_reverse
— Methodtest_reverse(f, Activity, args...; kwargs...)
Test Enzyme.autodiff_thunk
of f
in ReverseSplitWithPrimal
-mode against finite differences.
f
has all constraints of the same argument passed to Enzyme.autodiff_thunk
, with several additional constraints:
- If it mutates one of its arguments, it must not also return that argument.
- If the return value is a struct, then all floating point numbers contained in the struct or its fields must be in arrays.
Arguments
Activity
: the activity of the return value of f
.args
: Each entry is either an argument to f
, an activity type accepted by autodiff
, or a tuple of the form (arg, Activity)
, where Activity
is the activity type of arg
. If the activity type specified requires a shadow, one will be automatically generated.
Keywords
fdm=FiniteDifferences.central_fdm(5, 1)
: The finite differences method to use.fkwargs
: Keyword arguments to pass to f
.rtol
: Relative tolerance for isapprox
.atol
: Absolute tolerance for isapprox
.testset_name
: Name to use for a testset in which all tests are evaluated.
Examples
Here we test a rule for a function of scalars. Because we don't provide an activity annotation for y
, it is assumed to be Const
.
using Enzyme, EnzymeTestUtils
x = randn()
y = randn()
@@ -180,4 +180,4 @@
end
Here we test a rule for a function of an array in batch reverse-mode:
x = randn(3)
for Tret in (Const, Active), Tx in (Const, BatchDuplicated)
test_reverse(prod, Tret, (x, Tx))
-end
sourceEnzyme.API.runtimeActivity!
— MethodruntimeActivity!(val::Bool)
Enzyme runs an activity analysis which deduces which values, instructions, etc are necessary to be differentiated and therefore involved in the differentiation procedure. This runs at compile time. However, there may be implementation flaws in this analysis that means that Enzyme cannot deduce that an inactive (const) value is actually const. Alternatively, there may be some data which is conditionally active, depending on which runtime branch is taken. In these cases Enzyme conservatively presumes the value is active.
However, in certain cases, an insufficiently aggressive activity analysis may result in derivative errors – for example by mistakenly using the primal (const) argument and mistaking it for the duplicated shadow. As a result this may result in incorrect results, or accidental updates to the primal.
This flag enables runntime activity which tells all load/stores to check at runtime whether the value they are updating is indeed active (in addition to the compile-time activity analysis). This will remedy these such errors, but at a performance penalty of performing such checks.
It is on the Enzyme roadmap to add a PotentiallyDuplicated style activity, in addition to the current Const and Duplicated styles that will disable the need for this, which does not require the check when a value is guaranteed active, but still supports runtime-based activity information.
This function takes an argument to set the runtime activity value, true means it is on, and false means off. By default it is off.
sourceEnzyme.API.runtimeActivity
— MethodruntimeActivity()
Gets the current value of the runtime activity. See runtimeActivity!
for more information.
sourceSettings
This document was generated with Documenter.jl version 0.27.25 on Tuesday 19 March 2024. Using Julia version 1.10.2.
+end
Enzyme.API.runtimeActivity!
— MethodruntimeActivity!(val::Bool)
Enzyme runs an activity analysis which deduces which values, instructions, etc are necessary to be differentiated and therefore involved in the differentiation procedure. This runs at compile time. However, there may be implementation flaws in this analysis that means that Enzyme cannot deduce that an inactive (const) value is actually const. Alternatively, there may be some data which is conditionally active, depending on which runtime branch is taken. In these cases Enzyme conservatively presumes the value is active.
However, in certain cases, an insufficiently aggressive activity analysis may result in derivative errors – for example by mistakenly using the primal (const) argument and mistaking it for the duplicated shadow. As a result this may result in incorrect results, or accidental updates to the primal.
This flag enables runntime activity which tells all load/stores to check at runtime whether the value they are updating is indeed active (in addition to the compile-time activity analysis). This will remedy these such errors, but at a performance penalty of performing such checks.
It is on the Enzyme roadmap to add a PotentiallyDuplicated style activity, in addition to the current Const and Duplicated styles that will disable the need for this, which does not require the check when a value is guaranteed active, but still supports runtime-based activity information.
This function takes an argument to set the runtime activity value, true means it is on, and false means off. By default it is off.
Enzyme.API.runtimeActivity
— MethodruntimeActivity()
Gets the current value of the runtime activity. See runtimeActivity!
for more information.