Skip to content

Report a NaN or an infinity in the input of xGELSD, xGESDD, xGEEV, xLALSD and xBDSDC through INFO instead of stopping in XERBLA - #1382

Merged
langou merged 4 commits into
Reference-LAPACK:masterfrom
rmlarsen:dc-nan-info-not-xerbla
Nov 12, 2026
Merged

langou merged 4 commits into
Reference-LAPACK:masterfrom
rmlarsen:dc-nan-info-not-xerbla

Conversation

@rmlarsen

@rmlarsen rmlarsen commented Sep 7, 2026 •

Copy link
Copy Markdown
Contributor

AI disclosure: This PR was prepared with Claude Code.

Summary

xGELSD stops the program when A contains a NaN or an infinity, and so do xLALSD and xBDSDC when the bidiagonal does. In each case the routine scales by a norm that it never tests: xLANST propagates a NaN, xLASCL rejects CFROM = NaN as an illegal argument, and the reference XERBLA stops. An infinity gets the same treatment, because xGELSD's own scaling multiplies by zero and leaves a NaN where the infinity was. xGEEV returns INFO = -4 for a NaN in A (#1136), but it calls XERBLA first, and an infinity in A comes back as INFO = 0 with every eigenvalue a NaN.

Following the rule of #1136 (a NaN in the norm that a routine computes for scaling is reported as an error in that argument, here extended to an infinity), this PR tests those norms and returns INFO = -(position of the array):

