Skip to content
Open
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions dpnp/dpnp_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 3 additions & 5 deletions dpnp/dpnp_iface_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions dpnp/tests/test_arraycreation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
11 changes: 11 additions & 0 deletions dpnp/tests/test_memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading