Repository navigation
GlobalDiffEq: add adjoint-based global error control as a SciMLSensitivity extension - #3953
Conversation
ead709c to
78f31a5
Compare
78f31a5 to
d1c2053
Compare
| return SciMLSensitivity.QuadratureAdjoint(autojacvec = true) | ||
| end | ||
|
|
||
| GlobalDiffEq._is_quadrature_adjoint(::SciMLSensitivity.QuadratureAdjoint) = true |
There was a problem hiding this comment.
We should use the default solver that SciMLSensitivity gives.
| 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 |
There was a problem hiding this comment.
This should instead be computed via the adjoint problem's definition?
There was a problem hiding this comment.
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.
|
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. |
|
Noted — an every-step mode fits cleanly once the planned SciMLBase global-error interface (a |
d1c2053 to
6f85aba
Compare
d59406d to
b405854
Compare
cf48854 to
1c4c99a
Compare
1c4c99a to
a201066
Compare
| sample_count = length(directions) | ||
| factor = _sphere_projection_expectation(sample_count, T) / | ||
| _sphere_projection_expectation(length(sol.prob.u0), T) | ||
| return factor * LinearAlgebra.norm(projections) |
There was a problem hiding this comment.
it should put this in the new global error spot in the solution
| haskey(kwargs, :callback) && | ||
| throw(ArgumentError("GlobalAdjoint does not currently support callbacks")) |
There was a problem hiding this comment.
I don't see why this would be a limitation when the adjoint system does.
| tolerance_scale = min(0.5, options.safety * gtol / last_estimate) | ||
| local_abstol *= tolerance_scale | ||
| local_reltol *= tolerance_scale |
There was a problem hiding this comment.
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.
c73a4f0 to
1f52be4
Compare
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]>
1f52be4 to
03c656b
Compare
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]>
- 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]>
- 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]>
- 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]>
- 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]>
Adjoint-based global error estimation/control for
GlobalDiffEq, reworked toaddress the review. Updated per the six review threads:
Review responses
(ext:5–9, ext:61). The estimator no longer builds
ODEAdjointProblemandintegrates
⟨λ, P'−f⟩by hand. The dual-weighted residual∫ λᵀ(f(P)−P') dtis now computed by
SciMLSensitivity.adjoint_sensitivitiesitself: the defectr(t)=f(P)−P'is introduced as a scalar forcingε(u' = f + ε·r), sodG/dε = ∫ λᵀ r dtis returned directly as the parameter gradient — no manualquadrature. QuadGK and
_FixedParameter/SciMLStructures are droppedentirely (net −1 dependency plus the removed weakdep).
sensealgnow defaults to SciMLSensitivity'sown default adjoint family (
InterpolatingAdjoint), not a hardcodedQuadratureAdjoint; any adjoint sensealg may be passed.SciMLBase.has_global_error(::GlobalAdjoint) = true, and agtolsolve returns asolution whose
global_error[end]holds the endpoint estimate.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-productbackend is broken, verified against finite differences (forcing
= ref(t),abstol = reltol = 1e-10, flat acrosstol1e-5…1e-11):autojacvecReverseDiffVJP(false)ReverseDiffVJP(true)(compiled)autojacvec = true(auto)EnzymeVJPLLVM ERROR, 1.11 & 1.12)false(ForwardDiff Jacobian)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/∂pthrough the interpolant. The compiled ReverseDiff tape (
ReverseDiffVJP(true))is wrong even for
InterpolatingAdjoint— it freezes the interpolant value attrace time — and Enzyme fails to compile
ode_interpolationat all. The onlysetting correct across every adjoint method and VJP choice is the ForwardDiff
Jacobian (
autojacvec = false), so it is the default here; it also sidestepsthe 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)
global_erroralong the whole trajectory. This PR ships the endpoint mode(only
global_error[end]is populated).tstopscontrol option (adjoint.jl:372): the alternative thattakes step points from the adjoint and re-solves on that grid.
Verification (local, Julia 1.12)
GROUP=Core(includes the adjoint suite):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.99994367at
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