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.

  • UnconstrainedOptimizationAdjoint handles unconstrained problems by treating the stationarity condition ∇f(u*, p) = 0 as a steady-state nonlinear system and reusing the SteadyStateAdjoint machinery.
  • OptimizationAdjoint handles 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 at u*, 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 to true.

  • chunk_size: Chunk size for forward-mode differentiation if full Jacobians are built (autojacvec=false and autodiff=true). Default is 0 for automatic choice of chunk size.

  • diff_type: The method used by FiniteDiff.jl for constructing the Jacobian if the full Jacobian is required with autodiff=false.

  • objective_ad: Use automatic differentiation for computing the gradient of the objective function when not provided. Defaults to true.

  • 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.jl
    • true: the Jacobian is constructed via ForwardDiff.jl
    • TrackerVJP: 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. compile is a boolean for whether to precompile the tape, which should only be done if there are no branches (if or while statements) in the f function.
    • MooncakeVJP: Uses Mooncake.jl for the vjp.
  • linsolve: the linear solver used in the adjoint solve. Defaults to nothing, 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)

source
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 Hessian L_xx (the ∂²ₓ block of the KKT matrix), and only as a fallback: when the OptimizationFunction exposes no second-order information (no lag_h, and not both hess and cons_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, so autodiff has 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_j directly. The fallback differentiates the raw Lagrangian (raw objective + prep-free constraints), keeping DI preparation out of the differentiated path. Defaults to nothing, meaning use the OptimizationFunction's own ADType. Pass an ADTypes backend to override: AutoForwardDiff(), AutoFiniteDiff(), or AutoEnzyme() (always run in forward mode). Reverse-mode backends (AutoReverseDiff, AutoZygote, AutoTracker, AutoMooncake) are rejected, since the fallback nests forward-over-forward. For backward compatibility a Bool is also accepted: true → AutoForwardDiff(), false → AutoFiniteDiff(). Validated lazily — an irrelevant setting on a second-order solver does not error. Independent of autojacvec, which controls the outer VJP.

  • chunk_size: Chunk size for forward-mode differentiation if full Jacobians are built. Default is 0 for automatic choice of chunk size.

  • diff_type: The FiniteDiff.jl method used for the L_xx Hessian fallback when autodiff = false. Defaults to Val{:forward}. Ignored when autodiff is an explicit ADTypes backend.

  • 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 to true (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 stored grad/cons_j to be dual-tolerant (built with a ForwardDiff/Enzyme backend); for a non-dual-tolerant function use false. Independent of autodiff, which only backs the Lxx fallback 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. compile is a boolean for whether to precompile the tape, which should only be done if there are no branches (if or while statements) in the f function.
    • MooncakeVJP: Uses Mooncake.jl for the vjp.
  • linsolve: the linear solver used in the KKT solve. Defaults to nothing, 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 at u* when |c(u*) - bound| ≤ active_tol. Defaults to sqrt(eps(eltype(u*))) when nothing.

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

source