Skip to content

BUG: np.lexsort MemoryError on empty array with non-standard strides #14228

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
batterseapower opened this issue Aug 8, 2019 · 3 comments · Fixed by #14240
Closed

BUG: np.lexsort MemoryError on empty array with non-standard strides #14228

batterseapower opened this issue Aug 8, 2019 · 3 comments · Fixed by #14240

Comments

@batterseapower
Copy link
Contributor

Numpy 1.17.0 raises MemoryError when using lexsort on an empty array with non-standard strides. If strides is the natural one, or if there is >= 1 element in the input, then it works as expected.

Reproducing code example:

import numpy as np

xs = np.array([], dtype='i8')
assert xs.strides == (8,)
assert np.lexsort((xs,)).shape[0] == 0 # Works

ys = np.array([0], dtype='i8')
ys.strides = (16,)
assert np.lexsort((ys,)).shape[0] == 1 # Works

xs.strides = (16,)
assert np.lexsort((xs,)).shape[0] == 0 # MemoryError

Numpy/Python version information:

1.17.0 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 18:41:36) [MSC v.1900 64 bit (AMD64)]

@batterseapower
Copy link
Contributor Author

batterseapower commented Aug 8, 2019

The problem is that in the needcopy case of PyArray_LexSort, a length 0 array causes failure:

if (N == 0 || maxelsize == 0 || sizeof(npy_intp) == 0) {

No error code at the point when it fails, in which case the function assumes that the cause is memory exhaustion:

The fix is probably to ensure that needcopy=false if N=0.

@mattip mattip changed the title np.lexsort MemoryError on empty array with non-standard strides BUG: np.lexsort MemoryError on empty array with non-standard strides Aug 8, 2019
@mattip
Copy link
Member

mattip commented Aug 8, 2019

Thanks for tracking this down. Is this a regression from previous versions?

Would you like to make a pull request? Please add a failing test to numpy/core/tests/test_regression.py (if it is a regression) or numpy/core/tests/test_multiarray.py near the existing lexsort tests, make sure the test fails with python runtests.py, then add the fix and make sure the test passes.

@batterseapower
Copy link
Contributor Author

batterseapower commented Aug 8, 2019

It definitely does work in 1.13.1 and (I think) in 1.16 so this is a regression. I believe it was introduced in 03052b7 (#13691)

I will make a PR.

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

Successfully merging a pull request may close this issue.

4 participants