From 95d52193a150cc1603a84b24081dc36e724b6f06 Mon Sep 17 00:00:00 2001 From: Nikita Grigorian Date: Fri, 22 May 2026 00:29:33 -0700 Subject: [PATCH 1/3] validate mask shape when indexing arrays --- dpnp/tensor/_slicing.pxi | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/dpnp/tensor/_slicing.pxi b/dpnp/tensor/_slicing.pxi index 2f22894c4b1..73d292fbf90 100644 --- a/dpnp/tensor/_slicing.pxi +++ b/dpnp/tensor/_slicing.pxi @@ -107,6 +107,18 @@ cdef bint _is_boolean(object x) except *: return False +cdef _check_mask_shape(sh : tuple, ma_sh : tuple, Py_ssize_t axis): + cdef Py_ssize_t i, sh_i, m_sh + for i, ma_i in enumerate(ma_sh): + sh_i = sh[axis + i] + if ma_i not in (0, sh_i): + raise IndexError( + "boolean index did not match indexed array along dimension " + f"{axis + i}; dimension is {sh_i} but corresponding boolean " + f"dimension is {ma_i}" + ) + + def _basic_slice_meta(ind, shape : tuple, strides : tuple, offset : int): """ Give basic slicing index `ind` and array layout information produce @@ -353,6 +365,7 @@ def _basic_slice_meta(ind, shape : tuple, strides : tuple, offset : int): new_advanced_ind.append(ind_i) dt_k = ind_i.dtype.kind if dt_k == "b": + _check_mask_shape(shape, ind_i.shape, k) k_new = k + ind_i.ndim else: k_new = k + 1 From 1de524a962ccf66acff4faca998833d261219e56 Mon Sep 17 00:00:00 2001 From: Nikita Grigorian Date: Fri, 22 May 2026 00:39:07 -0700 Subject: [PATCH 2/3] add gh-2929 to changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2a738267346..c4f77a5a76d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -30,6 +30,7 @@ This release is compatible with NumPy 2.4.5. * Fixed incorrect `dpnp.tensor.acosh` result for `complex(±0, NaN)` special case to match the Python Array API specification [#2914](https://github.com/IntelPython/dpnp/pull/2914) * Fixed fork PR documentation workflow failures by implementing conditional publishing strategy: upstream PRs publish to GitHub Pages with comment, fork PRs upload artifacts [#2910](https://github.com/IntelPython/dpnp/pull/2910) * Fixed missing `libtensor` headers in the installed `dpnp` package [#2915](https://github.com/IntelPython/dpnp/pull/2915) +* Fixed some edge cases when indexing `dpnp.tensor.usm_ndarray`s and `dpnp.dpnp_array`s with boolean masks where the corresponding axis of the mask is incompatible with the axis of the indexed array [#2929](https://github.com/IntelPython/dpnp/pull/2929) ### Security From 20ff84d80ef7ac0d68827a6bb4a177d7a951a96e Mon Sep 17 00:00:00 2001 From: Nikita Grigorian Date: Fri, 22 May 2026 02:00:44 -0700 Subject: [PATCH 3/3] add a test for boolean mask validation --- dpnp/tests/tensor/test_usm_ndarray_indexing.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/dpnp/tests/tensor/test_usm_ndarray_indexing.py b/dpnp/tests/tensor/test_usm_ndarray_indexing.py index b81e5456872..5f8d208764d 100644 --- a/dpnp/tests/tensor/test_usm_ndarray_indexing.py +++ b/dpnp/tests/tensor/test_usm_ndarray_indexing.py @@ -2052,3 +2052,13 @@ def test_getitem_impl_fn_invalid_inp(): no_array_inds = (2, 3) with pytest.raises(TypeError): _take_multi_index(x, no_array_inds, 0, 0) + + +def test_boolean_mask_validation(): + x = dpt.reshape(dpt.arange(3**5, dtype="i4"), (3,) * 5) + ii = dpt.asarray(1) + i0 = dpt.asarray(0, dtype="?") + i1 = dpt.asarray(0, dtype="?") + + with pytest.raises(IndexError): + x[ii, i0[dpt.newaxis], ii, i1[dpt.newaxis], :]