Skip to content

xBDSQR loops forever when xGESDD is given a matrix containing Inf (THRESH becomes NaN) #1409

Open

Description

@ikrommyd

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 returns

Reproducer: 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)

  1. DGESDD only rejects a NaN ANRM (dgesdd.f:600). For ANRM = Inf it calls DLASCL(ANRM -> BIGNUM). That multiplies by BIGNUM/Inf = 0 (dlascl.f:250-256), which turns the Inf into NaN and every finite entry into 0. DGEBRD then produces d = [NaN, 0, 0], e = [0, 0], which goes through DBDSDC -> DLASDQ -> DBDSQR.
  2. In DBDSQR, SMINOA = NaN, so THRESH = MAX(TOL*SMINOA, MAXITR*(N*(N*UNFL))) (dbdsqr.f:416). What MAX returns for a NaN argument depends on the compiler. gfortran on x86-64 emits maxsd, which returns the NaN here. On arm64, gfortran's fmaxnm returns the finite value, and the routine terminates.
  3. With THRESH = NaN, the split test ABSE.LE.THRESH (dbdsqr.f:465) is never true, even though E is exactly 0. IDIR = 2 is chosen, because NaN >= 0 is false. The backward relative convergence test (dbdsqr.f:580-582) sees ABS(E(2)) = 0 <= TOL*ABS(D(3)) = 0, sets E(2) = ZERO (which it already is) and does GO TO 60. None of the loop's state has changed.
  4. ITER is only incremented at dbdsqr.f:623, after the convergence tests. So the ITERDIVN/MAXITDIVN check (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 infinite ANRM like NaN and return INFO = -4, instead of scaling the Inf into a NaN.
  • In xBDSQR, make THRESH NaN-safe, e.g. IF( DISNAN( THRESH ) ) THRESH = MAXITR*(N*(N*UNFL)). With that change the reproducer returns INFO = 0 and NaN singular values.
  • Or make xBDSQR return an error when D/E contain NaN, or count the GO TO 60 jumps 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

  1. mgates3 commented on Sep 11, 2026

    @mgates3
    Collaborator

    I think past internal discussions have endorsed the 1st option:

    • In xGESDD, treat an infinite ANRM like NaN and return INFO = -4, instead of scaling the Inf into a NaN.

    FWIW, for Anorm = inf or nan, SLATE assigns Anorm to all entries of Sigma (s) and (ideally) returns an error.

  2. ikrommyd commented on Sep 16, 2026

    @ikrommyd
    Author

    Is that an official suggestion? i.e should I proceed to make a PR?

  3. mgates3 commented on Sep 17, 2026

    @mgates3
    Collaborator

    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:

    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.

    There it's discussing eigen solvers, but it's equally applicable to the SVD.

  4. ikrommyd commented on Sep 17, 2026

    @ikrommyd
    Author

    Sounds good. I'll try to do it as soon as a find I small time slot. Don't wanna vibe it out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions