Skip to content

Stop paying for the inner residual the stage never reads - #4265

Merged
ChrisRackauckas merged 1 commit into
SciML:masterfrom
ChrisRackauckas-Claude:nsa-nfship
Aug 15, 2026
Merged

ChrisRackauckas merged 1 commit into
SciML:masterfrom
ChrisRackauckas-Claude:nsa-nfship

Conversation

@ChrisRackauckas-Claude

Copy link
Copy Markdown
Member

Please ignore until reviewed by @ChrisRackauckas.

Depends on SciML/NonlinearSolve.jl#1167 — but degrades gracefully, so it can merge before that releases (see below).

NonlinearSolveAlg pays one more rhs evaluation per implicit stage solve than NLNewton. For m Newton iterations NLNewton evaluates the stage residual m times; NSA evaluates it m + 1. The extra one is the residual at the converged iterate, produced by the evaluate_f! that ends NonlinearSolveFirstOrder's step! — nothing reads it, and the next stage's reinit! discards it. Being per stage solve rather than per step, it costs multi-stage SDIRK proportionally more (KenCarp4 has 5 implicit stages).

This became visible only after #4045 fixed a 22–37% undercount in NSA's stats.nf.

Budget

Bruss1D/FBDF at rtol 1e-9, identical trajectories both sides (naccept 1053, nreject 14), attributed by stacktrace() at a counted rhs — sums are exact:

call site NLNewton NSA
_compute_rhs! in compute_step! (1 per Newton iteration) 1738 —
evaluate_f! at the tail of each inner step! — 1760
reinit_common! from initialize! (1 per stage solve) — 1067
AD Jacobian 80 80
integrator init / FSAL 3 4
total counted 1821 2911

The excess is 1090 = 1067 stage solves + 22 extra Newton iterations + 1. The reinit! evaluation is necessary (F(z₀) at a new predictor is genuinely unknown — NLNewton computes the same thing in its first iteration); the waste is the trailing one.

Change

compute_step! asks the inner cache whether it supports deferral, steps with evaluate_residual = false when it does, and calls sync_inner_residual! at the three sites that read the residual (the next step's RHS via fnorm_prev, stalled_inner_step, and the inner_solve_failed veto). Everything else keeps the eager path.

Graceful degradation. The deferral is gated on isdefined, so against a NonlinearSolveBase without the new API NSA behaves exactly as today. Verified against registry NonlinearSolveBase 2.43.0: supports_deferred_residual undefined, sync_inner_residual! makes 0 f calls, and a 408-row battery is bit-for-bit identical to master (total f 403,721,198). The Project.toml floor is raised to 2.45 in this PR; if that is inconvenient before the upstream release, the floor is the only thing that needs deferring.

Effect

60 configurations — ROBER/HIRES/POLLU/VdPstiff/Bruss1D × TRBDF2/KenCarp4/FBDF/QNDF × rtol 1e-6/1e-9/1e-12:

NSA total nf  10,857,930 -> 6,587,656
per-config ratio: median 0.787  min 0.538  max 0.883
rows differing in trajectory, stats or error: 0 / 120

retcode, naccept, nreject, njacs, nw, nnonliniter, nnonlinconvfail and the error against a Rodas5P reference (reltol 1e-13/abstol 1e-15) are identical on every row — compared row by row, not by summary statistics. NLNewton rows are untouched.

NSA/NLNewton nf ratio: median 1.356 → 1.047, mean 1.453 → 1.100. On some configurations NSA becomes cheaper than NLNewton (ROBER/KenCarp4 313,399 → 170,969 against 193,131).

Tests

nsa_stats_tests.jl gains "one residual evaluation per Newton iteration". Negative control on unmodified master — 5 of 15 fail, each excess equal to that method's implicit stage count per step:

Evaluated: 1.0335570469798658 <= 0.2862903225806452
Evaluated: 1.1331658291457287 <= 0.25753768844221103
Evaluated: 2.168              <= 1.262

All nine nsa_*.jl files pass on this branch: 39 + 21 + 13 + 10 + 10 + 14 + 8 + 102 + 70 = 287, 0 failures. Runic clean. OrdinaryDiffEqNonlinearSolve 2.8.0 → 2.9.0.

What this does not fix

After it, the residual nf gap is small for FBDF (ROBER 1.09×, Bruss1D 1.02×). Two unrelated contributors remain on SDIRK, both worth their own investigation:

  • NSA takes more steps on some problems — ROBER/TRBDF2@1e-9 is 1.37× the f but also 1.36× the accepted steps, i.e. per-step cost is identical at 2.54. Purely step selection.
  • NSA hits nonlinear convergence failures where NLNewton has none (Bruss1D/TRBDF2@1e-9: 112 vs 0), each forcing a fresh Jacobian — that accounts for the whole remaining 1.73× there.

🤖 Generated with Claude Code

`NLNewton` evaluates the stage residual `m` times for an `m`-iteration stage;
`NonlinearSolveAlg` evaluated it `m + 1`. The extra one is the evaluation that ends the inner
`step!`: it is the right-hand side of the next Newton step, so on the step that converges the
stage nothing reads it and the next stage's `reinit!` discards it.

`step!` now takes `evaluate_residual = false` where the inner cache reports it can honour it,
and `sync_inner_residual!` brings the residual forward at the three places this file reads
it — the next step's right-hand side, the stall test that vetoes a motionless iterate, and
the residual check on a failed step. The inner cache tracks the outstanding evaluation, so a
sync when nothing was deferred costs nothing.

Trajectories are unchanged: `naccept`, `nreject`, `njacs`, `nw`, `nnonliniter`,
`nnonlinconvfail` and the error against a tight `Rodas5P` reference are identical on every
configuration of a 60-config sweep over ROBER/HIRES/POLLU/VdPstiff/Bruss1D x
TRBDF2/KenCarp4/FBDF/QNDF x rtol 1e-6/1e-9/1e-12.

The `NonlinearSolveBase` API this uses postdates the compat floor, so a shim keeps the
deferral off on an older version.

Co-Authored-By: Chris Rackauckas <[email protected]>
@ChrisRackauckas
ChrisRackauckas marked this pull request as ready for review August 15, 2026 11:10
@ChrisRackauckas
ChrisRackauckas merged commit 7573f2f into SciML:master Aug 15, 2026
53 of 172 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