Skip to content

Commit e7a4420

Browse files
author
Diptorup Deb
authored
Merge pull request #1049 from IntelPython/feature/remove_numpy_array_kernelargs
Feature/remove numpy array kernelargs
2 parents 6aca7c3 + 218df52 commit e7a4420

18 files changed

+102
-591
lines changed

numba_dpex/core/exceptions.py

Lines changed: 4 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ def __init__(self, kernel_name, dim, work_groups, work_items) -> None:
182182
super().__init__(self.message)
183183

184184

185-
class ComputeFollowsDataInferenceError(Exception):
185+
class ExecutionQueueInferenceError(Exception):
186186
"""Exception raised when an execution queue for a given array expression or
187187
a kernel function could not be deduced using the compute-follows-data
188188
programming model.
@@ -194,34 +194,20 @@ class ComputeFollowsDataInferenceError(Exception):
194194
which the array operands were allocated. Computation is required to occur
195195
on the same device where the arrays currently reside.
196196
197-
A ComputeFollowsDataInferenceError is raised when the execution queue using
197+
A ExecutionQueueInferenceError is raised when the execution queue using
198198
compute-follows-data rules could not be deduced. It may happen when arrays
199199
that have a device attribute such as ``dpctl.tensor.usm_ndarray`` are mixed
200200
with host arrays such as ``numpy.ndarray``. The error may also be raised if
201201
the array operands are allocated on different devices.
202202
203203
Args:
204204
kernel_name : Name of the kernel function for which the error occurred.
205-
ndarray_argnum_list: The list of ``numpy.ndarray`` arguments identified
206-
by the argument position that caused the error.
207205
usmarray_argnum_list: The list of ``dpctl.tensor.usm_ndarray`` arguments
208206
identified by the argument position that caused the error.
209207
"""
210208

