Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/remove unused kernel model fields #1293

Merged
merged 6 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions LICENSES.third-party
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,30 @@ SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.

numba
------------------------
Copyright (c) 2012, Anaconda, Inc.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.

Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
6 changes: 0 additions & 6 deletions numba_dpex/core/datamodel/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,18 +80,12 @@ class USMArrayDeviceModel(StructModel):
def __init__(self, dmm, fe_type):
ndim = fe_type.ndim
members = [
# meminfo never used in kernel, so we don'te care about addrspace
ZzEeKkAa marked this conversation as resolved.
Show resolved Hide resolved
("meminfo", types.MemInfoPointer(fe_type.dtype)),
# parent never used in kernel, so we don'te care about addrspace
("parent", types.pyobject),
("nitems", types.intp),
("itemsize", types.intp),
(
"data",
types.CPointer(fe_type.dtype, addrspace=fe_type.addrspace),
),
# sycl_queue never used in kernel, so we don'te care about addrspace
("sycl_queue", types.voidptr),
("shape", types.UniTuple(types.intp, ndim)),
("strides", types.UniTuple(types.intp, ndim)),
]
Expand Down
6 changes: 0 additions & 6 deletions numba_dpex/core/kernel_interface/arg_pack_unpacker.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,9 @@ def _unpack_usm_array(self, val):
strides = suai_attrs.strides
ndim = suai_attrs.dimensions

# meminfo
unpacked_array_attrs.append(ctypes.c_size_t(0))
# parent
unpacked_array_attrs.append(ctypes.c_size_t(0))
unpacked_array_attrs.append(ctypes.c_longlong(size))
unpacked_array_attrs.append(ctypes.c_longlong(itemsize))
unpacked_array_attrs.append(buf)
# queue: unused and passed as void*
unpacked_array_attrs.append(ctypes.c_size_t(0))
for ax in range(ndim):
unpacked_array_attrs.append(ctypes.c_longlong(shape[ax]))
for ax in range(ndim):
Expand Down
137 changes: 137 additions & 0 deletions numba_dpex/core/kernel_interface/arrayobj.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
# SPDX-FileCopyrightText: 2012 - 2024 Anaconda Inc.
# SPDX-FileCopyrightText: 2024 Intel Corporation
#
# SPDX-License-Identifier: BSD-2-Clause

"""
This package contains implementation of some numpy.np.arrayobj functions without
parent and meminfo fields required, because they don't make sense on device.
These functions intended to be used only in kernel targets like local/private or
usm array view.
"""


from numba.core import cgutils, types
from numba.np import arrayobj as np_arrayobj

from numba_dpex.core import types as dpex_types


def populate_array(array, data, shape, strides, itemsize):
"""
Helper function for populating array structures.
This avoids forgetting to set fields.

*shape* and *strides* can be Python tuples or LLVM arrays.

This is analog of numpy.np.arrayobj.populate_array without parent and
meminfo fields, because they don't make sense on device. This function
intended to be used only in kernel targets.
"""
context = array._context
builder = array._builder
datamodel = array._datamodel
# doesn't matter what this array type instance is, it's just to get the
# fields for the datamodel of the standard array type in this context
standard_array = dpex_types.Array(types.float64, 1, "C")
standard_array_type_datamodel = context.data_model_manager[standard_array]
required_fields = set(standard_array_type_datamodel._fields)
datamodel_fields = set(datamodel._fields)
# Make sure that the presented array object has a data model that is close
# enough to an array for this function to proceed.
if (required_fields & datamodel_fields) != required_fields:
missing = required_fields - datamodel_fields
msg = (
f"The datamodel for type {array._fe_type} is missing "
f"field{'s' if len(missing) > 1 else ''} {missing}."
)
raise ValueError(msg)

intp_t = context.get_value_type(types.intp)
if isinstance(shape, (tuple, list)):
shape = cgutils.pack_array(builder, shape, intp_t)
if isinstance(strides, (tuple, list)):
strides = cgutils.pack_array(builder, strides, intp_t)
if isinstance(itemsize, int):
itemsize = intp_t(itemsize)

attrs = dict(
shape=shape,
strides=strides,
data=data,
itemsize=itemsize,
)

# Calc num of items from shape
nitems = context.get_constant(types.intp, 1)
unpacked_shape = cgutils.unpack_tuple(builder, shape, shape.type.count)
# (note empty shape => 0d array therefore nitems = 1)
for axlen in unpacked_shape:
nitems = builder.mul(nitems, axlen, flags=["nsw"])
attrs["nitems"] = nitems

# Make sure that we have all the fields
got_fields = set(attrs.keys())
if got_fields != required_fields:
raise ValueError("missing {0}".format(required_fields - got_fields))

# Set field value
for k, v in attrs.items():
setattr(array, k, v)

return array


def make_view(context, builder, aryty, ary, return_type, data, shapes, strides):
"""
Build a view over the given array with the given parameters.

This is analog of numpy.np.arrayobj.make_view without parent and
meminfo fields, because they don't make sense on device. This function
intended to be used only in kernel targets.
"""
retary = np_arrayobj.make_array(return_type)(context, builder)
populate_array(
retary, data=data, shape=shapes, strides=strides, itemsize=ary.itemsize
)
return retary


def _getitem_array_generic(
context, builder, return_type, aryty, ary, index_types, indices
):
"""
Return the result of indexing *ary* with the given *indices*,
returning either a scalar or a view.

This is analog of numpy.np.arrayobj._getitem_array_generic without parent
and meminfo fields, because they don't make sense on device. This function
intended to be used only in kernel targets.
"""
dataptr, view_shapes, view_strides = np_arrayobj.basic_indexing(
context,
builder,
aryty,
ary,
index_types,
indices,
boundscheck=context.enable_boundscheck,
)

if isinstance(return_type, types.Buffer):
# Build array view
retary = make_view(
context,
builder,
aryty,
ary,
return_type,
dataptr,
view_shapes,
view_strides,
)
return retary._getvalue()
else:
# Load scalar from 0-d result
assert not view_shapes
return np_arrayobj.load_item(context, builder, aryty, dataptr)
8 changes: 8 additions & 0 deletions numba_dpex/core/targets/kernel_target.py
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,14 @@ def addrspacecast(self, builder, src, addrspace):
def get_ufunc_info(self, ufunc_key):
return self.ufunc_db[ufunc_key]

def populate_array(self, arr, **kwargs):
"""
Populate array structure.
"""
from numba_dpex.core.kernel_interface import arrayobj

return arrayobj.populate_array(arr, **kwargs)


class DpexCallConv(MinimalCallConv):
"""Custom calling convention class used by numba-dpex.
Expand Down
Loading
Loading