routine input INFO
xGELSD NaN or infinity in A -4
xGESDD NaN (already, #469) or, now, infinity in A -4
xGEEV NaN (already, #1136, but through XERBLA) or, now, infinity in A -4
xLALSD NaN or infinity in D / E -5 / -6
xBDSDC NaN or infinity in D / E -4 / -5

As in xGESDD, none of these returns calls XERBLA: the input is bad data, not a programming error, and the caller gets control back. Nothing changes for finite input.

Description

With a NaN at A(2,2) of a 6-by-4 least-squares problem, one process per call:

driver master
DGELS, DGELST, DGETSLS, DGELSY returns, INFO = 0, NaN solution
DGELSS returns, INFO = 3 (DBDSQR did not converge)
DGELSD, ZGELSD process stops: ** On entry to DLASCL parameter number 4 had an illegal value

The backtrace is DGELSD -> DLALSD -> DLASCL -> XERBLA, from the unconditional DLASCL( 'G', 0, 0, ORGNRM, ONE, ... ) after ORGNRM = DLANST( 'M', N, D, E ). xGESDD already returns INFO = -4 for a NaN in A, but an infinity passes that guard, becomes a NaN in the same way, and stops the process inside xBDSDC.

Fix

  • {s,d,c,z}gelsd.f: right after ANRM = xLANGE( 'M', ... ), return INFO = -4 if ANRM is a NaN or exceeds xLAMCH( 'Overflow' ).
  • {s,d,c,z}gesdd.f: the existing xISNAN( ANRM ) guard also tests ANRM > xLAMCH( 'Overflow' ). Without it, an infinity in A would reach xBDSDC as a NaN and come back as INFO = -5, which in xGESDD's argument list would name LDA.
  • {s,d,c,z}lalsd.f and {s,d}bdsdc.f: after the N = 0 quick return, xLANGE( 'M', ... ) of D and then of E, and a return with the position of the first one that is not finite. The test comes before any work: the lower-to-upper rotation for UPLO = 'L' mixes E into D, after which the two can no longer be told apart. It also covers every path, including the small-problem and singular-values-only paths of xBDSDC, which go through xLASDQ and previously returned INFO = 0 with a NaN result or a non-convergence count.
  • {s,d,c,z}geev.f: the NaN test on ANRM returns without calling XERBLA, also tests ANRM > xLAMCH( 'Overflow' ), and moves before the scaling branches, since an infinite ANRM also exceeds BIGNUM.

The INFO descriptions of all five routines document the new values. Eighteen source files.

For a complex A, xLANGE takes the absolute value of each entry, so in CGEEV, ZGEEV, CGESDD, ZGESDD, CGELSD and ZGELSD an entry with finite real and imaginary parts whose absolute value overflows also returns -4; master returns INFO = 0 for such a matrix, with NaN eigenvalues or singular values, because the scaling by BIGNUM / ANRM is a scaling by zero. Their INFO descriptions say so, as #1483 does for CGESVDMR3 and ZGESVDMR3.

Minimal reproducer

program minimal
  implicit none
  integer, parameter :: m = 6, n = 4
  double precision :: a(m, n), b(m), s(n), work(4096), z
  integer :: iwork(256), info, rank, i, j
  do j = 1, n
    do i = 1, m
      a(i, j) = 1 + mod(3*i + 5*j, 7) / 7d0
      if (i == j) a(i, j) = a(i, j) + 3
    end do
  end do
  z = 0
  a(2, 2) = z / z
  b = 1
  call dgelsd(m, n, 1, a, m, b, m, s, -1d0, rank, work, 4096, iwork, info)
  print '(a,i0)', 'DGELSD returned, info = ', info
end program
BEFORE (master):      ** On entry to DLASCL parameter number  4 had an illegal value
                     (program stopped by XERBLA)
AFTER (this branch):  DGELSD returned, info = -4

Validation

NaN, +Inf and -Inf at every position, all precisions, every path

A sweep program puts a NaN, +Inf or -Inf (in a complex A, the -Inf goes in the imaginary part) at each position of the input and checks the returned INFO. XERBLA is replaced by a routine that stops the process with status 99, so a completed run proves that no case reached it.

routine inputs cases per precision result
xGELSD A: shapes (1,1), (2,2), (5,3), (3,5), (25,25), (26,26), (40,25), (25,40), (80,60), every position up to 40 per shape 705 all -4
xGESDD the same matrices, JOBZ = 'N' and 'S' 1410 all -4
xLALSD D and E for N = 1, 2, 5, 25, 26, 60, UPLO = 'U' and 'L', every position 1392 all -5 / -6
xBDSDC the same, COMPQ = 'N', 'P' and 'I' 4176 all -4 / -5

22380 cases in total. On master the sweep stops on its first case, in SLASCL called from SGELSD.

Finite input: the same program writes every output (INFO, RANK, S, B, U, VT, D) of the four routines over the same shapes, sizes and options to a file; the file from this branch is bit-identical to the one from master.

Regression tests

The error-exit tests get the cases, since the point is that the routine returns at all and returns the right INFO:

  • xERRLS: xGELSD with a NaN and with an infinity in A (-4), and xLALSD with an infinity in D (-5) and a NaN in E (-6).
  • xERRBD: xBDSDC with an infinity in D (-4) and a NaN in E (-5).
  • xERRED: xGESDD and xGEEV with a NaN and with an infinity in A (-4).

Each case also fails if XERBLA is called. Built with only the test changes on top of master, every case fails in all precisions except xGESDD with a NaN, which master already handles (#469) (*** DGELSD did not return INFO = -4 without calling XERBLA for input with a NaN or an infinity (INFO = -4) ***, where -4 is the value DLASCL hands back after its XERBLA call, and so on). With the xGEEV of the previous revision, its NaN case calls XERBLA and its infinity case returns INFO = 0.

xERRLS also records what the least-squares drivers do with a NaN in B, which none of them checks: for each of xGELS, xGELST, xGETSLS, xGELSS, xGELSY and xGELSD, a NaN in one column of B gives INFO = 0, the solution for the other column, and a NaN in the solution for its own, without calling XERBLA. These cases pass on master as well; they pin down the current behavior (see "Not changed here" for an infinity). xGELSD needs a real workspace of about 800 for a 2 by 2 problem, so the arrays in xERRLS grow accordingly. The xERRLS and xERRED files join the -Onopropagate list that already keeps the NAG compiler from folding the SQRT( -ONE ) and ONE / ZERO that build the NaN and the infinity.

Test suite

The full LAPACK test suite passes on this branch, for the default API and for the _64 API: 119 of 119 CTest entries without the _64 API and all 118 _64 entries with it, each with 5739011 LAPACK tests and 319677 BLAS tests, 0 numerical errors and 0 other errors (13th Gen Intel(R) Core(TM) i7-13700HX, Linux under WSL2, GNU Fortran 15.2.0, CMAKE_BUILD_TYPE=Release). The previous revision passed on an Apple M4 (macOS 26.7, Homebrew GCC 16.2) with 5738981 LAPACK tests.

Performance

The new tests are one xLANGE( 'M', ... ) of D and one of E, which is O(N), in front of O(N^2) work or more in xLALSD and xBDSDC, and one comparison in xGELSD and xGESDD. I have not re-timed this revision; the timings in the earlier description were for the previous version of the patch and are no longer shown.

Not changed here

The least-squares drivers do not test B, and they solve with whatever it contains. With a NaN in one column of B that gives a NaN solution for that column and leaves the others exact (bit-identical to a run with a finite value in its place); xERRLS now checks this.

An infinity in B does not stay in its column. Every driver scales B by BIGNUM / BNRM = 0 when BNRM is infinite, which turns the infinity into a NaN and every other entry into zero, and undoing the scaling at the end multiplies by an infinity, which turns those zeros into NaNs. The solution for every right-hand side comes back NaN, with INFO = 0: in double precision, all six drivers, for shapes 6x4, 4x6, 60x40, 40x60 and 30x30, +Inf and -Inf. Skipping that scaling when BNRM is infinite would make an infinity behave like a NaN; that is not part of this PR.

Checklist

  • The documentation has been updated. (INFO descriptions of xGELSD, xGESDD, xGEEV, xLALSD and xBDSDC.)
  • If the PR solves a specific issue, it is set to be closed on merge. (No tracking issue; happy to open one.)

@codecov

codecov Bot commented Sep 7, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 90.61372% with 52 lines in your changes missing coverage. Please review.
✅ Project coverage is 70.12%. Comparing base (c9967aa) to head (451264e).
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
TESTING/LIN/cerrls.f 90.78% 7 Missing ⚠️
TESTING/LIN/derrls.f 90.78% 7 Missing ⚠️
TESTING/LIN/serrls.f 90.78% 7 Missing ⚠️
TESTING/LIN/zerrls.f 90.78% 7 Missing ⚠️
TESTING/EIG/cerred.f 87.50% 4 Missing ⚠️
TESTING/EIG/derred.f 87.50% 4 Missing ⚠️
TESTING/EIG/serred.f 87.50% 4 Missing ⚠️
TESTING/EIG/zerred.f 87.50% 4 Missing ⚠️
TESTING/EIG/derrbd.f 89.47% 2 Missing ⚠️
TESTING/EIG/serrbd.f 89.47% 2 Missing ⚠️
... and 4 more
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1382      +/-   ##
==========================================
+ Coverage   70.10%   70.12%   +0.02%     
==========================================
  Files        6213     6213              
  Lines      498005   498531     +526     
  Branches    23446    23446              
==========================================
+ Hits       349106   349615     +509     
- Misses     148462   148480      +18     
+ Partials      437      436       -1     
Components Coverage Δ
BLAS 99.73% <ø> (+0.02%) ⬆️
CBLAS 97.10% <ø> (ø)
LAPACK 83.52% <95.23%> (+0.01%) ⬆️
LAPACKE 2.17% <ø> (ø)
TMGLIB 55.69% <ø> (ø)
BLAS testing 88.77% <ø> (ø)
CBLAS testing 89.65% <ø> (ø)
LAPACK testing 82.36% <89.78%> (+0.02%) ⬆️
LAPACKE testing ∅ <ø> (∅)
Files with missing lines Coverage Δ
SRC/cgeev.f 98.40% <100.00%> (+2.36%) ⬆️
SRC/cgelsd.f 81.39% <100.00%> (+0.33%) ⬆️
SRC/cgesdd.f 96.01% <100.00%> (+0.31%) ⬆️
SRC/dbdsdc.f 77.91% <100.00%> (+1.13%) ⬆️
SRC/dgeev.f 97.94% <100.00%> (+2.02%) ⬆️
SRC/dgelsd.f 81.43% <100.00%> (+0.33%) ⬆️
SRC/dgesdd.f 95.06% <100.00%> (+0.44%) ⬆️
SRC/sbdsdc.f 77.91% <100.00%> (+1.13%) ⬆️
SRC/sgeev.f 97.94% <100.00%> (+2.02%) ⬆️
SRC/sgelsd.f 81.43% <100.00%> (+0.33%) ⬆️
... and 18 more

... and 5 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 c9967aa...451264e. Read the comment docs.

@rmlarsen
rmlarsen force-pushed the dc-nan-info-not-xerbla branch from 9e8fed1 to 24e080d Compare September 8, 2026 03:20
rmlarsen added a commit to rmlarsen/lapack that referenced this pull request Sep 8, 2026
…is a NaN

xLASQ1 scales the bidiagonal matrix by its largest entry SIGMX before
running dqds.  When D or E contains a NaN that survives the MAX
reductions computing SIGMX (with gfortran, a NaN in D(N)), SIGMX is a
NaN and xLASCL rejects it by stopping the process in XERBLA.  xBDSQR
without singular vectors and xGESVD with JOBU = JOBVT = 'N' take this
path; a NaN elsewhere in D returns INFO = 0 with NaN output.

Test SIGMX with xISNAN and return INFO = 1, the mechanism Reference-LAPACK#1382 uses
for xLALSD and xBDSDC.  xBDSQR treats every nonzero INFO from xLASQ1
as a request to finish with the QR algorithm, so it then reports the
non-convergence on the NaN data through its own INFO like it does for
other non-finite input.  Finite input never takes the new branch.

xERRBD gets the case as a regression test: a 4 by 4 bidiagonal with a
NaN in D(N), passed to xBDSQR without singular vectors, which must
return without calling XERBLA.  The error-exit tests are where it
belongs, since the routine returns no meaningful output for such a
matrix and the point of the test is that it returns at all.  All four
xERRBD files carry it, because the complex xBDSQR takes the same path
through the real xLASQ1.

Over 120 NaN/Inf cases of DBDSQR without vectors and DGESVD the parent
stops in XERBLA 3 times and this branch never; every other case
returns the same INFO on both.  The full LAPACK test suite passes:
5441901 LAPACK tests, 0 numerical errors, 0 other errors, the same
totals as the parent.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@rmlarsen

rmlarsen commented Sep 10, 2026 •

Copy link
Copy Markdown
Contributor Author

Verified on an Apple M4 (macOS, Homebrew gfortran 16.2, Release build with the CI flags). With this branch merged onto current master, the full test suite passes, the new tests fail without the fix, and the reproducer behaves as described above.

rmlarsen added a commit to rmlarsen/lapack that referenced this pull request Sep 15, 2026
…is a NaN

xLASQ1 scales the bidiagonal matrix by its largest entry SIGMX before
running dqds.  When D or E contains a NaN that survives the MAX
reductions computing SIGMX (with gfortran, a NaN in D(N)), SIGMX is a
NaN and xLASCL rejects it by stopping the process in XERBLA.  xBDSQR
without singular vectors and xGESVD with JOBU = JOBVT = 'N' take this
path; a NaN elsewhere in D returns INFO = 0 with NaN output.

Test SIGMX with xISNAN and return INFO = 1, the mechanism Reference-LAPACK#1382 uses
for xLALSD and xBDSDC.  xBDSQR treats every nonzero INFO from xLASQ1
as a request to finish with the QR algorithm, so it then reports the
non-convergence on the NaN data through its own INFO like it does for
other non-finite input.  Finite input never takes the new branch.

xERRBD gets the case as a regression test: a 4 by 4 bidiagonal with a
NaN in D(N), passed to xBDSQR without singular vectors, which must
return without calling XERBLA.  The error-exit tests are where it
belongs, since the routine returns no meaningful output for such a
matrix and the point of the test is that it returns at all.  All four
xERRBD files carry it, because the complex xBDSQR takes the same path
through the real xLASQ1.

Over 120 NaN/Inf cases of DBDSQR without vectors and DGESVD the parent
stops in XERBLA 3 times and this branch never; every other case
returns the same INFO on both.  The full LAPACK test suite passes:
5441901 LAPACK tests, 0 numerical errors, 0 other errors, the same
totals as the parent.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@rmlarsen
rmlarsen force-pushed the dc-nan-info-not-xerbla branch from 24e080d to 7fe8be8 Compare September 15, 2026 20:28
@mohawk2

mohawk2 commented Oct 25, 2026

Copy link
Copy Markdown
Contributor

@rmlarsen It's failing on one thing on the "Special Build Configurations / openmp-ubuntu-26.04-arm-gfortran (shared)" job:

 -------------------------- BLAS: Extended API (_64) -------------------------- 
[...]
Testing DOUBLE PRECISION 'Level 1 BLAS routines' (dblat1_64.out) - passed: 2529
-->  FAIL
failing to pass the threshold: 10

@rmlarsen
rmlarsen force-pushed the dc-nan-info-not-xerbla branch 2 times, most recently from 46dcd88 to 8fe6889 Compare November 4, 2026 18:49
@rmlarsen

rmlarsen commented Nov 4, 2026

Copy link
Copy Markdown
Contributor Author

@mohawk2 Thanks for flagging it. That failure doesn't come from this PR: dblat1_64 is the Level 1 BLAS test, and this PR changes no BLAS source or BLAS test. It's a rare false positive in the NRM2 checks. They measure only relative error, so a subnormal result that is off by one subnormal ulp gets rejected, and at INCX = 0 one unlucky random draw shows up as exactly ten failures. #1423 has the diagnosis and the fix to the test. The rerun after the Oct 28 push passed every job, including openmp-ubuntu-26.04-arm-gfortran (shared).

I've also just rebased onto master to resolve a conflict with #1458 in CMAKE/LAPACKTestHelpers.cmake: both PRs add a file pattern to the NAG -Onopropagate list, and the merged regex keeps both.

@rmlarsen

rmlarsen commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor Author

@mohawk2 @langou it isn't completely clear to me whether all my recently submitted MRs agree with https://arxiv.org/abs/2207.09281 in their choice of INFO values returned. What is the status of following that set of guidelines in LAPACK currently and is it the plan to eventually follow it strictly for all public APIs?

…when the bidiagonal contains a NaN

xLALSD and xBDSDC scale the bidiagonal by its largest entry before the
divide and conquer phase, guarded only by ORGNRM.EQ.ZERO.  xLANST
propagates a NaN, and xLASCL rejects CFROM = NaN as an illegal argument,
so the call ends in XERBLA and the reference XERBLA stops the program.
A NaN anywhere in A reaches xGELSD's bidiagonal, and so does an
infinity, because the driver's scaling multiplies by zero and leaves
NaN where the infinity was.  xGESDD guards its own input against a NaN
(Reference-LAPACK#469) but an infinity still reaches xBDSDC the same way.  Every other
least-squares driver returns from such input (xGELSS with INFO > 0 from
xBDSQR, the rest with INFO = 0); xGELSD is the only one that kills the
process.

Test the norm with xISNAN at the scaling sites of xLALSD (including the
N = 1 branch) and xBDSDC and return INFO = 1, the value xLASDA already
uses when a singular value fails to converge, in all precisions.
Document the new return in xLALSD, xBDSDC and xGELSD.

xERRLS gets the case as a regression test: a 2 by 2 matrix with a NaN,
passed to xGELSD, which must return INFO = 1 without calling XERBLA.
The error-exit tests are where it belongs, since the driver returns no
solution for such a matrix and the point of the test is that it returns
at all; on the parent it reports the illegal xLASCL argument and returns
INFO = -4 with a NaN solution.

The added test changes nothing for finite input: over the finite cases
of the sweep the solutions are bit-identical to the parent commit.  Over
2296 (precision, m, n, NaN or infinity, position) xGELSD cases and 810
xBDSDC cases with a NaN, this branch returns from every call; xGELSD
returns INFO = 1 in every case, xBDSDC in every divide and conquer case,
and no call reaches XERBLA.  The full LAPACK test suite passes: 5441901
LAPACK and 315872 BLAS tests, 0 numerical errors, 0 other errors, the
same totals as the parent.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@rmlarsen
rmlarsen force-pushed the dc-nan-info-not-xerbla branch from 8fe6889 to 4d9667f Compare November 6, 2026 19:04
@langou

langou commented Nov 6, 2026

Copy link
Copy Markdown
Contributor

My preference would be after the call to CLANGE at

ANRM = CLANGE( 'M', M, N, A, LDA, RWORK )

check whether ANRM is NaN or not. If ANRM is NaN, then return INFO = -4.

A precedent is #1136. See:

lapack/SRC/dgeev.f

Lines 344 to 359 in c9967aa

*
* Scale A if max element outside range [SMLNUM,BIGNUM]
*
ANRM = DLANGE( 'M', N, N, A, LDA, DUM )
SCALEA = .FALSE.
IF( ANRM.GT.ZERO .AND. ANRM.LT.SMLNUM ) THEN
SCALEA = .TRUE.
CSCALE = SMLNUM
ELSE IF( ANRM.GT.BIGNUM ) THEN
SCALEA = .TRUE.
CSCALE = BIGNUM
ELSE IF( DISNAN( ANRM ) ) THEN
INFO = -4
CALL XERBLA( 'DGEEV ', -INFO )
RETURN
END IF

I think we should also bail out for INFs.

The idea was to generalize this for more LAPACK routines with the rationale: "we often compute a norm of matrices in input for scaling, while doing so we might as well check whether the norm is NaN, if the norm of the matrix is NaN, we bail out with INFO = - ( position of the array A in the argument list ) so as to indicate an error in the data.

…ugh a negative INFO

Following review, and the rule of Reference-LAPACK#1136 for xGEEV: when the norm that a
routine computes for scaling is a NaN, or here also an infinity, return
INFO = -(position of the array) instead of a positive INFO.

xGELSD tests ANRM right after xLANGE and returns INFO = -4.  xGESDD's
existing NaN guard on ANRM also catches an infinity; without that, an
infinity in A reaches xBDSDC as a NaN and would come back as INFO = -5,
which in xGESDD's argument list names LDA.  xLALSD and xBDSDC, which
returned INFO = 1 at their scaling sites in the previous revision, now
test D and then E with xLANGE right after the N = 0 quick return and
return -5/-6 and -4/-5.  The test comes before the lower-to-upper
rotation, which mixes E into D, and covers every path, including the
xLASDQ paths of xBDSDC.  As in xGESDD, none of these returns calls
XERBLA: the caller gets control back.

The error-exit tests check every new return and that XERBLA is not
called: xGELSD with a NaN and an infinity and xLALSD with a bad D and E
in xERRLS, xBDSDC with a bad D and E in xERRBD, and xGESDD with a NaN
and an infinity in xERRED, which joins the NAG -Onopropagate list.  On
master every new case fails except xGESDD with a NaN (Reference-LAPACK#469).

A sweep of 22380 cases (NaN, +Inf and -Inf at every position of A, or
of D and E; all precisions, UPLO, JOBZ and COMPQ values, sizes on both
sides of the divide and conquer threshold) returns the expected INFO
everywhere without reaching XERBLA, and the outputs for finite input
are bit-identical to master.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@rmlarsen rmlarsen changed the title Return INFO = 1 from xLALSD and xBDSDC instead of stopping in XERBLA when the bidiagonal contains a NaN Report a NaN or an infinity in the input of xGELSD, xGESDD, xLALSD and xBDSDC through INFO instead of stopping in XERBLA Nov 6, 2026
@rmlarsen

rmlarsen commented Nov 6, 2026

Copy link
Copy Markdown
Contributor Author

@langou Thanks, done in 3d90a78. xGELSD now tests ANRM right after xLANGE and returns INFO = -4 for a NaN or an infinity in A.

One difference from the xGEEV precedent: these returns do not call XERBLA, as in xGESDD (#469). The reference XERBLA stops the program, and the point of this PR is that bad data comes back to the caller as an INFO. I'm happy to switch to the xGEEV behavior if you prefer it.

Following the same rule further down:

  • xGESDD's NaN guard now also catches an infinity. Without that, an infinity in A reaches xBDSDC as a NaN.
  • The guards in xLALSD and xBDSDC, which returned INFO = 1 in the previous revision, now return the negative position of D or E (-5/-6 and -4/-5). The test runs before any work, so that D and E are told apart before the lower-to-upper rotation mixes them, and on every path of xBDSDC.

I've updated the description. B is not tested in xGELSD. Should a NaN or an infinity there also return INFO = -6?

@langou

langou commented Nov 7, 2026

Copy link
Copy Markdown
Contributor

Lots of good questions from @rmlarsen.

(*) I note that the previous LAPACK convention was very consistent in the sense that if INFO < 0, this means an error, like LDA < M, so that we would "abort" by calling XERBLA. This is the first time that we return INFO < 0 and not XERBLA. Fine with me. I like it actually.

(*) There is some work #1411, #1407 to make XERBLA more user-friendly.

(*) The one question I am not sure is how much do we want to do Numerical Linear Algebra with Inf and NaN. Should we try to solve system of linear equation with an Inf? Should we try to solve system of linear equation with an NaN? In some cases, this is meaningful work. The distinction between Inf and NaN is significant. Sometimes Inf makes sense, and NaN does not. I can see more people wanted us to push through Inf through LAPACK than NaNs.

(*) The current LAPACK routines have mostly not been designed with Inf and NaN in mind as input parameters. We might handle some of this internally when we expect them to occur in our computation and then do the right thing, but I think we can say that we have not been designing routines with Inf and NaN as input data in mind. This is true for drivers, but also for our internal routines. We test our subroutines to make sure any Inf or NaN in input will not create an infinite loop but that's about it. Therefore I think that checking for Inf and NaN and bailing out makes sense. In particular when the first thing we do in the subroutine is compute the norm of an array. We would have a negative INFO parameter pointing to the first "invalid" array. We would not call XERBLA. Then, in the future, if we want and handle NaN and/or Inf in some particular subroutines, that's great, we can work on it, but, as of now, I do not think we understand enough the behavior of our subroutines with NaN and Inf in input and so, in my opinion, the sensible thing to do in my opinion is to bail out. If we want to guarantee a certain behavior with NaN and Inf, we should probably then create new routines. Some handle NaN and Inf, some bail out. (This is somewhat the essence of https://arxiv.org/abs/2207.09281.)

@langou

langou commented Nov 7, 2026

Copy link
Copy Markdown
Contributor

One difference from the xGEEV precedent: these returns do not call XERBLA, as in xGESDD (#469). The reference XERBLA stops the program, and the point of this PR is that bad data comes back to the caller as an INFO. I'm happy to switch to the xGEEV behavior if you prefer it.

Well actually, let us switch xGEEV so that xGEEV returns a negative INFO (it already does) but does not call XERBLA and does a quick return. Can you PR this? Maybe by adding to this PR?

@langou

langou commented Nov 7, 2026

Copy link
Copy Markdown
Contributor

The guards in xLALSD and xBDSDC, which returned INFO = 1 in the previous revision, now return the negative position of D or E (-5/-6 and -4/-5). The test runs before any work, so that D and E are told apart before the lower-to-upper rotation mixes them, and on every path of xBDSDC.

This sounds great. I am just worried that people (including us and LAPACK itself!) using xLALSD or xBDSDC do not expect a negative INFO coming back to them. In the sense that they expect a negative INFO to call XERBLA and abort. Maybe my main concern is that we are implementing features, they are good, but the code upstream need to start using these features. All good with me. I think it is a move in the good direction.

@langou

langou commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

B is not tested in xGELSD. Should a NaN or an infinity there also return INFO = -6?

I am pausing. ⏸️

We do compute the norm of B early on:

lapack/SRC/cgelsd.f

Lines 430 to 433 in c9967aa

*
* Scale B if max entry outside range [SMLNUM,BIGNUM].
*
BNRM = CLANGE( 'M', M, NRHS, B, LDB, RWORK )

So there would be no cost in detecting Inf or NaN in B. Great.

The thing is that, as much as, if there is Inf or NaN in A, the SVD solve will likely propagate these everywhere. The NaN and Inf in B seem more innocuous if we think about the sequence UNMBR, LALSD, UNMBR.

I am on the fence. If someone has an opinion, let it known. I am more leaning towards leaving B as is and solve with NaN and Inf in B and see what happens.

@mohawk2

mohawk2 commented Nov 7, 2026

Copy link
Copy Markdown
Contributor

I am on the fence. If someone has an opinion, let it known. I am more leaning towards leaving B as is and solve with NaN and Inf in B and see what happens.

That sounds like a good approach. I'd suggest adding tests for this behaviour with current code and seeing what the results are - they might make sense, in which case the test just permanently captures that, or they might not, in which case the code can be fixed.

@rmlarsen

rmlarsen commented Nov 8, 2026

Copy link
Copy Markdown
Contributor Author

@langou @mohawk2 thanks for the guidance. I'll try to update this MR accordingly.

…finity in A

Following review: xGEEV has returned INFO = -4 for a NaN in A since
Reference-LAPACK#1136, but it called XERBLA first, and the reference XERBLA stops the
program.  Return without calling XERBLA, as xGESDD and xGELSD do, so
that the caller gets control back.

Test an infinity as well.  An infinity in A passed the NaN test, was
scaled by zero in xGEEV's own scaling, which leaves a NaN in its place
and zeros elsewhere, and came back as INFO = 0 with every eigenvalue a
NaN.  Since an infinite ANRM also exceeds BIGNUM, the test now comes
right after xLANGE, before the scaling branches, in the same form as in
xGELSD and xGESDD.

xERRED checks both cases, and that XERBLA is not called.  With the
parent's xGEEV the NaN case calls XERBLA and the infinity case returns
INFO = 0, in all precisions.

Also following review, xERRLS records what the least-squares drivers
do with a NaN in B, which none of them checks: for each of xGELS,
xGELST, xGETSLS, xGELSS, xGELSY and xGELSD, a NaN in one column of B
gives INFO = 0, the solution for the other column, and a NaN in the
solution for its own, without calling XERBLA.  These cases pass on the
parent as well; they pin down the current behavior.  An infinity in B
is not tested: every driver scales B by zero when its norm is infinite
and by infinity when it undoes the scaling, so the whole solution comes
back NaN, and whether to change that is left open.

The full LAPACK test suite passes, with and without the _64 API.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@rmlarsen rmlarsen changed the title Report a NaN or an infinity in the input of xGELSD, xGESDD, xLALSD and xBDSDC through INFO instead of stopping in XERBLA Report a NaN or an infinity in the input of xGELSD, xGESDD, xGEEV, xLALSD and xBDSDC through INFO instead of stopping in XERBLA Nov 8, 2026
@rmlarsen

rmlarsen commented Nov 8, 2026

Copy link
Copy Markdown
Contributor Author

@langou @mohawk2 Thanks. Updated in eee8174:

xGEEV. Done in this PR: for a NaN in A, xGEEV now returns INFO = -4 without calling XERBLA. I also extended the test to an infinity, following the rule this PR uses for xGELSD and xGESDD. Until now an infinity in A passed the NaN test, xGEEV's own scaling multiplied it by zero, and the call came back with INFO = 0 and every eigenvalue a NaN. Because an infinite ANRM also exceeds BIGNUM, the test now sits right after xLANGE, before the scaling branches. If you would rather keep xGEEV to NaN only, that is a one-line change. xERRED checks both cases and that XERBLA is not called; with the previous xGEEV, the NaN case calls XERBLA and the infinity case returns INFO = 0.

Callers of xLALSD and xBDSDC. I went through the callers inside the library: xGELSD (calls xLALSD), xGESDD in all precisions (CGESDD and ZGESDD call SBDSDC and DBDSDC), and the new xBDSVDMR3 (falls back to xBDSDC). As far as I can see, the new negative INFO cannot reach any of them. xGELSD and xGESDD return -4 for a NaN or an infinity in A before they form the bidiagonal, and a finite A, scaled into [SMLNUM, BIGNUM], gives a finite bidiagonal. xBDSVDMR3 rejects a non-finite D or E with INFO = N+1 before its fallback. In LAPACKE, LAPACKE_?bdsdc already returns -5 / -6 for a NaN in D / E from its own NaN check, and those are the new Fortran -4 / -5 after LAPACKE's shift for MATRIX_LAYOUT, so an infinity now gets the code a NaN already got there. LAPACKE has no xLALSD wrapper. You are right, though, that outside callers who treat any negative INFO as a programming error will need to learn the new convention.

B. Following your suggestion, the drivers still solve with whatever B contains. As @mohawk2 suggested, I ran it to see what that gives. All six least-squares drivers (xGELS, xGELST, xGETSLS, xGELSS, xGELSY, xGELSD) behave the same way:

  • A NaN in one column of B: INFO = 0, a NaN solution for that column, and the other columns exact (bit-identical to a run with a finite value in its place). That looks sensible, and xERRLS now pins it down for all six drivers in all precisions (these cases pass on master too).
  • An infinity in one column of B: INFO = 0, but the solution for every column comes back NaN, the finite ones included. Each driver scales B by BIGNUM / BNRM = 0 when BNRM is infinite, which turns the infinity into a NaN and every other entry into zero, and undoing the scaling at the end multiplies by an infinity, which turns those zeros into NaNs. In double precision I checked all six drivers on 6x4, 4x6, 60x40, 40x60 and 30x30 problems, with +Inf and -Inf.

I have not changed or tested the infinity case. Skipping the scaling of B when BNRM is infinite (one condition per driver) makes an infinity behave like a NaN; I checked that for all six double precision drivers on the same problems. I'm happy to send that as a separate PR if you think it is worth doing. For what it's worth, LAPACKE_?gelsd already rejects a NaN in B with -7 through its default NaN check.

The full test suite passes with and without the _64 API: 5739011 LAPACK and 319677 BLAS tests, 0 numerical errors and 0 other errors. I've updated the description.

@rmlarsen

rmlarsen commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor Author

@mohawk2 @langou one more question: should we also change xBDSVDMR3 to return -5 for a NaN or Inf in D and -6 for one in E, without calling XERBLA? It could be a followup PR.

@mohawk2

mohawk2 commented Nov 8, 2026

Copy link
Copy Markdown
Contributor

@mohawk2 @langou one more question: should we also change xBDSVDMR3 to return -5 for a NaN or Inf in D and -6 for one in E, without calling XERBLA? It could be a followup PR.

I'm getting outside of my area of knowledge here. But it's quite surprising for me, to have a parameter error (negative INFO) and not call XERBLA. How common is that across the library?

rmlarsen added a commit to rmlarsen/lapack that referenced this pull request Nov 9, 2026
…tive INFO

xBDSVDMR3 rejects a NaN or an infinity in D or E before any work and
returns INFO = N+1 without calling XERBLA.  xGESVDMR3 passes that on
as INFO = MIN(M,N)+1 when A contains one, since a NaN reaches the
bidiagonal unscaled and an infinity is scaled by zero into a NaN; when
singular vectors are requested, the xORMBR calls that follow overwrite
it on three of the four paths (Reference-LAPACK#1482), and the call returns INFO = 0
with NS = 0.  Follow instead the rule that Reference-LAPACK#1382 applies to xGEEV,
xGESDD, xGELSD and xBDSDC, extending the NaN check that Reference-LAPACK#1136 added to
xGEEV: when the input contains a NaN or an infinity, return
INFO = -(position of the array), without calling XERBLA, so that the
caller gets control back.

xBDSVDMR3 now tests D and then E, in separate loops so that the two
can be told apart, and returns -5 or -6.  xGESVDMR3 tests ANRM right
after xLANGE, before the scaling, and returns -6, with the test that
Reference-LAPACK#1382 uses.  A finite A, scaled into [SMLNUM, BIGNUM], gives a finite
bidiagonal, so xBDSVDMR3's test no longer fires from xGESVDMR3.  For a
complex A, xLANGE takes the absolute value of each entry, so an entry
whose real and imaginary parts are finite but whose absolute value
overflows also returns -6: the largest singular value of A would
overflow too, and the parent returned INFO = 0 with NaN singular
values.  INFO = N+2 and the fallback's INFO stay positive: they report
a failed computation, not bad input.  The INFO descriptions of both
routines document the change, and the one of xGESVDMR3 drops a line,
inherited from xGESVDX, about an INFO = N*2+1 that xGESVDMR3 never
returns.

LAPACKE_?bdsvdmr3 already returns -6 / -7 for a NaN in D / E from its
own NaN check, which is the new Fortran -5 / -6 after LAPACKE's shift
for MATRIX_LAYOUT.  The NaN check of LAPACKE_?gesvdmr3 returned -6 for
A, which is its seventh argument, so a NaN would have come back as -6
and an infinity as -7; it now returns -7.  LAPACKE_?gesvdx, from which
it was copied, has the same -6, but it is released and is left alone.
Neither routine is in a release yet, so no released interface changes.

xERRBD checks xBDSVDMR3 with an infinity in D (-5) and a NaN in E
(-6), and xERRED checks xGESVDMR3 with a NaN and with an infinity in A
(-6), each also failing if XERBLA is called.  On the parent, the
xBDSVDMR3 cases return 3 (N+1) and the xGESVDMR3 cases 2 (MIN(M,N)+1).
Both routines check their workspace before the input, so the work
arrays of xERRBD and xERRED grow to the minimum sizes for these small
problems, and xERRED joins the NAG -Onopropagate list that keeps the
compiler from folding SQRT( -ONE ) and ONE / ZERO.

The full test suite passes, with and without the _64 API.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
CGEEV, ZGEEV, CGESDD, ZGESDD, CGELSD and ZGELSD test the xLANGE 'M'
norm of A for a NaN or a value above the overflow threshold.  For a
complex A, xLANGE takes the absolute value of each entry, so an entry
whose real and imaginary parts are finite but whose absolute value
overflows also returns INFO = -4.  Master returns INFO = 0 for such a
matrix, with NaN eigenvalues or singular values, because the scaling
by BIGNUM / ANRM is a scaling by zero.  The INFO descriptions now say
so, as Reference-LAPACK#1483 does for CGESVDMR3 and ZGESVDMR3.

Comments only.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@rmlarsen

rmlarsen commented Nov 9, 2026

Copy link
Copy Markdown
Contributor Author

Pushed 451264e, a comments-only follow-up. For a complex A, xLANGE( 'M', ... ) takes the absolute value of each entry, so the new guard also returns INFO = -4 for an entry whose real and imaginary parts are finite but whose absolute value overflows. On master, ZGEEV, ZGESDD and ZGELSD return INFO = 0 for such a matrix, with NaN eigenvalues or singular values, because the scaling by BIGNUM / ANRM is then a scaling by zero. The INFO descriptions of CGEEV, ZGEEV, CGESDD, ZGESDD, CGELSD and ZGELSD now say so, matching what #1483 documents for CGESVDMR3 and ZGESVDMR3. No code changes.

@langou

langou commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

@mohawk2 wrote:

I'm getting outside of my area of knowledge here. But it's quite surprising for me, to have a parameter error (negative INFO) and not call XERBLA. How common is that across the library?

Hi @mohawk2,

See first bullet at: #1382 (comment). I had written

I note that the previous LAPACK convention was very consistent in the sense that if INFO < 0, this means an error, like LDA < M, so that we would "abort" by calling XERBLA. This is the first time that we return INFO < 0 and not XERBLA. Fine with me. I like it actually.

So I agree that we need to be careful about this.

To answer your question, I think that, as of now, LAPACK always calls XERBLA after a negative INFO but for the one exception of GESDD.

GESDD's behavior was changed with #471. This pull request fell through the scrutiny cracks at the time. Maybe for the better. Serendipitous.

Returning negative INFO without a XERBLA call would be a change. I am glad you emphasize this. You are correct to emphasize this. This is not trivial.

That being said, I think this change is for the better.

@rmlarsen already gave good arguments for the change. Let me repeat what @rmlarsen wrote:

As in xGESDD, none of these returns calls XERBLA: the input is bad data, not a programming error, and the caller gets control back. Nothing changes for finite input.

These returns do not call XERBLA, as in xGESDD (#469). The reference XERBLA stops the program, and the point of this PR is that bad data comes back to the caller as an INFO.

I agree with all this.

Another compelling argument is that this would follow https://arxiv.org/pdf/2207.09281. (Disclaimer: Arguably I am biased as a co-author.) Here are some quotes.

Section 2.4.3 page 11,

Since these drivers already compute the matrix norm, it is easy to detect whether the input contains an Inf or NaN and return with INFO indicating this. If more than one input contains Infs or NaNs, only the first one would be reported, analogous to the previous usage of INFO.

Section 2.5.2 page 15,

WHAT = 1 : Also check input and output arguments for Infs and NaNs. If HOW = 0, we might stop checking early after discovering the first Inf or NaN, or continue checking all inputs and outputs if HOW> 0 (to report all problematic inputs and outputs using INFO ARRAY, as described below); this comment applies to higher values of WHAT too. If the checks performed for WHAT = 0 detect no errors, INFO will point to the first input argument containing an Inf or NaN, i.e. INFO = -k points to argument k. In general computation will continue if an input contains an Inf or NaN, unless the problem is mathematically ill-defined (eg computing eigenvalues and/or eigenvectors). If no input contains an Inf or NaN but an output does, INFO will return a unique positive value (details later) to identify the first such output. (Input-only arguments are only checked on input, and output-only arguments are only checked on output.) When WHAT = 1 we pass WHAT = 0 in internal LAPACK calls.

Section 2.5.2 page 16,

Here is how we use INFO to report Infs and NaNs in inputs and outputs. We note that argument INFO ARRAY can be used to report errors in more detail as described later, since using one scalar argument INFO to report errors limits the amount of information that can be returned. The values of INFO we use for reporting will necessarily be unique for each LAPACK routine, for the following reasons. If the k-th argument is the first one to contain an Inf or NaN on input, and INFO would otherwise be 0 based on standard error checking, then INFO is set to -k. For output, each LAPACK routine with an INFO argument currently defines what a positive return value of INFO means, e.g. INFO = k means pivot k is zero in SGESV. Therefore we will use the first positive unused values of INFO to point to the first output argument containing an Inf or NaN. For example, for SGESV, INFO = N+1 will mean argument A contains an Inf or NaN on output (and no input arguments contain an Inf or NaN, which would have been reported using INFO = -3 for A and INFO = -6 for B instead), and INFO = N+2 will mean argument B contains an Inf or NaN on output (and none of A on input, B on input or A on output contains an Inf or NaN). We choose not to distinguish Infs from NaNs in this reporting for 3 reasons. First, Infs and NaNs can be equally problematic, and we leave it to the user to decide how to react. Second, previous examples of inconsistencies in underlying operations (like complex division) mean that some operations could generate Infs, NaNs, or both, so we again leave it to the user to decide how to react. Third, this lets us continue to use INFO as before, with INFO=-k indicating that the k-th input argument is problematic, i.e., contains either an Inf or NaN.

Section 2.5.2 page 17,

We point out a variation on the proposed use of INFO above, and its pros and cons. Instead of using previously unused positive values of INFO to report Infs and NaNs in inputs, outputs, and in internal subroutine calls, we could use negative values. While the positive values may need to depend on the problem size (eg N+1 through N+4 for the SGESV example above), the negative values could always be assigned fixed values (say -100, -101 etc). The advantage of this is that INFO is easier to interpret: one does not need N in addition to INFO to interpret INFO. A disadvantage is that it violates the LAPACK convention of INFO < 0 meaning that an input is “incorrect,” and INFO > 0 meaning that an error occurred during execution. It is likely that many users may simply test for INFO < 0 in their code (if they test at all) to detect input errors, and so this variation would invalidate that assumption. Also, to completely interpret INFO requires knowing WHAT and HOW, eg to know whether inputs and outputs were checked for Infs and NaNs. This is why we include WHAT and HOW in INFO ARRAY below, which is designed to provide more complete and interpretable information in one data structure.

Julien.

@langou
langou merged commit c1e01a6 into Reference-LAPACK:master Nov 12, 2026
49 checks passed
rmlarsen added a commit to rmlarsen/lapack that referenced this pull request Nov 14, 2026
…tive INFO

xBDSVDMR3 rejects a NaN or an infinity in D or E before any work and
returns INFO = N+1 without calling XERBLA.  xGESVDMR3 passes that on
as INFO = MIN(M,N)+1 when A contains one, since a NaN reaches the
bidiagonal unscaled and an infinity is scaled by zero into a NaN; when
singular vectors are requested, the xORMBR calls that follow overwrite
it on three of the four paths (Reference-LAPACK#1482), and the call returns INFO = 0
with NS = 0.  Follow instead the rule that Reference-LAPACK#1382 applies to xGEEV,
xGESDD, xGELSD and xBDSDC, extending the NaN check that Reference-LAPACK#1136 added to
xGEEV: when the input contains a NaN or an infinity, return
INFO = -(position of the array), without calling XERBLA, so that the
caller gets control back.

xBDSVDMR3 now tests D and then E, in separate loops so that the two
can be told apart, and returns -5 or -6.  xGESVDMR3 tests ANRM right
after xLANGE, before the scaling, and returns -6, with the test that
bidiagonal, so xBDSVDMR3's test no longer fires from xGESVDMR3.  For a
complex A, xLANGE takes the absolute value of each entry, so an entry
whose real and imaginary parts are finite but whose absolute value
overflows also returns -6: the largest singular value of A would
overflow too, and the parent returned INFO = 0 with NaN singular
values.  INFO = N+2 and the fallback's INFO stay positive: they report
a failed computation, not bad input.  The INFO descriptions of both
routines document the change, and the one of xGESVDMR3 drops a line,
inherited from xGESVDX, about an INFO = N*2+1 that xGESVDMR3 never
returns.

LAPACKE_?bdsvdmr3 already returns -6 / -7 for a NaN in D / E from its
own NaN check, which is the new Fortran -5 / -6 after LAPACKE's shift
for MATRIX_LAYOUT.  The NaN check of LAPACKE_?gesvdmr3 returned -6 for
A, which is its seventh argument, so a NaN would have come back as -6
and an infinity as -7; it now returns -7.  LAPACKE_?gesvdx, from which
it was copied, has the same -6, but it is released and is left alone.
Neither routine is in a release yet, so no released interface changes.

xERRBD checks xBDSVDMR3 with an infinity in D (-5) and a NaN in E
(-6), and xERRED checks xGESVDMR3 with a NaN and with an infinity in A
(-6), each also failing if XERBLA is called.  On the parent, the
xBDSVDMR3 cases return 3 (N+1) and the xGESVDMR3 cases 2 (MIN(M,N)+1).
Both routines check their workspace before the input, so the work
arrays of xERRBD and xERRED grow to the minimum sizes for these small
problems, and xERRED joins the NAG -Onopropagate list that keeps the
compiler from folding SQRT( -ONE ) and ONE / ZERO.

The full test suite passes, with and without the _64 API.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

3 participants