Skip to content

Fused solve: in-front pivot orders and 2×2 blocks of a pivoted LDLᵀ factor (refs #167, stage 1) - #203

Merged
michel2323 merged 2 commits into
mainfrom
maint/fused-pivot-solve
Nov 16, 2026
Merged

michel2323 merged 2 commits into
mainfrom
maint/fused-pivot-solve

Conversation

@michel2323

Copy link
Copy Markdown
Member

Refs #167. Stage 1 of three maintenance PRs for it (pivot-aware fused solve, then diagonal pivoting in the fused factorization, then hybrid Bunch–Kaufman). Not a TASKS.md task.

What changed

  • The fused solve applies a factor's in-front pivoting (src/fused.jl): the row interchanges of pivot_type = 'D' and the interchanges and 2×2 blocks of Bunch–Kaufman, on a factor in the stock layout (N.piv, N.pivot_kind).
    • Forward: the inverse of L₁₁ reads y through the front's pivot order, after the children's updates have arrived; the result is stored in factor order.
    • Diagonal: the stock diagonal_sweep!, which handles 1×1 and 2×2 blocks.
    • Backward: computed in factor order and written back through the pivot order before the front signals its children.
    • The permutation is folded into the reads and writes of the existing kernels: no extra buffer, barrier or launch. Fronts wider than 256 use the stock dense LDLᵀ path. The derivation is a comment above _pj.
  • Internal only. FusedSolve has a field pivoted, set through Fused.set_pivoted!. FusedSolver, KKTSolver and solve_alg = "algo1" refuse pivoting as before; stage 2 supplies the factorization and lifts that. No CHANGELOG entry.
  • Tests: testset "fused solve on a factor with in-front pivoting" in test/test_solve_algo1.jl (stock pivoted factorization, fused solve), helper kkt_forest_matrix in test/matrices.jl. It asserts interchanges in subtrees, narrow fronts, the wide front and the level-batched entries, that the solve without the pivot order is wrong, agreement with the stock solve within tol(T), and bitwise reproducibility in deterministic mode.

The prototype's failure (#135)

The prototype's testset fused solve on a pivoted factor failed with a deterministic wrong answer, recorded in #167 as a logic bug in the solve. Two of its three configurations (no segment, and the chain kernel on the merged segment) never solve the regime-A subtrees, with or without pivoting: on main with pivot_type = 'N' they give a relative residual of 0.48 to 56 on matrices with subtrees, where the counter sweep gives 2e-14. So the testset was invalid; no kernel bug was found in the prototype.

Measurements (CUDA, H200, smoke scripts)

  • New testset: 240 of 240 (Float32, Float64). Against the stock solve on the same factor: at most 1e-15 in Float64, 8e-7 in Float32.
  • An independent check with other generators (saddle points, random symmetric with zero diagonals, grids with dense rows; n from 56 to 19,800; 'D' and 'B'; thresholds 0.01, 0.1, 0.5; both deterministic modes; graph capture and replay; refactorization without rebuilding the plan), 49 configurations against a host UMFPACK reference: the fused solve matches the stock solve on the same factor in every row, and the solve without the pivot order is wrong by 5e2 or more in every row.
  • With element growth in L (for instance 'D' at threshold 0.1) the fused solve is 2 to 100 times less accurate than the stock solve before refinement, as expected from the explicit inverse of L₁₁; three refinement steps bring both to 2e-14 on the case tried.
  • No-pivot path: same launches; in deterministic mode the results are bitwise equal to main on six solver/matrix pairs.

Tests

Run as plain scripts with the runtests.jl setup, CPU backend and CUDA: test_solve_algo1 all element types 582 of 582; in Float64 test_fused 516 of 516, test_kkt 977 of 977, test_solve 304 of 304, test_refinement 279 of 279. Not run locally: the other files, test_allocations, Int64 indices on the new path, AMDGPU, oneAPI; CI runs them.

Deviations

PLAN.md defines the fused pipeline without pivoting; this follows the owner's decision to do #167 as maintenance PRs.

🤖 Generated with Claude Code

https://claude.ai/code/session_01MsLzHkzHJjM2g5P6ZyZKhr

…LDLᵀ factor (refs #167)

Stage 1 of the pivoted fused pipeline: the partitioned-inverse fused solve runs on a factor with in-front
pivoting (pivot_type 'D' or Bunch-Kaufman, as the stock LDLᵀ leaves it in numeric.piv/pivot_kind). The
forward inverse GEMV of each front reads y through the front's pivot order (z = inv(L11) Pᵀ y), the
backward one scatters x back through it, the fronts wider than 256 take the LDLᵀ solve kind (the dense
calls with _piv_dense! around them) and the diagonal step goes through diagonal_sweep! (1x1 and 2x2).
Compile-time switch (Val) per sweep: the no-pivot/Cholesky instantiation has the same launches and
arithmetic and never reads piv. Internal entry point: FusedSolve.pivoted, set with
Fused.set_pivoted!(fs, true); DirectSolver solve_alg = "algo1", FusedSolver and KKTSolver are unchanged.
The bench-only chain kernel refuses a pivoted factor (NotSupportedError).

New testset in test_solve_algo1.jl against the stock solve on the same factor and the residual, with
interchanges asserted in subtrees, narrow fronts, the level-batched entries and the wide front
(new helper kkt_forest_matrix in test/matrices.jl).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01MsLzHkzHJjM2g5P6ZyZKhr
@michel2323 michel2323 added the claude:pr PR opened by the Claude implementer; handled by the Claude pipeline label Nov 16, 2026
Comment thread src/fused.jl
fwd1_kernel!(backend, WG)(fargs(a, b_ - a + 1)..., Val(false), Val(WG), Val(false); ndrange = WG * (b_ - a + 1))
fwd1_kernel!(backend, WG)(fargs(a, b_ - a + 1)..., Val(false), Val(WG), Val(true), N.piv, pvv;
ndrange = WG * (b_ - a + 1)) :
fwd1_kernel!(backend, WG)(fargs(a, b_ - a + 1)..., Val(false), Val(WG), Val(false), N.piv, pvv;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit (non-blocking): _dense_kind(Val(true)) hard-codes _SV_LDL_HERM, so the wide-front dense calls of a pivoted factor always run the Hermitian kind (tr = 'C'). That is correct today because the fused solve is real-only (_check_algo1 refuses complex, FusedSolver too), and algo2_diagonal! already picks the structure-aware kind through diagonal_sweep!. If the complex restriction is ever lifted, a complex symmetric "S" factor would need _SV_LDL_SYM here; SDS._solve_kind(S, eltype(ws.Y)) would give the right kind in both cases with no runtime cost beyond what _bwd_dense! already pays. Fine to leave as is for stage 1.

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

VERDICT: APPROVE (head c9fa9ce)

Reviewed the diff (src/fused.jl, test/matrices.jl, test/test_solve_algo1.jl) against AGENTS.md, PLAN.md §2.5/§2.7 and the stock LDLᵀ solve (src/solve/sweeps.jl, src/numeric/storage.jl).

Scope. Not a TASKS.md task but a maintenance PR for #167, as the body states and as the owner set up for #200/#202; PLAN.md, TASKS.md and Manifest.toml untouched; no public name, parameter or phase string changed. The public front ends still refuse pivoting with the fused solve (_check_algo1_pivoting, FusedSolver); set_pivoted! is internal and reached only from the test. The existing bench caller (bench/e2e/fused_probe.jl) keeps working through the new defaulted arguments.

Correctness. The folded permutation matches _sv_piv_apply! exactly: forward t = inv(L₁₁) · y[piv[c0 + j]] (stock: Y[c0 + k] = saved[piv[c0 + k] − c0]), backward Y[piv[c0 + k]] = x_k (stock: Y[c0 + p] = saved[k]). In all four kernels (fwd1, fwd2, bwd1, bwd2) every read of the front's slice of Y precedes the barrier that precedes the writes, and the rows below (rowval) stay in the pre-pivot numbering, so children and ancestors see the same numbering as in the level sweeps. The yb variant of fwd2 indexes the local copy with piv − c0 ∈ 1:w. The L₁₁ inverses are computed from the unit-lower panel exactly as the stock TRSV reads it, so 2×2 blocks need no special case there; the diagonal step goes through diagonal_sweep!, which handles PIVOT_KIND_2X2_*. Wide fronts use the stock _SV_LDL_HERM path; _piv_dense! uses rows 1:w of the front's ws.U buffer, which the fused deterministic pull leaves untouched (it zeroes and accumulates w+1:f only), and ws.U is always allocated so the non-deterministic path is safe too. _pj is Int-typed and Val-dispatched; the no-pivot instantiation never touches piv. The chain kernels refuse pivoting with NotSupportedError instead of solving wrongly. Graph capture is per (det, pivoted) variant and dropped on set_pivoted!.

Conventions. Generic T/INT, 1-D workgroups, @localmem sized by constants, no new atomics, no host sync or allocation added to the sweeps (compile-time Val branches), dense calls through the existing interface, no @allowscalar, errors from src/errors.jl.

Tests. The new testset runs on ALGO1_BACKENDS (GPU backends, as the rest of the file) over eltypes_among((Float32, Float64)), with Random.seed!(666), tol(T) and relres; it asserts interchanges in subtrees, narrow and wide fronts and the level-batched entries, non-vacuity (the unpivoted fused solve is wrong), agreement with the stock solve for both all_segments and both det, bitwise reproducibility and the sub-phase composition, and the chain-kernel refusal. The kkt_forest_matrix helper lives in test/matrices.jl. No @test_broken, nothing weakened. CI: CPU, CUDA, AMDGPU, Aurora and docs all green, consistent with the body's counts.

Blocking findings: none. One nit (non-blocking) inline on _dense_kind hard-coding the Hermitian kind.

…d element type (PR #203 review)

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01MsLzHkzHJjM2g5P6ZyZKhr
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

claude:pr PR opened by the Claude implementer; handled by the Claude pipeline

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant