From 9cdf55d33cdbb604d10696ba0cfcb5268e9c18eb Mon Sep 17 00:00:00 2001 From: "Komarova, Evseniia" Date: Wed, 6 Nov 2024 22:33:26 +0100 Subject: [PATCH] Add dpnp.byte_bounds implementation --- dpnp/__init__.py | 4 +++ dpnp/dpnp_iface_utils.py | 70 ++++++++++++++++++++++++++++++++++++++++ tests/test_usm_utils.py | 42 ++++++++++++++++++++++++ 3 files changed, 116 insertions(+) create mode 100644 dpnp/dpnp_iface_utils.py create mode 100644 tests/test_usm_utils.py diff --git a/dpnp/__init__.py b/dpnp/__init__.py index 165e57a1bb4..4339bd56c8b 100644 --- a/dpnp/__init__.py +++ b/dpnp/__init__.py @@ -54,9 +54,13 @@ from dpnp.dpnp_iface_types import * from dpnp.dpnp_iface import * from dpnp.dpnp_iface import __all__ as _iface__all__ +from dpnp.dpnp_iface_utils import * +from dpnp.dpnp_iface_utils import __all__ as _ifaceutils__all__ from dpnp._version import get_versions __all__ = _iface__all__ +__all__ += _ifaceutils__all__ + __version__ = get_versions()["version"] del get_versions diff --git a/dpnp/dpnp_iface_utils.py b/dpnp/dpnp_iface_utils.py new file mode 100644 index 00000000000..06ad9de73a3 --- /dev/null +++ b/dpnp/dpnp_iface_utils.py @@ -0,0 +1,70 @@ +# -*- coding: utf-8 -*- +# ***************************************************************************** +# Copyright (c) 2016-2024, Intel Corporation +# 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. +# ***************************************************************************** + +""" +Interface of the utils function of the DPNP + +Notes +----- +This module is a face or public interface file for the library + +""" + +import dpnp + +__all__ = ["byte_bounds"] + + +def byte_bounds(a): + """ + Returns a 2-tuple with pointers to the end-points of the array. + + For full documentation refer to + :obj:`dpctl.tensor.usm_ndarray._byte_bounds`. + + Parameters + ---------- + a : usm_ndarray + Input array + + Returns + ------- + (beg, end) : tuple of 2 integers + The first integer is the first byte of the array, the second + integer is just past the last byte of the array + + Examples + -------- + >>> import dpnp as np + >>> x = np.ones((3, 10, 7)) + >>> y = np.flip(x[:, 1::2], axis=1) + >>> beg_p, end_p = np.byte_bounds(y) + Bytes taken to store this array + >>> bytes_extent = end_p - beg_p + + """ + + return dpnp.get_usm_ndarray(a)._byte_bounds diff --git a/tests/test_usm_utils.py b/tests/test_usm_utils.py new file mode 100644 index 00000000000..b68821332f6 --- /dev/null +++ b/tests/test_usm_utils.py @@ -0,0 +1,42 @@ +import dpctl.tensor as dpt +from numpy.testing import assert_equal + +import dpnp + + +def test_byte_bounds(): + a = dpt.usm_ndarray((3, 4), dtype="int64", buffer="shared") + values = dpt.arange(12, dtype="int64") + for i in range(3): + a[i, :] = values[i * 4 : (i + 1) * 4] + low, high = dpnp.byte_bounds(a) + assert_equal(high - low, a.size * a.itemsize) + + +def test_unusual_order_positive_stride(): + a = dpt.usm_ndarray((3, 4), dtype="int64", buffer="shared") + values = dpt.arange(12, dtype="int64") + for i in range(3): + a[i, :] = values[i * 4 : (i + 1) * 4] + b = a.T + low, high = dpnp.byte_bounds(b) + assert_equal(high - low, b.size * b.itemsize) + + +def test_unusual_order_negative_stride(): + a = dpt.usm_ndarray((3, 4), dtype="int64", buffer="shared") + values = dpt.arange(12, dtype="int64") + for i in range(3): + a[i, :] = values[i * 4 : (i + 1) * 4] + b = a.T[::-1] + low, high = dpnp.byte_bounds(b) + assert_equal(high - low, b.size * b.itemsize) + + +def test_strided(): + a = dpt.usm_ndarray(12, dtype="int64", buffer="shared") + a[:] = dpt.arange(12, dtype="int64") + b = a[::2] + low, high = dpnp.byte_bounds(b) + expected_byte_diff = b.size * 2 * b.itemsize - b.itemsize + assert_equal(high - low, expected_byte_diff)