Repository navigation
Closed
Labels
Description
Description
Beginning with Lapack version 3.12.1 I obtain wrong eigenvalues and vectors. So far I've only tested on MacOS Tahoe using an M4 processor and compiling Lapack(e) with Clang 21.1.3 and GFortran 15.2.0, so other systems might not be affected.
- A small C++ test program (see below for the code) yields the following false results using Lapack 3.12.1 (testing matrix * eigenvector gives a different result than eigenvalue * eigenvector, which is obviously wrong):
M = [3,3](((1,0),(3,1.5),(5,2)),((3,-1.5),(2,0),(2.2,-1.7)),((5,-2),(2.2,1.7),(4,0)))
eval0: (7.93151,4.96788), evec0: [3]((0.545641,-0.186362),(0.429787,0.0385125),(0.693791,0)).
eval0 * evec0: [3]((5.25358,1.23254),(3.21754,2.44059),(5.50281,3.44666))
M * evec0: [3]((5.24619,1.96144),(3.74329,-2.47997),(6.0107,-1.20772))
eval1: (-2.49846,2.29741), evec1: [3]((0.753772,0),(-0.155329,0.396544),(-0.218818,0.450079)).
eval1 * evec1: [3]((-1.88327,1.73173),(-0.522943,-1.34761),(-0.487309,-1.62722))
M * evec1: [3]((-2.30128,2.7694),(2.23439,1.0246),(1.87774,0.901111))
eval2: (0.396735,0.0246189), evec2: [3]((-0.164256,-0.406065),(0.760621,0),(0.457211,-0.143336)).
eval2 * evec2: [3]((-0.0551693,-0.165144),(0.301765,0.0187256),(0.184921,-0.0456103))
M * evec2: [3]((4.69033,0.932609),(1.18157,-2.06441),(1.8688,-0.982102))
- Under Lapack 3.12.0 the program gives the following correct output (matrix * eigenvector = eigenvalue * eigenvector):
M = [3,3](((1,0),(3,1.5),(5,2)),((3,-1.5),(2,0),(2.2,-1.7)),((5,-2),(2.2,1.7),(4,0)))
eval0: (10.1646,3.74461e-16), evec0: [3]((0.542088,0.157607),(0.413968,-0.185275),(0.68964,0)).
eval0 * evec0: [3]((5.51011,1.60201),(4.20782,-1.88325),(7.00991,2.58243e-16))
M * evec0: [3]((5.51011,1.60201),(4.20782,-1.88325),(7.00991,-4.44089e-16))
eval1: (-3.65999,7.47669e-17), evec1: [3]((0.787496,0),(-0.339972,-0.031168),(-0.423307,0.290015)).
eval1 * evec1: [3]((-2.88223,5.88787e-17),(1.2443,0.114075),(1.5493,-1.06145))
M * evec1: [3]((-2.88223,-2.22045e-16),(1.2443,0.114075),(1.5493,-1.06145))
eval2: (0.495399,-5.13887e-18), evec2: [3]((0.0880892,-0.231064),(0.823256,0),(-0.510609,-0.0194141)).
eval2 * evec2: [3]((0.0436393,-0.114469),(0.40784,-4.23061e-18),(-0.252955,-0.00961772))
M * evec2: [3]((0.0436393,-0.114469),(0.40784,-4.44089e-16),(-0.252955,-0.00961772))
As far as I could reproduce the error, only complex calculations seem to be affected, not real ones. I've tried compiling different Lapack versions from its git history, and the error seems to have been introduced with pull request #1020.
Here's the source code for my minimal C++ test program (requires Boost):
#include <complex>
#include <iostream>
#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/io.hpp>
namespace ublas = boost::numeric::ublas;
using t_cplx = std::complex<double>;
extern "C"
{
#define lapack_complex_double t_cplx
#define lapack_complex_double_real(c) (c.real())
#define lapack_complex_double_imag(c) (c.imag())
#include <lapacke.h>
}
bool eigenvec_cplx(const ublas::matrix<t_cplx>& mat,
std::vector<ublas::vector<t_cplx>>& evecs,
std::vector<t_cplx>& evals)
{
const std::size_t order = mat.size1();
evecs.resize(order);
evals.resize(order);
for(std::size_t i = 0; i < order; ++i)
evecs[i].resize(order);
// allocate buffer for matrix and eigenvectors
std::unique_ptr<t_cplx, std::default_delete<t_cplx[]>>
buf(new t_cplx[2*order*order]);
t_cplx *buf_matrix = buf.get();
t_cplx *buf_evecs = buf.get() + order*order;
// copy matrix to buffer
for(std::size_t i = 0; i < order; ++i)
for(std::size_t j = 0; j < order; ++j)
buf_matrix[i*order + j] = mat(i, j);
// calculate eigenvectors and -values
int iInfo = LAPACKE_zgeev(LAPACK_ROW_MAJOR, 'N', 'V', order,
buf_matrix, order, evals.data(), nullptr, order, buf_evecs, order);
if(iInfo != 0)
{
std::cerr << "Lapack error: " << iInfo << "." << std::endl;
return false;
}
// copy buffer to eigenvectors
for(std::size_t i = 0; i < order; ++i)
for(std::size_t j = 0; j < order; ++j)
evecs[i][j] = buf_evecs[j*order + i];
return true;
}
int main()
{
// example matrix
ublas::matrix<t_cplx> M(3, 3);
M(0, 0) = t_cplx(1., 0.);
M(0, 1) = t_cplx(3., 1.5);
M(0, 2) = t_cplx(5., 2.);
M(1, 0) = t_cplx(3., -1.5);
M(1, 1) = t_cplx(2., 0.);
M(1, 2) = t_cplx(2.2, -1.7);
M(2, 0) = t_cplx(5., -2.);
M(2, 1) = t_cplx(2.2, 1.7);
M(2, 2) = t_cplx(4., 0.);
std::cout << "M = " << M << "\n" << std::endl;
std::vector<ublas::vector<t_cplx>> evecs;
std::vector<t_cplx> evals;
if(!eigenvec_cplx(M, evecs, evals))
return -1;
for(std::size_t i = 0; i < evals.size(); ++i)
{
std::cout << "eval" << i << ": " << evals[i]
<< ", evec" << i << ": " << evecs[i] << ".\n"
<< "eval" << i << " * evec" << i << ": " << evals[i]*evecs[i]
<< "\nM * evec" << i << ": " << ublas::prod(M, evecs[i])
<< "\n" << std::endl;
}
return 0;
}
Checklist
- I've included a minimal example to reproduce the issue
- I'd be willing to make a PR to solve this issue
Activity
jprhyne commentedon Oct 17, 2025
Thanks for the report,
I am trying to reproduce this, however the provided test file is giving expected results (correct eigenvalues with representation error on the order of 1e-16) I'm trying to find an M4 to do testing on. I cannot recreate the same results when running in an ARM64 VM from my laptop or on the baremetal.
tweber-ill commentedon Oct 17, 2025
Thanks. If it helps I compile Lapack using:
And the test program (test.cpp) with:
martin-frbg commentedon Oct 18, 2025
weirdly seems to depend on which fortran was used to build LAPACK - works for me with 15.1, breaks after updating to 15.2 (switching between clang 21 and apple clang 17 on the other hand appears to have no effect)
langou commentedon Oct 18, 2025
Thanks @martin-frbg.
So to repeat:
Can you confirm?
Oh my. That sounds fantastic.
martin-frbg commentedon Oct 18, 2025
let me recheck that my original 15.1 build was also using CMAKE_BUILD_TYPE=Release ... maybe it is another of those
-O3optimization effectstweber-ill commentedon Oct 18, 2025
That's a good point. When I compile Lapack 3.12.1 using CMAKE_BUILD_TYPE=Debug, it works!
martin-frbg commentedon Oct 18, 2025
Turns out it was a Release build according to the CMakeCache.txt. I'll try with
CMAKE_Fortran_FLAGS_RELEASE=-O2nextmartin-frbg commentedon Oct 18, 2025
Building with
-O2does not help, with-O1it works. Have not tried to isolate the changed files from the PR in the CMakeLists.txt to apply the-O1only to them specifically. (AFAIK gfortran does not support any pragma for doing this directly in the source file ?)martin-frbg commentedon Oct 19, 2025
This appears to suffice as a workaround (you may want to add slarf1f.f and clarf1f.f to the list for good measure) :
langou commentedon Oct 19, 2025
Thanks @martin-frbg. That's super useful. I re-read the code in xLARF1 and I do not understand what could be compiled away when we go from
-O1to-O2. The code is a tat complicated with theLASTVlogic and scanning the Householder vector for zeros.I am not sure what the next steps are.
-O1by default?-O2.-O1for these two routines (or four)?In any case, thanks for @martin-frbg, that really narrows it down. And thanks to @tweber-ill for the bug report.
martin-frbg commentedon Oct 19, 2025
I don't see anything obviously wrong with the code either, perhaps compiling to the intermediate assembler representation will reveal the difference between the two optimization levels. Reporting to the gcc folks does seem to be a good choice - at least with Fortran source there is little risk of being told off for forgetting to declare a cpu register as used :)
My patch was primarily meant as a quick workaround for @tweber-ill . I guess even if the gfortran team agrees that it is a genuine bug in 15.2, we'll be stuck with the issue for that one version, but perhaps they can suggest a code modification that provides sufficient hints to the compiler without affecting performance or introducing weird special cases into the build system
ACSimon33 commentedon Oct 23, 2025
I think I found the issue and an easy workaround. The issue vanishes for me if I put parentheses around the
DCONJGcall in zlarf1f.f, like this:Looking at the assembly, it seems that gcc 15.2.0 does some instruction contractions and uses the
fcmlainstruction:With fix:
Without fix:
Unfortunately, I can't inspect the values because when I use debug mode or print them, the issue always goes away. I guess I could debug the assembly and look at what's in the registers.
If someone can verify that the fix works on another machine, I can create a PR.
langou commentedon Oct 23, 2025
Oh my. Thanks so much @ACSimon33. Yes, it would be great to check the fix.
ACSimon33 commentedon Oct 23, 2025
I did some more digging, and I was able to print the values. I was trying to print inside the loop before, but that breaks the auto-vektorization that the compiler does here with
fcmla. Here are the values:With the fix:
Without the fix:
As you can see, the inputs to both are the same, but the outputs are completely wrong. So this seems, in fact, like a compiler bug.
2 remaining items
ACSimon33 commentedon Oct 23, 2025
Yes, I can do that.
langou commentedon Oct 23, 2025
@ACSimon33 Your post makes me think on whether we would want something like AXPYC in the BLAS. I think this can be accomplished by GEMV or GEMM but a Level-1 BLAS function for this could be nice.
ACSimon33 commentedon Oct 24, 2025
The bug was confirmed in the linked issue above. Official bug tracker here: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=122408
ilayn commentedon Oct 24, 2025
Fantastic surgery @ACSimon33 thank you for the follow up.
ACSimon33 commentedon Oct 24, 2025
Regarding the fix. I just did a quick search in all sources, and this pattern where we basically have an AXPYC comes up a lot, especially in BLAS. I checked some of the files manually, and the auto-vektorization is wrong there as well. If we look at the results from the test suite we can see this as well:
Putting parentheses around all of the
CONJGseems a bit excessive. So maybe it's better to disable vectorization for this compiler version and warn the user.ACSimon33 commentedon Oct 24, 2025
Compiling with
-fno-tree-loop-vectorizeworks, so we could check if we are on macOS aarch64 with GCC and add the flag and warn the user.langou commentedon Oct 25, 2025
( note to self ) Another way to mitigate this issue would be to add AXPYC to BLAS and using AXPYC within LAPACK. That way, one would only have the vectorization bug in only one file (AXPYC) and this can be removed with a "good" BLAS library. This sounds more manageable than having the bug in many LAPACK files. [ this would not fix the issues in the function that are in BLAS itself and that are not correctly vectorized ]
langou commentedon Nov 13, 2025
Hi all, looking at the bugzilla track ( https://gcc.gnu.org/bugzilla/show_bug.cgi?id=122408 ), this looks fixed on the gcc side so I am closing this issue. Feel welcome to reopen if anything else to be added. Julien.
martin-frbg commentedon Nov 13, 2025
Guess it depends on whether it is seen as sufficient that this compiler fix will reach the majority of ARM64 users sometime around April next year
langou commentedon Nov 13, 2025
If we want to add the flag '-fno-tree-loop-vectorize' when on ARM and when using this version of the compiler, then I do not have a problem with this type of workaround. That would indeed be helpful to significant amount of users. We can also have a warning somewhere, etc. I will not work on this so if someone wants to take the project that would be great.
ACSimon33 commentedon Nov 13, 2025
Adding the flag for this compiler version on arm64 makes sense, I think. I can open a PR with the changes tomorrow. I'm not sure though which compiler versions we should check ... since here the issue manifested only with 15.2.0 but apparently the issue existed since 14.3.0 (see bugzilla). So do we apply the flag to all compiler versions 14.3.0 - 15.2.0 ?
martin-frbg commentedon Nov 13, 2025
Technically we'd need this for 14. (minor version less than 5) and 15.(minor version less than 3) if the fix is scheduled for both 15.3 and a 14.5 , but I guess "hitting" 14.5 with it undeserved would not be that much of a problem