Skip to content

Respect active bounds in ForwardDiff least-squares sensitivities - #1271

Merged
ChrisRackauckas merged 1 commit into
SciML:masterfrom
ChrisRackauckas-Claude:fix/bounded-implicit-sensitivity
Sep 13, 2026
Merged

ChrisRackauckas merged 1 commit into
SciML:masterfrom
ChrisRackauckas-Claude:fix/bounded-implicit-sensitivity

Conversation

@ChrisRackauckas-Claude

Copy link
Copy Markdown
Member

For min (u-p)^2 with 0 ≤ u ≤ 1 and p=2, BoundedTrustRegion() returns u=1, but ForwardDiff currently reports du/dp=1 instead of zero. Differentiate the projected stationarity equation in the shared least-squares sensitivity helper so active/fixed variables and coupled free variables satisfy the constrained system. Scalar-state sensitivities also preserve scalar shape and use a derivative instead of an array Jacobian.

This covers direct solve and cached solve!/reinit!, in-place and out-of-place residuals, nested AD and analytic Jacobian/VJP callbacks. The documented assumptions are parameter-independent bounds, a locally unchanged active set, and a nonsingular stationarity Jacobian on free variables; no derivative is claimed at active-set transitions.

Verification

Julia 1.13.0, using /home/crackauc/.juliaup/bin/julia +1.13.0 --compiled-modules=existing. No dependencies or package versions changed.

The exact new regression file on the unfixed native solver introduction, ab11738:

julia --project -e 'include("test/Core/bounds_sensitivity_tests.jl")'
Bounded least-squares implicit sensitivities | Pass  Fail  Error  Total
                                            |   14    16     12     42

The 16 assertion failures include active-bound derivatives of 1 versus expected 0; the 12 errors are unsupported scalar jacobian, vec, or reshape calls. The native solver is absent from that commit's parent. Both relevant Base AD source files are identical between this introduction and current master, and the one-variable reproducer independently fails on clean current master with derivative = 1.0.

The identical regression file with this fix:

Bounded least-squares implicit sensitivities | Pass  Total
                                            |   42     42

Full local checks:

GROUP=Core julia --project -e 'using Pkg; Pkg.test(; julia_args=["--compiled-modules=existing"])'
82 testsets, 1538 passing assertions, 89 pre-existing broken assertions
Bounds: implicit sensitivities | 42  42
Testing NonlinearSolve tests passed

GROUP=QA julia --project -e 'using Pkg; Pkg.test(; julia_args=["--compiled-modules=existing"])'
QA | 28  28
Testing NonlinearSolve tests passed

GROUP=NonlinearSolveBase_QA julia --project -e 'using Pkg; Pkg.test(; julia_args=["--compiled-modules=existing"])'
QA | 20  20
Testing NonlinearSolveBase tests passed

