Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@ All notable changes to SparseDirectSolver.jl are listed here. The format follows
[Semantic Versioning](https://semver.org/). Task ids (`TNN`) refer to `TASKS.md`,
whose Report blocks hold the details and the measurements.

## [Unreleased]

### Changed

- LDLᵀ pivots as CHOLMOD's (#207): a NaN or infinite pivot is kept on both
pipelines (the stock kernel and the reference replaced a NaN pivot, the fused
LDLᵀ failed the factorization with `info ≠ 0`), and the inertia counts a
pivot as positive if it is `> 0`, zero if it is `0`, and negative otherwise,
NaN included. A NaN propagates through the rest of its front and its
ancestors, so `"info"` stays `0` and the NaN shows in `"inertia"`, as the
wrong inertia an interior-point method corrects by regularizing.

## [0.4.1] - 2026-11-17

### Added
Expand Down
10 changes: 5 additions & 5 deletions docs/src/lib/kkt.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ the analysis, with every other keyword kept, and `pipeline` then reports
`matching` (the `"matching_alg"` of the analysis: `"algo5"` and `"algo6"` scale
the matrix and give the 2×2 pivot pairs of the ordering) and pivot replacement
(`pivot_epsilon`, `pivot_sign`: a pivot below `pivot_epsilon` becomes
`±pivot_epsilon`, with the requested sign). On the fused pipeline a NaN or
infinite pivot fails the factorization (`info ≠ 0`), where the stock
kernel replaces a NaN pivot and accepts an infinite one (with
`pivoting = :diagonal` the fused subtrees and narrow fronts follow the stock
rule). Matching and the
`±pivot_epsilon`, with the requested sign). On both pipelines a NaN or
infinite pivot is kept, as CHOLMOD's, and the inertia counts a pivot as
positive if it is `> 0`, zero if it is `0`, and negative otherwise, NaN
included; a NaN propagates through the rest of its front and its ancestors,
so it shows as an inertia defect, not as `info ≠ 0`. Matching and the
pivot pairs are decided from the values of the analysis: when the solver is
built before the matrix holds its first values, as an interior-point method
builds it, pass `deferred_analysis = true`. `ir_n_steps = k` follows every
Expand Down
30 changes: 15 additions & 15 deletions src/fused.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1321,8 +1321,8 @@ const _SP_CTL = _SP_C0
# requested `psg[g]` (`pivot_sign` in factor-column order) when nonzero, else the sign of d (+ for 0).
# Returns (pivot, replaced). A pure function of (d, eps, psg[g]): the factorization stays bitwise
# deterministic. eps = 0 returns a zero pivot as ±0 (the caller fails the front, as before #194);
# NaN and Inf are not "tiny" and come back unchanged, so the caller's finiteness test fails them
# (the stock kernel replaces a NaN pivot and accepts an Inf one).
# NaN and Inf are not "tiny" and come back unchanged; the callers keep them, as CHOLMOD's dbound does,
# and the inertia counts a NaN as negative (as the stock kernel).
@inline function _ldl_static_pivot(dk::R, eps::R, psg, g) where {R}
if abs(dk) < eps || iszero(dk)
s = @inbounds psg[g]
Expand All @@ -1340,7 +1340,7 @@ end
if ctl[SDS._ST_STATUS] == 0
g = Int(ctl[_SP_C0]) + j - 1
dk, rep = _ldl_static_pivot(real(buf[lf + SDS._packed(j, j, f)]), eps, psg, g)
if isfinite(dk) && !iszero(dk) # NaN/Inf must fail like potrf, not "factor"
if !iszero(dk) # a NaN or Inf pivot is kept, as CHOLMOD's (the inertia counts a NaN as negative)

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.

The public docstrings still describe the behaviour this line removes. In this file, the execute! docstring (lines 2859–2867) lists "a non-finite pivot" among the causes of info ≠ 0 and says "Unlike the stock kernel, which replaces a NaN pivot and accepts an infinite one, the fused LDLᵀ fails the factorization on both under any pivot_epsilon, so a NaN or Inf in the matrix is reported rather than factored"; the getparam docstring (lines 2905–2907) describes "inertia" for "S" as "the counts of positive and negative entries of D". Outside the diff, src/kkt.jl:88–90 (KKTSolver docstring: "On the fused pipeline a NaN or infinite pivot fails the factorization (info ≠ 0)") and docs/src/lib/kkt.md:55–59 say the same. All four are rendered in the documentation and are now wrong. Fix: state the new rule once (a NaN or Inf pivot is kept on both pipelines, info stays 0, the inertia counts a pivot as positive if > 0, zero if 0, else negative, NaN included, as CHOLMOD) and drop the stock-vs-fused contrast.

