def test_putmask(self, xp, dtype): a = testing.shaped_random(self.shape, xp, dtype=dtype, seed=0) mask = testing.shaped_random(self.shape, xp, dtype=numpy.bool_, seed=1) values = testing.shaped_random(self.shape, xp, dtype=dtype, seed=2) ret = xp.putmask(a, mask, values) assert ret is None return a
def test_operator_matmul(self, xp, dtype1): if ((dtype1, dtype1) in self.skip_dtypes or (dtype1, dtype1) in self.skip_dtypes): return xp.array([]) x1 = testing.shaped_random(self.shape_pair[0], xp, dtype1) x2 = testing.shaped_random(self.shape_pair[1], xp, dtype1) return operator.matmul(x1, x2)
def test_putmask_differnt_dtypes_mask(self, xp, dtype): shape = (2, 3) a = testing.shaped_random(shape, xp, dtype=numpy.int64) mask = testing.shaped_random(shape, xp, dtype=dtype) values = testing.shaped_random((3, ), xp, dtype=numpy.int64) xp.putmask(a, mask, values) return a
def test_place_shape_unmatch_error(self, dtype): for xp in (numpy, cupy): a = testing.shaped_arange(self.shape, xp, dtype) mask = testing.shaped_random((3, 4), xp, numpy.bool_) vals = testing.shaped_random((1, ), xp, dtype) with pytest.raises(ValueError): xp.place(a, mask, vals)
def test_percentile_keepdims(self, xp, dtype, interpolation): a = testing.shaped_random((7, 2, 9, 2), xp, dtype) q = testing.shaped_random((5, ), xp, dtype=dtype, scale=100) return xp.percentile(a, q, axis=None, keepdims=True, interpolation=interpolation)
def test_cupy_matmul(self, xp, dtype1): if ((dtype1, dtype1) in self.skip_dtypes or (dtype1, dtype1) in self.skip_dtypes): return xp.array([]) shape1, shape2 = self.shape_pair x1 = testing.shaped_random(shape1, xp, dtype1) x2 = testing.shaped_random(shape2, xp, dtype1) return xp.matmul(x1, x2)
def test_where_two_arrays(self, xp, cond_type, x_type, y_type): m = testing.shaped_random(self.cond_shape, xp, xp.bool_) # Almost all values of a matrix `shaped_random` makes are not zero. # To make a sparse matrix, we need multiply `m`. cond = testing.shaped_random(self.cond_shape, xp, cond_type) * m x = testing.shaped_random(self.x_shape, xp, x_type, seed=0) y = testing.shaped_random(self.y_shape, xp, y_type, seed=1) return xp.where(cond, x, y)
def test_percentile_out(self, xp, dtype, interpolation): a = testing.shaped_random((10, 2, 3, 2), xp, dtype) q = testing.shaped_random((5, ), xp, dtype=dtype, scale=100) out = testing.shaped_random((5, 10, 2, 3), xp, dtype) return xp.percentile(a, q, axis=-1, interpolation=interpolation, out=out)
def test_place(self, xp, dtype): a = testing.shaped_arange(self.shape, xp, dtype) if self.n_vals == 0: mask = xp.zeros(self.shape, dtype=numpy.bool_) else: mask = testing.shaped_random(self.shape, xp, numpy.bool_) vals = testing.shaped_random((self.n_vals, ), xp, dtype) xp.place(a, mask, vals) return a
def test_putmask_differnt_dtypes_raises(self, a_dtype, val_dtype): shape = (2, 3) for xp in (numpy, cupy): a = testing.shaped_random(shape, xp, dtype=a_dtype) mask = testing.shaped_random(shape, xp, dtype=numpy.bool_) values = testing.shaped_random((3, ), xp, dtype=val_dtype) if not numpy.can_cast(val_dtype, a_dtype): with pytest.raises(TypeError): xp.putmask(a, mask, values)
def test_bool_subtract(self): xp = self.xp if xp is numpy and not testing.numpy_satisfies('>=1.14.0'): raise unittest.SkipTest('NumPy<1.14.0') shape = self.shape x = testing.shaped_random(shape, xp, dtype=numpy.bool_) y = testing.shaped_random(shape, xp, dtype=numpy.bool_) with pytest.raises(TypeError): xp.subtract(x, y)
def test_round(self, xp, dtype): if dtype == numpy.bool_: # avoid cast problem a = testing.shaped_random(self.shape, xp, scale=10, dtype=dtype) return a.round(0) if dtype == numpy.float16: # avoid accuracy problem a = testing.shaped_random(self.shape, xp, scale=10, dtype=dtype) return a.round(0) a = testing.shaped_random(self.shape, xp, scale=100, dtype=dtype) return a.round(self.decimals)
def func(xp): m = testing.shaped_random((2, 3), xp, xp.bool_) a = testing.shaped_random((2, 3), xp, dtype) * m c = xp.count_nonzero(a) if xp is cupy: # CuPy returns zero-dimensional array instead of # returning a scalar value self.assertIsInstance(c, xp.ndarray) self.assertEqual(c.dtype, 'l') self.assertEqual(c.shape, ()) return int(c)
def test_nanstd_huge(self, xp, dtype): a = testing.shaped_random((1024, 512), xp, dtype) if a.dtype.kind not in 'biu': a[:512, :256] = xp.nan return xp.nanstd(a, axis=1)
def test_put_index_scalar(self, xp): dtype = cupy.float32 a = testing.shaped_arange(self.shape, xp, dtype) inds = 4 vals = testing.shaped_random((4, ), xp, dtype) xp.put(a, inds, vals) return a
def test_place_empty_value_error(self, dtype): for xp in (numpy, cupy): a = testing.shaped_arange(self.shape, xp, dtype) mask = testing.shaped_arange(self.shape, xp, numpy.int) % 2 == 0 vals = testing.shaped_random((0, ), xp, dtype) with pytest.raises(ValueError): xp.place(a, mask, vals)
def test_put_inds_overflow_error(self, dtype): for xp in (numpy, cupy): a = testing.shaped_arange(self.shape, xp, dtype) inds = xp.array([2, -1, 3, 7]) vals = testing.shaped_random((4, ), xp, dtype) with pytest.raises(IndexError): xp.put(a, inds, vals, mode='raise')
def test_put_mode_error(self, dtype): for xp in (numpy, cupy): a = testing.shaped_arange(self.shape, xp, dtype) inds = xp.array([2, -1, 3, 0]) vals = testing.shaped_random((4, ), xp, dtype) with pytest.raises(ValueError): xp.put(a, inds, vals, mode='unknown')
def test_nanmean_with_nan_float(self, xp, dtype): a = testing.shaped_random(self.shape, xp, dtype) if a.dtype.kind not in 'biu': a[1, :] = xp.nan a[:, 3] = xp.nan return xp.nanmean(a, axis=self.axis, keepdims=self.keepdims)
def test_ihfft(self, xp, dtype): a = testing.shaped_random(self.shape, xp, dtype) out = xp.fft.ihfft(a, n=self.n, norm=self.norm) if xp is np and dtype in [np.float16, np.float32, np.complex64]: out = out.astype(np.complex64) return out
def test_ifftn(self, xp, dtype): a = testing.shaped_random(self.shape, xp, dtype) out = xp.fft.ifftn(a, s=self.s, axes=self.axes, norm=self.norm) if self.axes is not None and not self.axes: assert out is a return out return out
def test_put(self, xp, dtype): a = testing.shaped_arange(self.shape, xp, dtype) # Take care so that actual indices don't overlap. if self.mode == 'raise': inds = xp.array([2, -1, 3, 0]) else: inds = xp.array([2, -8, 3, 7]) vals = testing.shaped_random((self.n_vals, ), xp, dtype) xp.put(a, inds, vals, self.mode) return a
def test_nanstd_out(self, xp, dtype): a = testing.shaped_random((10, 20, 30), xp, dtype) z = xp.zeros((20, 30)) if a.dtype.kind not in 'biu': a[1, :] = xp.nan a[:, 3] = xp.nan xp.nanstd(a, axis=0, out=z) return z
def test_argminmax_dtype(self, in_dtype, result_dtype): a = testing.shaped_random(self.shape, cupy, in_dtype) if self.is_module: func = getattr(cupy, self.func) y = func(a, dtype=result_dtype) else: func = getattr(a, self.func) y = func(dtype=result_dtype) assert y.shape == () assert y.dtype == result_dtype
def test_median_invalid_axis(self): for xp in [numpy, cupy]: a = testing.shaped_random((3, 4, 5), xp) with pytest.raises(numpy.AxisError): return xp.median(a, -a.ndim - 1, keepdims=False) with pytest.raises(numpy.AxisError): return xp.median(a, a.ndim, keepdims=False) with pytest.raises(numpy.AxisError): return xp.median(a, (-a.ndim - 1, 1), keepdims=False) with pytest.raises(numpy.AxisError): return xp.median(a, (0, a.ndim,), keepdims=False)
def test_cub_max(self, xp, dtype, axis): a = testing.shaped_random(self.shape, xp, dtype) if self.order in ('c', 'C'): a = xp.ascontiguousarray(a) elif self.order in ('f', 'F'): a = xp.asfortranarray(a) if xp is numpy: return a.max(axis=axis) # xp is cupy, first ensure we really use CUB ret = cupy.empty(()) # Cython checks return type, need to fool it if len(axis) == len(self.shape): func = 'cupy.core._routines_statistics.cub.device_reduce' else: func = 'cupy.core._routines_statistics.cub.device_segmented_reduce' with testing.AssertFunctionIsCalled(func, return_value=ret): a.max(axis=axis) # ...then perform the actual computation return a.max(axis=axis)
def test_min_axis0(self, xp, dtype): a = testing.shaped_random((2, 3, 4), xp, dtype) return a.min(axis=0)
def test_min_axis_large(self, xp, dtype): a = testing.shaped_random((3, 1000), xp, dtype) return a.min(axis=0)
def test_min_all_keepdims(self, xp, dtype): a = testing.shaped_random((2, 3), xp, dtype) return a.min(keepdims=True)
def test_min_all(self, xp, dtype): a = testing.shaped_random((2, 3), xp, dtype) return a.min()