Repository navigation
Closed
Labels
Description
Dear all,
Based on a user bug report to SciPy scipy/scipy#13220, I have been scratching my head with this very basic example which causes sy/heevr fail (possibly during internal dstebz call) when eigenvectors are not requested. With eigenvector request it goes through.
This is my Cython calling code
cimport cython
cimport numpy as cnp
import numpy as np
from scipy.linalg.cython_lapack cimport dsyevr
cdef double[::1, :] A = np.array([[1.0, 0.0, 0.0, 0.0],
[0.0, 0.8100000000000002753, 0., 0.],
[0.0, 0.1800000000000001876, 0.04000000000000007022, 0.],
[-0.3999999999999999112, 0., 0., 0.1599999999999999201]],
order='F')
cdef int LDA = 4
cdef char* JOBZ = 'N'
cdef char* RANGE = 'I'
cdef char* UPLO = 'L'
cdef int N = 4
cdef double VL = 0.0
cdef double VU = 1.0
cdef int IL = 4
cdef int IU = 4
cdef double ABSTOL = 0.0
cdef int M
cdef double[::1] W = np.empty(4, dtype=float)
cdef double[::1, :] Z = np.empty([4, 4], order='F')
cdef int LDZ = 4
cdef int[::1] ISUPPZ = np.empty(4, dtype=int)
cdef double[::1] WORK = np.empty(132)
cdef int LWORK = 132
cdef int[::1] IWORK = np.empty(40, dtype=int)
cdef int LIWORK = 40
cdef int INFO
dsyevr(JOBZ, RANGE, UPLO, &N, &A[0, 0], &LDA, &VL, &VU, &IL, &IU, &ABSTOL,
&M, &W[0], &Z[0, 0], &LDZ, &ISUPPZ[0], &WORK[0], &LWORK, &IWORK[0], &LIWORK, &INFO)
print("info : ",INFO)
print("eigval array is : ",np.array(W))
print("Eigvec array is : ",np.array(Z))
print("Number of eigs found : ", M)and this is the output I get
info : 2
eigval array is : [1.16000000e+000 2.12162195e+161 1.94819817e+233 5.28562263e+180]
Eigvec array is :
[[6.23042070e-307 1.86918699e-306 1.69121096e-306 1.06810879e-306]
[1.42409073e-306 1.78019082e-306 1.42419938e-306 7.56603881e-307]
[8.45603441e-307 1.42417221e-306 1.60219306e-306 6.23059726e-307]
[1.06811422e-306 1.42417221e-306 1.37961641e-306 1.20955411e-312]]
Number of eigs found : 0
As you can see Z array is not referenced as expected and the correct eigenvalue is written to the W array first element however somehow the return is with M=0 and info set to 2. I don't have the toolchain to perform a full analysis however could you please point me to a direction why this might be happening?
Pinging @martin-frbg since this is both happening on OpenBLAS just for heads-up and also the user was on IntelMKL.
Activity
zerothi commentedon Dec 14, 2020
A is not symmetric?
ilayn commentedon Dec 14, 2020
Only the lower part is referenced through UPLO flag
langou commentedon Dec 14, 2020
Indeed there seems to be a problem here.
ilayn commentedon Dec 14, 2020
What I also noticed is that if I transpose and use the
uplo='U'it doesn't have a problem.oamarques commentedon Dec 15, 2020
As one can see, the script calls DSYEVR with JOBZ='N', RANGE='I' , and UPLO='L'. This indeed leads to a problem in DSTEBZ (bisection), a situation that I have not observed before. Given the input matrix (UPLO='L') we get the tridiagonal (D and E)
which obviously splits, with eigenvalues (close to) 0.0 and 1.16 in the top part, and (close to) 0.0 and 0.85 in the bottom part. DSTEBZ is called with RANGE=I and IL=IU=4 on that tridiagonal and then returns INFO=2. The comments for INFO in DSTEBZ say:
222 *> =2 or 3: RANGE='I' only: Not all of the eigenvalues
223 *> IL:IU were found.
224 *> Effect: M < IU+1-IL
225 *> Cause: non-monotonic arithmetic, causing the
226 *> Sturm sequence to be non-monotonic.
227 *> Cure: recalculate, using RANGE='A', and pick
228 *> out eigenvalues IL:IU. In some cases,
229 *> increasing the PARAMETER "FUDGE" may
230 *> make things work.
IL=IU=4 corresponds to an eigenvalue in the top part of the matrix. It remains to be investigated whether the problem in DSTEBZ is caused by a combination of the splitting and those eigenvalues close to 0.0 or something else. (If we set UPLO='U' then the tridiagonal is defined by the diagonal of the matrix and the eigenvalues are straightforward.)
If we call DSYEVR with JOBZ='V', RANGE='A' , and UPLO='L', then another algorithm is used: DSTEMR (MRRR) is invoked and we get
2.2204E-16
1.1102E-15
8.5000E-01
1.1600E+00
for the eigenvalues, as expected.
Osni
ilayn commentedon Dec 15, 2020
Ah yes, sorry for the terse comment; what I meant is if you transpose the original matrix so that the interested part is on the upper triangle and then use
UPLO='U'then surprisinglydstebzgoes through.By the way this also applies to the
hefamily ofevrdanmrc commentedon Dec 17, 2020
I apologize before hand, but both Julia and R are giving errors when calculating eigenvalues. Is this related? Here is the core dump:
Here is the code that generates the error, in R:
I will be glad to provide more details or open a new issue if it is necessary
martin-frbg commentedon Dec 17, 2020
@danmrc dsymv_thread_L in your backtrace makes it llikely that your libblas.so.3 is OpenBLAS rather than the reference implementation. Can you tell which version it is ? (Does not completely exclude the possibilty that your problem is related, but wrong eigenvalues should not lead to an array overflow)
danmrc commentedon Dec 17, 2020
I was not sure if it was an openblas issue and I was not sure if I opened an issue here or in openblas repo.
The OpenBLAS version is 0.3.13 and the LAPACK version is 3.9.0-3.
I tried uninstalled OpenBLAS and moved to BLAS and the error is gone, so this clears the issue, I guess? Thanks for the help
ilayn commentedon Apr 21, 2021
A similar to the original problem namely dsyevr failure is reported again to SciPy
This time it fails when the largest two eigenvalues are requested instead leads to the failure. N,LDA = 11 and IL, IU= 10,11
Just like the original case, when UPLO switch is changed the problem goes through without error( the array is tiny bit different in the lower part around eps value) but reports empty array. So I'm not sure what to make out of this.
oamarques commentedon Apr 22, 2021
I am unable to reproduce the problem for this matrix of dimension 11, with gfortran and LAPACK 3.9.0. I would have to know the version of LAPACK that is being used and how it was compiled. This is what I get in four different scenarios, where D and E defines the symmetric tridiagonal matrix DSYEVR deals with:
DSYEVR: JOBZ=N,RANGE=I,UPLO=L ==================
Calling DSTEBZ, RANGE=I, VLL=-6.9169-323, VUU= 9.8813-324, IL= 10, IU= 11
D and E:
9.34968975E-01 -1.90619961E-01
3.98659736E-01 -1.89051587E-01
1.27591122E-01 -1.11307662E-01
6.17567169E-01 -7.19276978E-02
4.47898019E-01 -1.88601707E-01
8.48919219E-02 -4.99831090E-13
1.00000000E+00 7.71553582E-16
1.00000000E+00 1.67136486E-15
1.00000000E+00 1.03555279E-15
1.00000000E+00 -2.31898783E-16
1.00000000E+00 0.00000000E+00
DSYEVR normal exit, m = 2, w(1:m) =
1.00000000E+00
1.00000000E+00
DSYEVR: JOBZ=N,RANGE=I,UPLO=U ==================
Calling DSTEBZ, RANGE=I, VLL= 0.0000E+00, VUU= 0.0000E+00, IL= 10, IU= 11
D and E:
1.00000000E+00 -2.56644189E-16
1.00000000E+00 8.12228593E-16
1.00000000E+00 -7.08603844E-16
1.00000000E+00 -7.19286148E-16
1.00000000E+00 5.11871794E-13
1.75439639E-01 -2.34607279E-01
3.49849638E-01 7.82007891E-02
6.04150656E-01 9.53012421E-02
4.50974046E-01 -1.48255688E-01
2.45746444E-01 3.90991548E-01
7.85416520E-01 0.00000000E+00
DSYEVR normal exit, m = 2, w(1:m) =
1.00000000E+00
1.00000000E+00
DSYEVR: JOBZ=V,RANGE=A,UPLO=L ==================
Calling DSTEMR
DSYEVR normal exit, m = 11, w(1:m) =
0.00000000E+00
9.35710688E-03
4.22925331E-01
5.03963181E-01
6.75331324E-01
1.00000000E+00
1.00000000E+00
1.00000000E+00
1.00000000E+00
1.00000000E+00
1.00000000E+00
DSYEVR: JOBZ=V,RANGE=A,UPLO=U ==================
Calling DSTEMR
DSYEVR normal exit, m = 11, w(1:m) =
4.44089210E-16
9.35710688E-03
4.22925331E-01
5.03963181E-01
6.75331324E-01
1.00000000E+00
1.00000000E+00
1.00000000E+00
1.00000000E+00
1.00000000E+00
1.00000000E+00
ilayn commentedon Apr 22, 2021
I'll cook up an example like the original post as soon as I switch to my other computer.
ilayn commentedon Apr 25, 2021
Here is an example that leads to info=2 in DSYEVR call on my machine
I receive
What is fascinating to me is that
max(abs(A - A.T))is 1.11e-16 hence this almost perfectly symmetric however as we discovered in these cases playing around with UPLO clears the error. Hence there is probably a branching that is not so robust somewhere.martin-frbg commentedon Apr 25, 2021
@ilayn again is this with a numpy/scipy that uses reference BLAS or OpenBLAS ? (In the latter case there might be FMA-induced rounding differences to what @oamarques would see with plain gfortran and reference BLAS (though the same problem might pop up with a sufficiently aggressive optimizing compiler)
ilayn commentedon Apr 25, 2021
This is the same machine as the original post (with OpenBLAS 3.11) the users are using MKL and OpenBLAS too. However @oamarques could also reproduce this with his analysis above and I think he is on the reference implementation. I am guessing he will be able to do it again since this much of sensitivity is not expected from these routines as they are generally backed by proper error analysis.
ilayn commentedon Mar 9, 2026
In the C translation work, I had the chance to look at this one more time and it seems like the issue is coming from this line
lapack/SRC/dstebz.f
Line 599 in 4f1773e
if I convert this
.LT.to.LE.then the problem goes away (same with the single precision).Apparently the example hits the pathological case where
GU == WLexactly but because of the strict inequality the eigenvalue count is skipped leading to erroneous result.If my understanding is correct then I can send a PR.
ilayn commentedon May 12, 2026
Gentle ping on the suggestion above
langou commentedon May 14, 2026
Thanks for the double gentle pings to @ilayn and @martin-frbg.
@ilayn wrote:
This sounds great to me. @ilayn: yes, can you send a PR? Can you tag (or "assign to")@oamarques in the PR?
ilayn commentedon May 23, 2026
Surprisingly there is already an open PR #1039 with additional fixes including this one. I will ping folks there. That would close this one upon merge.