A silent bug in cuSolverDx heev when exactly one eigenvalue is exactly zero

Hi all, I am writing a fused kernel that involves using cuSolverDx’s heev to compute eigenvalues and eigenvectors of small Hermitian positive semidefinite matrices of type complex float stored in column major. I am using cuSolverDx 0.5.0 with CUDA 13.0 on an RTX 6000 Ada.

When the matrix has exactly one eigenvalue that is exactly zero, sometimes cuSolverDx heev would give one correct eigenvalue with the rest of the eigenvalues being nan while returning info 0. Here’s what I mean by “sometimes”. For each of the following cases, I apply cuSolverDx heev to 2^20 random matrices.

  1. The bug does not happen on full rank matrices.
  2. When there is exactly one eigenvalue that is exactly zero, the bug occurs around 10 times out of the 2^20 random matrices I’ve tried.
  3. When there are exactly two eigenvalues that are exactly zero, the bug does not happen.

I have attached below a minimal reproducer with one of the failed cases involving a 4-by-4 rank-3 upper triangular matrix of type complex float stored in column major. The equivalent LAPACK 3.9.0’s cheev gives the correct answer. As additional reference, I also tried the failed case on cuSOLVER’s heevjBatched, XsyevBatched, and heevd, they also give the correct answer.

I am aware of a very recent bug report on a different routine (cuSOLVERDnXgeev), and I am not sure whether the two bugs are related.

// Minimal reproducer:
// cuSolverDx's heev returns nan eigenvalues with info = 0
// cuSOLVER's heevjBatched, XsyevBatched, and heevd return correct eigenvalues with info = 0
//
// MathDx 26.06.1, cuSolverDx 0.5.0, cuSOLVER 12.0.4, CUDA 13.0, LAPACK 3.9.0, RTX 6000 Ada
//
// Build:
//	X=external/mathdx; C=/usr/pubsw/packages/CUDA/13.0
//	$C/bin/nvcc -std=c++17 -arch=sm_89 -rdc=true -dlto -I$X/include -I$X/external/cutlass/include heev_nan_repro.cu \
//		-L$X/lib -lcusolverdx -lcusolver -llapack -Xlinker -rpath,$C/lib64 -o heev_nan_repro && ./heev_nan_repro

#include <cstdio>
#include <complex>
#include <cusolverDn.h>
#include <cusolverdx.hpp>

using Heev = decltype(cusolverdx::Size<4>() + cusolverdx::Precision<float>() + cusolverdx::Type<cusolverdx::type::complex>()
					  + cusolverdx::Function<cusolverdx::heev>() + cusolverdx::FillMode<cusolverdx::fill_mode::upper>() 
					  + cusolverdx::Arrangement<cusolverdx::arrangement::col_major>() + cusolverdx::Job<cusolverdx::job::no_vectors>() 
					  + cusolverdx::SM<890>() + cusolverdx::Thread());

extern "C" void cheev_(const char *jobz, const char *uplo, const int *n, std::complex<float> *a, const int *lda, float *w,
					   std::complex<float> *work, const int *lwork, float *rwork, int *info, size_t, size_t);

static const std::complex<float> G[16] = {
	{3.664379835e+00f, 0.f}, {}, {}, {}, 
	{-5.536674261e-01f, 9.496642947e-01f}, {5.122905374e-01f, 0.f}, {}, {}, 
	{-2.275886059e+00f, 9.832407832e-01f}, {4.101600945e-01f, 2.608992755e-01f}, {2.376560450e+00f, 0.f}, {}, 
	{2.731287479e-01f, 1.057614088e+00f}, {2.444558889e-01f, -3.210833073e-01f}, {-3.514528275e-03f, -8.952040076e-01f}, {7.113531232e-01f, 0.f}};

__global__ void kernel(std::complex<float> *a, float *w, int *info)
{
	Heev::a_data_type work[Heev::workspace_size];

	Heev().execute(reinterpret_cast<Heev::a_data_type *>(a), w, work, info);
}

