Repository navigation
OOP NonlinearSolveAlg: reuse W with SimpleNonlinearSolve solvers - #3798
ChrisRackauckas merged 3 commits into
Conversation
| ) | ||
| prob = if use_w_reuse | ||
| nlf_jac = let W = W | ||
| (z, p) -> convert(AbstractMatrix, W) |
There was a problem hiding this comment.
We shouldn't unconditionally convert.
|
good catch — split the closure so StaticWOperator returns its .W field directly, only WOperator path converts now |
350a7e8 to
0f20284
Compare
| end | ||
| else | ||
| let Ww = W | ||
| (z, p) -> convert(AbstractMatrix, Ww) |
There was a problem hiding this comment.
What about non-static opertators
|
for non-static |
d85b000 to
98bbc94
Compare
| end | ||
| else | ||
| let Ww = W | ||
| (z, p) -> Ww._concrete_form |
|
fixed — now hands the operator through directly |
833ee15 to
e414df1
Compare
|
Hi @ChrisRackauckas — quick nudge on this stack when you have a moment. All three PRs (#3795, #3796, #3798) have addressed the earlier review comments and are rebased on the latest master:
Once the next SciMLOperators release tags, I'll open a follow-up here to route OOP + SciMLOperator mass matrix through No rush — just wanted to make sure these weren't waiting on me. Thanks! |
|
Ran this branch against an OOP nonlinear problem and hit two defects in the W-reuse path — posting reproductions so they can be addressed: 1. The using OrdinaryDiffEqSDIRK, OrdinaryDiffEqNonlinearSolve, NonlinearSolve, ADTypes
using OrdinaryDiffEqNonlinearSolve: NonlinearSolveAlg
vdp(u, p, t) = [u[2], p[1] * ((1 - u[1]^2) * u[2] - u[1])]
prob = ODEProblem(vdp, [2.0, 0.0], (0.0, 1.0), [1.0e3])
integ = init(prob, TRBDF2(nlsolve = NonlinearSolveAlg(NewtonRaphson(; autodiff = AutoForwardDiff())), concrete_jac = true); reltol = 1e-8, abstol = 1e-10)
step!(integ)
# ERROR: MethodError: Cannot `convert` an object of type WOperator{false, ...} to an object of type Matrix{Float64}The 2. Even with the convert fixed, the Jacobian values are never refreshed — J is frozen at its build-time state for the entire integration. The update in The IIP counterpart (#3753) avoids this because Also worth folding in while touching this: pass |
e414df1 to
936de56
Compare
|
Thanks for the review @ChrisRackauckas-Claude — both defects reproduced and fixed. New commit Defect 1 (crash on convert): the OOP closure now returns the concrete matrix directly — Defect 2 (frozen J): added Ref indirection: the closure captures Tests: added two regressions at the end of
Locally: both new tests pass; existing |
bd5af11 to
d89e8d7
Compare
| nlf_jac = let W_ref = W_ref | ||
| (z, p) -> convert(AbstractMatrix, W_ref[]) | ||
| end | ||
| jac_prototype = copy(convert(AbstractMatrix, W)) |
There was a problem hiding this comment.
no it's still concretizing.
c752622 to
9cfc718
Compare
| W_ref = use_w_reuse ? Ref{typeof(W)}(W) : nothing | ||
| prob = if use_w_reuse | ||
| if W isa StaticWOperator | ||
| nlf_jac = let W_ref = W_ref | ||
| (z, p) -> W_ref[].W | ||
| end | ||
| NonlinearProblem( | ||
| NonlinearFunction{false, SciMLBase.FullSpecialize}( | ||
| nlf; jac = nlf_jac, jac_prototype = W.W | ||
| ), | ||
| copy(ztmp), nlp_params | ||
| ) | ||
| else | ||
| nlf_jac = let W_ref = W_ref | ||
| (z, p) -> convert(AbstractMatrix, W_ref[]) |
There was a problem hiding this comment.
OOP is purposefully not mutable. We want to make things fold away when static. This will violate that. I think it's simply not possible to do similar W-reuse in the OOP path without violating the principle of making this path non-mutating.
There was a problem hiding this comment.
For StaticWOperator the payload is an immutable SMatrix — reassigning nlcache.W[] re-points the Ref cell, the SMatrix itself is never mutated, so fold-away on the value side still holds. The mutation you're objecting to only shows up on the WOperator branch (copyto!(W.J, …)).
And picking NonlinearSolveAlg already forces a Matrix{Float64} jac cache inside construct_jacobian_cache — the OOP path isn't fold-away in that mode regardless of what this PR does. If Static-only reuse is still off the table given that, I'll close.
There was a problem hiding this comment.
I guess it's fine if there's a simplediffeq path that fully non-allocates with simplenonlinearsolve. But the conversion stuff is never okay.
That should get fixed upstream. Making everything dense is not a valid answer. |
9cfc718 to
f4cf1d3
Compare
|
Dropped WOperator branch. W-reuse only fires on StaticWOperator + AbstractSimpleNonlinearSolveAlgorithm — SMatrix rebuilt, Ref re-pointed, no convert. |
|
Why no WOperator? |
141a00b to
3c296e7
Compare
|
@ChrisRackauckas Rebased on master and fixed two bugs the Static+Simple gate exposed. Both surfaced because that path introduces execution paths that weren't previously exercised. 1. Inner cache doesn't support
2.
The Ref now holds the plain Test change
Validation All 11 Remaining CI reds are unrelated to this branch — |
2416303 to
00e6067
Compare
d90e92d to
4bbd780
Compare
4bbd780 to
2e5c5d8
Compare
97dc593 to
da17c26
Compare
af63663 to
2b2920d
Compare
2b2920d to
ee20ca4
Compare
|
rebased onto master now that #4137 landed. the crash guards moved there, so this is down to just the W reuse your allocation question was about: +99/-15 in src, one commit. the reuse numbers are unchanged from the July discussion, SimpleNewtonRaphson on the SVector van der Pol drops from 22532 inner iterations to 14026 over 1343 W assemblies, and the remaining allocation overhead is upstream in NonlinearSolveNoInitCache's solve!/reinit! as before. |
c16156b to
b1c3ec7
Compare
b1c3ec7 to
2427535
Compare
|
Because the upstream piece is still missing. NonlinearSolve's Jacobian cache does Once SciMLOperators gains |
2427535 to
e2f14cd
Compare
e2f14cd to
2ce6290
Compare
Out-of-place
NonlinearSolveAlgrebuilt the stage Jacobian on every inner iteration; when the inner solver is a SimpleNonlinearSolve algorithm andWis aStaticWOperator, it now hands the integrator's assembledWto the inner solver and keeps it across the step's outer iterations.On the
SVectorvan der Pol (p = 1e3, TRBDF2,reltol = 1e-8) SimpleNewtonRaphson drops from 22532 inner iterations to 14026 over 1343Wassemblies;@allocated step!(integ) == 0holds for both inner solvers behind a function barrier. Thenjacscolumn is not a regression, those inner solvers returnstats = nothingon master.WOperatoris left out until SciMLOperators gainssimilarandcopyto!for it; the remaining allocation overhead is inNonlinearSolveNoInitCache'ssolve!.AI Disclosure: Claude assisted with this change.