if li == 1
d[g] = dk
piv[g] = Int32(g)
Expand Down Expand Up @@ -1440,7 +1440,7 @@ end
w = Int(front_ncols[v])
if j <= w && st[1] == Int32(0)
dk, rep = _ldl_static_pivot(real(sh[SDS._packed(j, j, 64)]), eps, psg, g0 + j)
if isfinite(dk) && !iszero(dk) # NaN/Inf must fail like potrf, not "factor"
if !iszero(dk) # a NaN or Inf pivot is kept, as CHOLMOD's (the inertia counts a NaN as negative)
if li == 1
d[g0 + j] = dk
pivot_kind[g0 + j] = rep ? SDS.PIVOT_KIND_PERTURBED : SDS.PIVOT_KIND_1X1
Expand Down Expand Up @@ -1984,7 +1984,7 @@ end
# PIV: the wide fronts are factored without interchanges (identity local order)
PIV && (gpiv[gj] = gj % eltype(gpiv))
dj, rep = _ldl_static_pivot(dj, epsc, psg, gj)
if st[1] == Int32(0) && isfinite(dj) && !iszero(dj)
if st[1] == Int32(0) && !iszero(dj) # NaN/Inf kept, as CHOLMOD's
piv[1] = dj # d stays on the diagonal
sh[SDS._packed(j, j, 64)] = dj
d[gj] = dj
Expand Down Expand Up @@ -2858,13 +2858,11 @@ Phases (`execute!(phase, F, X, B; asynchronous = true)`):
`"factorization"`): the fused factorization of the current values, then the
inverses of the diagonal blocks; sets `"info"` (`0`, or the original column
of the first failed pivot: a non-positive Cholesky pivot, or for `"S"` a
zero pivot under `pivot_epsilon = 0`, or a non-finite pivot). A failed
factorization does not throw. Unlike the stock kernel, which replaces a NaN
pivot and accepts an infinite one, the fused LDLᵀ fails the factorization on
both under any `pivot_epsilon`, so a NaN or Inf in the matrix is reported
rather than factored (with `pivot_type = 'D'` only in the fronts wider than
64: the subtrees and the narrow fronts then run the stock pivot rule, NaN
and Inf included). `"S"` factors
zero pivot under `pivot_epsilon = 0`). A failed factorization does not
throw. As on the stock pipeline and in CHOLMOD, the LDLᵀ keeps a NaN or
infinite pivot (`"info"` stays `0`) and the inertia counts a NaN pivot as
negative; a NaN propagates through the rest of its front and its ancestors,
so it shows as an inertia defect rather than as `info ≠ 0`. `"S"` factors
its wide fronts in the fused kernel under either `wides` strategy (a front's
pivots may take both signs), so the in-kernel wide-front limit applies to it
on every part: a front with more than `1023 · 32` rows below a panel raises
Expand Down Expand Up @@ -2903,8 +2901,9 @@ run to run; `deterministic_mode = 1` runs the forward sweep without atomics
children's buffers in order), and the solution is bitwise reproducible too.
`getparam`
returns `"info"`, `"inertia"` (`(npos, nneg)`; `"SPD"`: the positive pivots and
`0`; `"S"`: the counts of positive and negative entries of `D`, a replaced
pivot counted with the sign it was given), `"npivots"` (`"S"`: the number of
`0`; `"S"`: the counts of the entries of `D` that are positive and that are
neither positive nor zero, NaN included, as CHOLMOD's; a replaced pivot is
counted with the sign it was given), `"npivots"` (`"S"`: the number of
replaced pivots, valid when `"info"` is `0`), the analysis data
`"perm_reorder_row"`, `"perm_reorder_col"`, `"perm_row"`, `"perm_col"`,
`"lu_nnz"`, `"flops"`, `"nsuperpanels"` of the inner solver, `"ir_n_steps"` (the
Expand Down Expand Up @@ -3346,7 +3345,8 @@ function SDS.getparam(F::FusedSolver{T, INT}, name::AbstractString) where {T, IN
name == "npivots" &&
return INT(count(==(SDS.PIVOT_KIND_PERTURBED), view(F.inner.numeric.pivot_kind, 1:size(F, 1))))
d = view(F.inner.numeric.d, 1:size(F, 1))
return (INT(count(>(zero(T)), d)), INT(count(<(zero(T)), d)))
# CHOLMOD's count (MadCore's inertia of its D): positive if > 0, zero if 0, else (NaN too) negative
return (INT(count(>(zero(T)), d)), INT(count(x -> !(x > zero(T)) && !iszero(x), d)))
Comment on lines +3348 to +3349

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): the fused "inertia" and "info" outputs and the stock "inertia" change meaning for matrices with a NaN, which MadNLP's binding reads; CHANGELOG.md has recorded every such behaviour change so far (e.g. the pivot_epsilon default under "Changed") but has no entry for this one. Add an Unreleased "Changed" entry. Also, with a NaN kept the whole remainder of the front and its ancestors turns NaN, so every later pivot counts as negative and the root pivot always does; worth one sentence in the docs so a reader understands why a NaN shows up as an inertia defect rather than as info ≠ 0.

end
name == "memory_estimates" && return _fused_memory_estimates(F)
name == "ir_n_steps" && return SDS.getparam(F.inner, name) # the steps of the last solve, as DirectSolver
Expand Down
5 changes: 2 additions & 3 deletions src/kkt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,8 @@ Keywords:
cuDSS), on both pipelines. With `:ldl` and `pivoting = :none` or
`:diagonal` a pivot `d` with `|d| < pivot_epsilon` becomes
`±pivot_epsilon`; `0` replaces none, and a zero pivot then fails the
factorization. On the fused pipeline a NaN or infinite pivot fails the
factorization (`info ≠ 0`); with `:diagonal` only in the fronts wider than
64, the others follow the stock rule.
factorization. On both pipelines a NaN or infinite pivot is kept, as
CHOLMOD's, and the inertia counts a NaN pivot as negative.
* `pivot_sign = nothing`: `:ldl` only, a vector of `n` entries in `(-1, 0, 1)`,
the `"pivot_sign"` a replaced pivot takes (an interior-point method passes
`1` for the primal and `-1` for the dual rows; `0` or `nothing`: the sign of
Expand Down
8 changes: 5 additions & 3 deletions src/numeric/ldlt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,9 @@ end
x = _fget(F, c, c)
dk = H ? T(real(x)) : x
kind = PIVOT_KIND_1X1
if !(abs(dk) >= ε) || iszero(dk) # tiny (or NaN, or exactly zero): perturb
# tiny or exactly zero: perturb. A NaN pivot is kept (as CHOLMOD's dbound) and counted as negative by the
# inertia below
if abs(dk) < ε || iszero(dk)
iszero(dk) && (ctl[_LT_STAT + STAT_NZERO] += one(IT))
dk = _perturbation_sign(dk, Int(psign[perm[piv[c0 + c - 1]]]), H) * ε
kind = PIVOT_KIND_PERTURBED
Expand All @@ -408,10 +410,10 @@ end
d[g] = dk
d[n + g] = zero(T)
pivot_kind[g] = kind
if H
if H # CHOLMOD's count (MadCore's inertia of its D): positive if > 0, zero if 0, else (NaN too) negative
if real(dk) > 0
ctl[_LT_STAT + STAT_NPOS] += one(IT)
elseif real(dk) < 0
elseif !iszero(real(dk))
ctl[_LT_STAT + STAT_NNEG] += one(IT)
Comment on lines +413 to 417

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.

No test exercises the new behaviour. grep -rn "NaN\|isnan\|isfinite\|\bInf\b" test/ finds nothing in test_numeric_ldlt.jl, test_reference_ldlt.jl, test_fused.jl or test_kkt.jl, so the change from "perturb a NaN pivot" (stock, reference) / "fail the factorization" (fused) to "keep it and count it as negative" lands with no coverage of either the kept pivot (pivot_kind == PIVOT_KIND_1X1, d NaN, npivots unchanged, fused info == 0) or the inertia ((npos, nneg) with the NaN pivots in nneg, inertia(K) with nzero == 0). Fix: add a testset that puts a NaN on the diagonal of a small KKT-like matrix from test/matrices.jl, over BACKENDS and eltypes_among(...) with Random.seed!(666), on the stock kernel, the reference and the fused pipeline (both pivot_type = 'N' and 'D', since the diagonal-pivoting path reaches the pivot through _lt_pass3!/line 553 and the fused kernels at 1343, 1443 and 1987 are three separate code paths), and checks the pivot kind, info and the inertia.

end
end
Expand Down
4 changes: 2 additions & 2 deletions src/reference/ldlt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ function _ldlt_front!(F::Matrix{T}, w::Int, prm::_LDLTParams, signof) where {T}
_swap_front!(F, k, c, w, lp)
d = prm.herm ? T(real(F[k, k])) : F[k, k]
kind[k] = PIVOT_KIND_1X1
if !(abs(d) >= prm.eps) || iszero(d) # tiny (or NaN, or exactly zero): perturb
if abs(d) < prm.eps || iszero(d) # tiny or exactly zero: perturb (a NaN pivot is kept, as CHOLMOD's)
iszero(d) && (st[STAT_NZERO] += 1)
d = _perturbation_sign(d, signof(lp[k]), prm.herm) * prm.eps
kind[k] = PIVOT_KIND_PERTURBED
Expand All @@ -406,7 +406,7 @@ function _ldlt_front!(F::Matrix{T}, w::Int, prm::_LDLTParams, signof) where {T}
F[k, k] = d
dd[k] = d
if prm.herm
real(d) > 0 ? (st[STAT_NPOS] += 1) : real(d) < 0 && (st[STAT_NNEG] += 1)
real(d) > 0 ? (st[STAT_NPOS] += 1) : !iszero(real(d)) && (st[STAT_NNEG] += 1) # NaN too, as CHOLMOD
end
for i in (k + 1):f
l = F[i, k] / d
Expand Down
47 changes: 47 additions & 0 deletions test/test_kkt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -807,3 +807,50 @@ end
@test rs.r2 <= tol(T)
end
end

# #207: a NaN pivot is kept on every path, as CHOLMOD's dbound keeps it, and the inertia counts a pivot as positive if
# it is > 0, zero if 0 and negative otherwise, NaN included. grid_kkt_matrix 10×10 with one primal diagonal entry set
# to NaN: the NaN reaches its pivot unreplaced and propagates through the front's ancestors, `"info"` stays 0, and the
# inertia has no zero eigenvalue and counts every NaN pivot as negative. The stock kernel, the fused pipeline (the
# default on CUDA, ROCm and oneAPI) with `:none` and with `:diagonal`, and the reference.
kkt_numeric(K) = (F = K.inner; (F isa SDS.FusedSolver ? F.inner : F).numeric)

function check_nan_pivots(d, kinds, n, inertia)
d, kinds = Array(d)[1:n], Array(kinds)[1:n]
nan = isnan.(d)
@test any(nan) # the NaN is kept, not replaced
@test !any(==(SDS.PIVOT_KIND_PERTURBED), kinds[nan])
npos, nneg = count(>(0), d), count(x -> !(x > 0) && !iszero(x), d)
@test count(nan) <= nneg # every NaN pivot counted as negative
@test inertia == (npos, 0, nneg)
@test npos + nneg == n
end

@testset "NaN pivot on $(backend_name(backend))" for backend in BACKENDS
@testset "$label: $T" for T in eltypes_among((Float32, Float64)),
(label, kw) in (("stock, :none", (; pivoting = :none, fused = false)),
("default pipeline, :none", (; pivoting = :none)),
("default pipeline, :diagonal", (; pivoting = :diagonal)))
Random.seed!(666)
A = grid_kkt_matrix(T, 10, 10, 1.0)
A[37, 37] = T(NaN)
n = size(A, 1)
args, _ = kkt_input(backend, A)
K = KKTSolver(args...; factorization = :ldl, pivot_epsilon = 1e-4, kw...)
@test getparam(K, "info") == 0
N = kkt_numeric(K)
check_nan_pivots(N.d, N.pivot_kind, n, SDS.inertia(K))
end
end

@testset "NaN pivot, reference: $T" for T in eltypes_among((Float32, Float64))
A = grid_kkt_matrix(T, 10, 10, 1.0)
A[37, 37] = T(NaN)
n = size(A, 1)
S, N, info, _ = reference_ldlt(A; opts = Options(pivot_type = 'N', pivot_epsilon = 1e-4))
@test info == 0
d = N.d[1:n]
npos, nneg = count(>(0), d), count(x -> !(x > 0) && !iszero(x), d)
check_nan_pivots(N.d, N.pivot_kind, n, (npos, 0, nneg))
@test SDS.pivot_stats(N).nneg == nneg && SDS.pivot_stats(N).npos == npos
end
Loading