Skip to content

GlobalDiffEq: add adjoint-based global error control as a SciMLSensitivity extension - #3953

Merged
ChrisRackauckas merged 1 commit into
SciML:masterfrom
ChrisRackauckas-Claude:globaldiffeq-adjoint
Sep 11, 2026
Merged

ChrisRackauckas merged 1 commit into
SciML:masterfrom
ChrisRackauckas-Claude:globaldiffeq-adjoint

Conversation

@ChrisRackauckas-Claude

@ChrisRackauckas-Claude ChrisRackauckas-Claude commented Jul 18, 2026 •

Copy link
Copy Markdown
Member

Ignore until reviewed by @ChrisRackauckas. Draft — not ready for merge.

Adjoint-based global error estimation/control for GlobalDiffEq, reworked to
address the review. Updated per the six review threads:

Review responses

  • Use SciMLSensitivity's own machinery, not a hand-rolled adjoint + QuadGK
    (ext:5–9, ext:61). The estimator no longer builds ODEAdjointProblem and
    integrates ⟨λ, P'−f⟩ by hand. The dual-weighted residual ∫ λᵀ(f(P)−P') dt
    is now computed by SciMLSensitivity.adjoint_sensitivities itself: the defect
    r(t)=f(P)−P' is introduced as a scalar forcing ε (u' = f + ε·r), so
    dG/dε = ∫ λᵀ r dt is returned directly as the parameter gradient — no manual
    quadrature. QuadGK and _FixedParameter/SciMLStructures are dropped
    entirely
    (net −1 dependency plus the removed weakdep).
  • Default adjoint (ext:5–9). sensealg now defaults to SciMLSensitivity's
    own default adjoint family (InterpolatingAdjoint), not a hardcoded
    QuadratureAdjoint; any adjoint sensealg may be passed.
  • Surface the estimate in the solution (adjoint.jl:259). SciMLBase.has_global_error(::GlobalAdjoint) = true, and a gtol solve returns a
    solution whose global_error[end] holds the endpoint estimate.
  • Callbacks no longer forbidden (adjoint.jl:326). The prohibition is removed;
    a benign callback estimates without error (covered by a test).

Adjoint VJP caveat (why the default is autojacvec = false)

The estimator differentiates a right-hand side that reads the forward solution's
dense interpolant sol(t). Differentiating that through a vector-Jacobian-product
backend is broken
, verified against finite differences (forcing = ref(t),
abstol = reltol = 1e-10, flat across tol 1e-5…1e-11):

autojacvec Quad / Gauss / GaussKronrod InterpolatingAdjoint
ReverseDiffVJP(false) 0.9502 (~5% wrong) 1.0
ReverseDiffVJP(true) (compiled) 0.9502 (~5% wrong) 0.9502 (~5% wrong)
autojacvec = true (auto) −0.0 / unsupported 1.0
EnzymeVJP crashes (LLVM ERROR, 1.11 & 1.12) crashes
false (ForwardDiff Jacobian) 1.0 1.0

The adjoint λ itself is correct (integrating Quadrature's own λ by hand gives
/FD = 1.0000000003); the error is entirely in the VJP evaluation of ∂f/∂p
through the interpolant. The compiled ReverseDiff tape (ReverseDiffVJP(true))
is wrong even for InterpolatingAdjoint
— it freezes the interpolant value at
trace time — and Enzyme fails to compile ode_interpolation at all. The only
setting correct across every adjoint method and VJP choice is the ForwardDiff
Jacobian (autojacvec = false)
, so it is the default here; it also sidesteps
the Enzyme-on-1.12 crash. A VJP would be cheaper for large systems, but every
ReverseDiff-tape path is wrong in at least the compiled case, so it is not safe to
default to one. Reported upstream:
SciML/SciMLSensitivity.jl#1649 (and the Enzyme compile
failure: EnzymeAD/Enzyme.jl#3565).

Not yet done (tracked follow-ups)

  • Every-step mode (issue thread): loss as the sum over saved steps, filling
    global_error along the whole trajectory. This PR ships the endpoint mode
    (only global_error[end] is populated).
  • Adjoint-tstops control option (adjoint.jl:372): the alternative that
    takes step points from the adjoint and re-solves on that grid.

Verification (local, Julia 1.12)

GROUP=Core (includes the adjoint suite):

Basic functionality                  |  1   1
Algorithm traits forwarding          |  3   3
Adjoint error estimation and control | 18  18
GLEE solvers                         | 91  91
BigFloat support                     |  2   2
     Testing GlobalDiffEq tests passed

GROUP=QA (Aqua + ExplicitImports + JET): Testing GlobalDiffEq tests passed.
Runic: clean.

The ε-forcing estimate was validated against the manual DWR projection and the
exact error on a linear problem: −dp/error = 0.99999952, 0.99999631, 0.99994367
at tol = 1e-3, 1e-4, 1e-5.

