Repository navigation
Quasi-Newton J⁻¹ init as a linsolve workspace: factorization + identity solve through the LinearSolve default - #1039
Conversation
|
Revised per review ( What changed relative to the previous revision:
Re-measured (LinearSolve 4.2.1): 480 B per repeated Verified locally: 🤖 Generated with Claude Code |
…olve Per review on SciML#1039 ("just remove the pinv stuff? Just use the default linear solver?"): purge the maybe_pinv!! mechanism and name entirely. The quasi-Newton inverse-Jacobian init is now Utils.inverse_jacobian!! / Utils.inverse_jacobian_workspace — solve A * X = I with the default linear solver. - Delete the generic pinv(A) fallback; widen the lincache-backed methods from StridedMatrix to AbstractMatrix. Structured input (Tridiagonal, Symmetric, ...) gets dense workspace buffers via the size form of similar; the identity RHS refill goes through the GPU-safe make_identity!!. - The SparseArrays extension keeps only the densifying workspace method; its call-side override is covered by the general AbstractMatrix method. - Number/Diagonal invert through Utils.safe_inv (zero-guarded inverse, bit-identical values to the old pinv scalar/broadcast paths), the scalar analog of the default solver's least-squares rescue. - SMatrix keeps pinv: Broyden(init_jacobian = Val(:true_jacobian)) with SVector states reaches this function (verified empirically, always with a nothing workspace); static solves bypass the LinearSolve cache and singular initial Jacobians must stay finite where \ would error. - Rename all call sites (NonlinearSolveQuasiNewton solve.jl, BroydenLowRankJacobian passthrough), the test file, and the QA ExplicitImports ignore lists. - NonlinearSolveQuasiNewton 1.13.4; its NonlinearSolveBase compat floor raised to 2.35 (it now requires the new internal names). Co-Authored-By: Claude Fable 5 <[email protected]> Co-Authored-By: Chris Rackauckas <[email protected]>
|
Final revision per review ("just remove the pinv stuff? Just use the default linear solver?"):
|
|
Renamed per review: On the StaticArrays question: LinearSolve does have a static fast path ( Post-rename verification: full |
962cada to
6162afe
Compare
|
Rebased onto current master (post-#1040) and squashed to a single clean commit. Since the branch's 5 incremental commits were iterations of the same functions against a since-diverged master, I reconstructed the net diff rather than replay conflicts: verified the result is exactly the intended 13 files with no scratch/leaked-env artifacts, hand-merged the 3 Project.tomls that both this PR and #1040 touch (no clobbering), and confirmed no Compat: LinearSolve floor raised to Verified locally:
Allocation note (LinearSolve 4.3, this branch): quasi-Newton SMatrix |
…1043) Widen the LinearSolve [compat] ceiling to include 5 in every monorepo Project.toml that still capped it at 4, so the full stack can resolve LinearSolve 5.0. Lower bounds are preserved (only the ceiling widens). Compat + version bumps (minor, per SemVer for a widened compat range): - NonlinearSolve (root): LinearSolve 3.48, 4 -> 3.48, 4, 5 v4.20.4 -> v4.21.0 - NonlinearSolveFirstOrder: LinearSolve 2.36.1, 3, 4 -> +5 v2.1.3 -> v2.2.0 - NonlinearSolveQuasiNewton: LinearSolve 2.36.1, 3, 4 -> +5 v1.13.3 -> v1.14.0 - docs env: LinearSolve 2, 3, 4 -> 2, 3, 4, 5 - test/gpu env: LinearSolve 3.48, 4 -> 3.48, 4, 5 NonlinearSolveBase is handled by #1039 (LinearSolve 4.3, 5) and is not touched here. No code changes: no package reads LinearSolution.cache (the 4->5 break); LinearSolve results are consumed only via retcode/u. Co-authored-by: ChrisRackauckas-Claude <[email protected]> Co-authored-by: Claude Fable 5 <[email protected]>
…ty solve Replace the bespoke pinv machinery in the quasi-Newton inverse-Jacobian initialization with a plain A·X = I solve through the shared LinearSolve stack. Utils.linsolve_workspace / linsolve_identity!! build a default-algorithm linear-solve cache once and solve against an identity RHS; no matrix is inverted. Sparse A routes through the same cache (pinv has no sparse method). Number/Diagonal use a zero-guarded safe_inv (bit-identical to the old pinv values). SMatrix solves A·X = I through LinearSolve's static fast path directly, whose SVD rescue (SciML/LinearSolve.jl#1085, LinearSolve ≥ 4.3) returns the finite min-norm generalized inverse on a singular initial Jacobian — the behavior pinv provided — allocation-free. Compat: LinearSolve "4.3, 5". Co-Authored-By: Claude Fable 5 <[email protected]> Co-Authored-By: Chris Rackauckas <[email protected]>
6162afe to
9f4a985
Compare
…e correct minimum (2.34) (#1050) PR #1039 raised the floor to "2.35", anticipating a future NonlinearSolveBase release. The internal rename it actually depends on (maybe_pinv!! -> linsolve_identity!!/linsolve_workspace) already landed in the currently registered NonlinearSolveBase 2.34.0. The 2.35 floor is unregistrable (no such version exists) and was causing NonlinearSolveQuasiNewton 1.13.3 (the currently registered version, which predates the rename) to fail to precompile against NonlinearSolveBase >= 2.34 for any fresh install.
Problem #1 (Generalized Rosenbrock) solved with Broyden(; init_jacobian = Val(:true_jacobian), update_rule = Val(:bad_broyden)) sits on a knife-edge. After #1039 changed the Jacobian inverse initialization from inv!(lu(A)) to linsolve_identity!!, the ulp-level difference (~1e-13) flips this problem between converging and not, so it randomly passes and fails. Marking it `broken` is not enough: when it happens to converge it errors as an unexpected pass. Use the harness's existing skip_tests mechanism instead so the case is skipped in both directions. Co-authored-by: ChrisRackauckas-Claude <[email protected]>
Part of #1020 allocation work; follows #1038 (the dense-LU alias fix, second rung of the allocation ladder).
Note: This PR should be ignored until reviewed by @ChrisRackauckas.
What
Quasi-Newton update rules that store J⁻¹ (Broyden family) need an explicit inverse of the initial Jacobian. On master this went through a bespoke
maybe_pinv!!mechanism:lu(A; check = false)(a full matrix copy) +LinearAlgebra.inv!(LAPACKgetri, whose work array is larger thanAitself), with an SVDpinv(A)fallback on singular input — re-run once persolve!, i.e. once per continuation step underreinit!/solve!cache reuse.Per review direction ("just remove the pinv stuff? Just use the default linear solver?"), the pinv machinery is removed entirely. The init is now plainly solve
A * X = Iwith the default linear solver:Utils.inverse_jacobian_workspace(A::AbstractMatrix)builds aconstruct_linear_solvercache (linsolve = nothing, the LinearSolve default algorithm) over workspace-owned buffers: a promoted dense copy ofAand an identity-matrix RHS. One factorization + one multi-RHSsolve!givesX = A⁻¹. Buffers opt intoalias_A = true/alias_b = trueso the dense LU refactorizes in place (lu!) instead of copyingAon every call.Utils.inverse_jacobian!!(workspace, A::AbstractMatrix)refills the RHS withI(via the GPU-safemake_identity!!) and solves. The lincachecopyto!sAinto its internal buffer, preserving the caller-never-mutated contract. Anothingworkspace (the preinverted-init path in NonlinearSolveQuasiNewton) builds a transient lincache; it allocates, but runs at most once per solve.pinv(A)fallback is deleted. The lincache method is widened fromStridedMatrixtoAbstractMatrix: structured input (e.g.Tridiagonal) gets dense workspace buffers via thesizeform ofsimilar, and the SparseArrays extension keeps only a workspace method that densifies (Matrix(A)) — its call-side override is gone since the general method now covers sparse input.maybe_pinv!!/maybe_pinv!!_workspaceare purged monorepo-wide (internalUtilsfunctions; the only consumer is NonlinearSolveQuasiNewton, verified by grep).Semantics on the native fast paths (mirroring
construct_linear_solver's routing)pinv— a different finite matrix with the same fitness for quasi-Newton initialization (finite, correct shape,A * X * A ≈ A). Regression-tested on those properties.Number/Diagonal: an explicit zero-guarded inverse (Utils.safe_inv:iszero(x) ? zero(inv(x)) : inv(x)), the scalar analog of the least-squares rescue. Bit-identical values to the oldpinv(x)/pinv.(D).SMatrix: keepspinv. Verified empirically thatBroyden(init_jacobian = Val(:true_jacobian))withSVectorstates reaches this function (always with anothingworkspace); static solves bypass the heap-allocated LinearSolve cache, and a singular initial Jacobian must stay finite where the native\would error. Singular-SA init still returns a finite matrix (solver reportsStalled, no throw), matching master.inverse_jacobian!!on a general matrix requires LinearSolve to be loaded, like every otherconstruct_linear_solverconsumer; the only caller (NonlinearSolveQuasiNewton) has it as a hard dependency.Merge with master
The branch merges current master: #1035 (the NLSB QA ExplicitImports fix) and #1038 (the
alias_A_for_refactorizationtrait). The predicted one-line conflict inconstruct_linear_solveris resolved here: thealiaskwarg now defaults tonothing, deferring to #1038's trait (owned-copy refactorization buffer); an explicitLinearAliasSpecifierfrom a caller that ownsA/boutright (this workspace) is a forced override that skips the defensive copy. Master's newlu_refactorization_allocs.jlsuite passes on the merged result.Versioning
NonlinearSolveBase is at 2.35.0 on this branch (public
construct_linear_solvergains thealiaskeyword). NonlinearSolveQuasiNewton now requires the new internal names, so its NonlinearSolveBase compat floor is raised to2.35and it is bumped to 1.13.4.Numbers (carried over from the previous revision, re-asserted by the test suite on this one)
inverse_jacobian!!repeated call, well-conditioned dense 51×51 Float64 (sizeof(A)= 20 808 B):lu+inv!)alias_A = true)lu!ipiv + wrapper)Per continuation step (the
@allocatedslope harness fromtest/Core/arclength_tests__item8.jl, N1 = 50 / N2 = 250 fixed-step, n = 50, default inner polyalgorithm): secant 54.4 → 34.4 KB/step, tangent 75.5 → 55.3 KB/step. On the merged tree the repeated-call measurement was re-run and is unchanged (480 B). #1038 is not yet merged, so these still include LinearSolve's ~21.5 KB/steplurefactorization copy in the descent lincaches that #1038 removes; the two changes compose.Tests (all run locally against the dev'd packages)
lib/NonlinearSolveBase/test/inverse_jacobian_workspace.jl(renamed, Core group): nonsingular general/triu/tril matchinv(A); input non-mutation; repeated-call stability; re-inverting the returned inverse;nothing-workspace path; singular finite/A*X*A ≈ A/no workspace poisoning;@allocated < sizeof(A)on repeated calls; sparse through the lincache; structured (Tridiagonal) through the lincache; Number/Diagonal/SMatrix fast paths incl. singular values.Pkg.test()for NonlinearSolveBase — passed (50 workspace assertions + full Core group).Pkg.test()for NonlinearSolveQuasiNewton — full suite passed.GROUP=QAfor both packages — passed (ExplicitImports ignore lists updated for the renames).test/Core/homotopy*+test/Core/arclength*files — passed.Broyden(init_jacobian = Val(:true_jacobian))with sparsejac_prototype+ SparseMatrixColorings converges (Success, resid ≈ 1.3e-13); same withSVectorstates (Success, resid ≈ 2.6e-14); scalar Broyden incl. zero init; Klement dense/SA; LimitedMemoryBroyden (theBroydenLowRankJacobianpassthrough).🤖 Generated with Claude Code