Sensitivity Algorithms for Optimization Problems
SciMLSensitivity provides adjoint algorithms for differentiating through the optimum u*(p) of a parameterized OptimizationProblem, giving dG/dp for any downstream loss G(u*(p)) via implicit differentiation rather than by differentiating through the iterations of the optimizer.
UnconstrainedOptimizationAdjointhandles unconstrained problems by treating the stationarity condition∇f(u*, p) = 0as a steady-state nonlinear system and reusing theSteadyStateAdjointmachinery.OptimizationAdjointhandles problems with equality, two-sided inequality, and variable-bound constraints by implicit differentiation of the KKT first-order optimality conditions. It detects the active inequality set atu*, recovers multipliers from the stationarity equation, and solves a single symmetric KKT linear system to produce the adjoint.
SciMLSensitivity.UnconstrainedOptimizationAdjoint — Type
UnconstrainedOptimizationAdjoint{CS, AD, FDT, VJP, LS, LK, OAD} <: AbstractAdjointSensitivityAlgorithm{CS, AD, FDT}An implementation of adjoint differentiation for unconstrained optimization problems. Uses the implicit function theorem applied to the optimality condition ∇f(u, p) = 0 to compute derivatives of the optimal solution u with respect to parameters p.
Internally, this method converts the optimization problem into a steady-state problem by treating the gradient-zero condition as a nonlinear system, then applies the steady-state adjoint method.
Constructor
UnconstrainedOptimizationAdjoint(; chunk_size = 0, autodiff = true,
diff_type = Val{:central}, objective_ad = true,
autojacvec = nothing, linsolve = nothing,
linsolve_kwargs = (;))Keyword Arguments
autodiff: Use automatic differentiation for constructing the Jacobian if the Jacobian needs to be constructed. Defaults totrue.chunk_size: Chunk size for forward-mode differentiation if full Jacobians are built (autojacvec=falseandautodiff=true). Default is0for automatic choice of chunk size.diff_type: The method used by FiniteDiff.jl for constructing the Jacobian if the full Jacobian is required withautodiff=false.objective_ad: Use automatic differentiation for computing the gradient of the objective function when not provided. Defaults totrue.autojacvec: Calculate the vector-Jacobian product (J'*v) via automatic differentiation with special seeding. The total set of choices are:nothing: uses an automatic algorithm to automatically choose the vjp. This is the default and recommended for most users.false: the Jacobian is constructed via FiniteDiff.jltrue: the Jacobian is constructed via ForwardDiff.jlTrackerVJP: Uses Tracker.jl for the vjp.ZygoteVJP: Uses Zygote.jl for the vjp.EnzymeVJP: Uses Enzyme.jl for the vjp.ReverseDiffVJP(compile=false): Uses ReverseDiff.jl for the vjp.compileis a boolean for whether to precompile the tape, which should only be done if there are no branches (iforwhilestatements) in theffunction.MooncakeVJP: Uses Mooncake.jl for the vjp.
linsolve: the linear solver used in the adjoint solve. Defaults tonothing, which uses a polyalgorithm to choose an efficient algorithm automatically.linsolve_kwargs: keyword arguments to be passed to the linear solver.
For more details on the vjp choices, please consult the sensitivity algorithms documentation page or the docstrings of the vjp types.
References
Johnson, S. G., Notes on Adjoint Methods for 18.336, Online at http://math.mit.edu/stevenj/18.336/adjoint.pdf (2007)
SciMLSensitivity.OptimizationAdjoint — Type
OptimizationAdjoint{CS, AD, FDT, VJP, LS, LK, AT} <: AbstractAdjointSensitivityAlgorithm{CS, AD, FDT}An implementation of adjoint differentiation for constrained optimization problems. Uses implicit differentiation of the KKT first-order optimality conditions to compute derivatives of the optimal solution u* with respect to parameters p, given a cotangent dgdu = dG/du* for some downstream loss G.
Handles equality constraints (lcons == ucons), two-sided inequality constraints (lcons ≤ cons(u, p) ≤ ucons), and variable box bounds (lb ≤ u ≤ ub). The active inequality set is detected by proximity at the optimum and refined by multiplier-sign checks (KKT requires inequality multipliers to be non-negative).
Given the cotangent Δu, the algorithm solves the symmetric KKT system
[ L_xx J_g' J_h' ] [ λ_x ] [ Δu ]
[ J_g 0 0 ] · [ λ_y ] = [ 0 ]
[ J_h 0 0 ] [ λ_z ] [ 0 ]once, then computes dG/dp = -λ' · ∂F/∂p as a single VJP through the KKT residual F(p) = [∇_x L(u*, p); g(u*, p); h_I(u*, p)]. No re-optimization is required.
Constructor
OptimizationAdjoint(; chunk_size = 0, autodiff = true,
diff_type = Val{:forward},
autojacvec = nothing,
linsolve = nothing, linsolve_kwargs = (;),
active_tol = nothing)Keyword Arguments
autodiff: the forward-mode backend for the one derivative the adjoint computes itself — the Lagrangian HessianL_xx(the∂²ₓblock of the KKT matrix), and only as a fallback: when theOptimizationFunctionexposes no second-order information (nolag_h, and not bothhessandcons_h), e.g. for gradient-only solvers like SLSQP. When second-order info is present it is used directly and this backend is never invoked, soautodiffhas no effect for second-order solvers. The residual's first-order stationarity terms are not affected by this setting — they reuse the stored (dual-tolerant)grad/cons_jdirectly. The fallback differentiates the raw Lagrangian (raw objective + prep-free constraints), keeping DI preparation out of the differentiated path. Defaults tonothing, meaning use theOptimizationFunction's own ADType. Pass anADTypesbackend to override:AutoForwardDiff(),AutoFiniteDiff(), orAutoEnzyme()(always run in forward mode). Reverse-mode backends (AutoReverseDiff,AutoZygote,AutoTracker,AutoMooncake) are rejected, since the fallback nests forward-over-forward. For backward compatibility aBoolis also accepted:true→AutoForwardDiff(),false→AutoFiniteDiff(). Validated lazily — an irrelevant setting on a second-order solver does not error. Independent ofautojacvec, which controls the outer VJP.chunk_size: Chunk size for forward-mode differentiation if full Jacobians are built. Default is0for automatic choice of chunk size.diff_type: The FiniteDiff.jl method used for theL_xxHessian fallback whenautodiff = false. Defaults toVal{:forward}. Ignored whenautodiffis an explicitADTypesbackend.autojacvec: Calculate the vector-Jacobian product (λ' · ∂F/∂p) through the KKT residual via automatic differentiation with special seeding. Choices:nothing: chooses an automatic algorithm. Defaults totrue(ForwardDiff via materialized Jacobian) and is recommended for most users.false: the Jacobian is constructed via FiniteDiff.jl.true: the Jacobian is constructed via ForwardDiff.jl. This requires the OptimizationFunction's storedgrad/cons_jto be dual-tolerant (built with a ForwardDiff/Enzyme backend); for a non-dual-tolerant function usefalse. Independent ofautodiff, which only backs theLxxfallback and never enters the outer VJP.ZygoteVJP: Uses Zygote.jl for the vjp.EnzymeVJP: Uses Enzyme.jl for the vjp.ReverseDiffVJP(compile=false): Uses ReverseDiff.jl for the vjp.compileis a boolean for whether to precompile the tape, which should only be done if there are no branches (iforwhilestatements) in theffunction.MooncakeVJP: Uses Mooncake.jl for the vjp.
linsolve: the linear solver used in the KKT solve. Defaults tonothing, which uses a polyalgorithm to choose an efficient algorithm automatically.linsolve_kwargs: keyword arguments to be passed to the linear solver.active_tol: proximity tolerance for active inequality / variable-bound detection. A constraint or bound is considered active atu*when|c(u*) - bound| ≤ active_tol. Defaults tosqrt(eps(eltype(u*)))whennothing.
For more details on the vjp choices, please consult the sensitivity algorithms documentation page or the docstrings of the vjp types.
References
Gould, S., Fernando, B., Cherian, A., Anderson, P., Cruz, R. S., & Guo, E., On Differentiating Parameterized Argmin and Argmax Problems with Application to Bi-level Optimization (2016), https://arxiv.org/abs/1607.05447