ADTypes.jl
Documentation for ADTypes.jl.
ADTypes.ADTypes — ModuleADTypes.jlADTypes.jl is a multi-valued logic system to choose an automatic differentiation (AD) package and specify its parameters.
ADTypes.AbstractADType — TypeAbstractADTypeAbstract supertype for all AD choices.
Dense AD
Forward mode
Algorithmic differentiation:
ADTypes.AutoForwardDiff — TypeAutoForwardDiff{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 — TypeAutoPolyesterForwardDiff{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 — TypeAutoMooncakeForwardStruct 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 — TypeAutoFiniteDiff{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 — TypeAutoFiniteDifferences{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 — TypeAutoGTPSA{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 — TypeAutoTaylorDiff{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 — TypeAutoMooncakeStruct 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 — TypeAutoReverseDiff{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 — TypeAutoTrackerStruct used to select the Tracker.jl backend for automatic differentiation.
Defined by ADTypes.jl.
Constructors
AutoTracker()ADTypes.AutoZygote — TypeAutoZygoteStruct used to select the Zygote.jl backend for automatic differentiation.
Defined by ADTypes.jl.
Constructors
AutoZygote()Forward or reverse mode
ADTypes.AutoEnzyme — TypeAutoEnzyme{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 — TypeAutoChainRules{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 — TypeAutoDiffractorStruct used to select the Diffractor.jl backend for automatic differentiation.
Defined by ADTypes.jl.
Constructors
AutoDiffractor()Symbolic mode
ADTypes.AutoFastDifferentiation — TypeAutoFastDifferentiationStruct used to select the FastDifferentiation.jl backend for automatic differentiation.
Defined by ADTypes.jl.
Constructors
AutoFastDifferentiation()ADTypes.AutoSymbolics — TypeAutoSymbolicsStruct used to select the Symbolics.jl backend for automatic differentiation.
Defined by ADTypes.jl.
Constructors
AutoSymbolics()Sparse AD
ADTypes.AutoSparse — TypeAutoSparse{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()
)ADTypes.dense_ad — Functiondense_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
Sparsity detector
ADTypes.sparsity_detector — Functionsparsity_detector(ad::AutoSparse)::AbstractSparsityDetectorReturn the sparsity pattern detector for a sparse AD choice.
See also
ADTypes.AbstractSparsityDetector — TypeAbstractSparsityDetectorAbstract supertype for sparsity pattern detectors.
Required methods
ADTypes.jacobian_sparsity — Functionjacobian_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 — Functionhessian_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 — TypeNoSparsityDetector <: AbstractSparsityDetectorTrivial sparsity detector, which always returns a full sparsity pattern (only ones, no zeroes).
See also
ADTypes.KnownJacobianSparsityDetector — TypeKnownJacobianSparsityDetector(jacobian_sparsity::AbstractMatrix) <: AbstractSparsityDetectorTrivial sparsity detector used to return a known Jacobian sparsity pattern.
See also
ADTypes.KnownHessianSparsityDetector — TypeKnownHessianSparsityDetector(hessian_sparsity::AbstractMatrix) <: AbstractSparsityDetectorTrivial sparsity detector used to return a known Hessian sparsity pattern.
See also
Coloring algorithm
ADTypes.coloring_algorithm — Functioncoloring_algorithm(ad::AutoSparse)::AbstractColoringAlgorithmReturn the coloring algorithm for a sparse AD choice.
See also
ADTypes.AbstractColoringAlgorithm — TypeAbstractColoringAlgorithmAbstract supertype for Jacobian/Hessian coloring algorithms.
Required methods
Note
The terminology and definitions are taken from the following paper:
What Color Is Your Jacobian? Graph Coloring for Computing Derivatives, Assefaw Hadish Gebremedhin, Fredrik Manne, and Alex Pothen (2005)
ADTypes.column_coloring — Functioncolumn_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 — Functionrow_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 — Functionsymmetric_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 — TypeNoColoringAlgorithm <: AbstractColoringAlgorithmTrivial coloring algorithm, which always returns a different color for each matrix column/row.
See also
No automatic differentiation
ADTypes.NoAutoDiff — TypeNoAutoDiffStruct used to select no automatic differentiation.
Defined by ADTypes.jl.
Constructors
NoAutoDiff()ADTypes.NoAutoDiffSelectedError — TypeNoAutoDiffSelectedError <: ExceptionSignifies that code tried to use automatic differentiation, but NoAutoDiff was specified.
Constructor
NoAutoDiffSelectedError(msg::String)Modes
ADTypes.mode — Functionmode(ad::AbstractADType)Return the differentiation mode of ad, as a subtype of AbstractMode.
ADTypes.AbstractMode — TypeAbstractModeAbstract supertype for the traits identifying differentiation modes.
Subtypes
ADTypes.ForwardMode — TypeForwardModeTrait 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.
ADTypes.ForwardOrReverseMode — TypeForwardOrReverseModeTrait 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 — TypeReverseModeTrait for AD choices that rely on reverse mode algorithmic differentiation.
ADTypes.SymbolicMode — TypeSymbolicModeTrait for AD choices that rely on symbolic differentiation.
Miscellaneous
ADTypes.Auto — FunctionADTypes.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()Deprecated
ADTypes.AutoTapir — TypeAutoTapirAutoTapir 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.