211-
def __init__(
212-
self, kernel_name, ndarray_argnum_list=None, *, usmarray_argnum_list
213-
) -> None:
214-
if ndarray_argnum_list and usmarray_argnum_list:
215-
ndarray_args = ",".join([str(i) for i in ndarray_argnum_list])
216-
usmarray_args = ",".join([str(i) for i in usmarray_argnum_list])
217-
self.message = (
218-
f'Kernel "{kernel_name}" has arguments of both usm_ndarray and '
219-
"non-usm_ndarray types. Mixing of arguments of different "
220-
"array types is disallowed. "
221-
f"Arguments {ndarray_args} are non-usm arrays, "
222-
f"and arguments {usmarray_args} are usm arrays."
223-
)
224-
elif usmarray_argnum_list is not None:
209+
def __init__(self, kernel_name, *, usmarray_argnum_list) -> None:
210+
if usmarray_argnum_list is not None:
225211
usmarray_args = ",".join([str(i) for i in usmarray_argnum_list])
226212
self.message = (
227213
f'Execution queue for kernel "{kernel_name}" could '
@@ -232,32 +218,6 @@ def __init__(
232218
super().__init__(self.message)
233219

234220

235-
class ExecutionQueueInferenceError(Exception):
236-
"""Exception raised when an execution queue could not be deduced for NumPy
237-
ndarray kernel arguments.
238-
239-
Args:
240-
kernel_name (str): Name of kernel where the error was raised.
241-
242-
.. deprecated:: 0.19
243-
"""
244-
245-
def __init__(self, kernel_name) -> None:
246-
warn(
247-
"The ExecutionQueueInferenceError class is deprecated, and will "
248-
+ "be removed once support for NumPy ndarrays as kernel arguments "
249-
+ "is removed.",
250-
DeprecationWarning,
251-
stacklevel=2,
252-
)
253-
self.message = (
254-
f'Kernel "{kernel_name}" was called with NumPy ndarray arguments '
255-
"outside a dpctl.device_context. The execution queue to be used "
256-
"could not be deduced."
257-
)
258-
super().__init__(self.message)
259-
260-
261221
class UnsupportedBackendError(Exception):
262222
"""Exception raised when the target device is not supported by dpex.
263223
@@ -357,29 +317,6 @@ def __init__(self, kernel_name, arg) -> None:
357317
super().__init__(self.message)
358318

359319

360-
class UnsupportedAccessQualifierError(Exception):
361-
"""Exception raised when an illegal access specifier value is specified for
362-
a NumPy array argument passed to a kernel.
363-
364-
Args:
365-
kernel_name (str): Name of kernel where the error was raised.
366-
array_val: name of the array argument with the illegal access specifier.
367-
illegal_access_type (str): The illegal access specifier string.
368-
legal_access_list (str): Joined string for the legal access specifiers.
369-
"""
370-
371-
def __init__(
372-
self, kernel_name, array_val, illegal_access_type, legal_access_list
373-
) -> None:
374-
self.message = (
375-
f"Invalid access type {illegal_access_type} applied to "
376-
f'array {array_val} argument passed to kernel "{kernel_name}". '
377-
f"Legal access specifiers are {legal_access_list}."
378-
)
379-
380-
super().__init__(self.message)
381-
382-
383320
class UnsupportedCompilationModeError(Exception):
384321
def __init__(self) -> None:
385322
self.message = (

numba_dpex/core/kernel_interface/arg_pack_unpacker.py

Lines changed: 21 additions & 152 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,7 @@
1010
from numba.core import types
1111

1212
import numba_dpex.utils as utils
13-
from numba_dpex.core.exceptions import (
14-
UnsupportedAccessQualifierError,
15-
UnsupportedKernelArgumentError,
16-
)
13+
from numba_dpex.core.exceptions import UnsupportedKernelArgumentError
1714
from numba_dpex.core.types import USMNdArray
1815
from numba_dpex.core.utils import get_info_from_suai
1916

@@ -28,42 +25,31 @@ def __init__(self, usm_mem, orig_val, packed_val, packed) -> None:
2825

2926
class Packer:
3027
"""Implements the functionality to unpack a Python object passed as an
31-
argument to a numba_dpex kernel fucntion into corresponding ctype object.
28+
argument to a numba_dpex kernel function into corresponding ctype object.
3229
"""
3330

34-
# TODO: Remove after NumPy support is removed
35-
_access_types = ("read_only", "write_only", "read_write")
36-
37-
def _check_for_invalid_access_type(self, array_val, access_type):
38-
if access_type and access_type not in Packer._access_types:
39-
raise UnsupportedAccessQualifierError(
40-
self._pyfunc_name,
41-
array_val,
42-
access_type,
43-
",".join(Packer._access_types),
44-
)
45-
46-
def _unpack_array_helper(self, size, itemsize, buf, shape, strides, ndim):
47-
"""
48-
Implements the unpacking logic for array arguments.
31+
def _unpack_usm_array(self, val):
32+
"""Flattens an object of USMNdArray type into ctypes objects to be
33+
passed as kernel arguments.
4934
5035
Args:
51-
size: Total number of elements in the array.
52-
itemsize: Size in bytes of each element in the array.
53-
buf: The pointer to the memory.
54-
shape: The shape of the array.
55-
ndim: Number of dimension.
36+
val : An object of dpctl.types.UsmNdArray type.
5637
5738
Returns:
58-
A list a ctype value for each array attribute argument
39+
list: A list of ctype objects representing the flattened usm_ndarray
5940
"""
6041
unpacked_array_attrs = []
61-
62-
# meminfo (FIXME: should be removed and the USMNdArray type modified
63-
# once NumPy support is removed)
42+
suai_attrs = get_info_from_suai(val)
43+
size = suai_attrs.size
44+
itemsize = suai_attrs.itemsize
45+
buf = suai_attrs.data
46+
shape = suai_attrs.shape
47+
strides = suai_attrs.strides
48+
ndim = suai_attrs.dimensions
49+
50+
# meminfo
6451
unpacked_array_attrs.append(ctypes.c_size_t(0))
65-
# parent (FIXME: Evaluate if the attribute should be removed and the
66-
# USMNdArray type modified once NumPy support is removed)
52+
# parent
6753
unpacked_array_attrs.append(ctypes.c_size_t(0))
6854
unpacked_array_attrs.append(ctypes.c_longlong(size))
6955
unpacked_array_attrs.append(ctypes.c_longlong(itemsize))
@@ -75,90 +61,7 @@ def _unpack_array_helper(self, size, itemsize, buf, shape, strides, ndim):
7561

7662
return unpacked_array_attrs
7763

78-
def _unpack_usm_array(self, val):
79-
"""Flattens an object of USMNdArray type into ctypes objects to be
80-
passed as kernel arguments.
81-
82-
Args:
83-
val : An object of dpctl.types.UsmNdArray type.
84-
85-
Returns:
86-
list: A list of ctype objects representing the flattened usm_ndarray
87-
"""
88-
suai_attrs = get_info_from_suai(val)
89-
90-
return self._unpack_array_helper(
91-
size=suai_attrs.size,
92-
itemsize=suai_attrs.itemsize,
93-
buf=suai_attrs.data,
94-
shape=suai_attrs.shape,
95-
strides=suai_attrs.strides,
96-
ndim=suai_attrs.dimensions,
97-
)
98-
99-
def _unpack_array(self, val, access_type):
100-
"""Deprecated to be removed once NumPy array support in kernels is
101-
removed.
102-
"""
103-
packed_val = val
104-
# Check if the NumPy array is backed by USM memory
105-
usm_mem = utils.has_usm_memory(val)
106-
107-
# If the NumPy array is not USM backed, then copy to a USM memory
108-
# object. Add an entry to the repack_map so that on exit from kernel
109-
# the data from the USM object can be copied back into the NumPy array.
110-
if usm_mem is None:
111-
self._check_for_invalid_access_type(val, access_type)
112-
usm_mem = utils.as_usm_obj(val, queue=self._queue, copy=False)
113-
114-
orig_val = val
115-
packed = False
116-
if not val.flags.c_contiguous:
117-
# If the numpy.ndarray is not C-contiguous
118-
# we pack the strided array into a packed array.
119-
# This allows us to treat the data from here on as C-contiguous.
120-
# While packing we treat the data as C-contiguous.
121-
# We store the reference of both (strided and packed)
122-
# array and during unpacking we use numpy.copyto() to copy
123-
# the data back from the packed temporary array to the
124-
# original strided array.
125-
packed_val = val.flatten(order="C")
126-
packed = True
127-
128-
if access_type == "read_only":
129-
utils.copy_from_numpy_to_usm_obj(usm_mem, packed_val)
130-
elif access_type == "read_write":
131-
utils.copy_from_numpy_to_usm_obj(usm_mem, packed_val)
132-
# Store to the repack map
133-
self._repack_list.append(
134-
_NumPyArrayPackerPayload(
135-
usm_mem, orig_val, packed_val, packed
136-
)
137-
)
138-
elif access_type == "write_only":
139-
self._repack_list.append(
140-
_NumPyArrayPackerPayload(
141-
usm_mem, orig_val, packed_val, packed
142-
)
143-
)
144-
else:
145-
utils.copy_from_numpy_to_usm_obj(usm_mem, packed_val)
146-
self._repack_list.append(
147-
_NumPyArrayPackerPayload(
148-
usm_mem, orig_val, packed_val, packed
149-
)
150-
)
151-
152-
return self._unpack_array_helper(
153-
packed_val.size,
154-
packed_val.dtype.itemsize,
155-
usm_mem,
156-
packed_val.shape,
157-
packed_val.strides,
158-
packed_val.ndim,
159-
)
160-
161-
def _unpack_argument(self, ty, val, access_specifier):
64+
def _unpack_argument(self, ty, val):
16265
"""
16366
Unpack a Python object into one or more ctype values using Numba's
16467
type-inference machinery.
@@ -176,8 +79,6 @@ def _unpack_argument(self, ty, val, access_specifier):
17679

17780
if isinstance(ty, USMNdArray):
17881
return self._unpack_usm_array(val)
179-
elif isinstance(ty, types.Array):
180-
return self._unpack_array(val, access_specifier)
18182
elif ty == types.int64:
18283
return ctypes.c_longlong(val)
18384
elif ty == types.uint64:
@@ -199,48 +100,22 @@ def _unpack_argument(self, ty, val, access_specifier):
199100
else:
200101
raise UnsupportedKernelArgumentError(ty, val, self._pyfunc_name)
201102

202-
def _pack_array(self):
203-
"""
204-
Deprecated to be removed once NumPy array support in kernels is
205-
removed.
206-
"""
207-
for obj in self._repack_list:
208-
utils.copy_to_numpy_from_usm_obj(obj._usm_mem, obj._packed_val)
209-
if obj._packed:
210-
np.copyto(obj._orig_val, obj._packed_val)
211-
212-
def __init__(
213-
self, kernel_name, arg_list, argty_list, access_specifiers_list, queue
214-
) -> None:
103+
def __init__(self, kernel_name, arg_list, argty_list, queue) -> None:
215104
"""Initializes new Packer object and unpacks the input argument list.
216105
217106
Args:
107+
kernel_name (str): The kernel function name.
218108
arg_list (list): A list of arguments to be unpacked
219109
argty_list (list): A list of Numba inferred types for each argument.
220-
access_specifiers_list(list): A list of access specifiers for
221-
NumPy arrays to optimize host to device memory copy.
222-
[Deprecated: can be removed along with NumPy array support]
223-
queue (dpctl.SyclQueue): The SYCL queue where the kernel is to be
224-
executed. The queue is required to allocate USM memory for NumPy
225-
arrays.
226-
[Deprecated: can be removed along with NumPy array support]
227110
"""
228111
self._pyfunc_name = kernel_name
229112
self._arg_list = arg_list
230113
self._argty_list = argty_list
231-
self._queue = queue
232-
# Create a list to store the numpy arrays that need to be
233-
# repacked beoe returning from a kernel.
234-
self._repack_list = []
235114

236115
# loop over the arg_list and generate the kernelargs list
237116
self._unpacked_args = []
238117
for i, val in enumerate(arg_list):
239-
arg = self._unpack_argument(
240-
ty=argty_list[i],
241-
val=val,
242-
access_specifier=access_specifiers_list[i],
243-
)
118+
arg = self._unpack_argument(ty=argty_list[i], val=val)
244119
if type(arg) == list:
245120
self._unpacked_args.extend(arg)
246121
else:
@@ -250,9 +125,3 @@ def __init__(
250125
def unpacked_args(self):
251126
"""Returns the list of unpacked arguments created by a Packer object."""
252127
return self._unpacked_args
253-
254-
@property
255-
def repacked_args(self):
256-
"""Returns the list of NumPy"""
257-
self._pack_array()
258-
return self._repack_list

0 commit comments

Comments
 (0)