int main()
{
	std::complex<float> *a;
	float *w;
	int *info;

	cudaMallocManaged(&a, sizeof G);
	cudaMallocManaged(&w, 4 * sizeof(float));
	cudaMallocManaged(&info, sizeof(int));

	// cuSolverDx heev
	std::copy(G, G + 16, a);
	kernel<<<1, 1>>>(a, w, info);
	cudaDeviceSynchronize();
	printf("cuSolverDx heev:           info %d, eigenvalues %g %g %g %g\n", *info, w[0], w[1], w[2], w[3]);

	// cuSOLVER
	cusolverDnHandle_t h;
	syevjInfo_t jp;
	cusolverDnParams_t xp;
	cusolverDnCreate(&h);
	cusolverDnCreateSyevjInfo(&jp);
	cusolverDnCreateParams(&xp);

	const auto jobz = CUSOLVER_EIG_MODE_NOVECTOR;
	const auto uplo = CUBLAS_FILL_MODE_UPPER;
	int lw;
	size_t dws, hws;
	void *dwork, *hwork;

	// cuSOLVER: cusolverDnCheevjBatched
	std::copy(G, G + 16, a);
	cusolverDnCheevjBatched_bufferSize(h, jobz, uplo, 4, (cuComplex *)a, 4, w, &lw, jp, 1);
	cudaMalloc(&dwork, lw * sizeof(cuComplex));
	cusolverDnCheevjBatched(h, jobz, uplo, 4, (cuComplex *)a, 4, w, (cuComplex *)dwork, lw, info, jp, 1);
	cudaDeviceSynchronize();
	printf("cuSOLVER heevjBatched:     info %d, eigenvalues %g %g %g %g\n", *info, w[0], w[1], w[2], w[3]);
	cudaFree(dwork);

	// cuSOLVER: cusolverDnXsyevBatched
	std::copy(G, G + 16, a);
	cusolverDnXsyevBatched_bufferSize(h, xp, jobz, uplo, 4, CUDA_C_32F, a, 4, CUDA_R_32F, w, CUDA_C_32F, &dws, &hws, 1);
	cudaMalloc(&dwork, dws + 1);
	hwork = malloc(hws + 1);
	cusolverDnXsyevBatched(h, xp, jobz, uplo, 4, CUDA_C_32F, a, 4, CUDA_R_32F, w, CUDA_C_32F, dwork, dws, hwork, hws, info, 1);
	cudaDeviceSynchronize();
	printf("cuSOLVER XsyevBatched:     info %d, eigenvalues %g %g %g %g\n", *info, w[0], w[1], w[2], w[3]);
	cudaFree(dwork);
	free(hwork);

	// cuSOLVER: cusolverDnCheevd
	std::copy(G, G + 16, a);
	cusolverDnCheevd_bufferSize(h, jobz, uplo, 4, (cuComplex *)a, 4, w, &lw);
	cudaMalloc(&dwork, lw * sizeof(cuComplex));
	cusolverDnCheevd(h, jobz, uplo, 4, (cuComplex *)a, 4, w, (cuComplex *)dwork, lw, info);
	cudaDeviceSynchronize();
	printf("cuSOLVER heevd:            info %d, eigenvalues %g %g %g %g\n", *info, w[0], w[1], w[2], w[3]);
	cudaFree(dwork);

	// LAPACK: cheev
	std::complex<float> b[16], work[64];
	float wl[4], rwork[12];
	int n = 4, lwork = 64, linfo;

	std::copy(G, G + 16, b);
	cheev_("N", "U", &n, b, &n, wl, work, &lwork, rwork, &linfo, 1, 1);
	printf("LAPACK cheev (reference):  info %d, eigenvalues %g %g %g %g\n", linfo, wl[0], wl[1], wl[2], wl[3]);
}

Output:

