|
| 1 | +import contextlib |
| 2 | +from functools import partial |
| 3 | + |
| 4 | +import numpy as np |
| 5 | +import pytest |
| 6 | + |
| 7 | +import pytensor.tensor as pt |
| 8 | +from pytensor import config |
| 9 | +from tests.link.mlx.test_basic import compare_mlx_and_py, mlx_mode |
| 10 | + |
| 11 | + |
| 12 | +@pytest.mark.parametrize("lower", [True, False]) |
| 13 | +def test_mlx_cholesky(lower): |
| 14 | + rng = np.random.default_rng(15) |
| 15 | + n = 3 |
| 16 | + |
| 17 | + A = pt.tensor("A", shape=(n, n)) |
| 18 | + A_val = rng.normal(size=(n, n)) |
| 19 | + A_val = (A_val @ A_val.T).astype(config.floatX) |
| 20 | + |
| 21 | + out = pt.linalg.cholesky(A, lower=lower) |
| 22 | + |
| 23 | + compare_mlx_and_py( |
| 24 | + [A], |
| 25 | + [out], |
| 26 | + [A_val], |
| 27 | + mlx_mode=mlx_mode, |
| 28 | + assert_fn=partial(np.testing.assert_allclose, atol=1e-6, strict=True), |
| 29 | + ) |
| 30 | + |
| 31 | + |
| 32 | +@pytest.mark.parametrize("assume_a", ["gen", "pos"]) |
| 33 | +def test_mlx_solve(assume_a): |
| 34 | + rng = np.random.default_rng(15) |
| 35 | + n = 3 |
| 36 | + |
| 37 | + A = pt.tensor("A", shape=(n, n)) |
| 38 | + b = pt.tensor("B", shape=(n, n)) |
| 39 | + |
| 40 | + out = pt.linalg.solve(A, b, b_ndim=2, assume_a=assume_a) |
| 41 | + |
| 42 | + A_val = rng.normal(size=(n, n)).astype(config.floatX) |
| 43 | + A_val = A_val @ A_val.T |
| 44 | + |
| 45 | + b_val = rng.normal(size=(n, n)).astype(config.floatX) |
| 46 | + |
| 47 | + context = ( |
| 48 | + contextlib.suppress() |
| 49 | + if assume_a == "gen" |
| 50 | + else pytest.warns( |
| 51 | + UserWarning, match=f"MLX solve does not support assume_a={assume_a}" |
| 52 | + ) |
| 53 | + ) |
| 54 | + |
| 55 | + with context: |
| 56 | + compare_mlx_and_py( |
| 57 | + [A, b], |
| 58 | + [out], |
| 59 | + [A_val, b_val], |
| 60 | + mlx_mode=mlx_mode, |
| 61 | + assert_fn=partial( |
| 62 | + np.testing.assert_allclose, atol=1e-6, rtol=1e-6, strict=True |
| 63 | + ), |
| 64 | + ) |
| 65 | + |
| 66 | + |
| 67 | +@pytest.mark.parametrize("lower, trans", [(False, False), (True, True)]) |
| 68 | +def test_mlx_SolveTriangular(lower, trans): |
| 69 | + rng = np.random.default_rng(15) |
| 70 | + |
| 71 | + A = pt.tensor("A", shape=(5, 5)) |
| 72 | + b = pt.tensor("B", shape=(5, 5)) |
| 73 | + |
| 74 | + A_val = rng.normal(size=(5, 5)).astype(config.floatX) |
| 75 | + b_val = rng.normal(size=(5, 5)).astype(config.floatX) |
| 76 | + |
| 77 | + out = pt.linalg.solve_triangular( |
| 78 | + A, |
| 79 | + b, |
| 80 | + trans=0, |
| 81 | + lower=lower, |
| 82 | + unit_diagonal=False, |
| 83 | + ) |
| 84 | + compare_mlx_and_py( |
| 85 | + [A, b], |
| 86 | + [out], |
| 87 | + [A_val, b_val], |
| 88 | + mlx_mode=mlx_mode, |
| 89 | + assert_fn=partial( |
| 90 | + np.testing.assert_allclose, atol=1e-6, rtol=1e-6, strict=True |
| 91 | + ), |
| 92 | + ) |
| 93 | + |
| 94 | + |
| 95 | +def test_mlx_LU(): |
| 96 | + rng = np.random.default_rng(15) |
| 97 | + |
| 98 | + A = pt.tensor("A", shape=(5, 5)) |
| 99 | + out = pt.linalg.lu(A, permute_l=False, p_indices=True) |
| 100 | + |
| 101 | + A_val = rng.normal(size=(5, 5)).astype(config.floatX) |
| 102 | + |
| 103 | + compare_mlx_and_py( |
| 104 | + [A], |
| 105 | + out, |
| 106 | + [A_val], |
| 107 | + mlx_mode=mlx_mode, |
| 108 | + assert_fn=partial(np.testing.assert_allclose, atol=1e-6, strict=True), |
| 109 | + ) |
0 commit comments