Skip to content

Guard against a NaN pivot in the split Cholesky factorization xPBSTF - #1502

Open
rmlarsen wants to merge 1 commit into
Reference-LAPACK:masterfrom
rmlarsen:pbstf-nan-guard
Open

rmlarsen wants to merge 1 commit into
Reference-LAPACK:masterfrom
rmlarsen:pbstf-nan-guard

Conversation

@rmlarsen

Copy link
Copy Markdown
Contributor

AI disclosure: This PR was prepared with Claude Code.

Summary

xPBSTF accepts a NaN pivot and returns INFO = 0, so a band matrix containing a NaN is "factored" into a NaN factor. This is the defect fixed for xPPTRF, xPBTF2 and xPTTRF in #1380, left in the split factorization. The generalized band drivers xSBGV, xSBGVD, xSBGVX and xHBGV, xHBGVD, xHBGVX document INFO = N+i when the leading minor of order i of B is not positive definite, and with a NaN in B they report something else. For N = 12, KD = 3, A = 2 on the diagonal and 0.5 beside it, and B = 4 and 1 with B(3,3) = NaN: DPBSTF returns INFO = 0, DSBGV returns INFO = 11 from DSTEQR instead of 15, and DSBGVX returns INFO = 0 with M = 0, a silent success with no eigenvalues. CHBGV does not return at all on some placements.

Description

The four pivot tests in each of SPBSTF, DPBSTF, CPBSTF and ZPBSTF are IF( AJJ.LE.ZERO ), and a NaN compares false. xPOTF2, xPOTRF2, xPBTF2, xPPTRF, xPSTF2 and xPTTRF test AJJ.LE.ZERO.OR.xISNAN( AJJ ).

A NaN on the diagonal at j is the pivot itself, so with the guard INFO = j. A NaN off the diagonal is scaled at the pivot of its column and enters, through the rank-one update, the diagonal of a row whose pivot is tested later, so it is reported at a later pivot. I found no position in the band that cannot reach a pivot, and the test below checks every one.

Fix

Add SISNAN / DISNAN to the four tests of each routine, in the style of xPBTF2. With the fix the reproducer gives DPBSTF INFO = 3, DSBGV INFO = 15 and DSBGVX INFO = 15. Matrices without a NaN take exactly the same path as before. xSBGST, xHBGST and the drivers only pass INFO on, so they are unchanged.

Reproducer

program repro
  use, intrinsic :: ieee_arithmetic, only: ieee_value, ieee_quiet_nan
  implicit none
  integer, parameter :: n = 12, kd = 3
  double precision :: ab(kd+1, n), bb(kd+1, n), w(n), z(n, n), work(7*n), q(n, n)
  integer :: info, m, iwork(5*n), ifail(n)
  external :: dpbstf, dsbgv, dsbgvx

  call init()
  call dpbstf('U', n, kd, bb, kd+1, info)
  print *, 'DPBSTF INFO =', info

  call init()
  call dsbgv('V', 'U', n, kd, kd, ab, kd+1, bb, kd+1, w, z, n, work, info)
  print *, 'DSBGV  INFO =', info

  call init()
  call dsbgvx('V', 'A', 'U', n, kd, kd, ab, kd+1, bb, kd+1, q, n, 0d0, 0d0, &
              1, n, 0d0, m, w, z, n, work, iwork, ifail, info)
  print *, 'DSBGVX INFO =', info, ' M =', m

contains
  ! A = 2 on the diagonal, 0.5 next to it; B = 4 and 1, with B(3,3) = NaN.
  subroutine init()
    ab = 0.0d0; ab(kd+1, :) = 2.0d0; ab(kd, :) = 0.5d0
    bb = 0.0d0; bb(kd+1, :) = 4.0d0; bb(kd, :) = 1.0d0
    bb(kd+1, 3) = ieee_value(1.0d0, ieee_quiet_nan)
  end subroutine
end program repro

On master:

 DPBSTF INFO =           0
 DSBGV  INFO =          11
 DSBGVX INFO =           0  M =           0