cuSolverDx heev:           info 0, eigenvalues nan nan nan 0.365793
cuSOLVER heevjBatched:     info 0, eigenvalues 1.50494e-08 0.365793 0.673537 6.22526
cuSOLVER XsyevBatched:     info 0, eigenvalues 2.19498e-07 0.365792 0.673537 6.22526
cuSOLVER heevd:            info 0, eigenvalues -1.17751e-07 0.365793 0.673537 6.22525
LAPACK cheev (reference):  info 0, eigenvalues 4.50742e-08 0.365793 0.673537 6.22526

Hi, thanks for sharing this issue! We have a fix, although we probably can’t get it into 0.6.0. But it will definitely be fixed in the subsequent release.

If you need a workaround in the meantime, one option is to increase the diagonal elements by a small value, around 2^-20 max_ij |A[i,j]|. Then, subtract the same value from each of the computed eigenvalues. This may increase the round-off error slightly but should prevent exact zeros from occurring.

And for our benefit, can you share what your use case is? Since the library is new, we’re very interested in understanding how it’s being used and what users need.

Hi @nlindquist, thanks for the quick answer! I look forward to this bug being fixed in the near future!

I did exactly the workaround that you proposed except that I used 2^-10 and it works great!

I also have a feature request. LAPACK’s ?heev rescales the input matrix when its magnitude falls outside of a safe range. From my experience with cuSolverDx, I believe cuSolverDx’s heev does not perform any scaling and I am not sure if htev performs any scaling internally. Out of caution, I apply the same scaling logic as LAPACK before and after heev. It would be great if this scaling logic can be incorporated into heev.

I am writing a customized kernel for batched singular value thresholding for a batch of matrices of shape B, M, N so that I can use it from within Python, MATLAB, and C++. In my use case, M (e.g., 5) is always smaller than N (e.g., 64), B is typically tens of thousands. In this case, I find an implementation that uses EVD on the Gram matrix significantly faster than a naive implementation that applies SVD to the original matrix. I am using cuSolverDx because the other operations besides EVD (i.e., matmul to get the Gram matrix, scaling, soft thresholding, matmul back) are very cheap and are worth fusing with EVD.

Hi,

Both heev and htev are designed to handle very large and very small inputs. For heev this isn’t an explicit scaling at the beginning and end, and instead it’s subroutines individually support large ranges. Do you have matrices where it generates an incorrect result?

And thanks for sharing your use case, that’s a helpful datapoint.

Hi,

Thanks for the clarification. I think I will keep the scaling. Here’s a matrix that fails silently with cuSolverDx’s heev but works with cuSOLVER’s heevjBatched, XsyevBatched, and heevd.

// Minimal reproducer:
// cuSolverDx's heev returns nan eigenvalues with info = 0 
// cuSolverDx's heev with LAPACK cheev's scaling, cuSOLVER's heevjBatched, XsyevBatched, and heevd
// return correct eigenvalues with info = 0
//
// MathDx 26.06.1, cuSolverDx 0.5.0, cuSOLVER 12.0.4, CUDA 13.0, LAPACK 3.9.0, RTX 6000 Ada
//
// Build:
//	X=../external/mathdx; C=/usr/pubsw/packages/CUDA/13.0
//	$C/bin/nvcc -std=c++17 -arch=sm_89 -rdc=true -dlto -I$X/include -I$X/external/cutlass/include heev_scale_repro.cu \
//		-L$X/lib -lcusolverdx -lcusolver -llapack -Xlinker -rpath,$C/lib64 -o heev_scale_repro && ./heev_scale_repro

#include <cstdio>
#include <complex>
#include <cusolverDn.h>
#include <cusolverdx.hpp>

using Heev =
	decltype(cusolverdx::Size<4>() + cusolverdx::Precision<float>() + cusolverdx::Type<cusolverdx::type::complex>()
		+ cusolverdx::Function<cusolverdx::heev>() + cusolverdx::FillMode<cusolverdx::fill_mode::upper>()
		+ cusolverdx::Arrangement<cusolverdx::arrangement::col_major>()
		+ cusolverdx::Job<cusolverdx::job::no_vectors>() + cusolverdx::SM<890>() + cusolverdx::Thread());

