Eigenvalues and eigenvectors (#1334)

* initial eigvalsh

* add compute_vectors

* add compute_vectors_

* return a pair

* add eigh to return only eigenvectors

* fixed typo

* merge merge Eighvalsh and Eigh into a single primitive

* use the same primate with the flag

* fix primatives

* use MULTI

* fix eval_gpu

* fix decleration

* rename EighPrimitive to Eigh

* tests

* tests

* fix rebase and format

* cleanup lapack

* format

* add cblas.h

---------

Co-authored-by: Awni Hannun <awni@apple.com>
This commit is contained in:
Kashif Rasul
2024-10-22 21:18:48 +02:00
committed by GitHub
parent c26208f67d
commit 3ddc07e936
23 changed files with 434 additions and 86 deletions

View File

@@ -405,4 +405,85 @@ void init_linalg(nb::module_& parent_module) {
Returns:
array: The cross product of ``a`` and ``b`` along the specified axis.
)pbdoc");
m.def(
"eigvalsh",
&eigvalsh,
"a"_a,
"UPLO"_a = "L",
nb::kw_only(),
"stream"_a = nb::none(),
R"pbdoc(
Compute the eigenvalues of a complex Hermitian or real symmetric matrix.
This function supports arrays with at least 2 dimensions. When the
input has more than two dimensions, the eigenvalues are computed for
each matrix in the last two dimensions.
Args:
a (array): Input array. Must be a real symmetric or complex
Hermitian matrix.
UPLO (str, optional): Whether to use the upper (``"U"``) or
lower (``"L"``) triangle of the matrix. Default: ``"L"``.
stream (Stream, optional): Stream or device. Defaults to ``None``
in which case the default stream of the default device is used.
Returns:
array: The eigenvalues in ascending order.
Note:
The input matrix is assumed to be symmetric (or Hermitian). Only
the selected triangle is used. No checks for symmetry are performed.
Example:
>>> A = mx.array([[1., -2.], [-2., 1.]])
>>> eigenvalues = mx.linalg.eigvalsh(A, stream=mx.cpu)
>>> eigenvalues
array([-1., 3.], dtype=float32)
)pbdoc");
m.def(
"eigh",
[](const array& a, const std::string UPLO, StreamOrDevice s) {
// TODO avoid cast?
auto result = eigh(a, UPLO, s);
return nb::make_tuple(result.first, result.second);
},
"a"_a,
"UPLO"_a = "L",
nb::kw_only(),
"stream"_a = nb::none(),
R"pbdoc(
Compute the eigenvalues and eigenvectors of a complex Hermitian or
real symmetric matrix.
This function supports arrays with at least 2 dimensions. When the input
has more than two dimensions, the eigenvalues and eigenvectors are
computed for each matrix in the last two dimensions.
Args:
a (array): Input array. Must be a real symmetric or complex
Hermitian matrix.
UPLO (str, optional): Whether to use the upper (``"U"``) or
lower (``"L"``) triangle of the matrix. Default: ``"L"``.
stream (Stream, optional): Stream or device. Defaults to ``None``
in which case the default stream of the default device is used.
Returns:
Tuple[array, array]:
A tuple containing the eigenvalues in ascending order and
the normalized eigenvectors. The column ``v[:, i]`` is the
eigenvector corresponding to the i-th eigenvalue.
Note:
The input matrix is assumed to be symmetric (or Hermitian). Only
the selected triangle is used. No checks for symmetry are performed.
Example:
>>> A = mx.array([[1., -2.], [-2., 1.]])
>>> w, v = mx.linalg.eigh(A, stream=mx.cpu)
>>> w
array([-1., 3.], dtype=float32)
>>> v
array([[ 0.707107, -0.707107],
[ 0.707107, 0.707107]], dtype=float32)
)pbdoc");
}

View File

@@ -268,6 +268,57 @@ class TestLinalg(mlx_tests.MLXTestCase):
with self.assertRaises(ValueError):
mx.linalg.cross(a, b)
def test_eigh(self):
tols = {"atol": 1e-5, "rtol": 1e-5}
def check_eigs_and_vecs(A_np, kwargs={}):
A = mx.array(A_np)
eig_vals, eig_vecs = mx.linalg.eigh(A, stream=mx.cpu, **kwargs)
eig_vals_np, _ = np.linalg.eigh(A_np, **kwargs)
self.assertTrue(np.allclose(eig_vals, eig_vals_np, **tols))
self.assertTrue(
mx.allclose(A @ eig_vecs, eig_vals[..., None, :] * eig_vecs, **tols)
)
eig_vals_only = mx.linalg.eigvalsh(A, stream=mx.cpu, **kwargs)
self.assertTrue(mx.allclose(eig_vals, eig_vals_only, **tols))
# Test a simple 2x2 symmetric matrix
A_np = np.array([[1.0, 2.0], [2.0, 4.0]], dtype=np.float32)
check_eigs_and_vecs(A_np)
# Test a larger random symmetric matrix
n = 5
np.random.seed(1)
A_np = np.random.randn(n, n).astype(np.float32)
A_np = (A_np + A_np.T) / 2
check_eigs_and_vecs(A_np)
# Test with upper triangle
check_eigs_and_vecs(A_np, {"UPLO": "U"})
# Test with batched input
A_np = np.random.randn(3, n, n).astype(np.float32)
A_np = (A_np + np.transpose(A_np, (0, 2, 1))) / 2
check_eigs_and_vecs(A_np)
# Test error cases
with self.assertRaises(ValueError):
mx.linalg.eigh(mx.array([1.0, 2.0])) # 1D array
with self.assertRaises(ValueError):
mx.linalg.eigh(
mx.array([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]])
) # Non-square matrix
with self.assertRaises(ValueError):
mx.linalg.eigvalsh(mx.array([1.0, 2.0])) # 1D array
with self.assertRaises(ValueError):
mx.linalg.eigvalsh(
mx.array([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]])
) # Non-square matrix
if __name__ == "__main__":
unittest.main()