With the fix:

 DPBSTF INFO =           3
 DSBGV  INFO =          15
 DSBGVX INFO =          15  M =           0

Regression test

xPBSTF is not tested by the LIN suite, and the xDRVSG drivers in the EIG suite only generate positive definite B, so there are four standalone programs, test_[sdcz]pbstf_nan in TESTING/EIG, registered in the CMake build like the other standalone test_* programs there (those have no entries in the Make build either, so these have none).

For six (N, KD) pairs, (12,3), (11,4), (7,1), (6,5), (2,1) and (1,0), and both values of UPLO, each program writes a NaN into every entry of the band of a positive definite B. It requires INFO = j from xPBSTF for a NaN at diagonal position j and some INFO in 1..N for a NaN off the diagonal. For a diagonal NaN it also requires INFO = N+j from xSBGV, xSBGVD and xSBGVX (xHBGV* for the complex programs) with JOBZ = 'N' and 'V'. For the finite matrix all of them must return 0.

On the parent commit the single and double programs each report 718 failures (250 from xPBSTF, 468 from the drivers). The complex programs do not finish: CHBGV hangs on a NaN in B, and the ZHBGV program also times out. With the fix all four programs pass.

Test suite

The full test suite passes, configured as in CI (CBLAS=ON, LAPACKE=ON, LAPACKE_WITH_TMG=ON) with the _64 API. All 341 CTest entries pass (the 333 of master and the eight new programs), with 5755188 LAPACK tests (5755190 for the _64 API), 11973112 LAPACKE tests, 319677 BLAS tests and 645492 CBLAS tests for each API, and 0 numerical errors and 0 other errors. The test counts are those of master; the new programs are not counted in them. A second build with CI's gfortran flags (-O2 -Wall -Wno-unused-dummy-argument -Wno-unused-variable -Wno-unused-label -Werror=conversion -fimplicit-none -frecursive -fcheck=all) passes the suite as well, and the changed files compile with the same warnings as on master. The builds used GNU Fortran and GCC 15.2.0 with CMAKE_BUILD_TYPE=Release on x86-64 Linux.

Not changed here

  • xSBGVX and xHBGVX with a NaN in A (not B) return INFO = 0 with M = 0; nothing checks A. This is a separate matter.
  • An Inf on the diagonal of B passes the pivot test, as in the other Cholesky routines.
  • The complex hang for a NaN in B is not looked into further here; with the guard it is not reached.

Checklist

  • The documentation has been updated. (Not needed: the routines now return the INFO they document.)
  • If the PR solves a specific issue, it is set to be closed on merge. (No tracking issue.)

xPBSTF rejects a pivot with

   IF( AJJ.LE.ZERO ) GO TO 50

at four sites per routine.  A NaN is not .LE.ZERO, so a band matrix that
contains one is factored into a NaN factor and returned with INFO = 0.
This is the defect fixed for xPPTRF, xPBTF2 and xPTTRF in Reference-LAPACK#1380; xPOTF2,
xPBTF2, xPPTRF, xPSTF2 and xPTTRF now all test AJJ.LE.ZERO.OR.xISNAN(
AJJ ).

The generalized band drivers xSBGV, xSBGVD, xSBGVX and the Hermitian
xHBGV* call xPBSTF and document INFO = N+i when the leading minor of
order i of B is not positive definite.  With the NaN undetected they
report something else.  For N = 12, KD = 3, UPLO = 'U', A = 2 on the
diagonal and 0.5 beside it, and B = 4 and 1 with B(3,3) = NaN, on the
parent commit DPBSTF returns INFO = 0, DSBGV returns INFO = 11 from
DSTEQR instead of 15, and DSBGVX returns INFO = 0 with M = 0, a silent
success with no eigenvalues.  The complex drivers CHBGV and ZHBGV do not
return at all for some placements.