extern "C" void cheev_(const char* jobz, const char* uplo, const int* n, std::complex<float>* a, const int* lda,
	float* w, std::complex<float>* work, const int* lwork, float* rwork, int* info, size_t, size_t);

static const std::complex<float> G[16] = {
	{ 1.806011300e-31f, 0.f }, {}, {}, {},
	{ -2.175562862e-31f, 2.368922749e-31f }, { 6.081163766e-31f, 0.f }, {}, {},
	{ -3.522202049e-33f, 1.518170937e-32f }, { 1.529445838e-32f, -1.505822663e-32f }, { 3.623797822e-33f, 0.f }, {},
	{ 1.039888661e-32f, -2.930575889e-32f }, { -6.445846783e-32f, 2.644617493e-32f }, { 5.311996064e-34f, -2.034203076e-33f }, { 1.115691517e-32f, 0.f } };

__global__ void kernel(std::complex<float>* a, float* w, int* info)
{
	Heev::a_data_type work[Heev::workspace_size];

	Heev().execute(reinterpret_cast<Heev::a_data_type*>(a), w, work, info);
}

int main()
{
	std::complex<float>* a;
	float* w;
	int* info;

	cudaMallocManaged(&a, sizeof G);
	cudaMallocManaged(&w, 4 * sizeof(float));
	cudaMallocManaged(&info, sizeof(int));

	// cuSolverDx heev
	std::copy(G, G + 16, a);
	kernel<<<1, 1>>>(a, w, info);
	cudaDeviceSynchronize();
	printf("cuSolverDx heev:           info %d, eigenvalues %g %g %g %g\n", *info, w[0], w[1], w[2], w[3]);

	// cuSolverDx heev with LAPACK cheev's scaling
	float anrm = 0.f;

	for (int k = 0; k < 16; k++)
		anrm = fmaxf(anrm, std::abs(G[k]));

	float sigma = sqrtf(0x1p-103f) / anrm;

	for (int k = 0; k < 16; k++)
		a[k] = sigma * G[k];

	kernel<<<1, 1>>>(a, w, info);
	cudaDeviceSynchronize();
	printf("cuSolverDx heev, scaled:   info %d, eigenvalues %g %g %g %g\n", *info, w[0] / sigma, w[1] / sigma,
		w[2] / sigma, w[3] / sigma);

	// cuSOLVER
	cusolverDnHandle_t h;
	syevjInfo_t jp;
	cusolverDnParams_t xp;
	cusolverDnCreate(&h);
	cusolverDnCreateSyevjInfo(&jp);
	cusolverDnCreateParams(&xp);

	const auto jobz = CUSOLVER_EIG_MODE_NOVECTOR;
	const auto uplo = CUBLAS_FILL_MODE_UPPER;
	int lw;
	size_t dws, hws;
	void *dwork, *hwork;

	// cuSOLVER: cusolverDnCheevjBatched
	std::copy(G, G + 16, a);
	cusolverDnCheevjBatched_bufferSize(h, jobz, uplo, 4, (cuComplex*)a, 4, w, &lw, jp, 1);
	cudaMalloc(&dwork, lw * sizeof(cuComplex));
	cusolverDnCheevjBatched(h, jobz, uplo, 4, (cuComplex*)a, 4, w, (cuComplex*)dwork, lw, info, jp, 1);
	cudaDeviceSynchronize();
	printf("cuSOLVER heevjBatched:     info %d, eigenvalues %g %g %g %g\n", *info, w[0], w[1], w[2], w[3]);
	cudaFree(dwork);

	// cuSOLVER: cusolverDnXsyevBatched
	std::copy(G, G + 16, a);
	cusolverDnXsyevBatched_bufferSize(
		h, xp, jobz, uplo, 4, CUDA_C_32F, a, 4, CUDA_R_32F, w, CUDA_C_32F, &dws, &hws, 1);
	cudaMalloc(&dwork, dws + 1);
	hwork = malloc(hws + 1);
	cusolverDnXsyevBatched(
		h, xp, jobz, uplo, 4, CUDA_C_32F, a, 4, CUDA_R_32F, w, CUDA_C_32F, dwork, dws, hwork, hws, info, 1);
	cudaDeviceSynchronize();
	printf("cuSOLVER XsyevBatched:     info %d, eigenvalues %g %g %g %g\n", *info, w[0], w[1], w[2], w[3]);
	cudaFree(dwork);
	free(hwork);

	// cuSOLVER: cusolverDnCheevd
	std::copy(G, G + 16, a);
	cusolverDnCheevd_bufferSize(h, jobz, uplo, 4, (cuComplex*)a, 4, w, &lw);
	cudaMalloc(&dwork, lw * sizeof(cuComplex));
	cusolverDnCheevd(h, jobz, uplo, 4, (cuComplex*)a, 4, w, (cuComplex*)dwork, lw, info);
	cudaDeviceSynchronize();
	printf("cuSOLVER heevd:            info %d, eigenvalues %g %g %g %g\n", *info, w[0], w[1], w[2], w[3]);
	cudaFree(dwork);

	// LAPACK: cheev
	std::complex<float> b[16], work[64];
	float wl[4], rwork[12];
	int n = 4, lwork = 64, linfo;

	std::copy(G, G + 16, b);
	cheev_("N", "U", &n, b, &n, wl, work, &lwork, rwork, &linfo, 1, 1);
	printf("LAPACK cheev (reference):  info %d, eigenvalues %g %g %g %g\n", linfo, wl[0], wl[1], wl[2], wl[3]);
}

