Skip to content

Fix bounded trust-region finite differences and matrix workspaces - #1272

Merged
ChrisRackauckas merged 2 commits into
SciML:masterfrom
ChrisRackauckas-Claude:fix/bounded-trust-region-finite-difference
Sep 13, 2026
Merged

ChrisRackauckas merged 2 commits into
SciML:masterfrom
ChrisRackauckas-Claude:fix/bounded-trust-region-finite-difference

Conversation

@ChrisRackauckas-Claude

@ChrisRackauckas-Claude ChrisRackauckas-Claude commented Sep 13, 2026 •

Copy link
Copy Markdown
Member

BoundedTrustRegion(autodiff = AutoFiniteDiff()) could evaluate the residual outside the box while constructing derivatives, and matrix-shaped states failed when flattened Jacobian products were written into matrix workspaces. Reuse the other native bounded methods' feasible finite-difference cache and operator products, and preserve scalar/static/matrix workspace shapes during those products. Both defects reproduce at the first BTR implementation, ab11738.

The shared differentiation code moves from native_bounded.jl into bounded_jacobian.jl so all five bounded methods share the same stencils. Sparse prototypes, analytic callbacks, matrix-free JVP/VJP paths, mixed derivative backends, fixed bounds, and parameter-changing cache reinitialization retain their existing machinery. No new public API or dependencies.

Ignore until reviewed by @ChrisRackauckas.

Verification on Julia 1.13.0, one Julia thread:

  • New official regression block: 144/144 pass.
  • FirstOrder QA: 20/20 pass.
  • FirstOrder NativeBounds: 5,989/5,989 pass.
  • FirstOrder Core: 8,739 passing assertions across 43 top-level summaries.
  • Runic, typos, and git diff --check: clean.

The full Core run covers 23f1b62; NativeBounds and QA were repeated after 9115eb4, the final guard that avoids unused finite-difference setup for analytic/AD paths. Core emits individual top-level summaries, so 8,739 is their sum. Literal final output:

Test Summary:           | Pass  Total   Time
SciMLOperator Jacobians |   14     14  14.8s
     Testing NonlinearSolveFirstOrder tests passed

Latest NativeBounds and QA output:

Test Summary:          | Pass  Total      Time
Native bounded methods | 5989   5989  11m12.3s
     Testing NonlinearSolveFirstOrder tests passed
Test Summary: | Pass  Total     Time
QA            |   20     20  1m59.1s
     Testing NonlinearSolveFirstOrder tests passed

Full groups used the existing test harness:

GROUP=NativeBounds julia +1.13.0 --compiled-modules=existing --project=lib/NonlinearSolveFirstOrder -e 'using Pkg; Pkg.test(; julia_args=["--compiled-modules=existing"])'
GROUP=Core julia +1.13.0 --compiled-modules=existing --project=lib/NonlinearSolveFirstOrder -e 'using Pkg; Pkg.test(; julia_args=["--compiled-modules=existing"])'
GROUP=QA julia +1.13.0 --compiled-modules=existing --project=lib/NonlinearSolveFirstOrder -e 'using Pkg; Pkg.test(; julia_args=["--compiled-modules=existing"])'

Boundary reproducer, run on clean master and again with the fix:

using NonlinearSolve, ADTypes, Test
f(u, p) = (all(0 .<= u .<= 1) || throw(DomainError(u)); u .- 2)
@testset "Bounded finite differences stay feasible" begin
    sol = solve(NonlinearLeastSquaresProblem(f, [1.0]; lb=0.0, ub=1.0),
        BoundedTrustRegion(autodiff=AutoFiniteDiff()); abstol=1e-9)
    @test sol.u ≈ [1.0]
end
Before:
  DomainError with [1.0]:
Test Summary:                            | Error  Total  Time
Bounded finite differences stay feasible |     1      1  6.0s
After:
Test Summary:                            | Pass  Total  Time
Bounded finite differences stay feasible |    1      1  4.6s

Matrix-state reproducer, run at BTR introduction and again with the fix:

using NonlinearSolve, Test
@testset "Bounded trust-region matrix state" begin
    prob = NonlinearProblem((u,p) -> u .- p, zeros(2,2), fill(0.75,2,2); lb=0.,ub=1.)
    sol = solve(prob, BoundedTrustRegion())
    @test sol.u ≈ fill(0.75,2,2)
end
Before:
  DimensionMismatch: incompatible destination size: the destination matrix of size (2, 2) is incompatible with the product of a matrix of size (4, 4) and a matrix of size (4, 1). The destination must be of size (4, 1).
Test Summary:                     | Error  Total  Time
Bounded trust-region matrix state |     1      1  9.2s
After:
Test Summary:                     | Pass  Total  Time
Bounded trust-region matrix state |    1      1  8.5s

GPU and downstream groups were not run. No docstrings, documentation, or public API changed, so a docs build was not needed. Mixed scalar-state/array-residual combinations are a separate pre-existing generalized-solver limitation, documented with a verified explicit BoundedGaussNewton workaround at #1270.

🤖 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).

ChrisRackauckas and others added 2 commits September 13, 2026 07:45
Reuse the native bounded finite-difference stencils for concrete Jacobians
and derivative products, including sparse prototypes and scalar caches.
Preserve multidimensional state and residual shapes when multiplying
Jacobian products into bounded trust-region workspaces.

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
Only construct additional bound vectors and product callbacks when the
selected derivative path requires them. Preserve the original problem
when no derivative callback needs wrapping.

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-Claude
ChrisRackauckas-Claude force-pushed the fix/bounded-trust-region-finite-difference branch from 99a5f0b to 9115eb4 Compare September 13, 2026 11:45
@ChrisRackauckas
ChrisRackauckas marked this pull request as ready for review September 13, 2026 13:46
@ChrisRackauckas
ChrisRackauckas merged commit 9bb6406 into SciML:master Sep 13, 2026
81 checks passed
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