ADTypes.jl
Documentation for ADTypes.jl.
ADTypes.ADTypes — Module
ADTypes.jlADTypes.jl is a multi-valued logic system to choose an automatic differentiation (AD) package and specify its parameters.
sourceADTypes.AbstractADType — Type
Dense AD
Forward mode
Algorithmic differentiation:
ADTypes.AutoForwardDiff — Type
AutoForwardDiff{chunksize,T}Struct used to select the ForwardDiff.jl backend for automatic differentiation.
Defined by ADTypes.jl.
Constructors
AutoForwardDiff(; chunksize=nothing, tag=nothing)Type parameters
chunksize: the preferred chunk size to evaluate several derivatives at once
Fields
tag::T: a custom tag to handle nested differentiation calls (usually not necessary)
ADTypes.AutoPolyesterForwardDiff — Type
AutoPolyesterForwardDiff{chunksize,T}Struct used to select the PolyesterForwardDiff.jl backend for automatic differentiation.
Defined by ADTypes.jl.
Constructors
AutoPolyesterForwardDiff(; chunksize=nothing, tag=nothing)Type parameters
chunksize: the preferred chunk size to evaluate several derivatives at once
Fields
tag::T: a custom tag to handle nested differentiation calls (usually not necessary)
ADTypes.AutoMooncakeForward — Type
AutoMooncakeForwardStruct used to select the Mooncake.jl backend for automatic differentiation in forward mode.
Defined by ADTypes.jl.
This struct was introduced when forward mode became available in Mooncake.jl v0.4.147. It was kept separate from AutoMooncake to avoid a breaking release of ADTypes.jl. AutoMooncake remains for reverse mode only.
Constructors
AutoMooncakeForward(; config=nothing)Fields
config: eithernothingor an instance ofMooncake.Config– see the docstring ofMooncake.Configfor more information.AutoForwardMooncake(; config=nothing)is equivalent toAutoForwardMooncake(; config=Mooncake.Config()), i.e. the default configuration.
Finite differences:
ADTypes.AutoFiniteDiff — Type
AutoFiniteDiff{T1,T2,T3}Struct used to select the FiniteDiff.jl backend for automatic differentiation.
Defined by ADTypes.jl.
Constructors
AutoFiniteDiff(;
fdtype=Val(:forward), fdjtype=fdtype, fdhtype=Val(:hcentral),
relstep=nothing, absstep=nothing, dir=true
)Fields
fdtype::T1: finite difference typefdjtype::T2: finite difference type for the Jacobianfdhtype::T3: finite difference type for the Hessianrelstep: relative finite difference step sizeabsstep: absolute finite difference step sizedir: direction of the finite difference step
ADTypes.AutoFiniteDifferences — Type
AutoFiniteDifferences{T}Struct used to select the FiniteDifferences.jl backend for automatic differentiation.
Defined by ADTypes.jl.
Constructors
AutoFiniteDifferences(; fdm)Fields
fdm::T: aFiniteDifferenceMethod
Taylor mode:
ADTypes.AutoGTPSA — Type
AutoGTPSA{D}Struct used to select the GTPSA.jl backend for automatic differentiation.
Defined by ADTypes.jl.
Constructors
AutoGTPSA(; descriptor=nothing)Fields
descriptor::D: can be either- a GTPSA
Descriptorspecifying the number of variables/parameters, parameter order, individual variable/parameter truncation orders, and maximum order. See the GTPSA.jl documentation for more details. nothingto automatically use aDescriptorgiven the context.
- a GTPSA
ADTypes.AutoTaylorDiff — Type
AutoTaylorDiff{order}Struct used to select the TaylorDiff.jl backend for automatic differentiation.
Defined by ADTypes.jl.
Constructors
AutoTaylorDiff(; order = 1)Type parameters
order: the order of the Taylor-mode automatic differentiation
Reverse mode
ADTypes.AutoMooncake — Type
AutoMooncakeStruct used to select the Mooncake.jl backend for automatic differentiation in reverse mode.
Defined by ADTypes.jl.
When forward mode became available in Mooncake.jl v0.4.147, another struct called AutoMooncakeForward was introduced. It was kept separate to avoid a breaking release of ADTypes.jl. AutoMooncake remains for reverse mode only.
Constructors
AutoMooncake(; config=nothing)Fields
config: eithernothingor an instance ofMooncake.Config– see the docstring ofMooncake.Configfor more information.AutoMooncake(; config=nothing)is equivalent toAutoMooncake(; config=Mooncake.Config()), i.e. the default configuration.
ADTypes.AutoReverseDiff — Type
AutoReverseDiff{compile}Struct used to select the ReverseDiff.jl backend for automatic differentiation.
Defined by ADTypes.jl.
Constructors
AutoReverseDiff(; compile::Union{Val, Bool} = Val(false))Fields
compile::Union{Val, Bool}: whether to allow pre-recording and reusing a tape (which speeds up the differentiation process).- If
compile=falseorcompile=Val(false), a new tape must be recorded at every call to the differentiation operator. - If
compile=trueorcompile=Val(true), a tape can be pre-recorded on an example input and then reused at every differentiation call.
The boolean version of this keyword argument is taken as the type parameter.
- If
Pre-recording a tape only captures the path taken by the differentiated function when executed on the example input. If said function has value-dependent branching behavior, reusing pre-recorded tapes can lead to incorrect results. In such situations, you should keep the default setting compile=Val(false). For more details, please refer to ReverseDiff's AbstractTape API documentation.
Despite what its name may suggest, the compile setting does not prescribe whether or not the tape is compiled with ReverseDiff.compile after being recorded. This is left as a private implementation detail.
ADTypes.AutoTracker — Type
AutoTrackerStruct used to select the Tracker.jl backend for automatic differentiation.
Defined by ADTypes.jl.
Constructors
AutoTracker()sourceADTypes.AutoZygote — Type
AutoZygoteStruct used to select the Zygote.jl backend for automatic differentiation.
Defined by ADTypes.jl.
Constructors
AutoZygote()sourceForward or reverse mode
ADTypes.AutoEnzyme — Type
AutoEnzyme{M,A}Struct used to select the Enzyme.jl backend for automatic differentiation.
Defined by ADTypes.jl.
Constructors
AutoEnzyme(; mode::M=nothing, function_annotation::Type{A}=Nothing)Type parameters
Adetermines how the functionfto differentiate is passed to Enzyme. It can be:- a subtype of
EnzymeCore.Annotation(likeEnzymeCore.ConstorEnzymeCore.Duplicated) to enforce a given annotation Nothingto simply passfand let Enzyme choose the most appropriate annotation
- a subtype of
Fields
mode::Mdetermines the autodiff mode (forward or reverse). It can be:- an object subtyping
EnzymeCore.Mode(likeEnzymeCore.ForwardorEnzymeCore.Reverse) if a specific mode is required nothingto choose the best mode automatically
- an object subtyping
ADTypes.AutoChainRules — Type
AutoChainRules{RC}Struct used to select an automatic differentiation backend based on ChainRulesCore.jl (see the list here).
Defined by ADTypes.jl.
Constructors
AutoChainRules(; ruleconfig)Fields
ruleconfig::RC: aChainRulesCore.RuleConfigobject.
ADTypes.AutoDiffractor — Type
AutoDiffractorStruct used to select the Diffractor.jl backend for automatic differentiation.
Defined by ADTypes.jl.
Constructors
AutoDiffractor()sourceForward, reverse, or sparse mode
ADTypes.AutoReactant — Type
AutoReactant{M<:AutoEnzyme}Struct used to select the Reactant.jl compilation atop Enzyme for automatic differentiation.
Defined by ADTypes.jl.
Constructors
AutoReactant(; mode::Union{AutoEnzyme,Nothing}=nothing)Fields
mode::Mspecifies the parameterization of differentiation. It can be:- an
AutoEnzymeobject if a specific mode is required nothingto choose the best mode automatically
- an
Symbolic mode
ADTypes.AutoFastDifferentiation — Type
AutoFastDifferentiationStruct used to select the FastDifferentiation.jl backend for automatic differentiation.
Defined by ADTypes.jl.
Constructors
AutoFastDifferentiation()sourceADTypes.AutoSymbolics — Type
AutoSymbolicsStruct used to select the Symbolics.jl backend for automatic differentiation.
Defined by ADTypes.jl.
Constructors
AutoSymbolics()sourceSparse AD
ADTypes.AutoSparse — Type
AutoSparse{D,S,C}Wraps an ADTypes.jl object to deal with sparse Jacobians and Hessians.
Fields
dense_ad::D: the underlying AD package, subtypingAbstractADTypesparsity_detector::S: the sparsity pattern detector, subtypingAbstractSparsityDetectorcoloring_algorithm::C: the coloring algorithm, subtypingAbstractColoringAlgorithm
Constructors
AutoSparse(
dense_ad;
sparsity_detector=ADTypes.NoSparsityDetector(),
coloring_algorithm=ADTypes.NoColoringAlgorithm()
)sourceADTypes.dense_ad — Function
dense_ad(ad::AutoSparse)::AbstractADType
dense_ad(ad::AbstractADType)::AbstractADTypeReturn the underlying AD package for a sparse AD choice, act as the identity on a dense AD choice.
See also
sourceSparsity detector
ADTypes.sparsity_detector — Function
sparsity_detector(ad::AutoSparse)::AbstractSparsityDetectorReturn the sparsity pattern detector for a sparse AD choice.
See also
sourceADTypes.AbstractSparsityDetector — Type
ADTypes.jacobian_sparsity — Function
jacobian_sparsity(f, x, sd::AbstractSparsityDetector)::AbstractMatrix{Bool}
jacobian_sparsity(f!, y, x, sd::AbstractSparsityDetector)::AbstractMatrix{Bool}Use detector sd to construct a (typically sparse) matrix S describing the pattern of nonzeroes in the Jacobian of f (resp. f!) applied at x (resp. (y, x)).
ADTypes.hessian_sparsity — Function
hessian_sparsity(f, x, sd::AbstractSparsityDetector)::AbstractMatrix{Bool}Use detector sd to construct a (typically sparse) matrix S describing the pattern of nonzeroes in the Hessian of f applied at x.
ADTypes.NoSparsityDetector — Type
NoSparsityDetector <: AbstractSparsityDetectorTrivial sparsity detector, which always returns a full sparsity pattern (only ones, no zeroes).
See also
sourceADTypes.KnownJacobianSparsityDetector — Type
KnownJacobianSparsityDetector(jacobian_sparsity::AbstractMatrix) <: AbstractSparsityDetectorTrivial sparsity detector used to return a known Jacobian sparsity pattern.
See also
sourceADTypes.KnownHessianSparsityDetector — Type
KnownHessianSparsityDetector(hessian_sparsity::AbstractMatrix) <: AbstractSparsityDetectorTrivial sparsity detector used to return a known Hessian sparsity pattern.
See also
sourceColoring algorithm
ADTypes.coloring_algorithm — Function
coloring_algorithm(ad::AutoSparse)::AbstractColoringAlgorithmReturn the coloring algorithm for a sparse AD choice.
See also
sourceADTypes.AbstractColoringAlgorithm — Type
AbstractColoringAlgorithmAbstract supertype for Jacobian/Hessian coloring algorithms.
Required methods
Note
The terminology and definitions are taken from the following paper:
sourceWhat Color Is Your Jacobian? Graph Coloring for Computing Derivatives, Assefaw Hadish Gebremedhin, Fredrik Manne, and Alex Pothen (2005)
ADTypes.column_coloring — Function
column_coloring(M::AbstractMatrix, ca::ColoringAlgorithm)::AbstractVector{<:Integer}Use algorithm ca to construct a structurally orthogonal partition of the columns of M.
The result is a coloring vector c of length size(M, 2) such that for every non-zero coefficient M[i, j], column j is the only column of its color c[j] with a non-zero coefficient in row i.
ADTypes.row_coloring — Function
row_coloring(M::AbstractMatrix, ca::ColoringAlgorithm)::AbstractVector{<:Integer}Use algorithm ca to construct a structurally orthogonal partition of the rows of M.
The result is a coloring vector c of length size(M, 1) such that for every non-zero coefficient M[i, j], row i is the only row of its color c[i] with a non-zero coefficient in column j.
ADTypes.symmetric_coloring — Function
symmetric_coloring(M::AbstractMatrix, ca::ColoringAlgorithm)::AbstractVector{<:Integer}Use algorithm ca to construct a symmetrically structurally orthogonal partition of the columns (or rows) of the symmetric matrix M.
The result is a coloring vector c of length size(M, 1) == size(M, 2) such that for every non-zero coefficient M[i, j], at least one of the following conditions holds:
- column
jis the only column of its colorc[j]with a non-zero coefficient in rowi; - column
iis the only column of its colorc[i]with a non-zero coefficient in rowj.
ADTypes.NoColoringAlgorithm — Type
NoColoringAlgorithm <: AbstractColoringAlgorithmTrivial coloring algorithm, which always returns a different color for each matrix column/row.
See also
sourceNo automatic differentiation
ADTypes.NoAutoDiff — Type
NoAutoDiffStruct used to select no automatic differentiation.
Defined by ADTypes.jl.
Constructors
NoAutoDiff()sourceADTypes.NoAutoDiffSelectedError — Type
NoAutoDiffSelectedError <: ExceptionSignifies that code tried to use automatic differentiation, but NoAutoDiff was specified.
Constructor
NoAutoDiffSelectedError(msg::String)sourceModes
ADTypes.mode — Function
ADTypes.AbstractMode — Type
ADTypes.ForwardMode — Type
ForwardModeTrait for AD choices that rely on forward mode algorithmic differentiation or finite differences.
These two paradigms are classified together because they can both efficiently compute Jacobian-vector products.
sourceADTypes.ForwardOrReverseMode — Type
ForwardOrReverseModeTrait for AD choices that can work either in ForwardMode or ReverseMode, depending on their configuration.
This trait should rarely be used, because more precise dispatches to ForwardMode or ReverseMode should be defined.
ADTypes.ReverseMode — Type
ADTypes.SymbolicMode — Type
Miscellaneous
ADTypes.Auto — Function
ADTypes.Auto(package::Symbol)
ADTypes.Auto(nothing)::NoAutoDiffA shortcut that converts an AD package name into an instance of AbstractADType, with all parameters set to their default values.
This function is type-unstable by design and might lead to suboptimal performance. In most cases, you should never need it: use the individual backend types directly.
Example
import ADTypes
backend = ADTypes.Auto(:Zygote)
# output
ADTypes.AutoZygote()sourceDeprecated
ADTypes.AutoTapir — Type
AutoTapirAutoTapir is deprecated following a package renaming, please use AutoMooncake instead.
Struct used to select the Tapir.jl backend for automatic differentiation.
Defined by ADTypes.jl.
Constructors
AutoTapir(; safe_mode=true)Fields
safe_mode::Bool: whether to run additional checks to catch errors early.