def test_select_odd_shaped_non_broadcastable(self, dtype): a = cupy.arange(10, dtype=dtype) b = cupy.arange(20, dtype=dtype) condlist = [a < 3, b > 8] choicelist = [a, b] with pytest.raises(ValueError): cupy.select(condlist, choicelist)
def test_density(self): # Check that the integral of the density equals 1. n = 100 v = dpnp.random.rand(n) a, b = dpnp.histogram(v, density=True) area = dpnp.sum(a * dpnp.diff(b)[0])[0] numpy.testing.assert_almost_equal(area, 1) # Check with non-constant bin widths v = dpnp.arange(10) bins = [0, 1, 3, 6, 10] a, b = dpnp.histogram(v, bins, density=True) numpy.testing.assert_array_equal(a, .1) numpy.testing.assert_equal(dpnp.sum(a * dpnp.diff(b))[0], 1) # Test that passing False works too a, b = dpnp.histogram(v, bins, density=False) numpy.testing.assert_array_equal(a, [1, 2, 3, 4]) # Variable bin widths are especially useful to deal with # infinities. v = dpnp.arange(10) bins = [0, 1, 3, 6, numpy.inf] a, b = dpnp.histogram(v, bins, density=True) numpy.testing.assert_array_equal(a, [.1, .1, .1, 0.]) # Taken from a bug report from N. Becker on the numpy-discussion # mailing list Aug. 6, 2010. counts, dmy = dpnp.histogram([1, 2, 3, 4], [0.5, 1.5, numpy.inf], density=True) numpy.testing.assert_equal(counts, [.25, 0])
def test_consuming_array_from_dpnp(offload_device, dtype): if not ensure_dpnp(): pytest.skip("No DPNP") import dpnp if skip_test(offload_device): pytest.skip("No device for " + offload_device) if ("opencl" not in dpctl.get_current_queue().sycl_device.filter_string and "opencl" in offload_device): pytest.skip("Bug in DPNP. See: IntelPython/dpnp#723") @dppy.kernel def data_parallel_sum(a, b, c): """ Vector addition using the ``kernel`` decorator. """ i = dppy.get_global_id(0) c[i] = a[i] + b[i] global_size = 1021 with dppy.offload_to_sycl_device(offload_device): a = dppy.asarray(dpnp.arange(global_size, dtype=dtype)) b = dppy.asarray(dpnp.arange(global_size, dtype=dtype)) c = dppy.asarray(dpnp.ones_like(a)) data_parallel_sum[global_size, dppy.DEFAULT_LOCAL_SIZE](a, b, c)
def test_select_default_scalar(self, dtype): a = cupy.arange(10) b = cupy.arange(20) condlist = [a < 3, b > 8] choicelist = [a, b] with pytest.raises(TypeError): cupy.select(condlist, choicelist, [dtype(2)])
def test_invalid_shape(self, shape): dp_array1 = dpnp.arange(10, dtype=dpnp.float64) dp_array2 = dpnp.arange(5, 15, dtype=dpnp.float64) dp_out = dpnp.empty(shape, dtype=dpnp.float64) with pytest.raises(ValueError): dpnp.power(dp_array1, dp_array2, out=dp_out)
def test_multi_dot(type): n = 16 a = inp.arange(n, dtype=type).reshape((4, 4)) b = inp.arange(n, dtype=type).reshape((4, 4)) c = inp.arange(n, dtype=type).reshape((4, 4)) d = inp.arange(n, dtype=type).reshape((4, 4)) a1 = numpy.arange(n, dtype=type).reshape((4, 4)) b1 = numpy.arange(n, dtype=type).reshape((4, 4)) c1 = numpy.arange(n, dtype=type).reshape((4, 4)) d1 = numpy.arange(n, dtype=type).reshape((4, 4)) result = inp.linalg.multi_dot([a, b, c, d]) expected = numpy.linalg.multi_dot([a1, b1, c1, d1]) numpy.testing.assert_array_equal(expected, result)
def permutation(x): """ Randomly permute a sequence, or return a permuted range. For full documentation refer to :obj:`numpy.random.permutation`. Examples -------- >>> arr = dpnp.random.permutation(10) >>> print(arr) [3 8 7 9 0 6 1 2 4 5] # random >>> arr = dpnp.random.permutation([1, 4, 9, 12, 15]) >>> print(arr) [12 1 4 9 15] # random >>> arr = dpnp.arange(9).reshape((3, 3)) >>> dpnp.random.permutation(arr) >>> print(arr) [[0 1 2] [3 4 5] [6 7 8]] # random """ if not use_origin_backend(x): if isinstance(x, (int, dpnp.integer)): arr = dpnp.arange(x) else: arr = dpnp.array(x) shuffle(arr) return arr return call_origin(numpy.random.permutation, x)
def test_invalid_dtype(self, dtype): dp_array = dpnp.arange(10, dtype=dpnp.float64) dp_out = dpnp.empty(10, dtype=dtype) with pytest.raises(ValueError): dpnp.sin(dp_array, out=dp_out)
def shaped_arange(shape, xp=dpnp, dtype=numpy.float64, order='C'): """Returns an array with given shape, array module, and dtype. Args: shape(tuple of int): Shape of returned ndarray. xp(numpy or cupy): Array module to use. dtype(dtype): Dtype of returned ndarray. order({'C', 'F'}): Order of returned ndarray. Returns: numpy.ndarray or cupy.ndarray: The array filled with :math:`1, \\cdots, N` with specified dtype with given shape, array module. Here, :math:`N` is the size of the returned array. If ``dtype`` is ``numpy.bool_``, evens (resp. odds) are converted to ``True`` (resp. ``False``). """ dtype = numpy.dtype(dtype) a = dpnp.arange(1, prod(shape) + 1, 1) if dtype == '?': a = a % 2 == 0 elif dtype.kind == 'c': a = a + a * 1j return xp.array(a.astype(dtype).reshape(shape), order=order, dtype=dtype)
def shaped_reverse_arange(shape, xp=dpnp, dtype=numpy.float32): """Returns an array filled with decreasing numbers. Args: shape(tuple of int): Shape of returned ndarray. xp(numpy or cupy): Array module to use. dtype(dtype): Dtype of returned ndarray. Returns: numpy.ndarray or cupy.ndarray: The array filled with :math:`N, \\cdots, 1` with specified dtype with given shape, array module. Here, :math:`N` is the size of the returned array. If ``dtype`` is ``numpy.bool_``, evens (resp. odds) are converted to ``True`` (resp. ``False``). """ dtype = numpy.dtype(dtype) size = prod(shape) # a = dpnp.arange(size, 0, -1) a = dpnp.arange(0, size) if dtype == '?': a = a % 2 == 0 elif dtype.kind == 'c': a = a + a * 1j return xp.array(a.astype(dtype).reshape(shape))
def test_invalid_shape(self, shape): dp_array = dpnp.arange(10, dtype=dpnp.float64) dp_out = dpnp.empty(shape, dtype=dpnp.float64) with pytest.raises(ValueError): dpnp.arctan2(dp_array, dp_array, out=dp_out)
def test_strides_true_devide(dtype, shape): a = numpy.arange(numpy.prod(shape), dtype=dtype).reshape(shape) b = a.T + 1 dpa = dpnp.reshape(dpnp.arange(numpy.prod(shape), dtype=dtype), shape) dpb = dpa.T + 1 result = dpnp.fmod(dpa, dpb) expected = numpy.fmod(a, b) numpy.testing.assert_allclose(result, expected)
def test_strides_copysign(dtype, shape): a = numpy.arange(numpy.prod(shape), dtype=dtype).reshape(shape) b = -a.T dpa = dpnp.reshape(dpnp.arange(numpy.prod(shape), dtype=dtype), shape) dpb = dpnp.negative(dpa.T) result = dpnp.copysign(dpa, dpb) expected = numpy.copysign(a, b) numpy.testing.assert_allclose(result, expected)
def test_strides_tan(dtype, shape): a = numpy.arange(numpy.prod(shape), dtype=dtype).reshape(shape) b = a[::2] dpa = dpnp.reshape(dpnp.arange(numpy.prod(shape), dtype=dtype), shape) dpb = dpa[::2] result = dpnp.tan(dpb) expected = numpy.tan(b) numpy.testing.assert_allclose(result, expected, rtol=1e-06)
def test_concatenate(self): # Test concatenate function # One sequence returns unmodified (but as array) r4 = list(range(4)) numpy.testing.assert_array_equal(dpnp.concatenate((r4, )), r4) # Any sequence numpy.testing.assert_array_equal(dpnp.concatenate((tuple(r4), )), r4) numpy.testing.assert_array_equal(dpnp.concatenate((dpnp.array(r4), )), r4) # 1D default concatenation r3 = list(range(3)) numpy.testing.assert_array_equal(dpnp.concatenate((r4, r3)), r4 + r3) # Mixed sequence types numpy.testing.assert_array_equal(dpnp.concatenate((tuple(r4), r3)), r4 + r3) numpy.testing.assert_array_equal( dpnp.concatenate((dpnp.array(r4), r3)), r4 + r3) # Explicit axis specification numpy.testing.assert_array_equal(dpnp.concatenate((r4, r3), 0), r4 + r3) # Including negative numpy.testing.assert_array_equal(dpnp.concatenate((r4, r3), -1), r4 + r3) # 2D a23 = dpnp.array([[10, 11, 12], [13, 14, 15]]) a13 = dpnp.array([[0, 1, 2]]) res = dpnp.array([[10, 11, 12], [13, 14, 15], [0, 1, 2]]) numpy.testing.assert_array_equal(dpnp.concatenate((a23, a13)), res) numpy.testing.assert_array_equal(dpnp.concatenate((a23, a13), 0), res) numpy.testing.assert_array_equal(dpnp.concatenate((a23.T, a13.T), 1), res.T) numpy.testing.assert_array_equal(dpnp.concatenate((a23.T, a13.T), -1), res.T) # Arrays much match shape numpy.testing.assert_raises(ValueError, dpnp.concatenate, (a23.T, a13.T), 0) # 3D res = dpnp.reshape(dpnp.arange(2 * 3 * 7), (2, 3, 7)) a0 = res[..., :4] a1 = res[..., 4:6] a2 = res[..., 6:] numpy.testing.assert_array_equal(dpnp.concatenate((a0, a1, a2), 2), res) numpy.testing.assert_array_equal(dpnp.concatenate((a0, a1, a2), -1), res) numpy.testing.assert_array_equal( dpnp.concatenate((a0.T, a1.T, a2.T), 0), res.T) out = dpnp.copy(res) rout = dpnp.concatenate((a0, a1, a2), 2, out=out) numpy.testing.assert_(out is rout) numpy.testing.assert_equal(res, rout)
def test_shared_memory(): X = dpnp.arange(4, dtype=numpy.int32) ms = dpctl.memory.MemoryUSMShared(X) assert ms.nbytes == X.size * X.itemsize X_host = ms.copy_to_host() X_copied = X_host.view(X.dtype) assert all([ X_copied[i] == X[i] for i in range(len(X))]) X_pattern = numpy.array([-7, 8, 18, -99], dtype=numpy.int32) ms.copy_from_host(X_pattern.view("|u1")) assert all([ X_pattern[i] == X[i] for i in range(len(X))])
def test_strides_reciprocal(dtype, shape): start, stop = 1, numpy.prod(shape) + 1 a = numpy.arange(start, stop, dtype=dtype).reshape(shape) b = a[::2] dpa = dpnp.reshape(dpnp.arange(start, stop, dtype=dtype), shape) dpb = dpa[::2] result = dpnp.reciprocal(dpb) expected = numpy.reciprocal(b) numpy.testing.assert_allclose(result, expected, rtol=1e-06)
def test_strides_1arg(func_name, dtype, shape): a = numpy.arange(numpy.prod(shape), dtype=dtype).reshape(shape) b = a[::2] dpa = dpnp.reshape(dpnp.arange(numpy.prod(shape), dtype=dtype), shape) dpb = dpa[::2] dpnp_func = _getattr(dpnp, func_name) result = dpnp_func(dpb) numpy_func = _getattr(numpy, func_name) expected = numpy_func(b) numpy.testing.assert_allclose(result, expected)
def test_strides_erf(dtype, shape): a = numpy.arange(numpy.prod(shape), dtype=dtype).reshape(shape) b = a[::2] dpa = dpnp.reshape(dpnp.arange(numpy.prod(shape), dtype=dtype), shape) dpb = dpa[::2] result = dpnp.erf(dpb) expected = numpy.empty_like(b) for idx, val in enumerate(b): expected[idx] = math.erf(val) numpy.testing.assert_allclose(result, expected)
def test_dpnp_create_array_in_context(offload_device, dtype): if not ensure_dpnp(): pytest.skip("No DPNP") import dpnp if skip_test(offload_device): pytest.skip("No device for " + offload_device) if ("opencl" not in dpctl.get_current_queue().sycl_device.filter_string and "opencl" in offload_device): pytest.skip("Bug in DPNP. See: IntelPython/dpnp#723") with dpctl.device_context(offload_device): a = dpnp.arange(1024, dtype=dtype) # noqa
def test_large_concatenate_axis_None(self): x = dpnp.arange(1, 100) r = dpnp.concatenate(x, None) numpy.testing.assert_array_equal(x, r) r = dpnp.concatenate(x, 100) numpy.testing.assert_array_equal(x, r)
def test_arr_weights_mismatch(self): a = dpnp.arange(10) + .5 w = dpnp.arange(11) + .5 with numpy.testing.assert_raises_regex(ValueError, "same shape as"): h, b = dpnp.histogram(a, range=[1, 9], weights=w, density=True)
def test_select_length_error(self, dtype): a = cupy.arange(10, dtype=dtype) condlist = [a > 3] choicelist = [a, a**2] with pytest.raises(ValueError): cupy.select(condlist, choicelist)
def test_select_type_error_condlist(self, dtype): a = cupy.arange(10, dtype=dtype) condlist = [[3] * 10, [2] * 10] choicelist = [a, a**2] with pytest.raises(AttributeError): cupy.select(condlist, choicelist)
def test_has_sycl_usm_array_interface(): X = dpnp.arange(4) assert hasattr(X, '__sycl_usm_array_interface__') assert X.__sycl_usm_array_interface__['shape'] == X.shape assert numpy.dtype(X.__sycl_usm_array_interface__['typestr']) == X.dtype