Skip to content

Commit 94189b8

Browse files
committed
Update linalg_tests/test_decomposition.py
1 parent 07e7a11 commit 94189b8

File tree

1 file changed

+76
-75
lines changed

1 file changed

+76
-75
lines changed

dpnp/tests/third_party/cupy/linalg_tests/test_decomposition.py

Lines changed: 76 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def random_matrix(shape, dtype, scale, sym=False):
2828
if dtype.kind in "u":
2929
assert sym, (
3030
"generating nonsymmetric matrix with uint cells is not"
31-
" supported."
31+
" supported"
3232
)
3333
# (singular value of numpy.ones((m, n))) <= \sqrt{mn}
3434
high_s = bias = high_s / (1 + numpy.sqrt(m * n))
@@ -58,6 +58,7 @@ def stacked_identity(xp, batch_shape, n, dtype):
5858

5959

6060
class TestCholeskyDecomposition:
61+
6162
@testing.numpy_cupy_allclose(atol=1e-3, type_check=has_support_aspect64())
6263
def check_L(self, array, xp):
6364
a = xp.asarray(array)
@@ -127,6 +128,7 @@ def test_empty(self, shape, xp, dtype):
127128

128129

129130
class TestCholeskyInvalid(unittest.TestCase):
131+
130132
def check_L(self, array):
131133
for xp in (numpy, cupy):
132134
a = xp.asarray(array)
@@ -151,6 +153,75 @@ def test_decomposition(self, dtype):
151153
self.check_L(A)
152154

153155

156+
@testing.parameterize(
157+
*testing.product(
158+
{
159+
"mode": ["r", "raw", "complete", "reduced"],
160+
}
161+
)
162+
)
163+
class TestQRDecomposition(unittest.TestCase):
164+
165+
@testing.for_dtypes("fdFD")
166+
def check_mode(self, array, mode, dtype):
167+
a_cpu = numpy.asarray(array, dtype=dtype)
168+
a_gpu = cupy.asarray(array, dtype=dtype)
169+
result_gpu = cupy.linalg.qr(a_gpu, mode=mode)
170+
if (
171+
mode != "raw"
172+
or numpy.lib.NumpyVersion(numpy.__version__) >= "1.22.0rc1"
173+
):
174+
result_cpu = numpy.linalg.qr(a_cpu, mode=mode)
175+
self._check_result(result_cpu, result_gpu)
176+
177+
def _check_result(self, result_cpu, result_gpu):
178+
if isinstance(result_cpu, tuple):
179+
for b_cpu, b_gpu in zip(result_cpu, result_gpu):
180+
assert b_cpu.dtype == b_gpu.dtype
181+
testing.assert_allclose(b_cpu, b_gpu, atol=1e-4)
182+
else:
183+
assert result_cpu.dtype == result_gpu.dtype
184+
testing.assert_allclose(result_cpu, result_gpu, atol=1e-4)
185+
186+
@testing.fix_random()
187+
@_condition.repeat(3, 10)
188+
def test_mode(self):
189+
self.check_mode(numpy.random.randn(2, 4), mode=self.mode)
190+
self.check_mode(numpy.random.randn(3, 3), mode=self.mode)
191+
self.check_mode(numpy.random.randn(5, 4), mode=self.mode)
192+
193+
@testing.with_requires("numpy>=1.22")
194+
@testing.fix_random()
195+
def test_mode_rank3(self):
196+
self.check_mode(numpy.random.randn(3, 2, 4), mode=self.mode)
197+
self.check_mode(numpy.random.randn(4, 3, 3), mode=self.mode)
198+
self.check_mode(numpy.random.randn(2, 5, 4), mode=self.mode)
199+
200+
@testing.with_requires("numpy>=1.22")
201+
@testing.fix_random()
202+
def test_mode_rank4(self):
203+
self.check_mode(numpy.random.randn(2, 3, 2, 4), mode=self.mode)
204+
self.check_mode(numpy.random.randn(2, 4, 3, 3), mode=self.mode)
205+
self.check_mode(numpy.random.randn(2, 2, 5, 4), mode=self.mode)
206+
207+
@testing.with_requires("numpy>=1.16")
208+
def test_empty_array(self):
209+
self.check_mode(numpy.empty((0, 3)), mode=self.mode)
210+
self.check_mode(numpy.empty((3, 0)), mode=self.mode)
211+
212+
@testing.with_requires("numpy>=1.22")
213+
def test_empty_array_rank3(self):
214+
self.check_mode(numpy.empty((0, 3, 2)), mode=self.mode)
215+
self.check_mode(numpy.empty((3, 0, 2)), mode=self.mode)
216+
self.check_mode(numpy.empty((3, 2, 0)), mode=self.mode)
217+
self.check_mode(numpy.empty((0, 3, 3)), mode=self.mode)
218+
self.check_mode(numpy.empty((3, 0, 3)), mode=self.mode)
219+
self.check_mode(numpy.empty((3, 3, 0)), mode=self.mode)
220+
self.check_mode(numpy.empty((0, 2, 3)), mode=self.mode)
221+
self.check_mode(numpy.empty((2, 0, 3)), mode=self.mode)
222+
self.check_mode(numpy.empty((2, 3, 0)), mode=self.mode)
223+
224+
154225
@testing.parameterize(
155226
*testing.product(
156227
{
@@ -160,6 +231,7 @@ def test_decomposition(self, dtype):
160231
)
161232
@testing.fix_random()
162233
class TestSVD(unittest.TestCase):
234+
163235
def setUp(self):
164236
self.seed = testing.generate_seed()
165237

@@ -197,7 +269,6 @@ def check_usv(self, shape, dtype):
197269

198270
# reconstruct the matrix
199271
k = s_cpu.shape[-1]
200-
201272
if len(shape) == 2:
202273
if self.full_matrices:
203274
a_gpu_usv = cupy.dot(u_gpu[:, :k] * s_gpu, vh_gpu[:k, :])
@@ -239,9 +310,7 @@ def check_usv(self, shape, dtype):
239310
]
240311
)
241312
@testing.numpy_cupy_allclose(
242-
rtol=1e-5,
243-
atol=1e-4,
244-
type_check=has_support_aspect64(),
313+
rtol=1e-5, atol=1e-4, type_check=has_support_aspect64()
245314
)
246315
def check_singular(self, shape, xp, dtype):
247316
array = testing.shaped_random(shape, xp, dtype=dtype, seed=self.seed)
@@ -293,7 +362,7 @@ def test_svd_rank3(self):
293362
self.check_usv((2, 4, 4))
294363
self.check_usv((2, 7, 3))
295364
self.check_usv((2, 4, 3))
296-
self.check_usv((2, 32, 32))
365+
self.check_usv((2, 32, 32)) # still use _gesvdj_batched
297366

