def test_invh(self): if not cusolver.check_availability('potrsBatched'): pytest.skip('potrsBatched is not available') a = self._create_symmetric_matrix(self.shape, self.dtype) with cupyx.errstate(linalg='ignore'): with self.assertRaises(cupy.cuda.cusolver.CUSOLVERError): cupyx.linalg.solve._batched_invh(a)
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)
def test_invh(self): if not cusolver.check_availability('potrsBatched'): pytest.skip('potrsBatched is not available') a = self._create_symmetric_matrix(self.shape, self.dtype) with cupyx.errstate(linalg='raise'): with self.assertRaises(numpy.linalg.LinAlgError): cupyx.linalg.invh(a)
def setUp(self): if not cusolver.check_availability('syevj'): pytest.skip('syevj is not available') if self.dtype in (numpy.complex64, numpy.complex128): self.a = numpy.array([[1, 2j, 3], [-2j, 5, 4j], [3, -4j, 9]], dtype=self.dtype) else: self.a = numpy.array([[1, 2, 3], [2, 5, 4], [3, 4, 9]], dtype=self.dtype)
def setUp(self): if not cusolver.check_availability('gels'): pytest.skip('gels is not available') if self.compute_type is not None: old_compute_type = _linalg.get_compute_type(self.dtype) _linalg.set_compute_type(self.dtype, self.compute_type) yield _linalg.set_compute_type(self.dtype, old_compute_type) else: yield
def setUp(self): if not cusolver.check_availability('gesvda'): pytest.skip('gesvda is not available') if self.dtype == numpy.complex64: a_real = numpy.random.random(self.shape).astype(numpy.float32) a_imag = numpy.random.random(self.shape).astype(numpy.float32) self.a = a_real + 1.j * a_imag elif self.dtype == numpy.complex128: a_real = numpy.random.random(self.shape).astype(numpy.float64) a_imag = numpy.random.random(self.shape).astype(numpy.float64) self.a = a_real + 1.j * a_imag else: self.a = numpy.random.random(self.shape).astype(self.dtype)
def test_csrlsvqr(self, dtype): if not cusolver.check_availability('csrlsvqr'): unittest.SkipTest('csrlsvqr is not available') a, b, test_tol = self._setup(dtype) ref_x = numpy.linalg.solve(a, b) cp_a = cupy.array(a) sp_a = cupyx.scipy.sparse.csr_matrix(cp_a) cp_b = cupy.array(b) x = cupy.cusolver.csrlsvqr(sp_a, cp_b, tol=self.tol, reorder=self.reorder) cupy.testing.assert_allclose(x, ref_x, rtol=test_tol, atol=test_tol)
def _potrf_batched(a): """Batched Cholesky decomposition. Decompose a given array of two-dimensional square matrices into ``L * L.T``, where ``L`` is a lower-triangular matrix and ``.T`` is a conjugate transpose operator. Args: a (cupy.ndarray): The input array of matrices with dimension ``(..., N, N)`` Returns: cupy.ndarray: The lower-triangular matrix. """ if not check_availability('potrfBatched'): raise RuntimeError('potrfBatched is not available') if a.dtype.char == 'f' or a.dtype.char == 'd': dtype = a.dtype.char else: dtype = numpy.promote_types(a.dtype.char, 'f').char if dtype == 'f': potrfBatched = cusolver.spotrfBatched elif dtype == 'd': potrfBatched = cusolver.dpotrfBatched elif dtype == 'F': potrfBatched = cusolver.cpotrfBatched else: # dtype == 'D': potrfBatched = cusolver.zpotrfBatched x = a.astype(dtype, order='C', copy=True) xp = cupy.core._mat_ptrs(x) n = x.shape[-1] ldx = x.strides[-2] // x.dtype.itemsize handle = device.get_cusolver_handle() batch_size = internal.prod(x.shape[:-2]) dev_info = cupy.empty(batch_size, dtype=numpy.int32) potrfBatched( handle, cublas.CUBLAS_FILL_MODE_UPPER, n, xp.data.ptr, ldx, dev_info.data.ptr, batch_size) cupy.linalg._util._check_cusolver_dev_info_if_synchronization_allowed( potrfBatched, dev_info) return cupy.tril(x)
def setUp(self): if not cusolver.check_availability('gesv'): pytest.skip('gesv is not available') self.dtype = numpy.dtype(self.dtype) self.r_dtype = self.dtype.char.lower() a = self._make_well_conditioned_matrix((self.n, self.n)) if self.nrhs is None: x_shape = (self.n, ) else: x_shape = (self.n, self.nrhs) self.x_ref = self._make_random_matrix(x_shape, cupy) b = numpy.dot(a, self.x_ref) self.tol = self._tol[self.r_dtype] self.a = cupy.array(a) self.b = cupy.array(b) if self.compute_type is not None: self.old_compute_type = _linalg.get_compute_type(self.dtype) _linalg.set_compute_type(self.dtype, self.compute_type)
def setUp(self): if not cusolver.check_availability('csrlsvqr'): pytest.skip('csrlsvqr is not available')
def _batched_invh(a): """Compute the inverse of an array of Hermitian matrices. This function computes an inverse of a real symmetric or complex hermitian positive-definite matrix using Cholesky factorization. If matrix ``a[i]`` is not positive definite, Cholesky factorization fails and it raises an error. Args: a (cupy.ndarray): Array of real symmetric or complex hermitian matrices with dimension (..., N, N). Returns: cupy.ndarray: The array of inverses of matrices ``a[i]``. """ if not check_availability('potrsBatched'): raise RuntimeError('potrsBatched is not available') if a.dtype.char == 'f' or a.dtype.char == 'd': dtype = a.dtype.char else: dtype = numpy.promote_types(a.dtype.char, 'f').char if dtype == 'f': potrfBatched = cusolver.spotrfBatched potrsBatched = cusolver.spotrsBatched elif dtype == 'd': potrfBatched = cusolver.dpotrfBatched potrsBatched = cusolver.dpotrsBatched elif dtype == 'F': potrfBatched = cusolver.cpotrfBatched potrsBatched = cusolver.cpotrsBatched elif dtype == 'D': potrfBatched = cusolver.zpotrfBatched potrsBatched = cusolver.zpotrsBatched else: msg = ('dtype must be float32, float64, complex64 or complex128' ' (actual: {})'.format(a.dtype)) raise ValueError(msg) a = a.astype(dtype, order='C', copy=True) ap = cupy.core.core._mat_ptrs(a) n = a.shape[-1] lda = a.strides[-2] // a.dtype.itemsize handle = device.get_cusolver_handle() uplo = cublas.CUBLAS_FILL_MODE_LOWER batch_size = int(numpy.prod(a.shape[:-2])) dev_info = cupy.empty(batch_size, dtype=numpy.int32) # Cholesky factorization potrfBatched(handle, uplo, n, ap.data.ptr, lda, dev_info.data.ptr, batch_size) cupy.linalg.util._check_cusolver_dev_info_if_synchronization_allowed( potrfBatched, dev_info) identity_matrix = cupy.eye(n, dtype=dtype) b = cupy.empty(a.shape, dtype) b[...] = identity_matrix nrhs = b.shape[-1] ldb = b.strides[-2] // a.dtype.itemsize bp = cupy.core.core._mat_ptrs(b) dev_info = cupy.empty(1, dtype=numpy.int32) # NOTE: potrsBatched does not currently support nrhs > 1 (CUDA v10.2) # Solve: A[i] * X[i] = B[i] potrsBatched(handle, uplo, n, nrhs, ap.data.ptr, lda, bp.data.ptr, ldb, dev_info.data.ptr, batch_size) cupy.linalg.util._check_cusolver_dev_info_if_synchronization_allowed( potrfBatched, dev_info) return b