diff --git a/CHANGELOG.md b/CHANGELOG.md index 7e067b3783b..96649123cfa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -60,6 +60,7 @@ Also, that release drops support for Python 3.9, making Python 3.10 the minimum * Suppressed a potential deprecation warning triggered during import of the `dpctl.tensor` module [#2709](https://github.com/IntelPython/dpnp/pull/2709) * Corrected a phonetic spelling issue due to incorrect using of `a nd` in docstrings [#2719](https://github.com/IntelPython/dpnp/pull/2719) * Resolved an issue causing `dpnp.linspace` to return an incorrect output shape when inputs were passed as arrays [#2712](https://github.com/IntelPython/dpnp/pull/2712) +* Resolved an issue where `dpnp` always returns the base allocation pointer, when the view start is expected [#2651](https://github.com/IntelPython/dpnp/pull/2651) ### Security diff --git a/dpnp/dpnp_array.py b/dpnp/dpnp_array.py index c42c9f8a2cb..6a2b2fd1977 100644 --- a/dpnp/dpnp_array.py +++ b/dpnp/dpnp_array.py @@ -98,6 +98,7 @@ def __init__( # or as USM memory allocation if isinstance(buffer, dpnp_array): buffer = buffer.get_array() + offset += buffer._element_offset if dtype is None and hasattr(buffer, "dtype"): dtype = buffer.dtype diff --git a/dpnp/dpnp_iface_indexing.py b/dpnp/dpnp_iface_indexing.py index 6375e5320e3..6e7ab778299 100644 --- a/dpnp/dpnp_iface_indexing.py +++ b/dpnp/dpnp_iface_indexing.py @@ -721,23 +721,21 @@ def diagonal(a, offset=0, axis1=0, axis2=1): a_straides = a.strides n, m = a_shape[-2:] st_n, st_m = a_straides[-2:] - # pylint: disable=W0212 - a_element_offset = a.get_array()._element_offset # Compute shape, strides and offset of the resulting diagonal array # based on the input offset if offset == 0: out_shape = a_shape[:-2] + (min(n, m),) out_strides = a_straides[:-2] + (st_n + st_m,) - out_offset = a_element_offset + out_offset = 0 elif 0 < offset < m: out_shape = a_shape[:-2] + (min(n, m - offset),) out_strides = a_straides[:-2] + (st_n + st_m,) - out_offset = a_element_offset + st_m * offset + out_offset = st_m * offset else: out_shape = a_shape[:-2] + (0,) out_strides = a_straides[:-2] + (1,) - out_offset = a_element_offset + out_offset = 0 return dpnp_array( out_shape, buffer=a, strides=out_strides, offset=out_offset diff --git a/dpnp/tests/test_arraycreation.py b/dpnp/tests/test_arraycreation.py index 1e20df9425b..65747488310 100644 --- a/dpnp/tests/test_arraycreation.py +++ b/dpnp/tests/test_arraycreation.py @@ -268,6 +268,16 @@ def test_linalg_trace(self, dtype, offset): expected = numpy.linalg.trace(a, offset=offset, dtype=dtype) assert_equal(result, expected) + @pytest.mark.parametrize("offset", [-1, 0, 1]) + def test_ndarray_offset(self, offset): + ia = dpnp.arange(8, dtype=dpnp.uint8).reshape((2, 2, 2)) + ia = dpnp.ndarray((2, 2), buffer=ia, offset=1) + a = ia.asnumpy() + + result = dpnp.linalg.trace(ia, offset=offset) + expected = numpy.linalg.trace(a, offset=offset) + assert_equal(result, expected) + @pytest.mark.parametrize( "func, args", diff --git a/dpnp/tests/test_memory.py b/dpnp/tests/test_memory.py index ce9c7e60f03..1bc0da8c153 100644 --- a/dpnp/tests/test_memory.py +++ b/dpnp/tests/test_memory.py @@ -31,3 +31,14 @@ def test_ndarray_from_data(self): a = dpnp.empty(5) b = dpnp.ndarray(a.shape, buffer=a.data) assert b.data.ptr == a.data.ptr + + def test_view_non_zero_offset(self): + n, m = 2, 8 + plane = n * m + + a = dpnp.empty(4 * plane) + sl = a[plane:] # non-zero offset view + + pl = dpnp.ndarray((n, m), dtype=a.dtype, buffer=sl) + assert pl.data.ptr == sl.data.ptr + assert a.data.ptr != sl.data.ptr