10
10
from numba .core import types
11
11
12
12
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
17
14
from numba_dpex .core .types import USMNdArray
18
15
from numba_dpex .core .utils import get_info_from_suai
19
16
@@ -28,42 +25,31 @@ def __init__(self, usm_mem, orig_val, packed_val, packed) -> None:
28
25
29
26
class Packer :
30
27
"""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.
32
29
"""
33
30
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.
49
34
50
35
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.
56
37
57
38
Returns:
58
- A list a ctype value for each array attribute argument
39
+ list: A list of ctype objects representing the flattened usm_ndarray
59
40
"""
60
41
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
64
51
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
67
53
unpacked_array_attrs .append (ctypes .c_size_t (0 ))
68
54
unpacked_array_attrs .append (ctypes .c_longlong (size ))
69
55
unpacked_array_attrs .append (ctypes .c_longlong (itemsize ))
@@ -75,90 +61,7 @@ def _unpack_array_helper(self, size, itemsize, buf, shape, strides, ndim):
75
61
76
62
return unpacked_array_attrs
77
63
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 ):
162
65
"""
163
66
Unpack a Python object into one or more ctype values using Numba's
164
67
type-inference machinery.
@@ -176,8 +79,6 @@ def _unpack_argument(self, ty, val, access_specifier):
176
79
177
80
if isinstance (ty , USMNdArray ):
178
81
return self ._unpack_usm_array (val )
179
- elif isinstance (ty , types .Array ):
180
- return self ._unpack_array (val , access_specifier )
181
82
elif ty == types .int64 :
182
83
return ctypes .c_longlong (val )
183
84
elif ty == types .uint64 :
@@ -199,48 +100,22 @@ def _unpack_argument(self, ty, val, access_specifier):
199
100
else :
200
101
raise UnsupportedKernelArgumentError (ty , val , self ._pyfunc_name )
201
102
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 :
215
104
"""Initializes new Packer object and unpacks the input argument list.
216
105
217
106
Args:
107
+ kernel_name (str): The kernel function name.
218
108
arg_list (list): A list of arguments to be unpacked
219
109
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]
227
110
"""
228
111
self ._pyfunc_name = kernel_name
229
112
self ._arg_list = arg_list
230
113
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 = []
235
114
236
115
# loop over the arg_list and generate the kernelargs list
237
116
self ._unpacked_args = []
238
117
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 )
244
119
if type (arg ) == list :
245
120
self ._unpacked_args .extend (arg )
246
121
else :
@@ -250,9 +125,3 @@ def __init__(
250
125
def unpacked_args (self ):
251
126
"""Returns the list of unpacked arguments created by a Packer object."""
252
127
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