def test_all(dtype, shape, filter_str): size = 1 for i in range(len(shape)): size *= shape[i] for i in range(2**size): t = i a = np.empty(size, dtype=dtype) for j in range(size): a[j] = 0 if t % 2 == 0 else j + 1 t = t >> 1 a = a.reshape(shape) def fn(a): return np.all(a) f = njit(fn) with dpctl.device_context(filter_str), dpnp_debug(): actual = f(a) expected = fn(a) np.testing.assert_allclose(actual, expected, rtol=1e-3, atol=0)
def test_eig(filter_str, eig_input, capfd): if skip_test(filter_str): pytest.skip() a = eig_input fn = get_fn("linalg.eig", 1) f = njit(fn) device = dpctl.SyclDevice(filter_str) with dppy.offload_to_sycl_device(device), dpnp_debug(): actual_val, actual_vec = f(a) captured = capfd.readouterr() assert "dpnp implementation" in captured.out expected_val, expected_vec = fn(a) # sort val/vec by abs value vvsort(actual_val, actual_vec) vvsort(expected_val, expected_vec) # NP change sign of vectors for i in range(expected_vec.shape[1]): if expected_vec[0, i] * actual_vec[0, i] < 0: expected_vec[:, i] = -expected_vec[:, i] assert np.allclose(actual_val, expected_val) assert np.allclose(actual_vec, expected_vec)
def test_unary_ops(filter_str, unary_op, input_arrays, get_shape, capfd): a = input_arrays[0] op, name = unary_op if name != "argsort" and name != "copy": a = np.reshape(a, get_shape) if name == "cumprod" and (filter_str == "opencl:cpu:0" or a.dtype == np.int32 or is_gen12(filter_str)): pytest.skip() if name == "cumsum" and (filter_str == "opencl:cpu:0" or a.dtype == np.int32 or is_gen12(filter_str)): pytest.skip() if name == "mean" and is_gen12(filter_str): pytest.skip() if name == "argmax" and is_gen12(filter_str): pytest.skip() actual = np.empty(shape=a.shape, dtype=a.dtype) expected = np.empty(shape=a.shape, dtype=a.dtype) f = njit(op) device = dpctl.SyclDevice(filter_str) with dpctl.device_context(device), dpnp_debug(): actual = f(a) captured = capfd.readouterr() assert "dpnp implementation" in captured.out expected = op(a) np.testing.assert_allclose(actual, expected, rtol=1e-3, atol=0)
def test_matrix_rank(filter_str, matrix_rank_input, capfd): fn = get_fn("linalg.matrix_rank", 1) f = njit(fn) device = dpctl.SyclDevice(filter_str) with dpctl.device_context(device), dpnp_debug(): actual = f(matrix_rank_input) captured = capfd.readouterr() assert "dpnp implementation" in captured.out expected = fn(matrix_rank_input) assert np.allclose(actual, expected)
def test_repeat(filter_str, arr): a = np.array(arr) repeats = 2 def fn(a, repeats): return np.repeat(a, repeats) f = njit(fn) with dpctl.device_context(filter_str), dpnp_debug(): actual = f(a, repeats) expected = fn(a, repeats) np.testing.assert_allclose(actual, expected, rtol=1e-3, atol=0)
def test_partition(array, kth, filter_str): a = np.array(array) def fn(a, kth): return np.partition(a, kth) f = njit(fn) device = dpctl.SyclDevice(filter_str) with dpctl.device_context(device), dpnp_debug(): actual = f(a, kth) expected = fn(a, kth) np.testing.assert_allclose(actual, expected, rtol=1e-3, atol=0)
def test_matrix_power(filter_str, matrix_power_input, power, dtype, capfd): a = np.array(matrix_power_input, dtype=dtype) fn = get_fn("linalg.matrix_power", 2) f = njit(fn) device = dpctl.SyclDevice(filter_str) with dpctl.device_context(device), dpnp_debug(): actual = f(a, power) captured = capfd.readouterr() assert "dpnp implementation" in captured.out expected = fn(a, power) assert np.allclose(actual, expected)
def test_rand(filter_str): @njit def f(): c = np.random.rand(3, 2) return c device = dpctl.SyclDevice(filter_str) with dpctl.device_context(device), dpnp_debug(): actual = f() actual = actual.ravel() assert np.all(actual >= 0.0) assert np.all(actual < 1.0)
def test_diagonal(array, offset, filter_str): a = np.array(array) def fn(a, offset): return np.diagonal(a, offset) f = njit(fn) device = dpctl.SyclDevice(filter_str) with dpctl.device_context(device), dpnp_debug(): actual = f(a, offset) expected = fn(a, offset) np.testing.assert_allclose(actual, expected, rtol=1e-3, atol=0)
def test_cholesky(filter_str, dtype, capfd): a = np.array([[1, -2], [2, 5]], dtype=dtype) fn = get_fn("linalg.cholesky", 1) f = njit(fn) device = dpctl.SyclDevice(filter_str) with dpctl.device_context(device), dpnp_debug(): actual = f(a) captured = capfd.readouterr() assert "dpnp implementation" in captured.out expected = fn(a) assert np.allclose(actual, expected)
def test_one_arg_fn(filter_str, one_arg_fn, unary_size, capfd): op, params = one_arg_fn name, low, high = params f = njit(op) device = dpctl.SyclDevice(filter_str) with dpctl.device_context(device), dpnp_debug(): actual = f(unary_size) captured = capfd.readouterr() assert "dpnp implementation" in captured.out if low is not None: assert np.all(actual >= low) if high is not None: assert np.all(actual < high)
def test_matmul(filter_str, dtype, capfd): a = np.array(np.random.random(10 * 2), dtype=dtype).reshape(10, 2) b = np.array(np.random.random(2 * 10), dtype=dtype).reshape(2, 10) fn = get_fn("matmul", 2) f = njit(fn) device = dpctl.SyclDevice(filter_str) with dpctl.device_context(device), dpnp_debug(): actual = f(a, b) captured = capfd.readouterr() assert "dpnp implementation" in captured.out expected = fn(a, b) assert np.allclose(actual, expected)
def test_matrix_rank(filter_str, matrix_rank_input, capfd): if skip_test(filter_str): pytest.skip() fn = get_fn("linalg.matrix_rank", 1) f = njit(fn) device = dpctl.SyclDevice(filter_str) with dppy.offload_to_sycl_device(device), dpnp_debug(): actual = f(matrix_rank_input) captured = capfd.readouterr() assert "dpnp implementation" in captured.out expected = fn(matrix_rank_input) assert np.allclose(actual, expected)
def test_full(filter_str, full_name, input_array, get_shape, capfd): a = np.reshape(input_array, get_shape) fn = get_op_fn(full_name, 2) actual = np.empty(shape=a.shape, dtype=a.dtype) expected = np.empty(shape=a.shape, dtype=a.dtype) f = njit(fn) device = dpctl.SyclDevice(filter_str) with dpctl.device_context(device), dpnp_debug(): actual = f(a, np.array([2])) captured = capfd.readouterr() assert "dpnp implementation" in captured.out expected = fn(a, np.array([2])) np.testing.assert_allclose(actual, expected, rtol=1e-3, atol=0)
def test_rand(filter_str): if skip_test(filter_str): pytest.skip() @njit def f(): c = np.random.rand(3, 2) return c device = dpctl.SyclDevice(filter_str) with dppy.offload_to_sycl_device(device), dpnp_debug(): actual = f() actual = actual.ravel() assert np.all(actual >= 0.0) assert np.all(actual < 1.0)
def test_diagonal(array, offset, filter_str): if skip_test(filter_str): pytest.skip() a = np.array(array) def fn(a, offset): return np.diagonal(a, offset) f = njit(fn) device = dpctl.SyclDevice(filter_str) with dppy.offload_to_sycl_device(device), dpnp_debug(): actual = f(a, offset) expected = fn(a, offset) np.testing.assert_allclose(actual, expected, rtol=1e-3, atol=0)
def test_take(filter_str, input_arrays, indices, capfd): a = input_arrays[0] fn = get_take_fn() actual = np.empty(shape=a.shape, dtype=a.dtype) expected = np.empty(shape=a.shape, dtype=a.dtype) f = njit(fn) device = dpctl.SyclDevice(filter_str) with dpctl.device_context(device), dpnp_debug(): actual = f(a, indices) captured = capfd.readouterr() assert "dpnp implementation" in captured.out expected = fn(a, indices) np.testing.assert_allclose(actual, expected, rtol=1e-3, atol=0)
def test_det(filter_str, det_input, dtype, capfd): if skip_test(filter_str): pytest.skip() a = np.array(det_input, dtype=dtype) fn = get_fn("linalg.det", 1) f = njit(fn) device = dpctl.SyclDevice(filter_str) with dppy.offload_to_sycl_device(device), dpnp_debug(): actual = f(a) captured = capfd.readouterr() assert "dpnp implementation" in captured.out expected = fn(a) assert np.allclose(actual, expected)
def test_dpnp_interacting_with_parfor(self): def f(a, b): c = np.sum(a) e = np.add(b, a) d = c + e return d device = dpctl.SyclDevice("opencl:gpu") with dpctl.device_context( device), assert_auto_offloading(), dpnp_debug(): njit_f = njit(f) got = njit_f(self.a, self.b) expected = f(self.a, self.b) max_abs_err = got.sum() - expected.sum() assert max_abs_err < 1e-4
def test_repeat(filter_str, arr): if skip_test(filter_str): pytest.skip() a = np.array(arr) repeats = 2 def fn(a, repeats): return np.repeat(a, repeats) f = njit(fn) with dppy.offload_to_sycl_device(filter_str), dpnp_debug(): actual = f(a, repeats) expected = fn(a, repeats) np.testing.assert_allclose(actual, expected, rtol=1e-3, atol=0)
def test_unary_ops(filter_str, unary_op, input_arrays, get_shape, capfd): a = input_arrays[0] op, name = unary_op if name != "argsort" and name != "sort": a = np.reshape(a, get_shape) actual = np.empty(shape=a.shape, dtype=a.dtype) expected = np.empty(shape=a.shape, dtype=a.dtype) f = njit(op) device = dpctl.SyclDevice(filter_str) with dpctl.device_context(device), dpnp_debug(): actual = f(a) captured = capfd.readouterr() assert "dpnp implementation" in captured.out expected = op(a) np.testing.assert_allclose(actual, expected, rtol=1e-3, atol=0)
def test_unary_ops(filter_str, unary_op, input_array, capfd): a = input_array if unary_op == "trace": a = input_array.reshape((2, 5)) fn = get_op_fn(unary_op, 1) actual = np.empty(shape=a.shape, dtype=a.dtype) expected = np.empty(shape=a.shape, dtype=a.dtype) f = njit(fn) device = dpctl.SyclDevice(filter_str) with dpctl.device_context(device), dpnp_debug(): actual = f(a) captured = capfd.readouterr() assert "dpnp implementation" in captured.out expected = fn(a) np.testing.assert_allclose(actual, expected, rtol=1e-3, atol=0)
def test_eigvals(filter_str, eig_input, capfd): a = eig_input fn = get_fn("linalg.eigvals", 1) f = njit(fn) device = dpctl.SyclDevice(filter_str) with dpctl.device_context(device), dpnp_debug(): actual_val = f(a) captured = capfd.readouterr() assert "dpnp implementation" in captured.out expected_val = fn(a) # sort val/vec by abs value vvsort(actual_val, None) vvsort(expected_val, None) assert np.allclose(actual_val, expected_val)
def test_hypergeometric(filter_str, three_arg_size): @njit def f(ngood, nbad, nsamp, size): res = np.random.hypergeometric(ngood, nbad, nsamp, size) return res ngood, nbad, nsamp = 100, 2, 10 device = dpctl.SyclDevice(filter_str) with dpctl.device_context(device), dpnp_debug(): actual = f(ngood, nbad, nsamp, three_arg_size) if np.isscalar(actual): assert actual >= 0 assert actual <= min(nsamp, ngood + nbad) else: actual = actual.ravel() assert np.all(actual >= 0) assert np.all(actual <= min(nsamp, ngood + nbad))
def test_three_arg_fn(filter_str, three_arg_fn, three_arg_size, capfd): if skip_test(filter_str): pytest.skip() op_name, first_arg, second_arg, low, high = three_arg_fn if op_name == "multinomial": pytest.skip("DPNP RNG Error: dpnp_rng_multinomial_c() failed") elif op_name == "multivariate_normal": pytest.skip( "No implementation of function Function(<class " "'numba_dppy.dpnp_glue.stubs.dpnp.multivariate_normal'>) found for signature" ) elif op_name == "negative_binomial": pytest.skip("DPNP RNG Error: dpnp_rng_negative_binomial_c() failed.") elif op_name == "gumbel": pytest.skip("DPNP error") op = get_three_arg_fn(op_name) f = njit(op) device = dpctl.SyclDevice(filter_str) with dppy.offload_to_sycl_device(device), dpnp_debug(): actual = f(first_arg, second_arg, three_arg_size) captured = capfd.readouterr() assert "dpnp implementation" in captured.out if low is not None and high is None: if second_arg: low = first_arg high = second_arg assert np.all(actual >= low) assert np.all(actual <= high) else: high = first_arg assert np.all(actual >= low) assert np.all(actual <= high) elif low is not None and high is not None: if np.isscalar(actual): assert actual >= low assert actual <= high else: actual = actual.ravel() assert np.all(actual >= low) assert np.all(actual <= high)
def test_multi_dot(filter_str, capfd): def fn(A, B, C, D): c = np.linalg.multi_dot([A, B, C, D]) return c A = np.random.random((10000, 100)) B = np.random.random((100, 1000)) C = np.random.random((1000, 5)) D = np.random.random((5, 333)) f = njit(fn) device = dpctl.SyclDevice(filter_str) with dpctl.device_context(device), dpnp_debug(): actual = f(A, B, C, D) captured = capfd.readouterr() assert "dpnp implementation" in captured.out expected = fn(A, B, C, D) assert np.allclose(actual, expected)
def test_binary_op(filter_str, binary_op, input_array, dtype, get_shape, capfd): if skip_test(filter_str): pytest.skip() a = np.reshape(input_array, get_shape) fn = get_op_fn(binary_op, 2) actual = np.empty(shape=a.shape, dtype=a.dtype) expected = np.empty(shape=a.shape, dtype=a.dtype) f = njit(fn) device = dpctl.SyclDevice(filter_str) with dppy.offload_to_sycl_device(device), dpnp_debug(): actual = f(a, dtype) captured = capfd.readouterr() assert "dpnp implementation" in captured.out expected = fn(a, dtype) np.testing.assert_allclose(actual, expected, rtol=1e-3, atol=0)
def test_dot(filter_str, dot_name, dot_input, dtype, capfd): a, b = dot_input if dot_name == "vdot": if a.size != b.size: pytest.skip("vdot only supports same sized arrays") a = a.astype(dtype) b = b.astype(dtype) fn = get_fn(dot_name, 2) f = njit(fn) device = dpctl.SyclDevice(filter_str) with dpctl.device_context(device), dpnp_debug(): actual = f(a, b) captured = capfd.readouterr() assert "dpnp implementation" in captured.out expected = fn(a, b) assert np.allclose(actual, expected)
def test_two_arg_fn(filter_str, two_arg_fn, unary_size, capfd): op_name, first_arg, low, high = two_arg_fn if op_name == "gamma": pytest.skip( "AttributeError: 'NoneType' object has no attribute 'ravel'") op = get_two_arg_fn(op_name) f = njit(op) device = dpctl.SyclDevice(filter_str) with dpctl.device_context(device), dpnp_debug(): actual = f(first_arg, unary_size) captured = capfd.readouterr() assert "dpnp implementation" in captured.out if low is not None and high is None: if np.isscalar(actual): assert actual >= low else: actual = actual.ravel() assert np.all(actual >= low)
def test_unary_ops(filter_str, unary_op, input_arrays, get_shape, capfd): if skip_test(filter_str): pytest.skip() a = input_arrays[0] op, name = unary_op if name != "cov": a = np.reshape(a, get_shape) actual = np.empty(shape=a.shape, dtype=a.dtype) expected = np.empty(shape=a.shape, dtype=a.dtype) f = njit(op) device = dpctl.SyclDevice(filter_str) with dppy.offload_to_sycl_device(device), dpnp_debug(): actual = f(a) captured = capfd.readouterr() assert "dpnp implementation" in captured.out expected = op(a) np.testing.assert_allclose(actual, expected, rtol=1e-3, atol=0)