Skip to content

BUG: Crash in np.unravel_index if debug assertions enabled. #29690

Closed

Description

@hawkinsp

Describe the issue:

In a debug build of NumPy:

In [1]: import numpy as np

In [2]: np.unravel_index(np.array([[1, 0, 1, 0]], dtype=np.uint32), (4,))
python3: ../numpy/_core/src/multiarray/nditer_constr.c:2081: npyiter_find_buffering_setup: Assertion `(axisdata->shape == 1) ? (prev_strides[iop] == strides[iop]) : 1' failed.

Reproduce the code example:

import numpy as np
np.unravel_index(np.array([[1, 0, 1, 0]], dtype=np.uint32), (4,))

Error message:

See above.

Python and NumPy Versions:

2.4.0.dev0+git20250905.366c0a3
3.12.10 (main, Apr 9 2025, 04:03:51) [Clang 20.1.0 ]

This also reproduces in 2.3.2.

Runtime Environment:

[{'numpy_version': '2.4.0.dev0+git20250905.366c0a3',
'python': '3.12.10 (main, Apr 9 2025, 04:03:51) [Clang 20.1.0 ]',
'uname': uname_result(system='Linux', node='redacted', release='6.12.32-redacted-amd64', version='#1 SMP PREEMPT_DYNAMIC Debian 6.12.32-redacted (2025-06-26)', machine='x86_64')},
{'simd_extensions': {'baseline': [], 'found': [], 'not_found': []}},
{'architecture': 'Zen',
'filepath': '/usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.29.so',
'internal_api': 'openblas',
'num_threads': 128,
'prefix': 'libopenblas',
'threading_layer': 'pthreads',
'user_api': 'blas',
'version': '0.3.29'}]

Context for the issue:

Found while debugging a crash in the JAX test suite.

Activity

  1. seberg commented on Sep 6, 2025

    @seberg
    Member

    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:

    diff --git a/numpy/_core/src/multiarray/nditer_constr.c b/numpy/_core/src/multiarray/nditer_constr.c
    index 4d4504221e..73b9767053 100644
    --- a/numpy/_core/src/multiarray/nditer_constr.c
    +++ b/numpy/_core/src/multiarray/nditer_constr.c
    @@ -2452,13 +2452,7 @@ npyiter_compute_index_strides(NpyIter *iter, npy_uint32 flags)
             indexstride = 1;
             for(idim = 0; idim < ndim; ++idim, NIT_ADVANCE_AXISDATA(axisdata, 1)) {
                 npy_intp shape = NAD_SHAPE(axisdata);
    -
    -            if (shape == 1) {
    -                NAD_STRIDES(axisdata)[nop] = 0;
    -            }
    -            else {
    -                NAD_STRIDES(axisdata)[nop] = indexstride;
    -            }
    +            NAD_STRIDES(axisdata)[nop] = indexstride;
                 indexstride *= shape;
             }
         }
    @@ -2468,13 +2462,7 @@ npyiter_compute_index_strides(NpyIter *iter, npy_uint32 flags)
             indexstride = 1;
             for(idim = 0; idim < ndim; ++idim, NIT_ADVANCE_AXISDATA(axisdata, -1)) {
                 npy_intp shape = NAD_SHAPE(axisdata);
    -
    -            if (shape == 1) {
    -                NAD_STRIDES(axisdata)[nop] = 0;
    -            }
    -            else {
    -                NAD_STRIDES(axisdata)[nop] = indexstride;
    -            }
    +            NAD_STRIDES(axisdata)[nop] = indexstride;
                 indexstride *= shape;
             }
         }

    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.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions