|
3 | 3 | # SPDX-License-Identifier: Apache-2.0
|
4 | 4 |
|
5 | 5 | import dpnp
|
6 |
| -from numba.core import cgutils, ir, types |
7 |
| -from numba.core.errors import NumbaNotImplementedError |
| 6 | +from numba.core import ir, types |
8 | 7 | from numba.core.ir_utils import get_np_ufunc_typ, mk_unique_var
|
9 | 8 | from numba.core.pythonapi import NativeValue, PythonAPI, box, unbox
|
10 |
| -from numba.np import numpy_support |
11 |
| - |
12 |
| -from numba_dpex.core.exceptions import UnreachableError |
13 |
| -from numba_dpex.core.runtime import context as dpexrt |
14 | 9 |
|
15 | 10 | from .usm_ndarray_type import USMNdArray
|
16 | 11 |
|
@@ -197,117 +192,3 @@ def __allocate__(
|
197 | 192 | out.extend([g_np_assign, attr_assign, typ_var_assign, alloc_assign])
|
198 | 193 |
|
199 | 194 | return out
|
200 |
| - |
201 |
| - |
202 |
| -# TODO: move this section to separate file |
203 |
| -# --------------- Boxing/Unboxing logic for dpnp.ndarray ----------------------# |
204 |
| - |
205 |
| - |
206 |
| -@unbox(USMNdArray) |
207 |
| -def unbox_dpnp_nd_array(typ, obj, c): |
208 |
| - """Converts a dpctl.tensor.usm_ndarray/dpnp.ndarray object to a Numba-dpex |
209 |
| - internal array structure. |
210 |
| -
|
211 |
| - Args: |
212 |
| - typ : The Numba type of the PyObject |
213 |
| - obj : The actual PyObject to be unboxed |
214 |
| - c : The unboxing context |
215 |
| -
|
216 |
| - Returns: A NativeValue object representing an unboxed |
217 |
| - dpctl.tensor.usm_ndarray/dpnp.ndarray |
218 |
| - """ |
219 |
| - # Reusing the numba.core.base.BaseContext's make_array function to get a |
220 |
| - # struct allocated. The same struct is used for numpy.ndarray |
221 |
| - # and dpnp.ndarray. It is possible to do so, as the extra information |
222 |
| - # specific to dpnp.ndarray such as sycl_queue is inferred statically and |
223 |
| - # stored as part of the DpnpNdArray type. |
224 |
| - |
225 |
| - # --------------- Original Numba comment from @ubox(types.Array) |
226 |
| - # |
227 |
| - # This is necessary because unbox_buffer() does not work on some |
228 |
| - # dtypes, e.g. datetime64 and timedelta64. |
229 |
| - # TODO check matching dtype. |
230 |
| - # currently, mismatching dtype will still work and causes |
231 |
| - # potential memory corruption |
232 |
| - # |
233 |
| - # --------------- End of Numba comment from @ubox(types.Array) |
234 |
| - nativearycls = c.context.make_array(typ) |
235 |
| - nativeary = nativearycls(c.context, c.builder) |
236 |
| - aryptr = nativeary._getpointer() |
237 |
| - |
238 |
| - ptr = c.builder.bitcast(aryptr, c.pyapi.voidptr) |
239 |
| - # FIXME : We need to check if Numba_RT as well as DPEX RT are enabled. |
240 |
| - if c.context.enable_nrt: |
241 |
| - dpexrtCtx = dpexrt.DpexRTContext(c.context) |
242 |
| - errcode = dpexrtCtx.arraystruct_from_python(c.pyapi, obj, ptr) |
243 |
| - else: |
244 |
| - raise UnreachableError |
245 |
| - |
246 |
| - # TODO: here we have minimal typechecking by the itemsize. |
247 |
| - # need to do better |
248 |
| - try: |
249 |
| - expected_itemsize = numpy_support.as_dtype(typ.dtype).itemsize |
250 |
| - except NumbaNotImplementedError: |
251 |
| - # Don't check types that can't be `as_dtype()`-ed |
252 |
| - itemsize_mismatch = cgutils.false_bit |
253 |
| - else: |
254 |
| - expected_itemsize = nativeary.itemsize.type(expected_itemsize) |
255 |
| - itemsize_mismatch = c.builder.icmp_unsigned( |
256 |
| - "!=", |
257 |
| - nativeary.itemsize, |
258 |
| - expected_itemsize, |
259 |
| - ) |
260 |
| - |
261 |
| - failed = c.builder.or_( |
262 |
| - cgutils.is_not_null(c.builder, errcode), |
263 |
| - itemsize_mismatch, |
264 |
| - ) |
265 |
| - # Handle error |
266 |
| - with c.builder.if_then(failed, likely=False): |
267 |
| - c.pyapi.err_set_string( |
268 |
| - "PyExc_TypeError", |
269 |
| - "can't unbox usm array from PyObject into " |
270 |
| - "native value. The object maybe of a " |
271 |
| - "different type", |
272 |
| - ) |
273 |
| - return NativeValue(c.builder.load(aryptr), is_error=failed) |
274 |
| - |
275 |
| - |
276 |
| -@box(USMNdArray) |
277 |
| -def box_array(typ, val, c): |
278 |
| - """Boxes a NativeValue representation of USMNdArray/DpnpNdArray type into a |
279 |
| - dpctl.tensor.usm_ndarray/dpnp.ndarray PyObject |
280 |
| -
|
281 |
| - Args: |
282 |
| - typ: The representation of the USMNdArray/DpnpNdArray type. |
283 |
| - val: A native representation of a Numba USMNdArray/DpnpNdArray type |
284 |
| - object. |
285 |
| - c: The boxing context. |
286 |
| -
|
287 |
| - Returns: A Pyobject for a dpctl.tensor.usm_ndarray/dpnp.ndarray boxed from |
288 |
| - the Numba-dpex native value. |
289 |
| - """ |
290 |
| - if c.context.enable_nrt: |
291 |
| - np_dtype = numpy_support.as_dtype(typ.dtype) |
292 |
| - dtypeptr = c.env_manager.read_const(c.env_manager.add_const(np_dtype)) |
293 |
| - dpexrtCtx = dpexrt.DpexRTContext(c.context) |
294 |
| - newary = dpexrtCtx.usm_ndarray_to_python_acqref( |
295 |
| - c.pyapi, typ, val, dtypeptr |
296 |
| - ) |
297 |
| - |
298 |
| - if not newary: |
299 |
| - c.pyapi.err_set_string( |
300 |
| - "PyExc_TypeError", |
301 |
| - "could not box native array into a dpnp.ndarray PyObject.", |
302 |
| - ) |
303 |
| - |
304 |
| - # Steals NRT ref |
305 |
| - # Refer: |
306 |
| - # numba.core.base.nrt -> numba.core.runtime.context -> decref |
307 |
| - # The `NRT_decref` function is generated directly as LLVM IR inside |
308 |
| - # numba.core.runtime.nrtdynmod.py |
309 |
| - c.context.nrt.decref(c.builder, typ, val) |
310 |
| - |
311 |
| - return newary |
312 |
| - else: |
313 |
| - raise UnreachableError |
0 commit comments