Repository navigation
Count Krylov Jacobian-vector products on the operator that performs them - #4193
Conversation
|
Confirming the FIRK Identical counts and identical single failure to the branch run. |
|
Root cause of the pre-existing FIRK QA failure above, so it does not get mistaken for something this PR should carry: it is a SciMLTesting regression, not an OrdinaryDiffEq one, and it affects the whole monorepo rather than FIRK.
CI brackets the registration exactly: a run at 2026-08-05T03:11 on v2.6.1 was green, a run at 2026-08-05T12:39 on v2.6.2 was red, and the General registration of v2.6.2 merged at 04:01 between them. It is already fixed upstream and merged — SciMLTesting PR #45, commit Deliberately not adding a local Links:
|
|
Re-ran the Core group on the exact pushed commit (the Verification section's numbers were taken before the |
`stats.nf` counts calls to `f`, and on the matrix-free Krylov path it did not. `linres.iters` is fewer than the products a Krylov solve applies (the warm start and the initial residual each cost one), the `linsolve.A.J isa AbstractSciMLOperator` gate missed the `jacvec` that `WOperator`'s `mul!` actually uses when a concrete `J` is kept for the preconditioner, and a user-supplied `MatrixOperator` Jacobian was billed per iteration for products that evaluate `f` zero times. Give `JVPCache` an `njvps` tally and drain it in `dolinsolve`, the approach `drain_jvp_count!` already took for FIRK's `ComplexWOperator`; the two now share one implementation. `nf` matches a user's own count of `f` calls exactly across matrix-free, `concrete_jac`, `MatrixOperator` and direct-solver runs. `rhs_evals_per_jvp` records why a finite-difference product counts as two evaluations and a dual-number product as one. Co-Authored-By: Chris Rackauckas <[email protected]> Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_01F75XsVeZ94QH3PmCuq6QUF
0eb39d1 to
8870681
Compare
Closes #4162.
Please ignore until reviewed by @ChrisRackauckas.
What
nfmeansnfcounts calls tof. That is whatincrement_nf!'s docstring says ("the RHS-evaluation counter"), what SciMLBase prints it as ("Number of function 1 evaluations"), and what every other increment site in the solver does —increment_nf!(stats, num_stages)afternum_stagescalls, and so on. It is not a cost model. This PR keeps that definition and makes the Krylov path obey it; the alternative, rescaling the finite-difference product to one "cost-equivalent" evaluation so it compares evenly with a dual-number product, would put a cost model into a counter nothing else treats as one, and would makenfdisagree with a user counting their ownfcalls. The choice is written down in the newrhs_evals_per_jvpdocstring.Under that definition the 2-vs-1 split the issue points at is correct:
FiniteDiff.finite_difference_jvp!really does callftwice per product (atxand at the perturbed point — verified in FiniteDiff 2.33.0, both:forwardand:central), and aForwardDiffproduct really is one call on a dual input. The measurement in the issue (24.96 µs vs 27.67 µs) says the finite-difference product is cheaper per unit of work, which is a real and useful fact, but it is not a statement about how many timesfran.What was actually wrong
The accounting was wrong in three other ways, all of which made
nfdisagree with the true call count. Counting realfcalls on a 24-state Allen-Cahn problem withFBDF(linsolve = KrylovJL_GMRES()), on master (9d23061ef8a2b155ca2b6ff78670f6e320967940):fcallsstats.nfon masterstats.nfwith this PRAutoForwardDiffAutoFiniteDiffconcrete_jac = true,AutoForwardDiffconcrete_jac = true,AutoFiniteDiffMatrixOperatorjac_prototype,AutoForwardDiffMatrixOperatorjac_prototype,AutoFiniteDifflinres.itersis not the number of products. A Krylov solve applies the operator more times than it reports iterations. The matrix-free rows under-count by 31% and 34%, which works out to about two extra products per solve over the 35 solves here: one is the Hegedűs warm start, which does an explicitmul!(similar(cache.b), cache.A, u)inLinearSolve/src/iterative_wrappers.jlbefore handing off to Krylov, and the rest is the initial residual.jacvecpath was invisible. The gate waslinsolve.A.J isa AbstractSciMLOperator, butWOperator'smul!usesW.jacvecwhenever it has one. Withconcrete_jac = truethe Krylov solver keeps a concreteJfor the preconditioner and does its products throughW.jacvec, so the gate was false and none of the products were counted: 62 reported against 243 real.MatrixOperatorJacobian applies a stored matrix and callsfzero times, but it satisfied the gate and was billed per iteration: 151 and 264 reported against 38 real.The fix
Count the products on the operator that performs them, which is the only place that sees all of them and is already the approach
drain_jvp_count!took for FIRK'sComplexWOperatorin #4166 ("the counter lives on the operator rather than being inferred from the Krylov iteration count").JVPCachegains annjvpstally;dolinsolvedrains it through adrain_jvp_count!that is now shared with FIRK rather than duplicated. All three defects fall out: aMatrixOperatorhas no tally, ajacvechas one wherever it sits in theW, and the tally counts the products the solver never reported.This also answers the
#4166follow-up noted in the issue. FIRK's separateComplexWOperator.jvpsfield goes away — aComplexWOperatorapplies the same real JVP operator itsWdoes, so those products are already on that operator's tally and onejvp_countermethod is all FIRK needs. FIRK's existingstats.nf == nf[]assertions infirk_krylov_tests.jlcover the migration.Step counts are untouched in every configuration above (
naccept = 27,nsolve = 35before and after): this changes reporting only.Failing before / passing after
New test asserts
stats.nf ==the counted number offcalls across the four configurations × two AD backends. On unmodified master, with therhs_evals_per_jvpunit testset removed since the helper does not exist there:The 10 passes are the direct-solver control and the
successful_retcodechecks. With the change:Related context, not fixed here
The larger half of #4162 is upstream and is not touched.
FiniteDiff.finite_difference_jvp!takes anf_inargument so a caller can hand it a cachedf(x), but DifferentiationInterface'spushforward!does not pass it — confirmed still true in DifferentiationInterface 0.7.20,ext/DifferentiationInterfaceFiniteDiffExt/twoarg.jl, which callsfinite_difference_jvp!(dy, fc!, x, dx, prep.cache; relstep, absstep, dir)with nof_in. So the base evaluation is recomputed on every matvec even thoughxis fixed for the whole linear solve, worth about 1.2× on that path. That fix belongs in DifferentiationInterface. When it lands,rhs_evals_per_jvpforAutoFiniteDiffbecomes 1 and this is the one line that has to change; the docstring says so.DifferentiationInterface is not a repo I have standing to file in, so this is reported here rather than opened there.
Verification
Julia 1.12.6. Resolved: LinearSolve 5.6.0, SciMLBase 3.43.0, SciMLOperators 1.26.1, ADTypes 1.22.4, FiniteDiff 2.33.0, DifferentiationInterface 0.7.20, Krylov 0.10.9.
GROUP=Core julia --project -e 'using Pkg; Pkg.test()'inlib/OrdinaryDiffEqDifferentiation:GROUP=Coreinlib/OrdinaryDiffEqFIRK, which carries the pre-existingstats.nf == nf[]assertions theComplexWOperatorcounter migration has to keep passing:GROUP=QAinlib/OrdinaryDiffEqDifferentiation(JET + Aqua, which is where the ExplicitImports checks live and so where the newpublicdeclarations are exercised):GROUP=QAinlib/OrdinaryDiffEqFIRKfails, but it fails identically on unmodified master —run_api_docsreports:SciMLBaseas an undocumented public name, which comes from the pre-existing@reexport using SciMLBaseinOrdinaryDiffEqFIRK.jland has nothing to do with this diff (Allocation Tests5/5 andJET Tests1/1 in the same group pass):I am confirming the master reproduction separately and will report it; it is not something this PR should carry a fix for.
Runic and
typosare clean over the diff and the new test file.Both PRs bump
lib/OrdinaryDiffEqDifferentiationto 3.8.0 (each adds public API), so whichever merges second will conflict on that one line inProject.tomland on thepublicblock; the rest of the two diffs is disjoint apart fromdolinsolveitself.Not verified
docs/file, rendered API entry, or public name is touched (JVPCacheis documented but internal, and the change to it is additive).dolinsolvebut whose matrix-free paths are the same code as the ones exercised above.AutoFiniteDifferences, whichrhs_evals_per_jvpstill maps to 2. It is unreachable through this path —prepare_jvpassertsDI.check_inplace(autodiff)and DifferentiationInterface declaresinplace_support(::AutoFiniteDifferences) = InPlaceNotSupported()— so the branch is dead rather than tested. I kept it rather than deleting it because deleting a dead branch is a separate question from fixing the accounting; note that 2 would be wrong for it anyway, sincecentral_fdm(5, 1)costs five evaluations per product.Worth pushing back on
njvpsis a plainIntincremented inmul!. That is safe today because nothing in this repo applies a sharedJVPCachefrom more than one thread (FIRK'sAdaptiveRadaustage operators all share theW1.jacvecand run sequentially), but it is a counter in a hot path with no synchronisation, and if a threaded stage solve ever lands it becomes a race. The pre-existingComplexWOperator.jvpshad the same property.dolinsolveattributes any products applied outside a linear solve to the next solve. The total stays right; the per-step attribution can be off by one solve.