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_invalid_shape(self): for xp in (numpy, dpnp): shape1, shape2 = self.shape_pair x1 = testing.shaped_arange(shape1, xp, numpy.float32) x2 = testing.shaped_arange(shape2, xp, numpy.float32) with pytest.raises(ValueError): xp.matmul(x1, x2)
def test_cross(self, xp, dtype_a, dtype_b): if dtype_a == dtype_b == numpy.bool_: # cross does not support bool-bool inputs. return xp.array(True) shape_a, shape_b, axisa, axisb, axisc = self.params a = testing.shaped_arange(shape_a, xp, dtype_a) b = testing.shaped_arange(shape_b, xp, dtype_b) return xp.cross(a, b, axisa, axisb, axisc)
def test_bincount_invalid_weight_length(self, x_type, w_type): for xp in (numpy, cupy): x = testing.shaped_arange((1, ), xp, x_type) w = testing.shaped_arange((2, ), xp, w_type) # TODO(imanishi): Split this test into a test for ValueError and # a test for TypeError. with pytest.raises((ValueError, TypeError)): xp.bincount(x, weights=w)
def check_atleast(self, func, xp): a = testing.shaped_arange((), xp) b = testing.shaped_arange((2,), xp) c = testing.shaped_arange((2, 2), xp) d = testing.shaped_arange((4, 3, 2), xp) e = 1 f = numpy.float32(1) return func(a, b, c, d, e, f)
def test_transposed_dot_with_out_f_contiguous(self, dtype): for xp in (numpy, cupy): a = testing.shaped_arange((2, 3, 4), xp, dtype).transpose(1, 0, 2) b = testing.shaped_arange((4, 2, 3), xp, dtype).transpose(2, 0, 1) c = xp.ndarray((3, 2, 3, 2), dtype=dtype, order='F') with pytest.raises(ValueError): # Only C-contiguous array is acceptable xp.dot(a, b, out=c)
def test_array_from_list_of_cupy(self, xp, dtype1, dtype2, src_order, dst_order): # compares numpy.array(<list of numpy.ndarray>) with # cupy.array(<list of cupy.ndarray>) a = [ testing.shaped_arange((3, 4), xp, dtype1, src_order), testing.shaped_arange((3, 4), xp, dtype2, src_order), ] return xp.array(a, order=dst_order)
def test_tensordot_with_int_axes(self, xp, dtype): if dtype in (numpy.uint8, numpy.int8, numpy.uint16, numpy.int16): a = testing.shaped_arange((1, 2, 3), xp, dtype) b = testing.shaped_arange((2, 3, 1), xp, dtype) return xp.tensordot(a, b, axes=2) else: a = testing.shaped_arange((2, 3, 4, 5), xp, dtype) b = testing.shaped_arange((3, 4, 5, 2), xp, dtype) return xp.tensordot(a, b, axes=3)
def test_tensordot_with_list_axes(self, xp, dtype): if dtype in (numpy.uint8, numpy.int8, numpy.uint16, numpy.int16): # Avoid overflow a = testing.shaped_arange((1, 2, 3), xp, dtype) b = testing.shaped_arange((3, 1, 2), xp, dtype) return xp.tensordot(a, b, axes=([2, 1], [0, 2])) else: a = testing.shaped_arange((2, 3, 4, 5), xp, dtype) b = testing.shaped_arange((3, 5, 4, 2), xp, dtype) return xp.tensordot(a, b, axes=([3, 2, 1], [1, 2, 0]))
def test_dot(self, xp, dtype_a, dtype_b): shape_a, shape_b = self.shape if self.trans_a: a = testing.shaped_arange(shape_a[::-1], xp, dtype_a).T else: a = testing.shaped_arange(shape_a, xp, dtype_a) if self.trans_b: b = testing.shaped_arange(shape_b[::-1], xp, dtype_b).T else: b = testing.shaped_arange(shape_b, xp, dtype_b) return xp.dot(a, b)
def test_transposed_tensordot_with_int_axes(self, xp, dtype): if dtype in (numpy.uint8, numpy.int8, numpy.uint16, numpy.int16): # Avoid overflow a = testing.shaped_arange((1, 2, 3), xp, dtype).transpose(2, 0, 1) b = testing.shaped_arange((3, 2, 1), xp, dtype).transpose(2, 1, 0) return xp.tensordot(a, b, axes=2) else: a = testing.shaped_arange((2, 3, 4, 5), xp, dtype).transpose(2, 0, 3, 1) b = testing.shaped_arange((5, 4, 3, 2), xp, dtype).transpose(3, 0, 2, 1) return xp.tensordot(a, b, axes=3)
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_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_invalid_sorter(self): for xp in (numpy, cupy): x = testing.shaped_arange((12, ), xp, xp.float64) bins = xp.array([10, 4, 2, 1, 8]) sorter = xp.array([0]) with pytest.raises(ValueError): xp.searchsorted(bins, x, sorter=sorter)
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_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_nansum_out(self, xp, dtype): a = testing.shaped_arange(self.shape, xp, dtype) if not issubclass(dtype, xp.integer): a[:, 1] = xp.nan b = xp.empty((self.shape[0], self.shape[2]), dtype=dtype) xp.nansum(a, axis=1, out=b) return b
def test_reshape_contiguity(self): shape_init, shape_final = self.shape_in_out a_cupy = testing.shaped_arange(shape_init, xp=cupy) a_cupy = cupy.asarray(a_cupy, order=self.order_init) b_cupy = a_cupy.reshape(shape_final, order=self.order_reshape) a_numpy = testing.shaped_arange(shape_init, xp=numpy) a_numpy = numpy.asarray(a_numpy, order=self.order_init) b_numpy = a_numpy.reshape(shape_final, order=self.order_reshape) assert b_cupy.flags.f_contiguous == b_numpy.flags.f_contiguous assert b_cupy.flags.c_contiguous == b_numpy.flags.c_contiguous testing.assert_array_equal(b_cupy.strides, b_numpy.strides) testing.assert_array_equal(b_cupy, b_numpy)
def test_cumsum_axis_out(self, xp, dtype): n = len(axes) shape = tuple(range(4, 4 + n)) a = testing.shaped_arange(shape, xp, dtype) out = xp.zeros(shape, dtype=dtype) xp.cumsum(a, axis=self.axis, out=out) return out
def test_histogram_density(self, xp, dtype): x = testing.shaped_arange((10, ), xp, dtype) y, bin_edges = xp.histogram(x, density=True) # check normalization area = xp.sum(y * xp.diff(bin_edges)) testing.assert_allclose(area, 1) return y, bin_edges
def test_digitize_all_nan_bins(self, xp): x = testing.shaped_arange((14, ), xp, xp.float32) x[5] = float('nan') bins = [float('nan'), float('nan'), float('nan'), float('nan')] bins = xp.array(bins, xp.float32) y = xp.digitize(x, bins, right=self.right) return y,
def test_digitize_nan_bins_decreasing_repeated(self, xp): x = testing.shaped_arange((14, ), xp, xp.float32) x[5] = float('nan') bins = [float('nan'), float('nan'), float('nan'), 5.0, 3.0, 1.0] bins = xp.array(bins, xp.float32) y = xp.digitize(x, bins, right=self.right) return y,
def test_bincount_too_small_minlength(self, dtype): for xp in (numpy, cupy): x = testing.shaped_arange((3, ), xp, dtype) # TODO(imanishi): Split this test into a test for ValueError and # a test for TypeError. with pytest.raises((ValueError, TypeError)): xp.bincount(x, minlength=-1)
def test_diff_invalid_axis(self): for xp in (numpy, cupy): a = testing.shaped_arange((2, 3, 4), xp) with pytest.raises(numpy.AxisError): xp.diff(a, axis=3) with pytest.raises(numpy.AxisError): xp.diff(a, axis=-4)
def test_array_from_nested_list_of_cupy(self, xp, dtype, src_order, dst_order): # compares numpy.array(<list of numpy.ndarray>) with # cupy.array(<list of cupy.ndarray>) a = [[testing.shaped_arange((3, 4), xp, dtype, src_order) + (12 * i)] for i in range(2)] return xp.array(a, order=dst_order)
def test_setitem_ndarray_nd(self, xp, dtype, order): if numpy.isscalar(self.index): pytest.skip() a = xp.zeros(self.shape, dtype=dtype, order=order) v = testing.shaped_arange((2, 3), xp, dtype, order) a.flat[self.index] = v return a
def test_empty_like_K_strides_reshape(self, dtype): # test strides that are both non-contiguous and non-descending a = testing.shaped_arange((2, 3, 4), numpy, dtype) a = a[:, ::2, :].swapaxes(0, 1) b = numpy.empty_like(a, order='K', shape=self.shape) b.fill(0) # GPU case ag = testing.shaped_arange((2, 3, 4), cupy, dtype) ag = ag[:, ::2, :].swapaxes(0, 1) bg = cupy.empty_like(ag, order='K', shape=self.shape) bg.fill(0) # make sure NumPy and CuPy strides agree self.assertEqual(b.strides, bg.strides) return
def test_nonint_sorter(self): for xp in (numpy, cupy): x = testing.shaped_arange((12, ), xp, xp.float64) bins = xp.array([10, 4, 2, 1, 8]) sorter = xp.array([], dtype=xp.float64) with pytest.raises(TypeError): xp.searchsorted(bins, x, sorter=sorter)
def test_columnar_slice(self, xp, dtype): # see cupy#2970 if self.shape == (2, 2, 2): pytest.skip( 'The length of each dimension must be the same after slicing') a = testing.shaped_arange(self.shape, xp, dtype) val = self._compute_val(xp) xp.fill_diagonal(a[:, 1:], val=val, wrap=self.wrap) return a
def test_put_values_scalar(self, xp): dtype = cupy.float32 a = testing.shaped_arange(self.shape, xp, dtype) # Take care so that actual indices don't overlap. inds = xp.array([2, 3, 5]) vals = 3.0 xp.put(a, inds, vals) return a