Developer Internals
These names are internal, not public API, and may change without notice. They are used by SciMLSensitivity adjoint, callback, shadowing, and optimization integrations. Application code should select a documented sensitivity algorithm through solve or use the documented problem wrappers rather than calling these helpers directly.
Adjoint and Optimization Internals
SciMLSensitivity.adjointdiffcache — Function
adjointdiffcache(g,sensealg,discrete,sol,dg,alg;quad=false)return (AdjointDiffCache, y)
SciMLSensitivity.OptimizationAdjointProblem — Function
OptimizationAdjointProblem(prob, opt_sol, sensealg, p, Δu) -> dpCompute the parameter sensitivity dp = dG/dp for a scalar loss G via one adjoint KKT solve.
Given Δu = dG/dx* ∈ Rⁿˣ (the cotangent of the optimal solution supplied by the caller), solves the adjoint system KKT * λ_full = [Δu; 0; ...; 0] (one linear solve, exploiting KKT symmetry), then returns dp = -∇_q Φ via a single vecjacobian! call. Rather than VJP'ing the vector KKT residual F(x*, q) = [∇_x L; g; h_I] against λ_full, the (constant) contraction is folded in up front as the scalar Φ(q) = λ_full · F(x*, q), so the outer pass only needs its gradient ∇_q Φ (a scalar VJP, unit seed). Variable-bound rows are dropped since ∂(lb-x)/∂p = 0.
The VJP is computed via sensealg.autojacvec (falls back to ForwardDiff otherwise).
SciMLSensitivity.build_adjoint_jac — Function
build_adjoint_jac(sol, sensealg, alg, f, tspan)Construct a jac(J_adj, λ, p, t) for the adjoint ODE of the state-only adjoint methods (GaussAdjoint, QuadratureAdjoint, whose adjoint state is just λ). The adjoint rhs -(∂f/∂u)ᵀλ is linear in λ, so its Jacobian is exactly -(∂f/∂u)ᵀ evaluated at the interpolated forward solution — independent of λ.
When the user supplies an analytical jac (with a jac_prototype), evaluate it at y(t) and negate-transpose. Otherwise, for dense in-place ODEs solved by a ForwardDiff-based implicit method, fall back to differentiating the forward rhs (ForwardDiff/FiniteDiff per sensealg's autodiff setting) and negate-transposing. Without this, the implicit adjoint solver differentiates the vjp-based adjoint rhs itself, which costs a full vjp (plus a forward-solution interpolation) per Jacobian column — and with ReverseDiffVJP re-records the tape with Dual numbers on every Jacobian evaluation.
Returns nothing when no cheap exact Jacobian can be built; the adjoint solver then falls back to its own AD/FD of the adjoint rhs.
SciMLSensitivity.enzyme_rhs — Function
enzyme_rhs(f)The function the Enzyme VJPs differentiate. They only ever call the right-hand side, so for an ODEFunction this is the bare user function rather than the container. The container also carries sys, observed, initialization_data, ... which Enzyme cannot prove derivative-free (e.g. the Dict{SymbolicT, SymbolicT} maps in ModelingToolkit's InitializationMetadata), so differentiating it means allocating a shadow of all of that and remake_zero!-ing it on every VJP call: for an MTK-generated 32-state linear ODE ~190 µs per call against a ~5 ns rhs, making GaussAdjoint(EnzymeVJP) gradients ~8x slower than necessary. DAEFunctions already get this treatment through dae_unwrapped_f.
An ODEFunction whose f.f is an AbstractSciMLOperator is kept whole: its call f(du, u, p, t) forwards as f.f(du, u, u, p, t), which the bare operator would not.
Contract: the shadow is built at configuration time and the primal at call time, in different places (adjointdiffcache/_vecjacobian!, the Gauss and Quadrature integrands). Both sides must go through enzyme_rhs so Duplicated(primal, shadow) sees matching types; any new method here has to be audited against every such pair.
SciMLSensitivity._init_originator_gradient — Function
_init_originator_gradient(originator, f, tunables)Compute ∂(f(tunables))/∂tunables for a scalar-valuedf, dispatched on the AD originator that triggered the surrounding _concrete_solve_adjoint rrule.
Used by the DAE/ODE initialization adjoint path to differentiate get_initial_values w.r.t. the tunable parameters. The default implementation uses Zygote (which composes with all remaining ChainRules-aware originators: ChainRulesOriginator, ReverseDiffOriginator, TrackerOriginator). The EnzymeOriginator and MooncakeOriginator methods use the corresponding native AD so the rrule does not pull in Zygote when those backends are driving differentiation. The Mooncake method lives in ext/SciMLSensitivityMooncakeExt.jl.
Callback Tracking
SciMLSensitivity.track_callbacks — Function
Appends a tracking process to determine the time of the callback to be used in the reverse pass. The rationale is explain in:
https://github.com/SciML/SciMLSensitivity.jl/issues/4
SciMLSensitivity.setup_reverse_callbacks — Function
Sets up callbacks for the adjoint pass. This is a version that has an effect at each event of the forward pass and defines the reverse pass values via the vjps as described in https://arxiv.org/pdf/1905.10403.pdf Equation 13.
For more information, see https://github.com/SciML/SciMLSensitivity.jl/issues/4
SciMLSensitivity._has_effective_callback — Function
_has_effective_callback(cb)Whether a callback keyword argument actually carries callbacks. An empty CallbackSet — e.g. the CallbackSet{Vector{Any}, Vector{Any}} that DiffEqBase's callback type-erasure injects into solve kwargs on Julia >= 1.12 — must behave like callback = nothing.
Derivative Wrappers and VJP Traits
SciMLSensitivity.jacobianvec! — Function
jacobianvec!(Jv, f, x, v, alg, (buffer, seed)) -> nothing
$Jv <- J(f(x))v$
SciMLSensitivity.supports_callback_vjp — Function
supports_callback_vjp(autojacvec) -> BoolReturn true if the VJP backend can differentiate the affect functions of tracked callbacks (i.e. has a dedicated branch in get_cb_diffcaches and a _vecjacobian! path that works with CallbackSensitivityFunction).
When false, the callback adjoint code falls back to ReverseDiffVJP() for its own VJP computations — the ODE adjoint may still use the user-requested backend, since the callback affect functions are traced separately.
SciMLSensitivity.supports_structured_vjp — Function
supports_structured_vjp(autojacvec) -> BoolReturn true if the VJP backend can natively differentiate through structured (non-array) parameter types like NamedTuples from Functors.jl.
When false, Functors.jl parameter structs are not supported and an informative error will be thrown.
SciMLSensitivity.sensealg_autodiff_as_bool — Function
sensealg_autodiff_as_bool(autodiff) -> BoolNormalize a sensealg autodiff keyword to the Bool type parameter still used internally by SciMLSensitivity.
Accepts:
Bool(true/false) — historical APIAutoForwardDiff— maps totrue(build Jacobians via AD)AutoFiniteDiff— maps tofalse(build Jacobians via finite differences)
Other ADTypes backends are rejected because sensealg Jacobian construction is either ForwardDiff chunked AD or FiniteDiff; there is no Enzyme/Zygote path for the dense Jacobian (autojacvec=false) branch.
SciMLSensitivity._get_sensitivity_vjp_verbose — Function
_get_sensitivity_vjp_verbose(verbose)Extract the verbosity setting for sensitivity VJP choice warnings.
Returns true if warnings should be displayed, false if they should be silenced. Handles:
Bool: used directlyNonlinearVerbosity(or similar types withsensitivity_vjp_choicefield): checks the toggle- Other types (e.g.
SciMLLoggingpresets): defaults totruefor backward compatibility
Parameter and Cotangent Helpers
SciMLSensitivity.recursive_copyto! — Function
recursive_copyto!(y, x)y[:] .= vec(x) for generic x and y. This is used to handle non-array parameters!
SciMLSensitivity.recursive_add! — Function
recursive_add!(y, x)y .+= x for generic x and y. This is used to handle non-array parameters!
SciMLSensitivity.recursive_sub! — Function
recursive_sub!(y, x)y .-= x for generic x and y. This is used to handle non-array parameters!
SciMLSensitivity.recursive_neg! — Function
neg!(x)x .*= -1 for generic x. This is used to handle non-array parameters!
SciMLSensitivity.recursive_adjoint — Function
recursive_adjoint(y)adjoint(y) for generic y. This is used to handle non-array parameters!
SciMLSensitivity.allocate_zeros — Function
allocate_zeros(x)zero.(x) for generic x. This is used to handle non-array parameters!
SciMLSensitivity.allocate_vjp — Function
allocate_vjp(λ, x)
allocate_vjp(x)similar(λ, size(x)) for generic x. This is used to handle non-array parameters!
SciMLSensitivity.mutable_zeros — Function
mutable_zeros(x)A zeroed mutable buffer shaped like x: allocate_zeros(x) when that is already mutable (or x is not an array), otherwise a zeroed similar(x) (e.g. an MVector for an SVector). Used for accumulation buffers derived from possibly immutable (static-array) tunables.
SciMLSensitivity.tunable_cotangent — Function
tunable_cotangent(p, Δp)Project the structural parameter cotangent Δp (nested Tangents or NamedTuples mirroring p, or an array for array parameters) onto the tunable portion of p. Returns a cotangent aligned with canonicalize(Tunable(), p)[1] — the children of p when p is only a Functors functor — or nothing when there is no contribution.
SciMLSensitivity.solution_parameter_cotangent — Function
solution_parameter_cotangent(Δ)The parameter cotangent a solution tangent Δ carries in Δ.prob.p, or nothing. Reverse rules on solution indexing (sol[sym] for an observed variable) put the observed function's parameter cotangent there, since that part of the gradient is not in Δ.u.
Transformed Functions and Shadowing Regularizers
SciMLSensitivity.TransformedFunction — Type
TransformedFunctionAbstract supertype for transformed differential-equation functions used inside SciMLSensitivity.
Concrete subtypes adapt a user-supplied model into the drift or sensitivity form required by a sensitivity algorithm. This is developer API for SciMLSensitivity internals and extensions; it is not a user-facing modeling interface.
SciMLSensitivity.TimeDilation — Type
TimeDilation{T1 <: Number} <: AbstractLSSregularizerA regularization method for LSS. See ?LSS for additional information and other methods.
Constructor
TimeDilation(alpha;
t0skip = zero(alpha),
t1skip = zero(alpha))