298367
@pytest.mark.skipif(
299368
is_cpu_device() and is_win_platform(), reason="SAT-7145"
@@ -355,7 +424,7 @@ def test_svd_rank4(self):
355424
self.check_usv((2, 2, 4, 4))
356425
self.check_usv((2, 2, 7, 3))
357426
self.check_usv((2, 2, 4, 3))
358-
self.check_usv((2, 2, 32, 32))
427+
self.check_usv((2, 2, 32, 32)) # still use _gesvdj_batched
359428

360429
@pytest.mark.skipif(
361430
is_cpu_device() and is_win_platform(), reason="SAT-7145"
@@ -387,71 +456,3 @@ def test_svd_rank4_empty_array(self):
387456
self.check_usv((0, 2, 3, 4))
388457
self.check_usv((1, 2, 0, 4))
389458
self.check_usv((1, 2, 3, 0))
390-
391-
392-
@testing.parameterize(
393-
*testing.product(
394-
{
395-
"mode": ["r", "raw", "complete", "reduced"],
396-
}
397-
)
398-
)
399-
class TestQRDecomposition(unittest.TestCase):
400-
@testing.for_dtypes("fdFD")
401-
def check_mode(self, array, mode, dtype):
402-
a_cpu = numpy.asarray(array, dtype=dtype)
403-
a_gpu = cupy.asarray(array, dtype=dtype)
404-
result_gpu = cupy.linalg.qr(a_gpu, mode=mode)
405-
if (
406-
mode != "raw"
407-
or numpy.lib.NumpyVersion(numpy.__version__) >= "1.22.0rc1"
408-
):
409-
result_cpu = numpy.linalg.qr(a_cpu, mode=mode)
410-
self._check_result(result_cpu, result_gpu)
411-
412-
def _check_result(self, result_cpu, result_gpu):
413-
if isinstance(result_cpu, tuple):
414-
for b_cpu, b_gpu in zip(result_cpu, result_gpu):
415-
assert b_cpu.dtype == b_gpu.dtype
416-
testing.assert_allclose(b_cpu, b_gpu, atol=1e-4)
417-
else:
418-
assert result_cpu.dtype == result_gpu.dtype
419-
testing.assert_allclose(result_cpu, result_gpu, atol=1e-4)
420-
421-
@testing.fix_random()
422-
@_condition.repeat(3, 10)
423-
def test_mode(self):
424-
self.check_mode(numpy.random.randn(2, 4), mode=self.mode)
425-
self.check_mode(numpy.random.randn(3, 3), mode=self.mode)
426-
self.check_mode(numpy.random.randn(5, 4), mode=self.mode)
427-
428-
@testing.with_requires("numpy>=1.22")
429-
@testing.fix_random()
430-
def test_mode_rank3(self):
431-
self.check_mode(numpy.random.randn(3, 2, 4), mode=self.mode)
432-
self.check_mode(numpy.random.randn(4, 3, 3), mode=self.mode)
433-
self.check_mode(numpy.random.randn(2, 5, 4), mode=self.mode)
434-
435-
@testing.with_requires("numpy>=1.22")
436-
@testing.fix_random()
437-
def test_mode_rank4(self):
438-
self.check_mode(numpy.random.randn(2, 3, 2, 4), mode=self.mode)
439-
self.check_mode(numpy.random.randn(2, 4, 3, 3), mode=self.mode)
440-
self.check_mode(numpy.random.randn(2, 2, 5, 4), mode=self.mode)
441-
442-
@testing.with_requires("numpy>=1.16")
443-
def test_empty_array(self):
444-
self.check_mode(numpy.empty((0, 3)), mode=self.mode)
445-
self.check_mode(numpy.empty((3, 0)), mode=self.mode)
446-
447-
@testing.with_requires("numpy>=1.22")
448-
def test_empty_array_rank3(self):
449-
self.check_mode(numpy.empty((0, 3, 2)), mode=self.mode)
450-
self.check_mode(numpy.empty((3, 0, 2)), mode=self.mode)
451-
self.check_mode(numpy.empty((3, 2, 0)), mode=self.mode)
452-
self.check_mode(numpy.empty((0, 3, 3)), mode=self.mode)
453-
self.check_mode(numpy.empty((3, 0, 3)), mode=self.mode)
454-
self.check_mode(numpy.empty((3, 3, 0)), mode=self.mode)
455-
self.check_mode(numpy.empty((0, 2, 3)), mode=self.mode)
456-
self.check_mode(numpy.empty((2, 0, 3)), mode=self.mode)
457-
self.check_mode(numpy.empty((2, 3, 0)), mode=self.mode)

0 commit comments

Comments
 (0)