-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathtest_decomposition.py
More file actions
513 lines (452 loc) · 16.9 KB
/
Copy pathtest_decomposition.py
File metadata and controls
513 lines (452 loc) · 16.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
from __future__ import annotations
import unittest
import numpy
import pytest
import dpnp as cupy
# from cupyx import cusolver
# from cupy.cuda import driver
# from cupy.cuda import runtime
# from cupy.linalg import _util
from dpnp.tests.helper import (
has_support_aspect64,
)
from dpnp.tests.third_party.cupy import testing
from dpnp.tests.third_party.cupy.testing import _condition
# import cupyx
def random_matrix(shape, dtype, scale, sym=False):
m, n = shape[-2:]
dtype = numpy.dtype(dtype)
assert dtype.kind in "iufc"
low_s, high_s = scale
bias = None
if dtype.kind in "iu":
# For an m \times n matrix M whose element is in [-0.5, 0.5], it holds
# (singular value of M) <= \sqrt{mn} / 2
err = numpy.sqrt(m * n) / 2.0
low_s += err
high_s -= err
if dtype.kind in "u":
assert sym, (
"generating nonsymmetric matrix with uint cells is not"
" supported"
)
# (singular value of numpy.ones((m, n))) <= \sqrt{mn}
high_s = bias = high_s / (1 + numpy.sqrt(m * n))
assert low_s <= high_s
a = numpy.random.standard_normal(shape)
if dtype.kind == "c":
a = a + 1j * numpy.random.standard_normal(shape)
u, s, vh = numpy.linalg.svd(a)
if sym:
assert m == n
vh = u.conj().swapaxes(-1, -2)
new_s = numpy.random.uniform(low_s, high_s, s.shape)
new_a = numpy.einsum("...ij,...j,...jk->...ik", u, new_s, vh)
if bias is not None:
new_a += bias
if dtype.kind in "iu":
new_a = numpy.rint(new_a)
return new_a.astype(dtype)
def stacked_identity(xp, batch_shape, n, dtype):
shape = batch_shape + (n, n)
idx = xp.arange(n)
x = xp.zeros(shape, dtype=dtype)
x[..., idx, idx] = 1
return x
class TestCholeskyDecomposition:
@testing.numpy_cupy_allclose(atol=1e-3, type_check=has_support_aspect64())
def check_L(self, array, xp):
a = xp.asarray(array)
return xp.linalg.cholesky(a)
@testing.for_dtypes(
[
numpy.int32,
numpy.int64,
numpy.uint32,
numpy.uint64,
numpy.float32,
numpy.float64,
numpy.complex64,
numpy.complex128,
]
)
def test_decomposition(self, dtype):
# A positive definite matrix
A = random_matrix((5, 5), dtype, scale=(10, 10000), sym=True)
self.check_L(A)
# np.linalg.cholesky only uses a lower triangle of an array
self.check_L(numpy.array([[1, 2], [1, 9]], dtype))
@testing.for_dtypes(
[
numpy.int32,
numpy.int64,
numpy.uint32,
numpy.uint64,
numpy.float32,
numpy.float64,
numpy.complex64,
numpy.complex128,
]
)
def test_batched_decomposition(self, dtype):
# if not cusolver.check_availability("potrfBatched"):
# pytest.skip("potrfBatched is not available")
Ab1 = random_matrix((3, 5, 5), dtype, scale=(10, 10000), sym=True)
self.check_L(Ab1)
Ab2 = random_matrix((2, 2, 5, 5), dtype, scale=(10, 10000), sym=True)
self.check_L(Ab2)
@pytest.mark.parametrize(
"shape",
[
# empty square
(0, 0),
(3, 0, 0),
# empty batch
(2, 0, 3, 4, 4),
],
)
@testing.for_dtypes(
[
numpy.int32,
numpy.uint16,
numpy.float32,
numpy.float64,
numpy.complex64,
numpy.complex128,
]
)
@testing.numpy_cupy_allclose(type_check=has_support_aspect64())
def test_empty(self, shape, xp, dtype):
a = xp.empty(shape, dtype=dtype)
return xp.linalg.cholesky(a)
class TestCholeskyInvalid(unittest.TestCase):
def check_L(self, array):
for xp in (numpy, cupy):
a = xp.asarray(array)
with pytest.raises(xp.linalg.LinAlgError):
xp.linalg.cholesky(a)
@testing.for_dtypes(
[
numpy.int32,
numpy.int64,
numpy.uint32,
numpy.uint64,
numpy.float32,
numpy.float64,
]
)
def test_decomposition(self, dtype):
A = numpy.array([[1, -2], [-2, 1]]).astype(dtype)
self.check_L(A)
@testing.parameterize(
*testing.product(
{
"mode": ["r", "raw", "complete", "reduced"],
}
)
)
class TestQRDecomposition(unittest.TestCase):
def _gram(self, x, xp):
# Gram matrix: X^H @ X
return xp.conjugate(x).swapaxes(-1, -2) @ x
def _get_R_from_raw(self, h, m, n, xp):
# Get reduced R from NumPy-style raw QR:
# R = triu((tril(h))^T), shape (..., k, n)
k = min(m, n)
Rt = xp.tril(h)
R = xp.swapaxes(Rt, -1, -2)
R = xp.triu(R[..., :m, :n])
return R[..., :k, :]
@testing.for_dtypes("fdFD")
def check_mode(self, array, mode, dtype):
# if runtime.is_hip and driver.get_build_version() < 307:
# if dtype in (numpy.complex64, numpy.complex128):
# pytest.skip("ungqr unsupported")
a_cpu = numpy.asarray(array, dtype=dtype)
a_gpu = cupy.asarray(array, dtype=dtype)
result_gpu = cupy.linalg.qr(a_gpu, mode=mode)
if (
mode != "raw"
or numpy.lib.NumpyVersion(numpy.__version__) >= "1.22.0rc1"
):
result_cpu = numpy.linalg.qr(a_cpu, mode=mode)
self._check_result(result_cpu, result_gpu, a_cpu, a_gpu, mode)
# def _check_result(self, result_cpu, result_gpu):
# if isinstance(result_cpu, tuple):
# for b_cpu, b_gpu in zip(result_cpu, result_gpu):
# assert b_cpu.dtype == b_gpu.dtype
# testing.assert_allclose(b_cpu, b_gpu, atol=1e-4)
# else:
# assert result_cpu.dtype == result_gpu.dtype
# testing.assert_allclose(result_cpu, result_gpu, atol=1e-4)
# QR is not unique:
# element-wise comparison with NumPy may differ by sign/phase.
# To verify correctness use mode-dependent functional checks:
# complete/reduced: check decomposition Q @ R = A
# raw/r: check invariant R^H @ R = A^H @ A
def _check_result(self, result_cpu, result_gpu, a_cpu, a_gpu, mode):
if mode in ("complete", "reduced"):
q_gpu, r_gpu = result_gpu
testing.assert_allclose(q_gpu @ r_gpu, a_gpu, atol=1e-4)
elif mode == "raw":
h_gpu, tau_gpu = result_gpu
h_cpu, tau_cpu = result_cpu
m, n = a_gpu.shape[-2], a_gpu.shape[-1]
r_gpu = self._get_R_from_raw(h_gpu, m, n, cupy)
r_cpu = self._get_R_from_raw(h_cpu, m, n, numpy)
exp_gpu = self._gram(a_gpu, cupy)
exp_cpu = self._gram(a_cpu, numpy)
testing.assert_allclose(
self._gram(r_gpu, cupy), exp_gpu, atol=1e-4, rtol=1e-4
)
testing.assert_allclose(
self._gram(r_cpu, numpy), exp_cpu, atol=1e-4, rtol=1e-4
)
assert tau_gpu.shape == tau_cpu.shape
if not has_support_aspect64(tau_gpu.sycl_device):
if tau_cpu.dtype == numpy.float64:
tau_cpu = tau_cpu.astype("float32")
elif tau_cpu.dtype == numpy.complex128:
tau_cpu = tau_cpu.astype("complex64")
assert tau_gpu.dtype == tau_cpu.dtype
else: # mode == "r"
r_gpu = result_gpu
r_cpu = result_cpu
exp_gpu = self._gram(a_gpu, cupy)
exp_cpu = self._gram(a_cpu, numpy)
testing.assert_allclose(
self._gram(r_gpu, cupy), exp_gpu, atol=1e-4, rtol=1e-4
)
testing.assert_allclose(
self._gram(r_cpu, numpy), exp_cpu, atol=1e-4, rtol=1e-4
)
@testing.fix_random()
@_condition.repeat(3, 10)
def test_mode(self):
self.check_mode(numpy.random.randn(2, 4), mode=self.mode)
self.check_mode(numpy.random.randn(3, 3), mode=self.mode)
self.check_mode(numpy.random.randn(5, 4), mode=self.mode)
@testing.with_requires("numpy>=1.22")
@testing.fix_random()
def test_mode_rank3(self):
self.check_mode(numpy.random.randn(3, 2, 4), mode=self.mode)
self.check_mode(numpy.random.randn(4, 3, 3), mode=self.mode)
self.check_mode(numpy.random.randn(2, 5, 4), mode=self.mode)
@testing.with_requires("numpy>=1.22")
@testing.fix_random()
def test_mode_rank4(self):
self.check_mode(numpy.random.randn(2, 3, 2, 4), mode=self.mode)
self.check_mode(numpy.random.randn(2, 4, 3, 3), mode=self.mode)
self.check_mode(numpy.random.randn(2, 2, 5, 4), mode=self.mode)
@testing.with_requires("numpy>=1.16")
def test_empty_array(self):
self.check_mode(numpy.empty((0, 3)), mode=self.mode)
self.check_mode(numpy.empty((3, 0)), mode=self.mode)
@testing.with_requires("numpy>=1.22")
def test_empty_array_rank3(self):
self.check_mode(numpy.empty((0, 3, 2)), mode=self.mode)
self.check_mode(numpy.empty((3, 0, 2)), mode=self.mode)
self.check_mode(numpy.empty((3, 2, 0)), mode=self.mode)
self.check_mode(numpy.empty((0, 3, 3)), mode=self.mode)
self.check_mode(numpy.empty((3, 0, 3)), mode=self.mode)
self.check_mode(numpy.empty((3, 3, 0)), mode=self.mode)
self.check_mode(numpy.empty((0, 2, 3)), mode=self.mode)
self.check_mode(numpy.empty((2, 0, 3)), mode=self.mode)
self.check_mode(numpy.empty((2, 3, 0)), mode=self.mode)
@testing.parameterize(
*testing.product(
{
"full_matrices": [True, False],
}
)
)
@testing.fix_random()
class TestSVD(unittest.TestCase):
def setUp(self):
self.seed = testing.generate_seed()
@testing.for_dtypes(
[
numpy.int32,
numpy.int64,
numpy.uint32,
numpy.uint64,
numpy.float32,
numpy.float64,
numpy.complex64,
numpy.complex128,
]
)
def check_usv(self, shape, dtype):
array = testing.shaped_random(shape, numpy, dtype=dtype, seed=self.seed)
a_cpu = numpy.asarray(array, dtype=dtype)
a_gpu = cupy.asarray(array, dtype=dtype)
result_cpu = numpy.linalg.svd(a_cpu, full_matrices=self.full_matrices)
result_gpu = cupy.linalg.svd(a_gpu, full_matrices=self.full_matrices)
# Check if the input matrix is not broken
testing.assert_allclose(a_gpu, a_cpu)
assert len(result_gpu) == 3
for i in range(3):
assert result_gpu[i].shape == result_cpu[i].shape
if has_support_aspect64():
assert result_gpu[i].dtype == result_cpu[i].dtype
else:
assert result_gpu[i].dtype.kind == result_cpu[i].dtype.kind
u_cpu, s_cpu, vh_cpu = result_cpu
u_gpu, s_gpu, vh_gpu = result_gpu
testing.assert_allclose(s_gpu, s_cpu, rtol=1e-5, atol=1e-4)
# reconstruct the matrix
k = s_cpu.shape[-1]
if len(shape) == 2:
if self.full_matrices:
a_gpu_usv = cupy.dot(u_gpu[:, :k] * s_gpu, vh_gpu[:k, :])
else:
a_gpu_usv = cupy.dot(u_gpu * s_gpu, vh_gpu)
else:
if self.full_matrices:
a_gpu_usv = cupy.matmul(
u_gpu[..., :k] * s_gpu[..., None, :], vh_gpu[..., :k, :]
)
else:
a_gpu_usv = cupy.matmul(u_gpu * s_gpu[..., None, :], vh_gpu)
testing.assert_allclose(a_gpu, a_gpu_usv, rtol=1e-4, atol=1e-4)
# assert unitary
u_len = u_gpu.shape[-1]
vh_len = vh_gpu.shape[-2]
testing.assert_allclose(
cupy.matmul(u_gpu.swapaxes(-1, -2).conj(), u_gpu),
stacked_identity(cupy, shape[:-2], u_len, dtype),
atol=1e-4,
)
testing.assert_allclose(
cupy.matmul(vh_gpu, vh_gpu.swapaxes(-1, -2).conj()),
stacked_identity(cupy, shape[:-2], vh_len, dtype),
atol=1e-4,
)
@testing.for_dtypes(
[
numpy.int32,
numpy.int64,
numpy.uint32,
numpy.uint64,
numpy.float32,
numpy.float64,
numpy.complex64,
numpy.complex128,
]
)
@testing.numpy_cupy_allclose(
rtol=1e-5, atol=1e-4, type_check=has_support_aspect64()
)
def check_singular(self, shape, xp, dtype):
array = testing.shaped_random(shape, xp, dtype=dtype, seed=self.seed)
a = xp.asarray(array, dtype=dtype)
a_copy = a.copy()
result = xp.linalg.svd(
a, full_matrices=self.full_matrices, compute_uv=False
)
# Check if the input matrix is not broken
assert (a == a_copy).all()
return result
@_condition.repeat(3, 10)
def test_svd_rank2(self):
self.check_usv((3, 7))
self.check_usv((2, 2))
self.check_usv((7, 3))
@_condition.repeat(3, 10)
def test_svd_rank2_no_uv(self):
self.check_singular((3, 7))
self.check_singular((2, 2))
self.check_singular((7, 3))
@testing.with_requires("numpy>=1.16")
def test_svd_rank2_empty_array(self):
self.check_usv((0, 3))
self.check_usv((3, 0))
self.check_usv((1, 0))
@testing.with_requires("numpy>=1.16")
@testing.numpy_cupy_array_equal(type_check=has_support_aspect64())
def test_svd_rank2_empty_array_compute_uv_false(self, xp):
array = xp.empty((3, 0))
return xp.linalg.svd(
array, full_matrices=self.full_matrices, compute_uv=False
)
@_condition.repeat(3, 10)
def test_svd_rank3(self):
self.check_usv((2, 3, 4))
self.check_usv((2, 3, 7))
self.check_usv((2, 4, 4))
self.check_usv((2, 7, 3))
self.check_usv((2, 4, 3))
self.check_usv((2, 32, 32)) # still use _gesvdj_batched
@_condition.repeat(3, 10)
def test_svd_rank3_loop(self):
# This tests the loop-based batched gesvd on CUDA (_gesvd_batched)
self.check_usv((2, 64, 64))
self.check_usv((2, 64, 32))
self.check_usv((2, 32, 64))
@_condition.repeat(3, 10)
def test_svd_rank3_no_uv(self):
self.check_singular((2, 3, 4))
self.check_singular((2, 3, 7))
self.check_singular((2, 4, 4))
self.check_singular((2, 7, 3))
self.check_singular((2, 4, 3))
@_condition.repeat(3, 10)
def test_svd_rank3_no_uv_loop(self):
# This tests the loop-based batched gesvd on CUDA (_gesvd_batched)
self.check_singular((2, 64, 64))
self.check_singular((2, 64, 32))
self.check_singular((2, 32, 64))
@testing.with_requires("numpy>=1.16")
def test_svd_rank3_empty_array(self):
self.check_usv((0, 3, 4))
self.check_usv((3, 0, 4))
self.check_usv((3, 4, 0))
self.check_usv((3, 0, 0))
self.check_usv((0, 3, 0))
self.check_usv((0, 0, 3))
@testing.with_requires("numpy>=1.16")
@testing.numpy_cupy_array_equal(type_check=has_support_aspect64())
def test_svd_rank3_empty_array_compute_uv_false1(self, xp):
array = xp.empty((3, 0, 4))
return xp.linalg.svd(
array, full_matrices=self.full_matrices, compute_uv=False
)
@testing.with_requires("numpy>=1.16")
@testing.numpy_cupy_array_equal(type_check=has_support_aspect64())
def test_svd_rank3_empty_array_compute_uv_false2(self, xp):
array = xp.empty((0, 3, 4))
return xp.linalg.svd(
array, full_matrices=self.full_matrices, compute_uv=False
)
@_condition.repeat(3, 10)
def test_svd_rank4(self):
self.check_usv((2, 2, 3, 4))
self.check_usv((2, 2, 3, 7))
self.check_usv((2, 2, 4, 4))
self.check_usv((2, 2, 7, 3))
self.check_usv((2, 2, 4, 3))
self.check_usv((2, 2, 32, 32)) # still use _gesvdj_batched
@_condition.repeat(3, 10)
def test_svd_rank4_loop(self):
# This tests the loop-based batched gesvd on CUDA (_gesvd_batched)
self.check_usv((3, 2, 64, 64))
self.check_usv((3, 2, 64, 32))
self.check_usv((3, 2, 32, 64))
@_condition.repeat(3, 10)
def test_svd_rank4_no_uv(self):
self.check_singular((2, 2, 3, 4))
self.check_singular((2, 2, 3, 7))
self.check_singular((2, 2, 4, 4))
self.check_singular((2, 2, 7, 3))
self.check_singular((2, 2, 4, 3))
@_condition.repeat(3, 10)
def test_svd_rank4_no_uv_loop(self):
# This tests the loop-based batched gesvd on CUDA (_gesvd_batched)
self.check_singular((3, 2, 64, 64))
self.check_singular((3, 2, 64, 32))
self.check_singular((3, 2, 32, 64))
@testing.with_requires("numpy>=1.16")
def test_svd_rank4_empty_array(self):
self.check_usv((0, 2, 3, 4))
self.check_usv((1, 2, 0, 4))
self.check_usv((1, 2, 3, 0))