Add the SISNAN / DISNAN term at all four sites of each of SPBSTF,
DPBSTF, CPBSTF and ZPBSTF.  A NaN on the diagonal is the pivot itself,
so INFO is its column.  A NaN off the diagonal is scaled at the pivot of
its column and enters the diagonal of an earlier-tested row through the
rank-one update, so it is reported at a later pivot; every position in
the band reaches a pivot.  With the fix the reproducer gives INFO = 3,
15 and 15.

Test: the xeigtst drivers only generate positive definite B, and
xPBSTF is tested nowhere else, so there are four standalone programs,
test_[sdcz]pbstf_nan, in TESTING/EIG, registered in CMake like the other
standalone test_* programs there.  For six (N, KD) pairs and both values
of UPLO each program writes a NaN into every entry of the band of a
positive definite B and requires INFO = j from xPBSTF for a NaN at
diagonal position j and some INFO in 1..N for a NaN off the diagonal.
For a diagonal NaN it also requires INFO = N+j from xSBGV, xSBGVD and
xSBGVX (xHBGV* in the complex case) for JOBZ = 'N' and 'V', and INFO = 0
from all of them for the finite matrix.  On the parent commit the single
and double programs report 718 failures each (250 from xPBSTF, 468 from
the drivers); the complex programs do not finish, because CHBGV and
ZHBGV hang on a NaN in B.  With the fix all four pass.  The 11172 SSG,
DSG, CSG and ZSG tests and the 810 SSB, DSB, CHB and ZHB tests pass
unchanged.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@rmlarsen
rmlarsen marked this pull request as draft November 22, 2026 03:15
@rmlarsen
rmlarsen marked this pull request as ready for review November 22, 2026 03:25
@codecov

codecov Bot commented Nov 22, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 83.43949% with 52 lines in your changes missing coverage. Please review.
✅ Project coverage is 70.67%. Comparing base (ef55c9b) to head (c1c76e6).
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
TESTING/EIG/test_cpbstf_nan.f90 82.66% 8 Missing and 5 partials ⚠️
TESTING/EIG/test_dpbstf_nan.f90 82.43% 8 Missing and 5 partials ⚠️
TESTING/EIG/test_spbstf_nan.f90 82.43% 8 Missing and 5 partials ⚠️
TESTING/EIG/test_zpbstf_nan.f90 82.66% 8 Missing and 5 partials ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1502      +/-   ##
==========================================
+ Coverage   70.63%   70.67%   +0.03%     
==========================================
  Files        6243     6247       +4     
  Lines      501290   501588     +298     
  Branches    23516    23564      +48     
==========================================
+ Hits       354100   354502     +402     
+ Misses     146327   146203     -124     
- Partials      863      883      +20     
Components Coverage Δ
BLAS 99.73% <ø> (ø)
CBLAS 96.95% <ø> (ø)
LAPACK 83.81% <100.00%> (+0.07%) ⬆️
LAPACKE 4.18% <ø> (ø)
TMGLIB 55.69% <ø> (ø)
BLAS testing 88.79% <ø> (ø)
CBLAS testing 89.65% <ø> (ø)
LAPACK testing 82.40% <82.55%> (+<0.01%) ⬆️
LAPACKE testing ∅ <ø> (∅)
Files with missing lines Coverage Δ
SRC/cpbstf.f 90.27% <100.00%> (+13.88%) ⬆️
SRC/dpbstf.f 89.06% <100.00%> (+9.37%) ⬆️
SRC/spbstf.f 89.06% <100.00%> (+9.37%) ⬆️
SRC/zpbstf.f 90.27% <100.00%> (+13.88%) ⬆️
TESTING/EIG/test_cpbstf_nan.f90 82.66% <82.66%> (ø)
TESTING/EIG/test_dpbstf_nan.f90 82.43% <82.43%> (ø)
TESTING/EIG/test_spbstf_nan.f90 82.43% <82.43%> (ø)
TESTING/EIG/test_zpbstf_nan.f90 82.66% <82.66%> (ø)

... and 14 files with indirect coverage changes


Continue to review full report in Codecov by Harness.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update ef55c9b...c1c76e6. Read the comment docs.

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant