Skip to content

Commit 880d71e

Browse files
Merge pull request #871 from IntelPython/fix-getitem
Fix getitem
2 parents c09ac88 + cea9454 commit 880d71e

File tree

5 files changed

+19
-9
lines changed

5 files changed

+19
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
5050

5151
* Fixed `dpctl.lsplatform()` to work correctly when used from within Jupyter notebook [#800](https://github.com/IntelPython/dpctl/pull/800).
5252
* Fixed script to drive debug build [#835](https://github.com/IntelPython/dpctl/pull/835) and fixed code to compile in debug mode [#836](https://github.com/IntelPython/dpctl/pull/836).
53+
* Fixed issue with slicing reported in gh-870 in [#871](https://github.com/IntelPython/dpctl/pull/871).
5354

5455
## [0.12.0] - 03/01/2022
5556

dpctl/tensor/_slicing.pxi

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ cdef object _basic_slice_meta(object ind, tuple shape,
110110
new_strides = list()
111111
k = 0
112112
new_offset = offset
113+
is_empty = False
113114
for i in range(len(ind)):
114115
ind_i = ind[i]
115116
if (ind_i is Ellipsis):
@@ -127,23 +128,27 @@ cdef object _basic_slice_meta(object ind, tuple shape,
127128
str_i = (1 if sh_i == 0 else sl_step) * strides[k]
128129
new_shape.append(sh_i)
129130
new_strides.append(str_i)
130-
if sh_i > 0:
131+
if sh_i > 0 and not is_empty:
131132
new_offset = new_offset + sl_start * strides[k]
133+
if sh_i == 0:
134+
is_empty = True
132135
k = k_new
133136
elif is_integral(ind_i):
134137
ind_i = ind_i.__index__()
135138
if 0 <= ind_i < shape[k]:
136139
k_new = k + 1
137-
new_offset = new_offset + ind_i * strides[k]
140+
if not is_empty:
141+
new_offset = new_offset + ind_i * strides[k]
138142
k = k_new
139143
elif -shape[k] <= ind_i < 0:
140144
k_new = k + 1
141-
new_offset = new_offset + (shape[k] + ind_i) * strides[k]
145+
if not is_empty:
146+
new_offset = new_offset + (shape[k] + ind_i) * strides[k]
142147
k = k_new
143148
else:
144149
raise IndexError(
145-
"Index {0} is out of range for "
146-
"axes {1} with size {2}".format(ind_i, k, shape[k]))
150+
("Index {0} is out of range for "
151+
"axes {1} with size {2}").format(ind_i, k, shape[k]))
147152
new_shape.extend(shape[k:])
148153
new_strides.extend(strides[k:])
149154
return (tuple(new_shape), tuple(new_strides), new_offset)

dpctl/tensor/_usmarray.pyx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -234,10 +234,10 @@ cdef class usm_ndarray:
234234
else:
235235
self._cleanup()
236236
raise ValueError(
237-
"buffer='{}' is not understood. "
237+
("buffer='{}' is not understood. "
238238
"Recognized values are 'device', 'shared', 'host', "
239239
"an instance of `MemoryUSM*` object, or a usm_ndarray"
240-
"".format(buffer))
240+
"").format(buffer))
241241
elif isinstance(buffer, usm_ndarray):
242242
_buffer = buffer.usm_data
243243
else:
@@ -246,8 +246,8 @@ cdef class usm_ndarray:
246246
if (_offset + ary_min_displacement < 0 or
247247
(_offset + ary_max_displacement + 1) * itemsize > _buffer.nbytes):
248248
self._cleanup()
249-
raise ValueError("buffer='{}' can not accomodate the requested "
250-
"array.".format(buffer))
249+
raise ValueError(("buffer='{}' can not accomodate "
250+
"the requested array.").format(buffer))
251251
self.base_ = _buffer
252252
self.data_ = (<char *> (<size_t> _buffer._pointer)) + itemsize * _offset
253253
self.shape_ = shape_ptr

dpctl/tests/test_usm_ndarray_ctor.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,9 @@ def test_empty_slice():
219219
assert Y.shape == X.shape
220220
Z = X[::2]
221221
assert Z.shape == X.shape
222+
X = dpt.empty((0, 4), dtype="u1")
223+
assert X[:, 1].shape == (0,)
224+
assert X[:, 1:3].shape == (0, 2)
222225

223226

224227
def test_slice_constructor_1d():

dpctl/tests/test_usm_ndarray_manipulation.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -835,6 +835,7 @@ def test_concat_1array(data):
835835
"data",
836836
[
837837
[(1,), (1,), 0],
838+
[(0, 2), (0, 2), 1],
838839
[(0, 2), (2, 2), 0],
839840
[(2, 1), (2, 2), -1],
840841
[(2, 2, 2), (2, 1, 2), 1],

0 commit comments

Comments
 (0)