Skip to content

LDLᵀ: keep NaN pivots and count them as negative in the inertia, as CHOLMOD - #207

Open
sshin23 wants to merge 2 commits into
mainfrom
cholmod-pivots
Open

sshin23 wants to merge 2 commits into
mainfrom
cholmod-pivots

Conversation

@sshin23

@sshin23 sshin23 commented Nov 28, 2026

Copy link
Copy Markdown
Contributor

LDLᵀ pivots are handled as CHOLMOD handles them:

  • a NaN pivot is kept instead of being replaced by the bound (stock kernel and reference) or failing the factorization (fused);
  • the inertia counts a pivot as positive if it is > 0, zero if it is 0, and negative otherwise, so a NaN pivot counts as negative (as MadCore's _inertia_ldlt does for CHOLMOD).

With this, an interior-point solver sees a NaN pivot as a wrong inertia and regularizes, as it does with CHOLMOD, instead of either solving with a replaced pivot or failing.

Benchmarked through MadNLP (ShiftedPreset and CondensedPreset on CUDA, full ACOPF/COPS/CUTEst/Mittelmann sets) together with the SDSSolver defaults of madsuite-org/MadNLP.jl sds-pivot-sign.

…e) and not failed (fused), and the inertia counts a NaN pivot as negative (positive if > 0, zero if 0, else negative)
Comment thread src/numeric/ldlt.jl Outdated
Comment on lines +400 to +401
# tiny or exactly zero: perturb. A NaN pivot is kept (as CHOLMOD's dbound): it counts in neither npos nor
# nneg, so the inertia shows it as a zero eigenvalue, the signal of a singular matrix

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.

This comment contradicts the code it introduces. It says a kept NaN pivot "counts in neither npos nor nneg, so the inertia shows it as a zero eigenvalue", but the count below (elseif !iszero(real(dk)), and the comment on it) puts a NaN into STAT_NNEG, which is also what the PR title, the reference kernel and the fused getparam do. Fix: replace the second sentence with the actual rule, e.g. "A NaN pivot is kept (as CHOLMOD's dbound) and counted as negative by the inertia below".

Comment thread src/numeric/ldlt.jl
Comment on lines +413 to 417
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)

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.

Comment thread src/fused.jl
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.

Comment thread src/fused.jl
Comment on lines +3349 to +3350
# 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)))

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.

@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: CHANGES_REQUESTED (head f156dcd)

This is not a TNN task PR (no TASKS.md section, no Report), so it was reviewed against AGENTS.md, PLAN.md and the PR body.

Checked:

  • Scope: three source files, no PLAN.md/TASKS.md/Manifest changes, no public name or parameter string changed.
  • Correctness of the new rule: abs(d) < eps || iszero(d) leaves a NaN unperturbed in the stock kernel and the reference; the fused kernels keep a NaN/Inf pivot at the three pivot sites (regime A, narrow fronts, wide fronts) and leave info at 0; the 2×2 path still rejects a NaN through isfinite(det); _ldl_static_pivot already returned NaN/Inf unchanged. The inertia count (> 0 positive, 0 zero, else negative) is consistent across the stock kernel, the reference and the fused getparam, and inertia(K) derives nzero from it, so a NaN lands in nneg, as the PR says.
  • Conventions: generic in T/INT, no new allocation or host synchronization in the kernels, no @allowscalar.
  • CI on this head: CPU-only, cuda, amdgpu, Aurora and the docs build pass.

Blocking findings (inline comments):

  1. src/numeric/ldlt.jl:400-401: the new comment says a NaN pivot counts in neither npos nor nneg and shows as a zero eigenvalue; the code four lines below counts it as negative.
  2. No test covers the new behaviour on any path (stock kernel, reference, fused with pivot_type 'N' and 'D', KKTSolver.inertia); nothing in test/ puts a NaN or Inf pivot through LDLᵀ. The old behaviour was untested too, which is why CI stayed green.
  3. Public docstrings and docs still describe the old behaviour: src/fused.jl execute! (lines 2859–2867: "a non-finite pivot" fails, "Unlike the stock kernel, which replaces a NaN pivot …") and getparam (2905–2907), src/kkt.jl:88–90 (KKTSolver), docs/src/lib/kkt.md:55–59.

Non-blocking: no CHANGELOG entry for a behaviour change that MadNLP observes through info and inertia.

…aN counts as negative), docstrings and docs describe the kept NaN on both pipelines, CHANGELOG entry, and tests of the kept pivot and the inertia on the stock kernel, the reference and the fused pipeline (:none and :diagonal)

This branch has not been deployed

No deployments
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.

2 participants