Skip to content

Eigenvector issues in 3.12.1 #1160

Closed

Description

@tweber-ill

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

  1. jprhyne commented on Oct 17, 2025

    @jprhyne
    Contributor

    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.

  2. tweber-ill commented on Oct 17, 2025

    @tweber-ill
    Author

    Thanks. If it helps I compile Lapack using:

    cmake -DCMAKE_BUILD_TYPE=Release -DLAPACKE=TRUE -DBUILD_SHARED_LIBS=FALSE -B build_lapacke
    cmake --build build_lapacke --parallel 4 && sudo cmake --install build_lapacke --strip
    

    And the test program (test.cpp) with:

    clang++ -I/usr/local/include -I/opt/homebrew/include -L/usr/local/lib -L/opt/homebrew/Cellar/gcc/15.2.0/lib/gcc/current/ -llapacke -llapack -lblas -lgfortran -o test test.cpp
    
  3. martin-frbg commented on Oct 18, 2025

    @martin-frbg
    Collaborator

    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)

  4. langou commented on Oct 18, 2025

    @langou
    Contributor

    Thanks @martin-frbg.

    So to repeat:

    • M4 + gcc 15.1 + LAPACK 3.12.0 ✅
    • M4 + gcc 15.1 + LAPACK 3.12.1 ✅
    • M4 + gcc 15.2 + LAPACK 3.12.0 ✅
    • M4 + gcc 15.2 + LAPACK 3.12.1 ❌

    Can you confirm?

    Oh my. That sounds fantastic.

  5. martin-frbg commented on Oct 18, 2025

    @martin-frbg
    Collaborator

    let me recheck that my original 15.1 build was also using CMAKE_BUILD_TYPE=Release ... maybe it is another of those -O3 optimization effects

  6. tweber-ill commented on Oct 18, 2025

    @tweber-ill
    Author

    That's a good point. When I compile Lapack 3.12.1 using CMAKE_BUILD_TYPE=Debug, it works!

  7. martin-frbg commented on Oct 18, 2025

    @martin-frbg
    Collaborator

    Turns out it was a Release build according to the CMakeCache.txt. I'll try with CMAKE_Fortran_FLAGS_RELEASE=-O2 next

  8. martin-frbg commented on Oct 18, 2025

    @martin-frbg
    Collaborator

    Building with -O2 does not help, with -O1 it works. Have not tried to isolate the changed files from the PR in the CMakeLists.txt to apply the -O1 only to them specifically. (AFAIK gfortran does not support any pragma for doing this directly in the source file ?)

  9. martin-frbg commented on Oct 19, 2025

    @martin-frbg
    Collaborator

    This appears to suffice as a workaround (you may want to add slarf1f.f and clarf1f.f to the list for good measure) :

    diff --git a/SRC/CMakeLists.txt b/SRC/CMakeLists.txt
    index 676397e76..5feee7dd2 100644
    --- a/SRC/CMakeLists.txt
    +++ b/SRC/CMakeLists.txt
    @@ -519,6 +519,10 @@ if(BUILD_COMPLEX16)
     endif()
     list(REMOVE_DUPLICATES SOURCES)
     
    +set_property(SOURCE dlarf1f.f zlarf1f.f PROPERTY
    +COMPILE_OPTIONS -O1
    +)
    +
     # Use special target for MOD files to guarantee they are built before
     # any other files that depend on them
     add_library(const_mod_file OBJECT ${CONSTMOD})
    
  10. langou commented on Oct 19, 2025

    @langou
    Contributor

    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 -O1 to -O2. The code is a tat complicated with the LASTV logic and scanning the Householder vector for zeros.

    I am not sure what the next steps are.

    • Is this something we report to the GCC community?
    • Should we compile the whole LAPACK with -O1 by default?
    • Maybe we can identify the precise lines in xLARF1 that creates issues in -O2.
    • Should we implement the patch of @martin-frbg and have -O1 for 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.

  11. martin-frbg commented on Oct 19, 2025

    @martin-frbg
    Collaborator

    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

  12. ACSimon33 commented on Oct 23, 2025

    @ACSimon33
    Collaborator

    I think I found the issue and an easy workaround. The issue vanishes for me if I put parentheses around the DCONJG call in zlarf1f.f, like this:

     DO I = 1, LASTC
        C( 1, I ) = C( 1, I ) - TAU * ( DCONJG( WORK( I ) ) )
     END DO

    Looking at the assembly, it seems that gcc 15.2.0 does some instruction contractions and uses the fcmla instruction:

    With fix:

    L20:
    	ldp	d6, d5, [x0], 16
    	ldp	d16, d7, [x19, 16]
    	fmul	d4, d17, d6
    	fnmul	d3, d5, d17
    	fmsub	d4, d5, d18, d4
    	fnmsub	d3, d18, d6, d3
    	fsub	d4, d7, d4
    	fsub	d3, d16, d3
    	stp	d3, d4, [x19, 16]
    	add	x19, x19, x8
    	cmp	x0, x5
    	bne	L20

    Without fix:

    L20:
    	movi	v6.4s, 0
    	ldr	q7, [x2], 16
    	ldr	q16, [x19]
    	fcmla	v6.2d, v18.2d, v7.2d, #0
    	fcmla	v6.2d, v18.2d, v7.2d, #90
    	fsub	v6.2d, v16.2d, v6.2d
    	str	q6, [x19]
    	add	x19, x19, x8
    	cmp	x2, x5
    	bne	L20

    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.

  13. langou commented on Oct 23, 2025

    @langou
    Contributor

    Oh my. Thanks so much @ACSimon33. Yes, it would be great to check the fix.

  14. ACSimon33 commented on Oct 23, 2025

    @ACSimon33
    Collaborator

    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:

     DEBUG: TAU before axpyc    (1.4728662437434605,0.23643312187173018)
     DEBUG: C before axpyc      (-2.1436603049703535,2.5061910918403401)      (-0.41338214631162939,-0.82634248903546315)
     DEBUG: WORK() before axpyc (-4.8164282673245946,-2.4747354207066810)     (-8.05342014898501324E-002,1.4805148384806834)
     DEBUG: C after axpyc       (5.5354037267080765,4.44089209850062616E-016) (-0.64480878470980318,1.3732987925997295)
     
     DEBUG: TAU before axpyc    (1.4728662437434605,-0.23643312187173018)
     DEBUG: C before axpyc      (0.0000000000000000,0.0000000000000000)
     DEBUG: WORK() before axpyc (-0.12286613891709036,0.55559738757650390)
     DEBUG: C after axpyc       (0.31232701335856755,0.78927101247699150)
    

    Without the fix:

     DEBUG: TAU before axpyc    (1.4728662437434605,0.23643312187173018)
     DEBUG: C before axpyc      (-2.1436603049703535,2.5061910918403401)    (-0.41338214631162939,-0.82634248903546315)
     DEBUG: WORK() before axpyc (-4.8164282673245946,-2.4747354207066810)   (-8.05342014898501324E-002,1.4805148384806834)
     DEBUG: C after axpyc       (4.3651848840596159,7.2899085267102839)     (5.52767057690135011E-002,-2.9879018653192713)
     
     DEBUG: TAU before axpyc    (1.4728662437434605,-0.23643312187173018)
     DEBUG: C before axpyc      (0.0000000000000000,0.0000000000000000)
     DEBUG: WORK() before axpyc (-0.12286613891709036,0.55559738757650390)
     DEBUG: C after axpyc       (4.96037636615866417E-002,-0.84737026206997823)
    

    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.

  15. 2 remaining items

  16. ACSimon33 commented on Oct 23, 2025

    @ACSimon33
    Collaborator

    Yes, I can do that.

  17. langou commented on Oct 23, 2025

    @langou
    Contributor

    @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.

  18. ACSimon33 commented on Oct 24, 2025

    @ACSimon33
    Collaborator
  19. ilayn commented on Oct 24, 2025

    @ilayn
    Contributor

    Fantastic surgery @ACSimon33 thank you for the follow up.

  20. ACSimon33 commented on Oct 24, 2025

    @ACSimon33
    Collaborator

    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:

                            -->   LAPACK TESTING SUMMARY  <--
                    Processing LAPACK Testing output found in the TESTING directory
    SUMMARY                 nb test run     numerical error         other error  
    ================        ===========     =================       ================  
    REAL                    1567464         32      (0.002%)        0       (0.000%)
    DOUBLE PRECISION        1563140         4       (0.000%)        4       (0.000%)
    COMPLEX                 1019392         235     (0.023%)        0       (0.000%)
    COMPLEX16               301825          37022   (12.266%)       639     (0.212%)
    
    --> ALL PRECISIONS      4451821         37293   (0.838%)        643     (0.014%)**
    

    Putting parentheses around all of the CONJG seems a bit excessive. So maybe it's better to disable vectorization for this compiler version and warn the user.

  21. ACSimon33 commented on Oct 24, 2025

    @ACSimon33
    Collaborator

    Compiling with -fno-tree-loop-vectorize works, so we could check if we are on macOS aarch64 with GCC and add the flag and warn the user.

  22. langou commented on Oct 25, 2025

    @langou
    Contributor

    ( 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 ]

  23. langou commented on Nov 13, 2025

    @langou
    Contributor

    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.

  24. martin-frbg commented on Nov 13, 2025

    @martin-frbg
    Collaborator

    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

  25. langou commented on Nov 13, 2025

    @langou
    Contributor

    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.

  26. reopened this on Nov 13, 2025
  27. ACSimon33 commented on Nov 13, 2025

    @ACSimon33
    Collaborator

    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 ?

  28. martin-frbg commented on Nov 13, 2025

    @martin-frbg
    Collaborator

    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

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