Skip to content

Commit 6b089a3

Browse files
author
Diptorup Deb
committed
Port existing kernel tests to experimental.
1 parent ca5a29d commit 6b089a3

File tree

9 files changed

+515
-0
lines changed

9 files changed

+515
-0
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# SPDX-FileCopyrightText: 2020 - 2024 Intel Corporation
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
import dpnp
6+
import numpy
7+
import pytest
8+
9+
import numba_dpex.experimental as dpex
10+
from numba_dpex.tests._helper import get_all_dtypes
11+
12+
N = 1024
13+
14+
15+
@dpex.kernel
16+
def kernel_scalar(item, a, b, c):
17+
i = item.get_id(0)
18+
b[i] = a[i] * c
19+
20+
21+
@dpex.kernel
22+
def kernel_array(item, a, b, c):
23+
i = item.get_id(0)
24+
b[i] = a[i] * c[i]
25+
26+
27+
list_of_dtypes = get_all_dtypes(
28+
no_bool=True, no_int=True, no_float=True, no_none=True
29+
)
30+
31+
list_of_usm_types = ["shared", "device", "host"]
32+
33+
34+
@pytest.fixture(params=list_of_dtypes)
35+
def input_arrays(request):
36+
a = dpnp.ones(N, dtype=request.param)
37+
c = dpnp.zeros(N, dtype=request.param)
38+
b = dpnp.empty_like(a)
39+
return a, b, c
40+
41+
42+
def test_numeric_kernel_arg_complex_scalar(input_arrays):
43+
"""Tests passing complex type scalar and dpnp arrays to a kernel function.
44+
45+
Args:
46+
input_arrays (dpnp.ndarray): Array arguments to be passed to a kernel.
47+
"""
48+
a, b, _ = input_arrays
49+
s = a.dtype.type(2 + 1j)
50+
51+
dpex.call_kernel(kernel_scalar, dpex.Range(N), a, b, s)
52+
53+
nb = dpnp.asnumpy(b)
54+
nexpected = numpy.full_like(nb, fill_value=2 + 1j)
55+
56+
assert numpy.allclose(nb, nexpected)
57+
58+
59+
def test_numeric_kernel_arg_complex_array(input_arrays):
60+
"""Tests passing complex type dpnp arrays to a kernel function.
61+
62+
Args:
63+
input_arrays (dpnp.ndarray): Array arguments to be passed to a kernel.
64+
"""
65+
66+
a, b, c = input_arrays
67+
68+
dpex.call_kernel(kernel_array, dpex.Range(N), a, b, c)
69+
70+
nb = dpnp.asnumpy(b)
71+
nexpected = numpy.full_like(nb, fill_value=0 + 0j)
72+
73+
assert numpy.allclose(nb, nexpected)
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# SPDX-FileCopyrightText: 2020 - 2024 Intel Corporation
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
import dpnp
6+
7+
import numba_dpex.experimental as ndpx
8+
from numba_dpex import float32
9+
from numba_dpex import kernel_api as kapi
10+
11+
COEFFICIENTS = dpnp.asarray(
12+
[
13+
[
14+
-0.008086340206185568,
15+
0.242590206185567,
16+
-0.6172680412371134,
17+
0,
18+
],
19+
[
20+
0.015431701030927836,
21+
-1.168492268041237,
22+
27.60438144329897,
23+
-188.1443298969072,
24+
],
25+
[
26+
-0.0036404639175257733,
27+
0.5480025773195877,
28+
-23.890463917525775,
29+
326.8041237113402,
30+
],
31+
[
32+
-0.010869845360824743,
33+
1.4155283505154639,
34+
-58.59149484536083,
35+
789.4845360824743,
36+
],
37+
[
38+
-0.0028801546391752576,
39+
0.21707474226804124,
40+
1.3311855670103092,
41+
-209.22680412371133,
42+
],
43+
[
44+
0.042390463917525774,
45+
-7.9316365979381445,
46+
490.25386597938143,
47+
-9987.680412371134,
48+
],
49+
[
50+
-0.061681701030927835,
51+
13.923518041237113,
52+
-1039.6069587628865,
53+
25709.072164948455,
54+
],
55+
[
56+
0.029336340206185568,
57+
-7.920811855670103,
58+
707.9394329896908,
59+
-20892.164948453606,
60+
],
61+
],
62+
dtype=dpnp.float32,
63+
)
64+
65+
66+
@ndpx.kernel()
67+
def _kernel(nditem: kapi.NdItem, coefficients):
68+
c = kapi.PrivateArray(4, dtype=float32)
69+
gr: kapi.Group = nditem.get_group()
70+
gr_id = gr.get_group_id(0)
71+
72+
c[0] = coefficients[gr_id][0]
73+
c[1] = coefficients[gr_id][1]
74+
c[2] = coefficients[gr_id][2]
75+
c[3] = coefficients[gr_id][3]
76+
77+
78+
def test_setting_private_from_dpnp_ndarray():
79+
N_SEGMENTS = 8
80+
LOCAL_SIZE = 128
81+
# Number of points in the batch. Each work item processes one batch
82+
N_POINTS_PER_WORK_ITEM = 4
83+
# Number of points processed by a work group
84+
N_POINTS_PER_WORK_GROUP = N_POINTS_PER_WORK_ITEM * LOCAL_SIZE
85+
# Total number of points
86+
N_POINTS = N_POINTS_PER_WORK_GROUP * N_SEGMENTS
87+
global_range = ndpx.Range(N_POINTS // N_POINTS_PER_WORK_ITEM)
88+
local_range = ndpx.Range(LOCAL_SIZE)
89+
try:
90+
ndpx.call_kernel(
91+
_kernel, ndpx.NdRange(global_range, local_range), COEFFICIENTS
92+
)
93+
except Exception as e:
94+
assert (
95+
False
96+
), f"'_kernel' raised an exception {e} when passing a dpnp.ndarray arg"
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# SPDX-FileCopyrightText: 2020 - 2024 Intel Corporation
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
import dpnp
6+
import numpy
7+
8+
import numba_dpex.experimental as dpex
9+
10+
11+
@dpex.device_func
12+
def g(a):
13+
return a + dpnp.float32(1)
14+
15+
16+
@dpex.kernel
17+
def f(item, a, b):
18+
i = item.get_id(0)
19+
b[i] = g(a[i])
20+
21+
22+
def test_func_call_from_kernel():
23+
a = dpnp.ones(1024)
24+
b = dpnp.ones(1024)
25+
26+
dpex.call_kernel(f, dpex.Range(1024), a, b)
27+
nb = dpnp.asnumpy(b)
28+
assert numpy.all(nb == 2)
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# SPDX-FileCopyrightText: 2023 - 2024 Intel Corporation
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
import dpnp
6+
import numpy as np
7+
8+
import numba_dpex.experimental as ndpx
9+
10+
11+
def make_write_values_kernel(n_rows):
12+
"""Uppermost kernel to set 1s in a certain way. The uppermost kernel
13+
function invokes two levels of inner functions to set 1s in an empty matrix
14+
in a certain way.
15+
16+
Args:
17+
n_rows (int): Number of rows to iterate.
18+
19+
Returns:
20+
numba_dpex.core.kernel_interface.dispatcher.JitKernel:
21+
A KernelDispatcher object that encapsulates a @kernel decorated
22+
numba_dpex compiled kernel object.
23+
"""
24+
write_values = make_write_values_kernel_func()
25+
26+
@ndpx.kernel
27+
def write_values_kernel(array_in):
28+
for row_idx in range(n_rows):
29+
is_even = (row_idx % 2) == 0
30+
write_values(array_in, row_idx, is_even)
31+
32+
return write_values_kernel
33+
34+
35+
def make_write_values_kernel_func():
36+
"""An upper function to set 1 or 3 ones. A function to set
37+
one or three 1s. If the row index is even it will set three 1s,
38+
otherwise one 1. It uses the inner function to do this.
39+
40+
Returns:
41+
numba_dpex.core.kernel_interface.func.DpexFunctionTemplate:
42+
A DpexFunctionTemplate that encapsulates a @func decorated
43+
numba_dpex compiled function object.
44+
"""
45+
write_when_odd = make_write_values_kernel_func_inner(1)
46+
write_when_even = make_write_values_kernel_func_inner(3)
47+
48+
@ndpx.device_func
49+
def write_values(array_in, row_idx, is_even):
50+
if is_even:
51+
write_when_even(array_in, row_idx)
52+
else:
53+
write_when_odd(array_in, row_idx)
54+
55+
return write_values
56+
57+
58+
def make_write_values_kernel_func_inner(n_cols):
59+
"""Inner function to set 1s. An inner function to set 1s in
60+
n_cols number of columns.
61+
62+
Args:
63+
n_cols (int): Number of columns to be set to 1.
64+
65+
Returns:
66+
numba_dpex.core.kernel_interface.func.DpexFunctionTemplate:
67+
A DpexFunctionTemplate that encapsulates a @func decorated
68+
numba_dpex compiled function object.
69+
"""
70+
71+
@ndpx.device_func
72+
def write_values_inner(array_in, row_idx):
73+
for idx in range(n_cols):
74+
array_in[row_idx, idx] = 1
75+
76+
return write_values_inner
77+
78+
79+
def test_qualname_basic():
80+
"""A basic test function to test
81+
qualified name disambiguation.
82+
"""
83+
ans = np.zeros((10, 10), dtype=np.int64)
84+
for i in range(ans.shape[0]):
85+
if i % 2 == 0:
86+
ans[i, 0:3] = 1
87+
else:
88+
ans[i, 0] = 1
89+
90+
a = dpnp.zeros((10, 10), dtype=dpnp.int64)
91+
92+
kernel = make_write_values_kernel(10)
93+
ndpx.call_kernel(kernel, ndpx.NdRange((1,), (1,)), a)
94+
95+
assert np.array_equal(dpnp.asnumpy(a), ans)
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# SPDX-FileCopyrightText: 2020 - 2024 Intel Corporation
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
import numpy
6+
import pytest
7+
8+
import numba_dpex.experimental as dpex
9+
from numba_dpex import kernel_api as kapi
10+
11+
N = 1024
12+
13+
14+
@dpex.kernel
15+
def vecadd_kernel(item: kapi.Item, a, b, c):
16+
i = item.get_id(0)
17+
c[i] = a[i] + b[i]
18+
19+
20+
def test_passing_numpy_arrays_as_kernel_args():
21+
"""
22+
Negative test to verify that NumPy arrays cannot be passed to a kernel.
23+
"""
24+
a = numpy.ones(N)
25+
b = numpy.ones(N)
26+
c = numpy.zeros(N)
27+
28+
with pytest.raises(Exception):
29+
dpex.call_kernel(vecadd_kernel, dpex.Range(N), a, b, c)
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# SPDX-FileCopyrightText: 2020 - 2024 Intel Corporation
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
import math
6+
7+
import dpnp
8+
import numpy
9+
import pytest
10+
11+
import numba_dpex.experimental as dpex
12+
from numba_dpex.tests._helper import get_all_dtypes
13+
14+
list_of_unary_ops = ["fabs", "exp", "log", "sqrt", "sin", "cos", "tan"]
15+
16+
17+
@pytest.fixture(params=list_of_unary_ops)
18+
def unary_op(request):
19+
return request.param
20+
21+
22+
list_of_dtypes = get_all_dtypes(
23+
no_bool=True, no_int=True, no_float16=True, no_none=True, no_complex=True
24+
)
25+
26+
27+
@pytest.fixture(params=list_of_dtypes)
28+
def input_arrays(request):
29+
# The size of input and out arrays to be used
30+
N = 2048
31+
a = dpnp.arange(N, dtype=request.param)
32+
b = dpnp.arange(N, dtype=request.param)
33+
return a, b
34+
35+
36+
def test_binary_ops(unary_op, input_arrays):
37+
a, b = input_arrays
38+
uop = getattr(math, unary_op)
39+
dpnp_uop = getattr(dpnp, unary_op)
40+
41+
@dpex.kernel
42+
def f(item, a, b):
43+
i = item.get_id(0)
44+
b[i] = uop(a[i])
45+
46+
dpex.call_kernel(f, dpex.Range(a.size), a, b)
47+
48+
expected = dpnp_uop(a)
49+
50+
np_expected = dpnp.asnumpy(expected)
51+
np_actual = dpnp.asnumpy(b)
52+
53+
assert numpy.allclose(np_expected, np_actual)

0 commit comments

Comments
 (0)