Opt PureKLU and RFLU out of split dual AD path (rebase of #1023)#1041
Merged
ChrisRackauckas merged 5 commits intoJun 14, 2026
Merged
Conversation
Two fixes for the direct dual opt-out path:
1. PureKLUFactorization's sparse init_cacheval methods were restricted to
Float64/ComplexF64 eltypes, so Dual-eltype sparse matrices fell through
to the nothing fallback and solve! crashed on cacheval.nzval. PureKLU's
kernels are pure Julia and generic over Union{Real, Complex}, so widen
the specializations accordingly.
2. The direct dual path built its inner problem from dual_A/dual_b as
stored, but when only A carries duals b is a plain array, and __init
takes the solution eltype from b, so the dual solution could not be
stored (MethodError: no method matching Float64(::Dual)). This also
affected the pre-existing GenericLUFactorization opt-out. Promote b to
the cache's dual type in _solve_direct_dual!, and only take the direct
path when A itself carries duals: with duals just in b, the split path
(one primal factorization + partials back-solves) is strictly cheaper
and works for all algorithms.
Adds test coverage for the duals-only-in-A and duals-only-in-b cases for
GenericLUFactorization, RFLUFactorization, and PureKLUFactorization.
Co-authored-by: Chris Rackauckas (Claude) <accounts@chrisrackauckas.com>
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
The original PR widened the `{T, Int64}` / `{T, Int32}` PureKLU `init_cacheval`
methods from `Union{Float64, ComplexF64}` to `Union{Real, Complex}` to let
ForwardDiff duals dispatch on the direct dual solve path. After rebasing onto
main, that is unnecessary: SciML#1037 ("Default to PureKLU for generic-eltype sparse
LU") added a catch-all `where {T <: Number, Ti <: Integer}` method that already
builds the correct empty cache for any number eltype (duals included). The
widened specializations only duplicated it, so revert that file to main.
Verified: test/core/forwarddiff_overloads.jl (incl. the PureKLU sparse-dual
cases) passes with this file unchanged from main.
Co-Authored-By: Chris Rackauckas <accounts@chrisrackauckas.com>
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.
Rebases and finalizes #1023 (branch
ChrisRackauckas-patch-1) onto the currentmain. The original PR went stale (mergeable_state: dirty); I couldn't push to the SciML-owned branch, so this is the rebased version from a fork. It carries the same commits (authored by @ChrisRackauckas), now conflict-free onmain. Supersedes #1023 — close that one in favor of this.What it does (unchanged intent)
Opt
PureKLUFactorizationandRFLUFactorizationout of the split primal/partials dual-AD path so they solve the Dual problem directly, and only take the direct path whenAitself carries duals (with duals only inb, the split path — one primal factorization + partials back-solves — is cheaper than factorizing in dual arithmetic). Also promotes a plainbto the dual eltype on the direct path.Net change is now just
LinearSolveForwardDiffExt.jl+ the testThe original PR also widened two PureKLU
init_cachevalmethods inLinearSolveSparseArraysExt.jlfromUnion{Float64, ComplexF64}toUnion{Real, Complex}. That is dropped here — after rebasing ontomain, it's redundant: #1037 ("Default to PureKLU for generic-eltype sparse LU") added a catch-allwhere {T <: Number, Ti <: Integer}method that already builds the correct empty cache for any number eltype, duals included. The widened specializations only duplicated it, so that file is left identical tomain. (The last commit reverts it, with the reasoning; verified the PureKLU sparse-dual tests still pass.)Conflicts resolved during rebase
mainindependently evolved both touched spots:ext/LinearSolveForwardDiffExt.jl—_use_direct_dual_solvehad gainedSpecializedLUFactorization/SpecializedQRFactorizationonmain. Resolved as the union:GenericLU || SpecializedLU || SpecializedQR || PureKLU || RFLU.test/core/forwarddiff_overloads.jl—mainswitched the direct-path comparison to a robustdual_isapproxhelper (plain≈over Dual vectors NaNs when a primal diff is exactly zero). Kept that helper and theSpecialized*+ least-squares tests, and re-expressed the PR'sGenericLU/RFLUchecks throughdual_isapprox(they take the same direct path, so the same ulp/NaN concern applies). Unioned theusing SpecializingFactorizations/using RecursiveFactorizationimports.Local verification
Julia 1.12 / Linux.
Runic --checkclean.test/core/forwarddiff_overloads.jlruns green end-to-end (all testsets, including the mergeddual_isapproxchecks forSpecializedLU/SpecializedQR/GenericLU/RFLU, the least-squares case, the plain-A/plain-bopt-out cases, and thePureKLUsparse-dual tests) — both with and without the now-dropped ext change.Please ignore until reviewed by @ChrisRackauckas.
🤖 Generated with Claude Code