Not verified

Downstream packages, GPU paths, allowed-to-fail CI jobs.

🤖 Generated with Claude Code (model: claude-opus-4-8)
https://claude.ai/code/session_01MfUh8tgp3iBmMv9nkBqoyY

Comment on lines +6 to +9
return SciMLSensitivity.QuadratureAdjoint(autojacvec = true)
end

GlobalDiffEq._is_quadrature_adjoint(::SciMLSensitivity.QuadratureAdjoint) = true

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should use the default solver that SciMLSensitivity gives.

Comment on lines +38 to +61
function integrand(t)
u = sol(t, continuity = :right)
du = sol(t, Val{1}, continuity = :right)
rhs = if isinplace
value = similar(u)
prob.f(value, u, prob.p, t)
value
else
prob.f(u, prob.p, t)
end
lambda = adjoint_sol(t, continuity = :right)
return LinearAlgebra.dot(lambda, du - rhs)
end

projection = zero(eltype(prob.u0))
for i in 1:(length(sol.t) - 1)
left = sol.t[i]
right = sol.t[i + 1]
left == right && continue
interval_projection, _ = QuadGK.quadgk(
integrand, left, right; atol = abstol, rtol = reltol
)
projection += interval_projection
end

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should instead be computed via the adjoint problem's definition?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah this is simulating a discrete cost function at the solution times, it should just use that functionality in the adjoint system rather than trying to recreate it. Plus the default adjoint, GaussAdjoint, will do this more efficiently.

@ChrisRackauckas

Copy link
Copy Markdown
Member

Just doing the endpoint is one mode, but a more useful mode is probably to do this on every step. That would be done by instead changing the loss to be computed on the sum of the steps, and modifying the corresponding adjoint problem similarly.

@ChrisRackauckas-Claude

Copy link
Copy Markdown
Member Author

Noted — an every-step mode fits cleanly once the planned SciMLBase global-error interface (a has_global_error trait + a sol.global_error array-of-arrays) is in place: make the adjoint loss the sum over the saved steps and build the matching adjoint problem, so sol.global_error is filled along the trajectory instead of only at the endpoint. I've written up the interface + sequencing plan on #3956. I'll do the every-step adjoint as a follow-up on top of that interface; this PR lands the endpoint estimator/control. (Also: this PR is now unstacked — it applies directly to master with just the adjoint commit.)

sample_count = length(directions)
factor = _sphere_projection_expectation(sample_count, T) /
_sphere_projection_expectation(length(sol.prob.u0), T)
return factor * LinearAlgebra.norm(projections)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it should put this in the new global error spot in the solution

Comment thread lib/GlobalDiffEq/src/adjoint.jl Outdated
Comment on lines +325 to +326
haskey(kwargs, :callback) &&
throw(ArgumentError("GlobalAdjoint does not currently support callbacks"))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see why this would be a limitation when the adjoint system does.

Comment on lines +370 to +372
tolerance_scale = min(0.5, options.safety * gtol / last_estimate)
local_abstol *= tolerance_scale
local_reltol *= tolerance_scale

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is one of the approaches, but the other gives you dts from the adjoint method and then you just use adaptive=false, tstops = dts for that. That version should be implemented as an option as well.