Output:

cuSolverDx heev:           info 0, eigenvalues nan nan nan nan
cuSolverDx heev, scaled:   info 0, eigenvalues 1.55034e-37 1.77181e-37 1.46369e-32 7.88861e-31
cuSOLVER heevjBatched:     info 0, eigenvalues 1.76972e-37 1.78015e-37 1.4637e-32 7.88861e-31
cuSOLVER XsyevBatched:     info 0, eigenvalues 1.78457e-37 2.06707e-37 1.4637e-32 7.88861e-31
cuSOLVER heevd:            info 0, eigenvalues 1.50311e-37 1.76487e-37 1.46369e-32 7.88861e-31
LAPACK cheev (reference):  info 0, eigenvalues 1.6638e-37 1.80182e-37 1.4637e-32 7.88861e-31

In my use case, this situation is rarely possible because singular value thresholding sets any singular value below the threshold tau to exactly zero. However, small singular values could still happen when the input data itself is not normalized or weirdly scaled.

Hi,

Thanks for sharing this reproducer. This is fixed in cuSolverDx 0.6.0 (as well as a related issue for QR). The new version shipped last week as part of the MathDx 26.10, so you should just need to download the new version

Running your reproducer with cuSolverDx 0.6.0 gives me

cuSolverDx heev:           info 0, eigenvalues 1.75865e-37 1.85767e-37 1.4637e-32 7.88861e-31
cuSolverDx heev, scaled:   info 0, eigenvalues 1.55034e-37 1.77181e-37 1.46369e-32 7.88861e-31
cuSOLVER heevjBatched:     info 0, eigenvalues 1.76972e-37 1.78015e-37 1.4637e-32 7.88861e-31
cuSOLVER XsyevBatched:     info 0, eigenvalues 1.78457e-37 2.06707e-37 1.4637e-32 7.88861e-31
cuSOLVER heevd:            info 0, eigenvalues 1.50311e-37 1.76487e-37 1.46369e-32 7.88861e-31
LAPACK cheev (reference):  info 0, eigenvalues 1.79164e-37 2.22485e-37 1.4637e-32 7.88861e-31