def check_copy(self, dtype, src_id, dst_id): with cuda.Device(src_id): src = testing.shaped_arange((2, 3, 4), dtype=dtype) with cuda.Device(dst_id): dst = cupy.empty((2, 3, 4), dtype=dtype) core.elementwise_copy(src, dst) testing.assert_allclose(src, dst)
def test_count_nonzero_int_axis(self, dtype): for ax in six.moves.range(3): def func(xp): m = testing.shaped_random((2, 3, 4), xp, xp.bool_) a = testing.shaped_random((2, 3, 4), xp, dtype) * m return xp.count_nonzero(a, axis=ax) testing.assert_allclose(func(numpy), func(cupy))
def test_farid_v_vertical(): """Vertical Farid on an edge should be a vertical line.""" i, j = cp.mgrid[-5:6, -5:6] image = (j >= 0).astype(float) result = filters.farid_v(image) # Check if result match transform direction assert cp.all(result[j == 0] == result[j == 0][0]) assert_allclose(result[cp.abs(j) > 2], 0, atol=1e-10)
def test_farid_h_horizontal(): """Horizontal Farid on an edge should be a horizontal line.""" i, j = cp.mgrid[-5:6, -5:6] image = (i >= 0).astype(float) result = filters.farid_h(image) # Check if result match transform direction assert cp.all(result[i == 0] == result[i == 0][0]) assert_allclose(result[cp.abs(i) > 2], 0, atol=1e-10)
def test_scharr_horizontal(): """Scharr on an edge should be a horizontal line.""" i, j = cp.mgrid[-5:6, -5:6] image = (i >= 0).astype(float) result = filters.scharr(image) * np.sqrt(2) # Check if result match transform direction assert_allclose(result[i == 0], 1) assert cp.all(result[cp.abs(i) > 1] == 0)
def test_prewitt_horizontal(): """Prewitt on an edge should be a horizontal line.""" i, j = cp.mgrid[-5:6, -5:6] image = (i >= 0).astype(float) result = filters.prewitt(image) * np.sqrt(2) # Check if result match transform direction assert_allclose(result[i == 0], 1) assert_allclose(result[cp.abs(i) > 1], 0, atol=1e-10)
def test_permutation_seed1(self, dtype): a = testing.shaped_random((10, ), cupy, dtype) b = cupy.copy(a) cupy.random.seed(0) pa = cupy.random.permutation(a) cupy.random.seed(0) pb = cupy.random.permutation(b) testing.assert_allclose(pa, pb)
def test_sum_axis2_float16(self): # Note that the above test example overflows in float16. We use a # smaller array instead. a = testing.shaped_arange((2, 30, 4), dtype='e') sa = a.sum(axis=1) b = testing.shaped_arange((2, 30, 4), numpy, dtype='f') sb = b.sum(axis=1) testing.assert_allclose(sa, sb.astype('e'))
def _test_setdiag(self, scipy_a, cupyx_a, x, k): scipy_a = scipy_a.copy() cupyx_a = cupyx_a.copy() scipy_a.setdiag(x, k=k) cupyx_a.setdiag(cupy.array(x), k=k) testing.assert_allclose(scipy_a.data, cupyx_a.data) testing.assert_array_equal(scipy_a.row, cupyx_a.row) testing.assert_array_equal(scipy_a.col, cupyx_a.col)
def test_shuffle_seed1(self, dtype): a = testing.shaped_random((10, ), cupy, dtype) b = cupy.copy(a) cupy.random.seed(0) cupy.random.shuffle(a) cupy.random.seed(0) cupy.random.shuffle(b) testing.assert_allclose(a, b)
def test_syevj(self): a = cupy.array(self.a, order=self.order) w, v = cusolver.syevj(a, UPLO=self.UPLO, with_eigen_vector=True) # check eignvalue equation testing.assert_allclose(self.a.dot(v.get()), w * v, rtol=1e-3, atol=1e-4)
def test_betaincinv_specific_vals(self): # specific values borrowed from SciPy test suite special = cupyx.scipy.special assert special.betaincinv(1, 1, 1) == 1.0 testing.assert_allclose(special.betaincinv(0.0342, 171, 0.25), 8.4231316935498957e-21, rtol=3e-12, atol=0)
def test_count_nonzero_int_axis(self, dtype): for ax in range(3): def func(xp): m = testing.shaped_random((2, 3, 4), xp, xp.bool_) a = testing.shaped_random((2, 3, 4), xp, dtype) * m return xp.count_nonzero(a, axis=ax) testing.assert_allclose(func(numpy), func(cupy))
def test_4d_input_pixel(): phantom = img_as_float(binary_blobs(length=32, n_dim=4)) reference_image = fft.fftn(phantom) shift = (-2.0, 1.0, 5.0, -3) shifted_image = fourier_shift(reference_image, shift) result, error, diffphase = phase_cross_correlation(reference_image, shifted_image, space="fourier") assert_allclose(result, -cp.array(shift), atol=0.05)
def test_frexp(self, dtype): numpy_a = numpy.array([-300, -20, -10, -1, 0, 1, 10, 20, 300], dtype=dtype) numpy_b, numpy_c = numpy.frexp(numpy_a) cupy_a = cupy.array(numpy_a) cupy_b, cupy_c = cupy.frexp(cupy_a) testing.assert_allclose(cupy_b, numpy_b) testing.assert_array_equal(cupy_c, numpy_c)
def test_4d_input_subpixel(): phantom = img_as_float(cp.asarray(binary_blobs(length=32, n_dim=4))) reference_image = fft.fftn(phantom) subpixel_shift = (-2.3, 1.7, 5.4, -3.2) shifted_image = fourier_shift(reference_image, subpixel_shift) result, error, diffphase = phase_cross_correlation( reference_image, shifted_image, upsample_factor=10, space="fourier" ) assert_allclose(result, -cp.asarray(subpixel_shift), atol=0.05)
def run_all_reduce(rank, n_workers, dtype): dev = cuda.Device(rank) dev.use() comm = NCCLBackend(n_workers, rank) in_array = cupy.arange(2 * 3 * 4, dtype='f').reshape(2, 3, 4) out_array = cupy.zeros((2, 3, 4), dtype='f') comm.all_reduce(in_array, out_array) testing.assert_allclose(out_array, 2 * in_array)
def run_send_recv(rank, n_workers, dtype): dev = cuda.Device(rank) dev.use() comm = NCCLBackend(n_workers, rank) in_array = cupy.arange(10, dtype='f') for i in range(n_workers): out_array = cupy.zeros((10, ), dtype='f') comm.send_recv(in_array, out_array, i) testing.assert_allclose(out_array, in_array)
def test_subpixel_precision(): reference_image = fft.fftn(cp.asarray(camera())) subpixel_shift = (-2.4, 1.32) shifted_image = fourier_shift(reference_image, subpixel_shift) # subpixel precision result, error, diffphase = phase_cross_correlation( reference_image, shifted_image, upsample_factor=100, space="fourier" ) assert_allclose(result[:2], -cp.asarray(subpixel_shift), atol=0.05)
def test_infinite_limits(self): # Test that large arguments converge to the hard-coded limits # at infinity. assert_allclose( sc.gammainc(1000, 100), sc.gammainc(cp.inf, 100), atol=1e-200, # Use `atol` since the function converges to 0. rtol=0, ) assert sc.gammainc(100, 1000) == sc.gammainc(100, cp.inf)
def test_correlation(): reference_image = fft.fftn(cp.array(camera())) shift = (-7, 12) shifted_image = fourier_shift(reference_image, shift) # pixel precision result, error, diffphase = phase_cross_correlation(reference_image, shifted_image, space="fourier") assert_allclose(result[:2], -cp.array(shift))
def run_reduce_scatter(rank, n_workers, dtype): dev = cuda.Device(rank) dev.use() comm = NCCLBackend(n_workers, rank) in_array = 1 + cupy.arange(n_workers * 10, dtype='f').reshape( n_workers, 10) out_array = cupy.zeros((10, ), dtype='f') comm.reduce_scatter(in_array, out_array, 10) testing.assert_allclose(out_array, 2 * in_array[rank])
def test_beta_specific_vals(self): # specific values borrowed from SciPy test suite special = cupyx.scipy.special assert special.beta(1, 1) == 1.0 testing.assert_allclose(special.beta(-100.3, 1e-200), special.gamma(1e-200)) testing.assert_allclose(special.beta(0.0342, 171), 24.070498359873497, rtol=1e-13, atol=0)
def run_init(rank, n_workers): dev = cuda.Device(rank) dev.use() comm = init_process_group(n_workers, rank) # Do a simple call to verify we got a valid comm in_array = cupy.zeros(1) if rank == 0: in_array = in_array + 1 comm.broadcast(in_array, 0) testing.assert_allclose(in_array, cupy.ones(1))
def test_betaln_specific_vals(self): # specific values borrowed from SciPy test suite special = cupyx.scipy.special assert special.betaln(1, 1) == 0.0 testing.assert_allclose(special.betaln(-100.3, 1e-200), special.gammaln(1e-200)) testing.assert_allclose(special.betaln(0.0342, 170), 3.1811881124242447, rtol=1e-14, atol=0)
def test_poisson(): seed = 42 data = camerad # 512x512 grayscale uint8 cam_noisy = random_noise(data, mode='poisson', seed=seed) cam_noisy2 = random_noise(data, mode='poisson', seed=seed, clip=False) cp.random.seed(seed=seed) expected = cp.random.poisson(img_as_float(data) * 256) / 256.0 assert_allclose(cam_noisy, cp.clip(expected, 0.0, 1.0)) assert_allclose(cam_noisy2, expected)
def check_returned(self, a, axis, weights): average_cpu, sum_weights_cpu = numpy.average( a, axis, weights, returned=True) result = cupy.average( cupy.asarray(a), axis, weights, returned=True) self.assertTrue(isinstance(result, tuple)) self.assertEqual(len(result), 2) average_gpu, sum_weights_gpu = result testing.assert_allclose(average_cpu, average_gpu) testing.assert_allclose(sum_weights_cpu, sum_weights_gpu)
def test_subclass_unary_op(self): a = cupy.array([0, 1, 2]).view(C) a.info = 1 outa = cupy.sin(a) assert isinstance(outa, C) assert outa.info is not None and outa.info == 1 b = a.get() outb = numpy.sin(b) testing.assert_allclose(outa, outb)
def run_broadcast(rank, n_workers, root, dtype): dev = cuda.Device(rank) dev.use() comm = NCCLBackend(n_workers, rank) expected = cupy.arange(2 * 3 * 4, dtype=dtype).reshape((2, 3, 4)) if rank == root: in_array = expected else: in_array = cupy.zeros((2, 3, 4), dtype=dtype) comm.broadcast(in_array, root) testing.assert_allclose(in_array, expected)
def test_count_nonzero_tuple_axis(self, dtype): for ax in six.moves.range(3): for ay in six.moves.range(3): if ax == ay: continue def func(xp): m = testing.shaped_random((2, 3, 4), xp, xp.bool_) a = testing.shaped_random((2, 3, 4), xp, dtype) * m return xp.count_nonzero(a, axis=(ax, ay)) testing.assert_allclose(func(numpy), func(cupy))
def test_size_one_dimension_input(): # take a strip of the input image reference_image = fft.fftn(cp.asarray(camera())[:, 15]).reshape((-1, 1)) subpixel_shift = (-2.4, 4) shifted_image = fourier_shift(reference_image, subpixel_shift) # subpixel precision result, error, diffphase = phase_cross_correlation( reference_image, shifted_image, upsample_factor=20, space="fourier" ) assert_allclose(result[:2], -cp.asarray((-2.4, 0)), atol=0.05)
def test_real_input(self): reference_image = cp.array(camera()) subpixel_shift = (-2.4, 1.32) shifted_image = fourier_shift(cp.asnumpy(cp.fft.fftn(reference_image)), subpixel_shift) shifted_image = cp.fft.ifftn(cp.array(shifted_image)) # subpixel precision result, error, diffphase = register_translation_cu( reference_image, shifted_image, 100) assert_allclose(result[:2], -cp.array(subpixel_shift), atol=0.05)
def test_frexp(self, dtype): numpy_a = numpy.array([-300, -20, -10, -1, 0, 1, 10, 20, 300], dtype=dtype) @cupy.fuse() def g(x): return cupy.frexp(x) numpy_b, numpy_c = g(numpy_a) cupy_a = cupy.array(numpy_a) cupy_b, cupy_c = g(cupy_a) testing.assert_allclose(cupy_b, numpy_b) testing.assert_array_equal(cupy_c, numpy_c)
def test_copy_multigpu(self, dtype): with cuda.Device(0): src = cupy.random.uniform(-1, 1, (2, 3)).astype(dtype) with cuda.Device(1): dst = src.copy() testing.assert_allclose(src, dst, rtol=0, atol=0)