def test_concatenate_large_2(self, xp, dtype): a = testing.shaped_arange((2, 3, 4), xp, dtype) b = testing.shaped_reverse_arange((2, 3, 2), xp, dtype) c = testing.shaped_arange((2, 3, 3), xp, dtype) d = testing.shaped_arange((2, 3, 5), xp, dtype) e = testing.shaped_arange((2, 3, 2), xp, dtype) return xp.concatenate((a, b, c, d, e), axis=-1)
def test_broadcast(self): a = testing.shaped_arange((2, 1, 3, 4)) b = testing.shaped_arange((3, 1, 4)) bc = cupy.broadcast(a, b) self.assertEqual((2, 3, 3, 4), bc.shape) self.assertEqual(2 * 3 * 3 * 4, bc.size) self.assertEqual(4, bc.nd)
def test_concatenate_large_f_contiguous(self, xp, dtype): a = testing.shaped_arange((2, 3, 4), xp, dtype) b = testing.shaped_arange((2, 3, 2), xp, dtype).T c = testing.shaped_arange((2, 3, 3), xp, dtype) d = testing.shaped_arange((2, 3, 2), xp, dtype).T e = testing.shaped_arange((2, 3, 2), xp, dtype) return xp.concatenate((a, b, c, d, e), axis=-1)
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 check_dot(self, shape_a, shape_b, trans_a, trans_b, xp, dtype): a = testing.shaped_arange(shape_a, xp, dtype) b = testing.shaped_arange(shape_b, xp, dtype) if trans_a: a = a.T if trans_b: b = b.T return xp.dot(a, b)
def test_get_multigpu(self, dtype): with cuda.Device(1): src = testing.shaped_arange((2, 3), xp=cupy, dtype=dtype) src = cupy.asfortranarray(src) with cuda.Device(0): dst = src.get() expected = testing.shaped_arange((2, 3), xp=numpy, dtype=dtype) np_testing.assert_array_equal(dst, expected)
def test_cupy_matmul(self, xp, dtype1, dtype2): if dtype1 == numpy.float16 and dtype2 == numpy.int8: return xp.array([]) if dtype2 == numpy.float16 and dtype1 == numpy.int8: return xp.array([]) x1 = testing.shaped_arange(self.shape_pair[0], xp, dtype1) x2 = testing.shaped_arange(self.shape_pair[1], xp, dtype2) return xp.matmul(x1, x2)
def test_take(self, xp, dtype): a = testing.shaped_arange(self.shape, xp, dtype) if self.axis is None: m = a.size else: m = a.shape[self.axis] i = testing.shaped_arange(self.indices_shape, xp, numpy.int32) % m return a.take(i, self.axis)
def check_typecast(self, val, dtype): operators = [operator.add, operator.sub, operator.mul, operator.div] for op in operators: err = numpy.geterr() numpy.seterr(divide='ignore', invalid='ignore') a = op(val, (testing.shaped_arange((5,), numpy, dtype) - 2)) numpy.seterr(**err) b = op(val, (testing.shaped_arange((5,), cupy, dtype) - 2)) self.assertEqual(a.dtype, b.dtype)
def test_stack_value(self): a = testing.shaped_arange((2, 3), cupy) b = testing.shaped_arange((2, 3), cupy) c = testing.shaped_arange((2, 3), cupy) s = cupy.stack((a, b, c)) self.assertEqual(s.shape, (3, 2, 3)) cupy.testing.assert_array_equal(s[0], a) cupy.testing.assert_array_equal(s[1], b) cupy.testing.assert_array_equal(s[2], c)
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 check_typecast(self, val, dtype): operators = [operator.add, operator.sub, operator.mul] if six.PY3: operators.append(operator.truediv) else: operators.append(operator.div) for op in operators: with testing.NumpyError(divide='ignore', invalid='ignore'): a = op(val, (testing.shaped_arange((5,), numpy, dtype) - 2)) b = op(val, (testing.shaped_arange((5,), cupy, dtype) - 2)) self.assertEqual(a.dtype, b.dtype)
def test_dot_with_out(self, xp, dtype_a, dtype_b, dtype_c): 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) c = xp.empty((shape_a[0], shape_b[-1]), dtype=dtype_c) xp.dot(a, b, out=c) return c
def test_copyto_multigpu(self, xp, dtype): with cuda.Device(0): a = testing.shaped_arange((2, 3, 4), xp, dtype) with cuda.Device(1): b = xp.empty((2, 3, 4), dtype=dtype) xp.copyto(b, a) return b
def test_take(self, xp, dtype): a = testing.shaped_arange(self.shape, xp, dtype) r1 = wrap_take(a, self.indices, self.axis) r2 = xp.zeros_like(r1) wrap_take(a, self.indices, self.axis, out=r2) testing.assert_array_equal(r1, r2) return r2
def test_stack_with_axis_value(self): a = testing.shaped_arange((2, 3), cupy) s = cupy.stack((a, a), axis=1) self.assertEqual(s.shape, (2, 2, 3)) cupy.testing.assert_array_equal(s[:, 0, :], a) cupy.testing.assert_array_equal(s[:, 1, :], a)
def test_stack_with_negative_axis_value(self): a = testing.shaped_arange((2, 3), cupy) s = cupy.stack((a, a), axis=-1) self.assertEqual(s.shape, (2, 3, 2)) cupy.testing.assert_array_equal(s[:, :, 0], a) cupy.testing.assert_array_equal(s[:, :, 1], a)
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 check_savez(self, savez, dtype): a1 = testing.shaped_arange((2, 3, 4), dtype=dtype) a2 = testing.shaped_arange((3, 4, 5), dtype=dtype) sio = six.BytesIO() savez(sio, a1, a2) s = sio.getvalue() sio.close() sio = six.BytesIO(s) with cupy.load(sio) as d: b1 = d['arr_0'] b2 = d['arr_1'] sio.close() testing.assert_array_equal(a1, b1) testing.assert_array_equal(a2, b2)
def test_broadcast_to_numpy19(self, xp, dtype): # Note that broadcast_to is only supported on numpy>=1.10 a = testing.shaped_arange((3, 1, 4), xp, dtype) if xp is cupy: b = xp.broadcast_to(a, (2, 3, 3, 4)) else: dummy = xp.empty((2, 3, 3, 4)) b, _ = xp.broadcast_arrays(a, dummy) return b
def test_is_contiguous(self): a = testing.shaped_arange((2, 3, 4)) self.assertTrue(a.flags.c_contiguous) b = a.transpose(2, 0, 1) self.assertFalse(b.flags.c_contiguous) c = a[::-1] self.assertFalse(c.flags.c_contiguous) d = a[:, :, ::2] self.assertFalse(d.flags.c_contiguous)
def test_external_clip(self, xp, dtype): a = testing.shaped_arange((2, 3, 4), xp, dtype) @cupy.fuse() def g(x, y, z): return cupy.clip(x, y, z) ty = numpy.dtype(dtype).type return g(a, ty(3), ty(13))
def test_dot_with_out(self, xp, dtype_a, dtype_b, dtype_c): 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) if a.ndim == 0 or b.ndim == 0: shape_c = shape_a + shape_b else: shape_c = shape_a[:-1] + shape_b[:-2] + shape_b[-1:] c = xp.empty(shape_c, dtype=dtype_c) out = xp.dot(a, b, out=c) self.assertIs(out, c) return c
def check_binary(self, name, xp, dtype, no_bool=False): if no_bool and numpy.dtype(dtype).char == '?': return numpy.int_(0) a = testing.shaped_arange((2, 3), xp, dtype) b = testing.shaped_reverse_arange((2, 3), xp, dtype) @cupy.fuse() def g(x, y): return getattr(cupy, name)(x, y) return g(a, b)
def test_adv_getitem(self, xp, dtype): indexes = list(self.indexes) a = testing.shaped_arange(self.shape, xp, dtype) if xp is numpy: for i, s in enumerate(indexes): if isinstance(s, cupy.ndarray): indexes[i] = s.get() return a[tuple(indexes)]
def test_save_load(self, dtype): a = testing.shaped_arange((2, 3, 4), dtype=dtype) sio = six.BytesIO() cupy.save(sio, a) s = sio.getvalue() sio.close() sio = six.BytesIO(s) b = cupy.load(sio) sio.close() testing.assert_array_equal(a, b)
def test_array_from_numpy(self, xp, dtype): a = testing.shaped_arange((2, 3, 4), numpy, dtype) return xp.array(a)
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
def test_array_copy_with_dtype(self, xp, dtype1, dtype2): a = testing.shaped_arange((2, 3, 4), xp, dtype1) return xp.array(a, dtype=dtype2)
def test_reshape_with_multiple_unknown_dimensions(self): a = testing.shaped_arange((2, 3, 4)) a.reshape(3, -1, -1)
def test_transposed_reshape2(self, xp): a = testing.shaped_arange((2, 3, 4), xp).transpose(2, 0, 1) return a.reshape(2, 3, 4)
def test_choose_conv_method_zero_dim(self, dtype): a = testing.shaped_arange((), cupy, dtype) b = testing.shaped_arange((5, ), cupy, dtype) with pytest.raises(NotImplementedError): cupyx.scipy.signal.choose_conv_method(a, b, mode=self.mode)
def test_external_ravel(self, xp): a = testing.shaped_arange((2, 3, 4), xp) a = a.transpose(2, 0, 1) return xp.ravel(a)
def test_poly1d_order(self, xp, dtype): a = testing.shaped_arange((10, ), xp, dtype) return xp.poly1d(a).order
def test_poly1d_neg(self, xp, dtype): a = testing.shaped_arange((5, ), xp, dtype) return -xp.poly1d(a)
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
def test_poly1d_getitem3(self, xp, dtype): a = testing.shaped_arange((10, ), xp, dtype) with cupyx.allow_synchronize(False): return xp.poly1d(a)[100]
def check_unary(self, name, xp, dtype): a = testing.shaped_arange((2, 3), xp, dtype) return (a, )
def test_ascontiguousarray_on_noncontiguous_array(self): a = testing.shaped_arange((2, 3, 4)) b = a.transpose(2, 0, 1) c = cupy.ascontiguousarray(b) self.assertTrue(c.flags.c_contiguous) testing.assert_array_equal(b, c)
def test_asarray(self, xp, dtype): a = testing.shaped_arange((2, 3, 4), xp, dtype) return xp.asarray(a)
def test_ascontiguousarray_on_contiguous_array(self): a = testing.shaped_arange((2, 3, 4)) b = cupy.ascontiguousarray(a) self.assertIs(a, b)
def test_transposed_reshape(self, xp): a = testing.shaped_arange((2, 3, 4), xp).T return a.reshape(4, 6)
def test_choose_conv_method_int(self, dtype): a = testing.shaped_arange((10, ), cupy, dtype) b = testing.shaped_arange((5, ), cupy, dtype) assert cupyx.scipy.signal.choose_conv_method( a, b, mode=self.mode) == 'direct'
def check_binary(self, name, xp, dtype): a = testing.shaped_arange((2, 3), xp, dtype) b = testing.shaped_reverse_arange((2, 3), xp, dtype) return a, b
def test_reshape_with_changed_arraysize(self): a = testing.shaped_arange((2, 3, 4)) a.reshape(2, 4, 4)
def test_poly1d_roots(self, xp, dtype): a = testing.shaped_arange((4, ), xp, dtype) out = xp.poly1d(a, True, variable=self.variable) assert out.variable == (self.variable or 'x') return out.coeffs
def test_reshape_with_unknown_dimension(self, xp): a = testing.shaped_arange((2, 3, 4), xp) return a.reshape(3, -1)
def test_array_copy_with_dtype_being_none(self, xp): a = testing.shaped_arange((2, 3, 4), xp) return xp.array(a, dtype=None)
def test_array_no_copy_ndmin(self, xp, dtype): a = testing.shaped_arange((2, 3, 4), xp, dtype) b = xp.array(a, copy=False, ndmin=5) self.assertEqual(a.shape, (2, 3, 4)) a.fill(0) return b
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
def func(xp): a = testing.shaped_arange((1, 1, 1, 2, 2), xp) return a.strides
def test_array_no_copy(self, xp, dtype): a = testing.shaped_arange((2, 3, 4), xp, dtype) b = xp.array(a, copy=False) a.fill(0) return b
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
def test_asarray_is_not_copied(self, xp, dtype): a = testing.shaped_arange((2, 3, 4), xp, dtype) b = xp.asarray(a) a.fill(0) return b
def test_poly1d_str(self, xp, dtype): a = testing.shaped_arange((5, ), xp, dtype) return str(xp.poly1d(a))
def test_poly1d_call(self, xp, dtype): a = testing.shaped_arange((5, ), xp, dtype) b = xp.poly1d(a) return b(a)