diff --git a/CHANGELOG.md b/CHANGELOG.md index ee526053..df27de22 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/docs/src/lib/kkt.md b/docs/src/lib/kkt.md index a2ba4052..bc75d0a2 100644 --- a/docs/src/lib/kkt.md +++ b/docs/src/lib/kkt.md @@ -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 diff --git a/src/fused.jl b/src/fused.jl index e1b7f2fa..1966b516 100644 --- a/src/fused.jl +++ b/src/fused.jl @@ -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] @@ -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) if li == 1 d[g] = dk piv[g] = Int32(g) @@ -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 @@ -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 @@ -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 @@ -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 @@ -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))) 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 diff --git a/src/kkt.jl b/src/kkt.jl index d04c6081..d08bb2d5 100644 --- a/src/kkt.jl +++ b/src/kkt.jl @@ -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 diff --git a/src/numeric/ldlt.jl b/src/numeric/ldlt.jl index 486b225e..78cab4a5 100644 --- a/src/numeric/ldlt.jl +++ b/src/numeric/ldlt.jl @@ -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 @@ -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) end end diff --git a/src/reference/ldlt.jl b/src/reference/ldlt.jl index 1b1f06de..31df1bbd 100644 --- a/src/reference/ldlt.jl +++ b/src/reference/ldlt.jl @@ -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 @@ -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 diff --git a/test/test_kkt.jl b/test/test_kkt.jl index 47af000a..52054002 100644 --- a/test/test_kkt.jl +++ b/test/test_kkt.jl @@ -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