From 5a33d431e015e1973562c4adb799464adf81fbfc Mon Sep 17 00:00:00 2001 From: Vladislav Perevezentsev Date: Mon, 29 Sep 2025 06:14:29 -0700 Subject: [PATCH 1/4] Add _make_nonsingular_np to TestLuFactor --- dpnp/tests/test_linalg.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/dpnp/tests/test_linalg.py b/dpnp/tests/test_linalg.py index a25c237f846..e879a2b98fe 100644 --- a/dpnp/tests/test_linalg.py +++ b/dpnp/tests/test_linalg.py @@ -1885,6 +1885,16 @@ def _apply_pivots_rows(A_dp, piv_dp): rows = dpnp.asarray(rows) return A_dp[rows] + @staticmethod + def _make_nonsingular_np(shape, dtype, order): + A = generate_random_numpy_array(shape, dtype, order) + m, n = shape + k = min(m, n) + for i in range(k): + off = numpy.sum(numpy.abs(A[i, :n])) - numpy.abs(A[i, i]) + A[i, i] = A.dtype.type(off + 1.0) + return A + @staticmethod def _split_lu(lu, m, n): L = dpnp.tril(lu, k=-1) @@ -1899,7 +1909,7 @@ def _split_lu(lu, m, n): @pytest.mark.parametrize("order", ["C", "F"]) @pytest.mark.parametrize("dtype", get_all_dtypes(no_bool=True)) def test_lu_factor(self, shape, order, dtype): - a_np = generate_random_numpy_array(shape, dtype, order) + a_np = self._make_nonsingular_np(shape, dtype, order) a_dp = dpnp.array(a_np, order=order) lu, piv = dpnp.linalg.lu_factor( @@ -2001,12 +2011,7 @@ def test_empty_inputs(self, shape): ], ) def test_strided(self, sl): - base = ( - numpy.arange(7 * 7, dtype=dpnp.default_float_type()).reshape( - 7, 7, order="F" - ) - + 0.1 - ) + base = self._make_nonsingular_np((7, 7), dpnp.default_float_type(), "F") a_np = base[sl] a_dp = dpnp.array(a_np) From 68d46d02e7af04eecb30adb0098218d52bc85ef3 Mon Sep 17 00:00:00 2001 From: Vladislav Perevezentsev Date: Mon, 29 Sep 2025 06:52:18 -0700 Subject: [PATCH 2/4] Add _make_nonsingular_nd_np to TestLuFactorBatched --- dpnp/tests/test_linalg.py | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/dpnp/tests/test_linalg.py b/dpnp/tests/test_linalg.py index e879a2b98fe..d3317a8d001 100644 --- a/dpnp/tests/test_linalg.py +++ b/dpnp/tests/test_linalg.py @@ -2052,6 +2052,23 @@ def _apply_pivots_rows(A_dp, piv_dp): rows = dpnp.asarray(rows) return A_dp[rows] + @staticmethod + def _make_nonsingular_nd_np(shape, dtype, order): + A = generate_random_numpy_array(shape, dtype, order) + m, n = shape[-2], shape[-1] + k = min(m, n) + A3 = A.reshape((-1, m, n)) + for B in A3: + for i in range(k): + off = numpy.sum(numpy.abs(B[i, :n])) - numpy.abs(B[i, i]) + B[i, i] = A.dtype.type(off + 1.0) + + A = A3.reshape(shape) + # A3.reshape returns an array in C order by default + if order != "C": + A = numpy.array(A, order=order) + return A + @staticmethod def _split_lu(lu, m, n): L = dpnp.tril(lu, k=-1) @@ -2068,7 +2085,7 @@ def _split_lu(lu, m, n): @pytest.mark.parametrize("order", ["C", "F"]) @pytest.mark.parametrize("dtype", get_all_dtypes(no_bool=True)) def test_lu_factor_batched(self, shape, order, dtype): - a_np = generate_random_numpy_array(shape, dtype, order) + a_np = self._make_nonsingular_nd_np(shape, dtype, order) a_dp = dpnp.array(a_np, order=order) lu, piv = dpnp.linalg.lu_factor( @@ -2092,7 +2109,8 @@ def test_lu_factor_batched(self, shape, order, dtype): @pytest.mark.parametrize("dtype", get_float_complex_dtypes()) @pytest.mark.parametrize("order", ["C", "F"]) def test_overwrite(self, dtype, order): - a_dp = dpnp.arange(2 * 2 * 3, dtype=dtype).reshape(3, 2, 2, order=order) + a_np = self._make_nonsingular_nd_np((3, 2, 2), dtype, order) + a_dp = dpnp.array(a_np, order=order) a_dp_orig = a_dp.copy() lu, piv = dpnp.linalg.lu_factor( a_dp, overwrite_a=True, check_finite=False @@ -2123,13 +2141,11 @@ def test_empty_inputs(self, shape): assert piv.shape == (*shape[:-2], min(m, n)) def test_strided(self): - a = ( - dpnp.arange(5 * 3 * 3, dtype=dpnp.default_float_type()).reshape( - 5, 3, 3, order="F" - ) - + 0.1 + a_np = self._make_nonsingular_nd_np( + (5, 3, 3), dpnp.default_float_type(), "F" ) - a_stride = a[::2] + a_dp = dpnp.array(a_np, order=order) + a_stride = a_dp[::2] lu, piv = dpnp.linalg.lu_factor(a_stride, check_finite=False) for i in range(a_stride.shape[0]): L, U = self._split_lu(lu[i], 3, 3) From b9ccb28ee3645c86a99b63d635e7f1adc2359979 Mon Sep 17 00:00:00 2001 From: Vladislav Perevezentsev Date: Mon, 29 Sep 2025 07:42:34 -0700 Subject: [PATCH 3/4] Pass correct order --- dpnp/tests/test_linalg.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dpnp/tests/test_linalg.py b/dpnp/tests/test_linalg.py index d3317a8d001..5c5ae3c1c65 100644 --- a/dpnp/tests/test_linalg.py +++ b/dpnp/tests/test_linalg.py @@ -2144,7 +2144,7 @@ def test_strided(self): a_np = self._make_nonsingular_nd_np( (5, 3, 3), dpnp.default_float_type(), "F" ) - a_dp = dpnp.array(a_np, order=order) + a_dp = dpnp.array(a_np, order="F") a_stride = a_dp[::2] lu, piv = dpnp.linalg.lu_factor(a_stride, check_finite=False) for i in range(a_stride.shape[0]): From 6133cf037b9bed2d2f9692366c398fadf969ee7a Mon Sep 17 00:00:00 2001 From: Vladislav Perevezentsev Date: Tue, 30 Sep 2025 05:20:50 -0700 Subject: [PATCH 4/4] Update _make_nonsingular_nd_np to keep order --- dpnp/tests/test_linalg.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/dpnp/tests/test_linalg.py b/dpnp/tests/test_linalg.py index d558d94a857..0180ecc7d6d 100644 --- a/dpnp/tests/test_linalg.py +++ b/dpnp/tests/test_linalg.py @@ -2054,9 +2054,8 @@ def _make_nonsingular_nd_np(shape, dtype, order): B[i, i] = A.dtype.type(off + 1.0) A = A3.reshape(shape) - # A3.reshape returns an array in C order by default - if order != "C": - A = numpy.array(A, order=order) + # Ensure reshapes did not break memory order + A = numpy.array(A, order=order) return A @staticmethod