def test_offsets(self): if (runtime.is_hip and self.dtype == numpy.float32 and driver.get_build_version() == 400): pytest.xfail('generated wrong result -- may be buggy?') assert self.m.offsets.dtype == numpy.int32 testing.assert_array_equal(self.m.offsets, cupy.array([0, -1], self.dtype))
class TestMemPool: @pytest.mark.skipif(runtime.is_hip, reason='HIP does not support async allocator') @pytest.mark.skipif(driver._is_cuda_python() and runtime.runtimeGetVersion() < 11020, reason='cudaMemPool_t is supported since CUDA 11.2') @pytest.mark.skipif(not driver._is_cuda_python() and driver.get_build_version() < 11020, reason='cudaMemPool_t is supported since CUDA 11.2') @pytest.mark.skipif(runtime.deviceGetAttribute( runtime.cudaDevAttrMemoryPoolsSupported, 0) == 0, reason='cudaMemPool_t is not supported on device 0') def test_mallocFromPoolAsync(self): # also test create/destroy a pool props = runtime.MemPoolProps(runtime.cudaMemAllocationTypePinned, runtime.cudaMemHandleTypeNone, runtime.cudaMemLocationTypeDevice, 0) # on device 0 pool = runtime.memPoolCreate(props) assert pool > 0 s = cupy.cuda.Stream() ptr = runtime.mallocFromPoolAsync(128, pool, s.ptr) assert ptr > 0 runtime.freeAsync(ptr, s.ptr) runtime.memPoolDestroy(pool)
def setUp(self): if runtime.is_hip and self.axis in (0, -2): HIP_version = driver.get_build_version() if HIP_version < 5_00_00000: # internally a temporary CSC matrix is generated and thus # casues problems (see test_csc.py) pytest.xfail('spmv is buggy (trans=True)')
def check_mode(self, array, mode, dtype, batched=False): 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 ((not batched) or (numpy.lib.NumpyVersion(numpy.__version__) >= '1.22.0')): result_cpu = numpy.linalg.qr(a_cpu, mode=mode) self._check_result(result_cpu, result_gpu) else: # We still want to test it to gain confidence... # TODO(leofang): Use @testing.with_requires('numpy>=1.22') once # NumPy 1.22 is out, and clean up this helper function batch_shape = a_cpu.shape[:-2] batch_size = prod(batch_shape) a_cpu = a_cpu.reshape(batch_size, *a_cpu.shape[-2:]) for i in range(batch_size): res_cpu = numpy.linalg.qr(a_cpu[i], mode=mode) if isinstance(result_gpu, tuple): q_gpu, r_gpu = result_gpu q_gpu = q_gpu.reshape(batch_size, *q_gpu.shape[-2:]) idx = -1 if mode == 'raw' else -2 r_gpu = r_gpu.reshape(batch_size, *r_gpu.shape[idx:]) res_gpu = (q_gpu[i], r_gpu[i]) self._check_result(res_cpu, res_gpu) else: # mode == 'r' res_gpu = result_gpu.reshape( batch_size, *result_gpu.shape[-2:])[i] self._check_result(res_cpu, res_gpu)
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)
def test_csrgemm2_abpd(self): if not cupy.cusparse.check_availability('csrgemm2'): pytest.skip('csrgemm2 is not available.') if runtime.is_hip and driver.get_build_version() < 402: pytest.xfail('csrgemm2 is buggy') a = sparse.csr_matrix(self.a) b = sparse.csr_matrix(self.b) d = sparse.csr_matrix(self.d) c = cupy.cusparse.csrgemm2(a, b, d=d, alpha=self.alpha, beta=self.beta) expect = self.alpha * self.a.dot(self.b) + self.beta * self.d testing.assert_array_almost_equal(c.toarray(), expect.toarray())
class TestIX_(unittest.TestCase): @testing.numpy_cupy_array_equal() def test_ix_list(self, xp): return xp.ix_([0, 1], [2, 4]) @pytest.mark.xfail(runtime.is_hip and driver.get_build_version() < 402, reason='HIP may have a bug') @testing.for_all_dtypes() @testing.numpy_cupy_array_equal() def test_ix_ndarray(self, xp, dtype): return xp.ix_(xp.array([0, 1], dtype), xp.array([2, 3], dtype)) @testing.numpy_cupy_array_equal(type_check=False) def test_ix_empty_ndarray(self, xp): return xp.ix_(xp.array([])) @testing.numpy_cupy_array_equal() def test_ix_bool_ndarray(self, xp): return xp.ix_(xp.array([True, False] * 2))
def test_csrsm2(self, dtype): if not cusparse.check_availability('csrsm2'): pytest.skip('csrsm2 is not available') if runtime.is_hip: if (self.transa == 'H' or (driver.get_build_version() < 400 and ((self.format == 'csc' and self.transa == 'N') or (self.format == 'csr' and self.transa == 'T')))): pytest.xfail('may be buggy') if (self.format == 'csc' and numpy.dtype(dtype).char in 'FD' and self.transa == 'H'): pytest.skip('unsupported combination') self._setup(dtype) x = self.b.copy(order=self.order) cusparse.csrsm2(self.a, x, alpha=self.alpha, lower=self.lower, unit_diag=self.unit_diag, transa=self.transa, blocking=self.blocking, level_info=self.level_info) testing.assert_allclose(x, self.ref_x, atol=self.tol, rtol=self.tol)
from cupy.cuda import compiler # NOQA from cupy.cuda import device # NOQA from cupy.cuda import driver # NOQA from cupy.cuda import function # NOQA from cupy.cuda import memory # NOQA from cupy.cuda import memory_hook # NOQA from cupy.cuda import memory_hooks # NOQA from cupy.cuda import pinned_memory # NOQA from cupy.cuda import profiler # NOQA from cupy.cuda import runtime # NOQA from cupy.cuda import stream # NOQA _available = None _cuda_path = None if driver.get_build_version() >= 8000: from cupy.cuda import cusolver # NOQA cusolver_enabled = True else: cusolver_enabled = False try: from cupy.cuda import nvtx # NOQA nvtx_enabled = True except ImportError: nvtx_enabled = False try: from cupy.cuda import thrust # NOQA thrust_enabled = True except ImportError:
class TestCooMatrixScipyComparison: @property def make(self): return globals()[self.make_method] @testing.numpy_cupy_equal(sp_name='sp') def test_dtype(self, xp, sp): m = self.make(xp, sp, self.dtype) return m.dtype @testing.numpy_cupy_equal(sp_name='sp') def test_nnz(self, xp, sp): m = self.make(xp, sp, self.dtype) return m.getnnz() @testing.numpy_cupy_array_equal(sp_name='sp') def test_asfptype(self, xp, sp): m = _make(xp, sp, self.dtype) return m.asfptype() @testing.numpy_cupy_allclose(sp_name='sp') def test_toarray(self, xp, sp): m = self.make(xp, sp, self.dtype) return m.toarray() @testing.numpy_cupy_allclose(sp_name='sp') def test_A(self, xp, sp): m = self.make(xp, sp, self.dtype) return m.A @testing.numpy_cupy_allclose(sp_name='sp') def test_tocoo(self, xp, sp): m = _make(xp, sp, self.dtype) return m.tocoo() @testing.numpy_cupy_allclose(sp_name='sp') def test_tocoo_copy(self, xp, sp): m = _make(xp, sp, self.dtype) n = m.tocoo(copy=True) assert m.data is not n.data assert m.row is not n.row assert m.col is not n.col return n @testing.numpy_cupy_allclose(sp_name='sp') def test_tocsc(self, xp, sp): m = self.make(xp, sp, self.dtype) out = m.tocsc() assert out.has_canonical_format return out @testing.numpy_cupy_allclose(sp_name='sp') def test_tocsc_copy(self, xp, sp): m = _make(xp, sp, self.dtype) n = m.tocsc(copy=True) assert m.data is not n.data assert n.has_canonical_format return n @testing.numpy_cupy_allclose(sp_name='sp') def test_tocsr(self, xp, sp): m = self.make(xp, sp, self.dtype) out = m.tocsr() assert out.has_canonical_format return out @testing.numpy_cupy_allclose(sp_name='sp') def test_tocsr_copy(self, xp, sp): m = _make(xp, sp, self.dtype) n = m.tocsr(copy=True) assert m.data is not n.data assert n.has_canonical_format return n # dot @testing.numpy_cupy_allclose(sp_name='sp', _check_sparse_format=False) def test_dot_scalar(self, xp, sp): m = _make(xp, sp, self.dtype) return m.dot(2.0) @testing.numpy_cupy_allclose(sp_name='sp', _check_sparse_format=False) def test_dot_numpy_scalar(self, xp, sp): m = _make(xp, sp, self.dtype) return m.dot(numpy.dtype(self.dtype).type(2.0)) @testing.numpy_cupy_allclose(sp_name='sp') def test_dot_csr(self, xp, sp): m = _make(xp, sp, self.dtype) x = _make3(xp, sp, self.dtype) return m.dot(x) def test_dot_csr_invalid_shape(self): for xp, sp in ((numpy, scipy.sparse), (cupy, sparse)): m = _make(xp, sp, self.dtype) x = sp.csr_matrix((5, 3), dtype=self.dtype) with pytest.raises(ValueError): m.dot(x) @pytest.mark.skipif(runtime.is_hip and driver.get_build_version() < 400, reason='no working implementation') @testing.numpy_cupy_allclose(sp_name='sp') def test_dot_csc(self, xp, sp): m = _make(xp, sp, self.dtype) x = _make3(xp, sp, self.dtype).tocsc() return m.dot(x) @testing.numpy_cupy_allclose(sp_name='sp') def test_dot_sparse(self, xp, sp): m = _make(xp, sp, self.dtype) x = _make3(xp, sp, self.dtype).tocoo() return m.dot(x) @testing.numpy_cupy_allclose(sp_name='sp', _check_sparse_format=False) def test_dot_zero_dim(self, xp, sp): m = _make(xp, sp, self.dtype) x = xp.array(2, dtype=self.dtype) return m.dot(x) @testing.numpy_cupy_allclose(sp_name='sp') def test_dot_dense_vector(self, xp, sp): m = _make(xp, sp, self.dtype) x = xp.arange(4).astype(self.dtype) return m.dot(x) def test_dot_dense_vector_invalid_shape(self): for xp, sp in ((numpy, scipy.sparse), (cupy, sparse)): m = _make(xp, sp, self.dtype) x = xp.arange(5).astype(self.dtype) with pytest.raises(ValueError): m.dot(x) @testing.numpy_cupy_allclose(sp_name='sp') def test_dot_dense_matrix(self, xp, sp): m = _make(xp, sp, self.dtype) x = xp.arange(8).reshape(4, 2).astype(self.dtype) return m.dot(x) def test_dot_dense_matrix_invalid_shape(self): for xp, sp in ((numpy, scipy.sparse), (cupy, sparse)): m = _make(xp, sp, self.dtype) x = xp.arange(10).reshape(5, 2).astype(self.dtype) with pytest.raises(ValueError): m.dot(x) def test_dot_dense_ndim3(self): for xp, sp in ((numpy, scipy.sparse), (cupy, sparse)): m = _make(xp, sp, self.dtype) x = xp.arange(24).reshape(4, 2, 3).astype(self.dtype) with pytest.raises(ValueError): m.dot(x) def test_dot_unsupported(self): for xp, sp in ((numpy, scipy.sparse), (cupy, sparse)): m = _make(xp, sp, self.dtype) with pytest.raises(TypeError): m.dot(None) # __add__ @testing.numpy_cupy_allclose(sp_name='sp', _check_sparse_format=False) def test_add_zero(self, xp, sp): m = _make(xp, sp, self.dtype) return m + 0 def test_add_scalar(self): for xp, sp in ((numpy, scipy.sparse), (cupy, sparse)): m = _make(xp, sp, self.dtype) with pytest.raises(NotImplementedError): m + 1 @testing.numpy_cupy_allclose(sp_name='sp') def test_add_csr(self, xp, sp): m = _make(xp, sp, self.dtype) n = _make2(xp, sp, self.dtype) return m + n @testing.numpy_cupy_allclose(sp_name='sp') def test_add_coo(self, xp, sp): m = _make(xp, sp, self.dtype) n = _make2(xp, sp, self.dtype).tocoo() return m + n @testing.numpy_cupy_allclose(sp_name='sp') def test_add_dense(self, xp, sp): m = _make(xp, sp, self.dtype) n = xp.arange(12).reshape(3, 4) return m + n # __radd__ @testing.numpy_cupy_allclose(sp_name='sp', _check_sparse_format=False) def test_radd_zero(self, xp, sp): m = _make(xp, sp, self.dtype) return 0 + m def test_radd_scalar(self): for xp, sp in ((numpy, scipy.sparse), (cupy, sparse)): m = _make(xp, sp, self.dtype) with pytest.raises(NotImplementedError): 1 + m @testing.numpy_cupy_allclose(sp_name='sp') def test_radd_dense(self, xp, sp): m = _make(xp, sp, self.dtype) n = xp.arange(12).reshape(3, 4) return n + m # __sub__ @testing.numpy_cupy_allclose(sp_name='sp', _check_sparse_format=False) def test_sub_zero(self, xp, sp): m = _make(xp, sp, self.dtype) return m - 0 def test_sub_scalar(self): for xp, sp in ((numpy, scipy.sparse), (cupy, sparse)): m = _make(xp, sp, self.dtype) with pytest.raises(NotImplementedError): m - 1 @testing.numpy_cupy_allclose(sp_name='sp') def test_sub_csr(self, xp, sp): m = _make(xp, sp, self.dtype) n = _make2(xp, sp, self.dtype) return m - n @testing.numpy_cupy_allclose(sp_name='sp') def test_sub_coo(self, xp, sp): m = _make(xp, sp, self.dtype) n = _make2(xp, sp, self.dtype).tocoo() return m - n @testing.numpy_cupy_allclose(sp_name='sp') def test_sub_dense(self, xp, sp): m = _make(xp, sp, self.dtype) n = xp.arange(12).reshape(3, 4) return m - n # __rsub__ @testing.numpy_cupy_allclose(sp_name='sp', _check_sparse_format=False) def test_rsub_zero(self, xp, sp): m = _make(xp, sp, self.dtype) return 0 - m def test_rsub_scalar(self): for xp, sp in ((numpy, scipy.sparse), (cupy, sparse)): m = _make(xp, sp, self.dtype) with pytest.raises(NotImplementedError): 1 - m @testing.numpy_cupy_allclose(sp_name='sp') def test_rsub_dense(self, xp, sp): m = _make(xp, sp, self.dtype) n = xp.arange(12).reshape(3, 4) return n - m # __mul__ @testing.numpy_cupy_allclose(sp_name='sp', _check_sparse_format=False) def test_mul_scalar(self, xp, sp): m = _make(xp, sp, self.dtype) return m * 2.0 @testing.numpy_cupy_allclose(sp_name='sp', _check_sparse_format=False) def test_mul_numpy_scalar(self, xp, sp): m = _make(xp, sp, self.dtype) return m * numpy.dtype(self.dtype).type(2.0) @testing.numpy_cupy_allclose(sp_name='sp') def test_mul_csr(self, xp, sp): m = _make(xp, sp, self.dtype) x = _make3(xp, sp, self.dtype) return m * x def test_mul_csr_invalid_shape(self): for xp, sp in ((numpy, scipy.sparse), (cupy, sparse)): m = _make(xp, sp, self.dtype) x = sp.csr_matrix((5, 3), dtype=self.dtype) with pytest.raises(ValueError): m * x @pytest.mark.skipif(runtime.is_hip and driver.get_build_version() < 400, reason='no working implementation') @testing.numpy_cupy_allclose(sp_name='sp') def test_mul_csc(self, xp, sp): m = _make(xp, sp, self.dtype) x = _make3(xp, sp, self.dtype).tocsc() return m * x @testing.numpy_cupy_allclose(sp_name='sp') def test_mul_sparse(self, xp, sp): m = _make(xp, sp, self.dtype) x = _make3(xp, sp, self.dtype).tocoo() return m * x @testing.numpy_cupy_allclose(sp_name='sp', _check_sparse_format=False) def test_mul_zero_dim(self, xp, sp): m = _make(xp, sp, self.dtype) x = xp.array(2, dtype=self.dtype) return m * x @testing.numpy_cupy_allclose(sp_name='sp') def test_mul_dense_vector(self, xp, sp): m = _make(xp, sp, self.dtype) x = xp.arange(4).astype(self.dtype) return m * x def test_mul_dense_vector_invalid_shape(self): for xp, sp in ((numpy, scipy.sparse), (cupy, sparse)): m = _make(xp, sp, self.dtype) x = xp.arange(5).astype(self.dtype) with pytest.raises(ValueError): m * x @testing.numpy_cupy_allclose(sp_name='sp') def test_mul_dense_matrix(self, xp, sp): m = _make(xp, sp, self.dtype) x = xp.arange(8).reshape(4, 2).astype(self.dtype) return m * x def test_mul_dense_matrix_invalid_shape(self): for xp, sp in ((numpy, scipy.sparse), (cupy, sparse)): m = _make(xp, sp, self.dtype) x = xp.arange(10).reshape(5, 2).astype(self.dtype) with pytest.raises(ValueError): m * x def test_mul_dense_ndim3(self): for xp, sp in ((numpy, scipy.sparse), (cupy, sparse)): m = _make(xp, sp, self.dtype) x = xp.arange(24).reshape(4, 2, 3).astype(self.dtype) with pytest.raises(ValueError): m * x def test_mul_unsupported(self): for xp, sp in ((numpy, scipy.sparse), (cupy, sparse)): m = _make(xp, sp, self.dtype) with pytest.raises(TypeError): m * None # __rmul__ @testing.numpy_cupy_allclose(sp_name='sp', _check_sparse_format=False) def test_rmul_scalar(self, xp, sp): m = _make(xp, sp, self.dtype) return 2.0 * m @testing.numpy_cupy_allclose(sp_name='sp', _check_sparse_format=False) def test_rmul_numpy_scalar(self, xp, sp): m = _make(xp, sp, self.dtype) return numpy.dtype(self.dtype).type(2.0) * m @testing.numpy_cupy_allclose(sp_name='sp') def test_rmul_csr(self, xp, sp): m = _make(xp, sp, self.dtype) x = _make3(xp, sp, self.dtype) return x * m @pytest.mark.skipif(runtime.is_hip and driver.get_build_version() < 400, reason='no working implementation') @testing.numpy_cupy_allclose(sp_name='sp', _check_sparse_format=False) def test_rmul_csc(self, xp, sp): m = _make(xp, sp, self.dtype) x = _make3(xp, sp, self.dtype).tocsc() return x * m @testing.numpy_cupy_allclose(sp_name='sp') def test_rmul_sparse(self, xp, sp): m = _make(xp, sp, self.dtype) x = _make3(xp, sp, self.dtype).tocoo() return x * m @testing.numpy_cupy_allclose(sp_name='sp', _check_sparse_format=False) def test_rmul_zero_dim(self, xp, sp): m = _make(xp, sp, self.dtype) x = xp.array(2, dtype=self.dtype) return x * m @testing.numpy_cupy_allclose(sp_name='sp') def test_rmul_dense_matrix(self, xp, sp): m = _make(xp, sp, self.dtype) x = xp.arange(12).reshape(4, 3).astype(self.dtype) return x * m def test_rmul_dense_ndim3(self): for xp, sp in ((numpy, scipy.sparse), (cupy, sparse)): m = _make(xp, sp, self.dtype) x = xp.arange(24).reshape(4, 2, 3).astype(self.dtype) with pytest.raises(ValueError): x * m def test_rmul_unsupported(self): for xp, sp in ((numpy, scipy.sparse), (cupy, sparse)): m = _make(xp, sp, self.dtype) with pytest.raises(TypeError): None * m # Note: '@' operator is almost equivalent to '*' operator. Only test the # cases where '@' raises an exception and '*' does not. def test_matmul_scalar(self): for xp, sp in ((numpy, scipy.sparse), (cupy, sparse)): m = self.make(xp, sp, self.dtype) x = 2.0 with pytest.raises(ValueError): m @ x with pytest.raises(ValueError): x @ m def test_matmul_numpy_scalar(self): for xp, sp in ((numpy, scipy.sparse), (cupy, sparse)): m = self.make(xp, sp, self.dtype) x = numpy.dtype(self.dtype).type(2.0) with pytest.raises(ValueError): m @ x with pytest.raises(ValueError): x @ m def test_matmul_scalar_like_array(self): for xp, sp in ((numpy, scipy.sparse), (cupy, sparse)): m = self.make(xp, sp, self.dtype) x = xp.array(2.0, self.dtype) with pytest.raises(ValueError): m @ x with pytest.raises(ValueError): x @ m # __pow__ @testing.numpy_cupy_allclose(sp_name='sp', _check_sparse_format=False) def test_pow_0(self, xp, sp): m = _make_square(xp, sp, self.dtype) return m ** 0 @testing.numpy_cupy_allclose(sp_name='sp') def test_pow_1(self, xp, sp): m = _make_square(xp, sp, self.dtype) return m ** 1 @testing.numpy_cupy_allclose(sp_name='sp') def test_pow_2(self, xp, sp): m = _make_square(xp, sp, self.dtype) return m ** 2 @testing.numpy_cupy_allclose(sp_name='sp') def test_pow_3(self, xp, sp): m = _make_square(xp, sp, self.dtype) return m ** 3 def test_pow_neg(self): for xp, sp in ((numpy, scipy.sparse), (cupy, sparse)): m = _make_square(xp, sp, self.dtype) with pytest.raises(ValueError): m ** -1 def test_sum_tuple_axis(self): for xp, sp in ((numpy, scipy.sparse), (cupy, sparse)): m = _make(xp, sp, self.dtype) with pytest.raises(TypeError): m.sum(axis=(0, 1)) def test_sum_float_axis(self): for xp, sp in ((numpy, scipy.sparse), (cupy, sparse)): m = _make(xp, sp, self.dtype) with pytest.raises(TypeError): m.sum(axis=0.0) def test_sum_too_large_axis(self): for xp, sp in ((numpy, scipy.sparse), (cupy, sparse)): m = _make(xp, sp, self.dtype) with pytest.raises(ValueError): m.sum(axis=3) @testing.numpy_cupy_allclose(sp_name='sp') def test_transpose(self, xp, sp): m = self.make(xp, sp, self.dtype) return m.transpose() def test_transpose_axes_int(self): for xp, sp in ((numpy, scipy.sparse), (cupy, sparse)): m = _make(xp, sp, self.dtype) with pytest.raises(ValueError): m.transpose(axes=0) @testing.numpy_cupy_equal(sp_name='sp') def test_eliminate_zeros(self, xp, sp): m = self.make(xp, sp, self.dtype) m.eliminate_zeros() return m.nnz
class TestPoly1d: @testing.for_all_dtypes() @testing.numpy_cupy_array_equal() def test_poly1d_leading_zeros(self, xp, dtype): a = xp.array([0, 0, 1, 2, 3], dtype) return xp.poly1d(a) @testing.for_all_dtypes(no_bool=True) @testing.numpy_cupy_array_equal() def test_poly1d_neg(self, xp, dtype): a = testing.shaped_arange((5, ), xp, dtype) return -xp.poly1d(a) @testing.for_all_dtypes() @testing.numpy_cupy_equal() def test_poly1d_order(self, xp, dtype): a = testing.shaped_arange((10, ), xp, dtype) return xp.poly1d(a).order @testing.for_all_dtypes() @testing.numpy_cupy_equal() def test_poly1d_order_leading_zeros(self, xp, dtype): a = xp.array([0, 0, 1, 2, 3, 0], dtype) return xp.poly1d(a).order @pytest.mark.skipif(runtime.is_hip and driver.get_build_version() < 402, reason='syevj not available') @testing.for_signed_dtypes() @testing.numpy_cupy_allclose(rtol=1e-6) def test_poly1d_roots(self, xp, dtype): a = xp.array([-3, -2.5, 3], dtype) out = xp.poly1d(a).roots # The current `cupy.roots` doesn't guarantee the order of results. return xp.sort(out) @testing.for_all_dtypes() @testing.numpy_cupy_equal() def test_poly1d_getitem1(self, xp, dtype): a = testing.shaped_arange((10, ), xp, dtype) with cupyx.allow_synchronize(False): return xp.poly1d(a)[-1] @testing.for_all_dtypes() @testing.numpy_cupy_equal() def test_poly1d_getitem2(self, xp, dtype): a = testing.shaped_arange((10, ), xp, dtype) with cupyx.allow_synchronize(False): return xp.poly1d(a)[5] @testing.for_all_dtypes() @testing.numpy_cupy_equal() def test_poly1d_getitem3(self, xp, dtype): a = testing.shaped_arange((10, ), xp, dtype) with cupyx.allow_synchronize(False): return xp.poly1d(a)[100] @testing.for_all_dtypes() @testing.numpy_cupy_equal() def test_poly1d_getitem4(self, xp, dtype): a = xp.array([0, 0, 1, 2, 3, 0], dtype) with cupyx.allow_synchronize(False): return xp.poly1d(a)[2] @testing.for_all_dtypes() @testing.numpy_cupy_array_equal() def test_poly1d_setitem(self, xp, dtype): a = testing.shaped_arange((10, ), xp, dtype) b = xp.poly1d(a) with cupyx.allow_synchronize(False): b[100] = 20 return b @testing.for_all_dtypes() @testing.numpy_cupy_array_equal() def test_poly1d_setitem_leading_zeros(self, xp, dtype): a = xp.array([0, 0, 0, 2, 3, 0], dtype) b = xp.poly1d(a) with cupyx.allow_synchronize(False): b[1] = 10 return b @testing.for_all_dtypes() def test_poly1d_setitem_neg(self, dtype): for xp in (numpy, cupy): a = testing.shaped_arange((10, ), xp, dtype) b = xp.poly1d(a) with pytest.raises(ValueError): b[-1] = 20 @testing.for_all_dtypes() def test_poly1d_get1(self, dtype): a1 = testing.shaped_arange((10, ), cupy, dtype) a2 = testing.shaped_arange((10, ), numpy, dtype) b1 = cupy.poly1d(a1, variable='z').get() b2 = numpy.poly1d(a2, variable='z') assert b1 == b2 @testing.for_all_dtypes() def test_poly1d_get2(self, dtype): a1 = testing.shaped_arange((), cupy, dtype) a2 = testing.shaped_arange((), numpy, dtype) b1 = cupy.poly1d(a1).get() b2 = numpy.poly1d(a2) assert b1 == b2 @testing.for_all_dtypes(no_bool=True) def test_poly1d_set(self, dtype): arr1 = testing.shaped_arange((10, ), cupy, dtype) arr2 = numpy.ones(10, dtype=dtype) a = cupy.poly1d(arr1) b = numpy.poly1d(arr2, variable='z') a.set(b) assert a.variable == b.variable testing.assert_array_equal(a.coeffs, b.coeffs) @testing.for_all_dtypes() @testing.numpy_cupy_equal() def test_poly1d_repr(self, xp, dtype): a = testing.shaped_arange((5, ), xp, dtype) return repr(xp.poly1d(a)) @testing.for_all_dtypes() @testing.numpy_cupy_equal() def test_poly1d_str(self, xp, dtype): a = testing.shaped_arange((5, ), xp, dtype) return str(xp.poly1d(a)) @testing.for_all_dtypes() @testing.numpy_cupy_allclose(rtol=1e-6) def test_poly1d_call(self, xp, dtype): a = testing.shaped_arange((5, ), xp, dtype) b = xp.poly1d(a) return b(a)
class TestPolyRoutinesNdim: @testing.for_all_dtypes() def test_polyroutine_ndim(self, dtype): for xp in (numpy, cupy): func = getattr(xp, self.fname) a = testing.shaped_arange((2, 3, 4), xp, dtype) b = testing.shaped_arange((10, 5), xp, dtype) with pytest.raises(ValueError): func(a, b) @testing.gpu @testing.parameterize(*testing.product({ 'input': [[2, -1, -2], [-4, 10, 4]], })) @pytest.mark.skipif(runtime.is_hip and driver.get_build_version() < 402, reason='syevj not available') class TestRootsReal: @testing.for_signed_dtypes() @testing.numpy_cupy_allclose(rtol=1e-6) def test_roots_array(self, xp, dtype): a = xp.array(self.input, dtype) out = xp.roots(a) return xp.sort(out) @testing.for_signed_dtypes() @testing.numpy_cupy_allclose(rtol=1e-6) def test_roots_poly1d(self, xp, dtype): a = xp.array(self.input, dtype) out = xp.roots(xp.poly1d(a)) return xp.sort(out)
raise RuntimeError('_wheel.json is not found in the package') # Now that conda-forge docker images have libcuda.so, so "import cupy" would not fail # on Linux. However, tests would fail on the Azure CI since there is no GPU. See the # discussion in https://github.com/conda-forge/cupy-feedstock/pull/59#issuecomment-629584090. # On Windows, this will fail because Windows has no driver stub. try: import cupy except Exception as e: if sys.platform.startswith('win32'): print("No driver available on Windows. Exiting...") sys.exit(0) # Ensure CuPy picks up the correct CUDA_VERSION from cupy.cuda import driver ver = driver.get_build_version() cuda_ver = os.environ.get('cuda_compiler_version').split('.') cuda_ver = int(cuda_ver[0]) * 1000 + int(cuda_ver[1]) * 10 if ver != cuda_ver: raise ValueError('CUDA version {0} != cuda_compiler_version {1}'.format( ver, cuda_ver)) try: # Print CuPy runtime info # this line would fail if there is no GPU cupy.show_config() except Exception as e: print('Got an error: \n%s' % str(e)) print("No GPU available. Exiting without running CuPy's tests.") sys.exit(0)