Repository navigation
Stop paying for the inner residual the stage never reads - #4265
Merged
Merged
Conversation
`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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Please ignore until reviewed by @ChrisRackauckas.
Depends on SciML/NonlinearSolve.jl#1167 — but degrades gracefully, so it can merge before that releases (see below).
NonlinearSolveAlgpays one more rhs evaluation per implicit stage solve thanNLNewton. FormNewton iterationsNLNewtonevaluates the stage residualmtimes; NSA evaluates itm + 1. The extra one is the residual at the converged iterate, produced by theevaluate_f!that endsNonlinearSolveFirstOrder'sstep!— nothing reads it, and the next stage'sreinit!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:_compute_rhs!incompute_step!(1 per Newton iteration)evaluate_f!at the tail of each innerstep!reinit_common!frominitialize!(1 per stage solve)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 —NLNewtoncomputes 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 withevaluate_residual = falsewhen it does, and callssync_inner_residual!at the three sites that read the residual (the next step's RHS viafnorm_prev,stalled_inner_step, and theinner_solve_failedveto). Everything else keeps the eager path.Graceful degradation. The deferral is gated on
isdefined, so against aNonlinearSolveBasewithout the new API NSA behaves exactly as today. Verified against registryNonlinearSolveBase2.43.0:supports_deferred_residualundefined,sync_inner_residual!makes 0fcalls, and a 408-row battery is bit-for-bit identical to master (totalf403,721,198). TheProject.tomlfloor 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:
retcode,naccept,nreject,njacs,nw,nnonliniter,nnonlinconvfailand the error against aRodas5Preference (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
nfratio: median 1.356 → 1.047, mean 1.453 → 1.100. On some configurations NSA becomes cheaper thanNLNewton(ROBER/KenCarp4 313,399 → 170,969 against 193,131).Tests
nsa_stats_tests.jlgains "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:All nine
nsa_*.jlfiles pass on this branch: 39 + 21 + 13 + 10 + 10 + 14 + 8 + 102 + 70 = 287, 0 failures. Runic clean.OrdinaryDiffEqNonlinearSolve2.8.0 → 2.9.0.What this does not fix
After it, the residual
nfgap is small for FBDF (ROBER 1.09×, Bruss1D 1.02×). Two unrelated contributors remain on SDIRK, both worth their own investigation:fbut also 1.36× the accepted steps, i.e. per-step cost is identical at 2.54. Purely step selection.NLNewtonhas 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