@ChrisRackauckas-Claude
ChrisRackauckas-Claude force-pushed the globaldiffeq-adjoint branch 2 times, most recently from c73a4f0 to 1f52be4 Compare September 11, 2026 00:59
Standalone (unstacked) sublibrary addition: GlobalAdjoint and
adjoint_error_estimate (Cao and Petzold 2004, closes
SciML/GlobalDiffEq.jl#4), as the package extension
GlobalDiffEqSciMLSensitivityExt on SciMLSensitivity + QuadGK weakdeps.
sensealg defaults to nothing and resolves to
QuadratureAdjoint(autojacvec = true) when the extension loads; solving
without it raises an instructive error.

Verified locally on current master: Core (adjoint suite 14/14) and QA
(23 pass, 1 pre-existing declared broken).

Co-Authored-By: Chris Rackauckas <[email protected]>
@ChrisRackauckas
ChrisRackauckas marked this pull request as ready for review September 11, 2026 08:52
@ChrisRackauckas
ChrisRackauckas merged commit eed19e7 into SciML:master Sep 11, 2026
126 of 133 checks passed
ChrisRackauckas referenced this pull request Sep 11, 2026
Add two orthogonal enum options to GlobalAdjoint:

  - scope (GlobalAdjointScope): EndpointError (default) fills only
    sol.global_error[end]; TrajectoryError estimates the error 2-norm at every
    saved time via a discrete adjoint cost placed at each t_i, filling
    sol.global_error along the whole trajectory.
  - control (GlobalAdjointControl): ToleranceRefinement (default) tightens local
    tolerances and re-solves adaptively; StepGridRefinement re-solves
    non-adaptively on exactly the accepted step grid (adaptive=false, stepping on
    the adjoint-informed dts) for a reproducible fixed-step schedule.

Both keep the autojacvec=false (ForwardDiff Jacobian) path, since the estimator
differentiates a RHS that reads the solution interpolant (SciML/SciMLSensitivity.jl#1649).

Closes #4493 and #4494.



Agent-Harness: Claude Code
Agent-Model: claude-opus-4-8
Agent-Session: https://claude.ai/code/session_01MfUh8tgp3iBmMv9nkBqoyY

Co-authored-by: ChrisRackauckas-Claude <[email protected]>
Co-authored-by: Claude <[email protected]>
ChrisRackauckas added a commit that referenced this pull request Sep 11, 2026
- Unify companion global error estimators into GlobalErrorEstimation (#4492)
- Add adjoint-based global error control as a SciMLSensitivity extension (#3953)
- ExpRK: cache ishermitian (rebase of #4305, + review fixes) (#4497)
- Mark discrete time-grid search helpers as Enzyme inactive (#4496)
- GlobalAdjoint: every-step trajectory scope and step-grid control (#4495)
- GlobalAdjoint: defer to adjoint_sensitivities default instead of pinning an adjoint (#4498)
- Release GlobalDiffEq 1.7.1 (#4501)
- Fix GlobalDiffEq precompilation on Julia 1.13 and companion accuracy (#4500)
- Make mass-matrix identity/zero/diagonal checks GPU-safe (#4502)
- Fix BDF step rejection for backward-in-time integration (#4503)



Agent-Harness: Claude Code
Agent-Model: claude-opus-5[1m]
Claude-Session: https://claude.ai/code/session_014FEzNTLFutCmTEAZ3zBg5R

Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
ChrisRackauckas added a commit that referenced this pull request Sep 11, 2026
- Precompile SDE solves through specialization levels (#4467)
- Add 32-bit InterfaceI CI lane via test_groups.toml (#4480)
- incorporate time/stab filter for larger steps in FBDF (#3878)
- Keep 32-bit CI lane hard-failing. (#4482)
- Correct and validate FBDF time and stability filters (#4484)
- Keep DAE init from overwriting uprev during callback truncation (#4486)
- DiffEqBase: Int64 interp_points range length for 32-bit (#4487)
- Release OrdinaryDiffEqBDF 2.4.7 (#4490)
- Unify companion global error estimators into GlobalErrorEstimation (#4492)
- Add adjoint-based global error control as a SciMLSensitivity extension (#3953)



Agent-Harness: Claude Code
Agent-Model: claude-opus-5[1m]
Claude-Session: https://claude.ai/code/session_014FEzNTLFutCmTEAZ3zBg5R

Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
ChrisRackauckas added a commit that referenced this pull request Sep 12, 2026
- Release OrdinaryDiffEqBDF 2.4.7 (#4490)
- Unify companion global error estimators into GlobalErrorEstimation (#4492)
- Add adjoint-based global error control as a SciMLSensitivity extension (#3953)
- ExpRK: cache ishermitian (rebase of #4305, + review fixes) (#4497)
- Mark discrete time-grid search helpers as Enzyme inactive (#4496)
- GlobalAdjoint: every-step trajectory scope and step-grid control (#4495)
- GlobalAdjoint: defer to adjoint_sensitivities default instead of pinning an adjoint (#4498)
- Release GlobalDiffEq 1.7.1 (#4501)
- Fix GlobalDiffEq precompilation on Julia 1.13 and companion accuracy (#4500)
- Make mass-matrix identity/zero/diagonal checks GPU-safe (#4502)



Agent-Harness: Claude Code
Agent-Model: claude-opus-5[1m]
Claude-Session: https://claude.ai/code/session_014FEzNTLFutCmTEAZ3zBg5R

Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
ChrisRackauckas added a commit that referenced this pull request Sep 13, 2026
- Unify companion global error estimators into GlobalErrorEstimation (#4492)
- Add adjoint-based global error control as a SciMLSensitivity extension (#3953)
- ExpRK: cache ishermitian (rebase of #4305, + review fixes) (#4497)
- Mark discrete time-grid search helpers as Enzyme inactive (#4496)
- GlobalAdjoint: every-step trajectory scope and step-grid control (#4495)
- GlobalAdjoint: defer to adjoint_sensitivities default instead of pinning an adjoint (#4498)
- Release GlobalDiffEq 1.7.1 (#4501)
- Fix GlobalDiffEq precompilation on Julia 1.13 and companion accuracy (#4500)
- Make mass-matrix identity/zero/diagonal checks GPU-safe (#4502)
- Fix BDF step rejection for backward-in-time integration (#4503)



Agent-Harness: Claude Code
Agent-Model: claude-opus-5[1m]
Claude-Session: https://claude.ai/code/session_014FEzNTLFutCmTEAZ3zBg5R

Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants