Repository navigation
Description
Description
This was reported downstream in numpy/numpy#32591, where it also hangs with OpenBLAS and Apple Accelerate. I've reproduced this personally on arm64 macOS with Accelerate and on x86 Linux with reference LAPACK and OpenBLAS too. The analysis of why the hang happens has been produced by an AI agent.
DGESDD with JOBZ='A' or 'S' never returns for a 3x3 matrix that contains a single Inf. The infinite loop is in DBDSQR. I reproduced it with reference LAPACK 3.12.1 (gfortran 15, x86-64 Linux), and the code involved is unchanged on master.
Reproducer: DGESDD
#include <math.h>
#include <stdio.h>
void dgesdd_(char *jobz, int *m, int *n, double *a, int *lda, double *s,
double *u, int *ldu, double *vt, int *ldvt, double *work,
int *lwork, int *iwork, int *info);
int main(int argc, char **argv)
{
char jobz = argc > 1 ? argv[1][0] : 'A';
int n = 3, lwork = 1000, iwork[8 * 3], info;
double a[9] = {INFINITY, 1, 1,
1, 1, 1,
1, 1, 1};
double s[3], u[9], vt[9], work[1000];
dgesdd_(&jobz, &n, &n, a, &n, s, u, &n, vt, &n, work, &lwork, iwork, &info);
printf("jobz=%c info=%d s=[%g %g %g]\n", jobz, info, s[0], s[1], s[2]);
return 0;
}gcc repro_gesdd.c -o repro_gesdd liblapack.a -lblas -lgfortran -lm
./repro_gesdd N # jobz=N info=0 s=[nan -nan -nan]
./repro_gesdd A # never returnsReproducer: DBDSQR directly
#include <math.h>
#include <stdio.h>
void dbdsqr_(char*,int*,int*,int*,int*,double*,double*,double*,int*,double*,int*,double*,int*,double*,int*);
int main(void) {
int n = 3, ncvt = 3, nru = 3, ncc = 0, ldc = 1, info;
double d[3] = {NAN, 0, 0}, e[2] = {0, 0};
double vt[9] = {1,0,0, 0,1,0, 0,0,1}, u[9] = {1,0,0, 0,1,0, 0,0,1}, c[1], work[12];
dbdsqr_("U", &n, &ncvt, &nru, &ncc, d, e, vt, &n, u, &n, c, &ldc, work, &info);
printf("info=%d d=[%g %g %g]\n", info, d[0], d[1], d[2]);
}This never returns. With NCVT = NRU = 0 it returns info=0, because that case goes through DLASQ1 instead.
🤖 AI text below 🤖
Analysis (line numbers from SRC/, v3.12.1 and master)
DGESDDonly rejects a NaNANRM(dgesdd.f:600). ForANRM = Infit callsDLASCL(ANRM -> BIGNUM). That multiplies byBIGNUM/Inf = 0(dlascl.f:250-256), which turns theInfinto NaN and every finite entry into 0.DGEBRDthen producesd = [NaN, 0, 0],e = [0, 0], which goes throughDBDSDC -> DLASDQ -> DBDSQR.- In
DBDSQR,SMINOA = NaN, soTHRESH = MAX(TOL*SMINOA, MAXITR*(N*(N*UNFL)))(dbdsqr.f:416). WhatMAXreturns for a NaN argument depends on the compiler. gfortran on x86-64 emitsmaxsd, which returns the NaN here. On arm64, gfortran'sfmaxnmreturns the finite value, and the routine terminates. - With
THRESH = NaN, the split testABSE.LE.THRESH(dbdsqr.f:465) is never true, even thoughEis exactly 0.IDIR = 2is chosen, becauseNaN >= 0is false. The backward relative convergence test (dbdsqr.f:580-582) seesABS(E(2)) = 0 <= TOL*ABS(D(3)) = 0, setsE(2) = ZERO(which it already is) and doesGO TO 60. None of the loop's state has changed. ITERis only incremented atdbdsqr.f:623, after the convergence tests. So theITERDIVN/MAXITDIVNcheck (dbdsqr.f:447-451) never fires, and the loop runs forever.
In short: the convergence tests GO TO 60 without counting an iteration, and they assume that a zeroed E will always cause a split on the next pass. That assumption fails when THRESH is NaN.
Related
For N > SMLSIZ, the same input reaches DBDSDC, which calls DLASCL with ORGNRM = NaN (dbdsdc.f:373-376). That gives INFO = -4 in DLASCL, and reference XERBLA then calls STOP. DGELSD also hangs on this kind of input (seen through NumPy's lstsq).
Possible fixes (any one would stop the hang)
- In
xGESDD, treat an infiniteANRMlike NaN and returnINFO = -4, instead of scaling theInfinto a NaN. - In
xBDSQR, makeTHRESHNaN-safe, e.g.IF( DISNAN( THRESH ) ) THRESH = MAXITR*(N*(N*UNFL)). With that change the reproducer returnsINFO = 0and NaN singular values. - Or make
xBDSQRreturn an error whenD/Econtain NaN, or count theGO TO 60jumps from the convergence tests toward the iteration limit.
Checklist
- I've included a minimal example to reproduce the issue
- I'd be willing to make a PR to solve this issue
With guidance from the maintainers on how you would want to deal with this and solve it, I'd be willing to make a PR.
Activity
mgates3 commentedon Sep 11, 2026
I think past internal discussions have endorsed the 1st option:
FWIW, for Anorm = inf or nan, SLATE assigns Anorm to all entries of Sigma (
s) and (ideally) returns an error.ikrommyd commentedon Sep 16, 2026
Is that an official suggestion? i.e should I proceed to make a PR?
mgates3 commentedon Sep 17, 2026
Yes, I think a PR to return an INFO error for the SVD when norm( A ) is inf or nan would be welcome.
In Proposed Consistent Exception Handling for the BLAS and LAPACK, section 2.4.3, Demmel et al. said:
There it's discussing eigen solvers, but it's equally applicable to the SVD.
ikrommyd commentedon Sep 17, 2026
Sounds good. I'll try to do it as soon as a find I small time slot. Don't wanna vibe it out.
feat: catalogue entry 98, svd of a lower Bidiagonal with finite entri…