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.OptimizationAdjointProblem — Function
OptimizationAdjointProblem(prob, opt_sol, sensealg, p, Δu) -> dp

Compute 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).

source
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.

source
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.

source
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.

source

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

source
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

source
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.

source

Derivative Wrappers and VJP Traits

SciMLSensitivity.supports_callback_vjp — Function
supports_callback_vjp(autojacvec) -> Bool

Return 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.

source
SciMLSensitivity.supports_structured_vjp — Function
supports_structured_vjp(autojacvec) -> Bool

Return 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.

source
SciMLSensitivity.sensealg_autodiff_as_bool — Function
sensealg_autodiff_as_bool(autodiff) -> Bool

Normalize a sensealg autodiff keyword to the Bool type parameter still used internally by SciMLSensitivity.

Accepts:

  • Bool (true/false) — historical API
  • AutoForwardDiff — maps to true (build Jacobians via AD)
  • AutoFiniteDiff — maps to false (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.

source
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 directly
  • NonlinearVerbosity (or similar types with sensitivity_vjp_choice field): checks the toggle
  • Other types (e.g. SciMLLogging presets): defaults to true for backward compatibility
source

Parameter and Cotangent Helpers

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.

source
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.

source
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.

source

Transformed Functions and Shadowing Regularizers

SciMLSensitivity.TransformedFunction — Type
TransformedFunction

Abstract 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.

source
SciMLSensitivity.TimeDilation — Type
TimeDilation{T1 <: Number} <: AbstractLSSregularizer

A regularization method for LSS. See ?LSS for additional information and other methods.

Constructor

TimeDilation(alpha;
    t0skip = zero(alpha),
    t1skip = zero(alpha))
source