Skip to content

NonlinearSolveAlg: report the work actually performed - #4045

Merged
ChrisRackauckas merged 1 commit into
SciML:masterfrom
ChrisRackauckas-Claude:nsa-stats-accounting
Jul 28, 2026
Merged

ChrisRackauckas merged 1 commit into
SciML:masterfrom
ChrisRackauckas-Claude:nsa-stats-accounting

Conversation

@ChrisRackauckas-Claude

Copy link
Copy Markdown
Member

Please ignore until reviewed by @ChrisRackauckas.

initialize! forwarded the inner NonlinearSolve cache's counters verbatim. That gets both nf and njacs wrong, in opposite directions, so any work-precision comparison between NonlinearSolveAlg and NLNewton is reading a different quantity for each.

Measured against a counting f/jac on ROBER, rtol=1e-8, atol=1e-10:

nf reported / real njacs reported / real
TRBDF2 NLNewton 20050 / 20050 9 / 9
TRBDF2 NSA 21326 / 27833 (−23.4%) 1598 / 34 (+4600%)
KenCarp4 NSA 7009 / 9024 (−22.3%) 205 / 59 (+247%)
FBDF NSA 1317 / 2085 (−36.8%) 115 / 10 (+1050%)

nf

reinit! evaluates the residual at the new z and then zeroes the inner counters, so that evaluation never appears in cache.cache.stats.nf. It is exactly one lost f call per stage, which the arithmetic confirms: FBDF misses 768 calls over 768 stages, TRBDF2 misses 6507 over 2 stages x ~3254 steps.

njacs

Under W reuse the inner solver's "Jacobian" is WReuseJac, which copies the W assembled here rather than evaluating anything — its njacs equals nw in every configuration. Meanwhile the real evaluations in _update_nlsolvealg_W! were counted nowhere, because calc_J! (which does the counting for the legacy path) is bypassed there.

So: count reinit!'s evaluation, record the real Jacobian evaluation where it happens, and only take the inner cache's njacs when the inner solver actually owns the Jacobian.

After

case nf err njacs reported / real
analytic jac −0.0% 59 / 59
AD jac −0.0% 59 / —
NLNewton (unchanged) 0.0% 53 / 53

njacs is exact; nf is within 0.1%. The few remaining calls are the inner cache's construction, which happens outside the per-stage accounting.

Known remaining gap

Out-of-place improves from −50% to −25% but is still short. There W === nothing, the inner solver owns the Jacobian, and it does not count the f evaluations its AD performs — that is upstream in NonlinearSolve rather than here, so I did not paper over it from this side. Its njacs (1145) is genuine: without W reuse the inner solver really does build a Jacobian per iteration.

Test

New nsa_stats_tests.jl, registered in runtests.jl, asserting for KenCarp4/TRBDF2/FBDF x NLNewton/NonlinearSolveAlg that nf never overstates, is within 1% of the real count, and njacs matches exactly. Verified it fails without the source change:

   Evaluated: 1598 == 34
   Evaluated: 1317 >= 2064.15
   Evaluated: 115 == 10
Test Summary:                                       | Pass  Fail  Total
stats                                               |   18     6     24

The six failures are exactly the NonlinearSolveAlg rows; the NLNewton rows pass either way, as they should. With the change 24/24, and the other four NSA files are unchanged (smooth_est 10, sparse 10, matrixfree 14, jacreuse 11). Runic clean.

🤖 Generated with Claude Code

`initialize!` forwarded the inner NonlinearSolve cache's counters verbatim, which
got both `nf` and `njacs` wrong, in opposite directions.

`nf`: `reinit!` evaluates the residual at the new `z` and *then* zeroes the inner
counters, so that evaluation was never visible -- one lost `f` call per stage.
On ROBER at rtol 1e-8 that is a 22-37% undercount (FBDF reported 1317 against
2085 real calls).

`njacs`: under `W` reuse the inner solver's "Jacobian" is `WReuseJac`, which only
copies the `W` this package assembled, so its `njacs` tracks `nw` rather than
Jacobian evaluations, while the real evaluations in `_update_nlsolvealg_W!` were
counted nowhere -- `calc_J!`, which does the counting for the legacy path, is
bypassed there. TRBDF2 reported 1598 Jacobian evaluations against 34 real ones.

Count `reinit!`'s evaluation, record the real Jacobian evaluation where it
happens, and only take the inner cache's `njacs` when the inner solver actually
owns the Jacobian. `njacs` becomes exact and `nf` lands within 0.1%; NLNewton is
untouched and stays exact.

Out-of-place improves 50% -> 25% but is still short: there the inner solver owns
the Jacobian and does not count the evaluations its AD does, which is upstream of
this package.

Co-Authored-By: Chris Rackauckas <[email protected]>
@ChrisRackauckas
ChrisRackauckas marked this pull request as ready for review July 28, 2026 09:04
@ChrisRackauckas
ChrisRackauckas merged commit 8037f9f into SciML:master Jul 28, 2026
9 of 22 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