You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
BUG: Crash in np.unravel_index if debug assertions enabled. #29690
Thanks for the report, the problem comes from using an index, but I am not sure if the assert is just bad for that or whether we should change the code. Both passes the tests:
diff --git a/numpy/_core/src/multiarray/nditer_constr.c b/numpy/_core/src/multiarray/nditer_constr.c
index 3ed5cf1a02..4d4504221e 100644
--- a/numpy/_core/src/multiarray/nditer_constr.c+++ b/numpy/_core/src/multiarray/nditer_constr.c@@ -2077,8 +2077,12 @@ npyiter_find_buffering_setup(NpyIter *iter, npy_intp buffersize)
npy_intp *strides = NAD_STRIDES(axisdata);
for (int iop = 0; iop < nop; iop++) {
- /* Check that we set things up nicely (if shape is ever 1) */- assert((axisdata->shape == 1) ? (prev_strides[iop] == strides[iop]) : 1);+ /*+ * Check that we set things up nicely (if shape is ever 1). Except+ * for index operands, which will disrupt coalescing.+ */+ assert((axisdata->shape != 1) || (prev_strides[iop] == strides[iop])+ || (op_itflags[iop] & (NPY_ITER_C_INDEX|NPY_ITER_F_INDEX)));
if (op_single_stride_dims[iop] == idim) {
/* Best case: the strides still collapse for this operand. */
diff --git a/numpy/lib/tests/test_index_tricks.py b/numpy/lib/tests/test_index_tricks.py
index 387fdfec28..81e47ec3df 100644
--- a/numpy/lib/tests/test_index_tricks.py+++ b/numpy/lib/tests/test_index_tricks.py@@ -199,6 +199,11 @@ def test_empty_array_unravel(self):
with assert_raises(ValueError):
np.unravel_index([1], (2, 1, 0))
+ def test_regression_size_1_index(self):+ # actually tests the nditer size one index tracking+ # regression test for gh-29690+ np.unravel_index(np.array([[1, 0, 1, 0]], dtype=np.uint32), (4,))+
class TestGrid:
def test_basic(self):
a = mgrid[-1:1:10j]
This would just allow this. It prevents coalescing dimensions for the buffering, but I don't think that should matter, because I think we we reached here, we shouldn't care about it.
Or, the arguably simpler fix:
which passes the test suite also, and I don't see how it can possibly be wrong. But it might be that the 0 here is (still?) important to prevent the exact thing that I added the assert() to ensure (allow coalescing axes).
Which is all a bit confusing, because I don't see why unravel index would need any of this at all. From what I can tell the only thing might be to ensure the right memory order of the result array at best.
If we want a fix to backport, we should just go with the first version, since it's the no-brainer. The second might be nice to try (since it simplifies code), but there is a chance the tests (and me) is missing the point of the explicit zero here. (And I haven't thought about it very long, but then, this code was never changed, so there is also a good chance its unnecessary.)
Activity
seberg commentedon Sep 6, 2025
Thanks for the report, the problem comes from using an index, but I am not sure if the assert is just bad for that or whether we should change the code. Both passes the tests:
This would just allow this. It prevents coalescing dimensions for the buffering, but I don't think that should matter, because I think we we reached here, we shouldn't care about it.
Or, the arguably simpler fix:
which passes the test suite also, and I don't see how it can possibly be wrong. But it might be that the 0 here is (still?) important to prevent the exact thing that I added the
assert()to ensure (allow coalescing axes).Which is all a bit confusing, because I don't see why
unravelindex would need any of this at all. From what I can tell the only thing might be to ensure the right memory order of the result array at best.If we want a fix to backport, we should just go with the first version, since it's the no-brainer. The second might be nice to try (since it simplifies code), but there is a chance the tests (and me) is missing the point of the explicit zero here. (And I haven't thought about it very long, but then, this code was never changed, so there is also a good chance its unnecessary.)
Disable `LaxBackedNumpyTests.testUnravelIndex` under debug mode build.
Disable `LaxBackedNumpyTests.testUnravelIndex` under debug mode build.
Disable `LaxBackedNumpyTests.testUnravelIndex` under debug mode build.