julia --project=docs docs/make.jl
[ Info: RenderDocument: rendering document.
[ Info: Automatic `version="4.34.0"` for inventory from ../Project.toml
Exit 0

Runic, typos on changed files, and git diff --check passed. GPU, downstream packages, LTS, and reverse-mode sensitivities were not verified.

Please ignore this draft until reviewed by @ChrisRackauckas.

🤖 Generated with Codex CLI 0.154.0 (model: gpt-6-astra). Session: local transcript /home/crackauc/.codex/sessions/2026/09/12/rollout-2026-09-12T04-30-15-01a094bd-179a-7972-97ec-36badb9448cd.jsonl.

Differentiate the projected stationarity equation so active and fixed variables
have zero sensitivity while the coupled free variables use their constrained
stationarity system. Preserve scalar states across derivative, Jacobian, and
VJP callbacks, and exercise solve, cached solve, and parameter reinitialization.

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Codex <[email protected]>
Agent-Harness: Codex CLI 0.154.0
Agent-Model: gpt-6-astra
Agent-Session: local transcript /home/crackauc/.codex/sessions/2026/09/12/rollout-2026-09-12T04-30-15-01a094bd-179a-7972-97ec-36badb9448cd.jsonl
@ChrisRackauckas
ChrisRackauckas marked this pull request as ready for review September 13, 2026 13:34
@ChrisRackauckas
ChrisRackauckas merged commit f187c49 into SciML:master Sep 13, 2026
100 checks passed
ChrisRackauckas added a commit that referenced this pull request Sep 13, 2026
…mode adjoint (#1275)

* Add reverse-mode adjoint for NonlinearLeastSquaresProblem

Zygote/Mooncake/Enzyme through solve(::NonlinearLeastSquaresProblem)
previously hit SciMLBase's AdjointNotFoundError: the solve_up rrule
routes to _concrete_solve_adjoint, and no dispatch existed for NLLS
(SciMLSensitivity's ConcreteNonlinearProblem union excludes it).

The adjoint differentiates the projected stationarity equation
G(u,p) = u − Π(u − ∇φ) rather than the residual system — the residual
is rectangular and need not vanish at active bounds. This is the
reverse-mode counterpart of the ForwardDiff fix from #1271 and shares
its assumptions: parameter-independent bounds, locally unchanged active
set, nonsingular stationarity Jacobian on the free variables. Components
pinned at an active bound get zero sensitivity; free components solve
J_u' λ = Δu and accumulate dp = −J_p' λ.

The primal is re-solved via solve_call on the already-concrete problem
so the returned NonlinearSolution's prob.f.f stays unwrapped on the
Enzyme originator path, matching the rule's declared primal type (the
traced forward solve skips AutoSpecializeCallable wrapping inside
within_autodiff, but the wrap would otherwise re-run inside the rule).

SteadyStateAdjoint is the only supported sensealg; other sensitivity
algorithms throw an explanatory ArgumentError. Scalar/array states and
parameters, rectangular residuals, analytic jac/vjp callbacks, and
save_idxs are covered by the new Adjoint-group test, which compares
ForwardDiff against Zygote, Enzyme, and Mooncake.

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Agent-Harness: Devin CLI 3000.10.21
Agent-Model: SWE-2 Max
Agent-Session: /home/crackauc/.local/share/devin/cli/summaries/history_036c03678c564696.md (local transcript; earlier Codex CLI 0.154.0 / gpt-6-astra session at /home/crackauc/.codex/sessions/2026/09/12/rollout-2026-09-12T04-30-15-01a094bd-179a-7972-97ec-36badb9448cd.jsonl)

* Move the NLLS _concrete_solve_adjoint dispatch to SciMLSensitivity

Concrete-solve adjoint dispatches live in SciMLSensitivity (its
ConcreteNonlinearProblem dispatch covers NonlinearProblem et al.), so the
reverse-mode hook moves to a SciMLSensitivityNonlinearSolveBaseExt
extension there. NonlinearSolveBase keeps the shared machinery the
dispatch calls: nlls_solve_adjoint_dp (projected-stationarity adjoint)
and implicit_sensitivity_solve (shared with the ForwardDiff path).

The regression test moves to SciMLSensitivity's suite alongside the
other nonlinear-solve adjoint tests.

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Agent-Harness: Devin CLI 3000.10.21
Agent-Model: SWE-2 Max
Agent-Session: /home/crackauc/.local/share/devin/cli/summaries/history_036c03678c564696.md (local transcript)

* Bump NonlinearSolveBase to 2.49.6

SciMLSensitivity's NLLS adjoint dispatch sets its NonlinearSolveBase
compat floor to 2.49.6 — the first release containing
nlls_solve_adjoint_dp and implicit_sensitivity_solve.

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Agent-Harness: Devin CLI 3000.10.21
Agent-Model: SWE-2 Max
Agent-Session: /home/crackauc/.local/share/devin/cli/summaries/history_036c03678c564696.md (local transcript)

* Declare solve_call and nlls_solve_adjoint_dp public

SciMLSensitivity's NLLS adjoint dispatch calls both, and ExplicitImports
requires cross-package accesses to be public. solve_call gains a
docstring since it is now developer-facing API.

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Agent-Harness: Devin CLI 3000.10.21
Agent-Model: SWE-2 Max
Agent-Session: /home/crackauc/.local/share/devin/cli/summaries/history_036c03678c564696.md (local transcript)

---------

Co-authored-by: ChrisRackauckas-Claude <[email protected]>
Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
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