Skip to content
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

Reshaped 0-size arrays have non-zero strides #21918

Open
takagi opened this issue Jul 4, 2022 · 3 comments
Open

Reshaped 0-size arrays have non-zero strides #21918

takagi opened this issue Jul 4, 2022 · 3 comments
Labels

Comments

@takagi
Copy link

takagi commented Jul 4, 2022

Describe the issue:

Since NumPy 1.23, the strides of empty (0 size) arrays are set to zero as changed in #21477. However, once such arrays are reshaped, the results have non-zero strides computed in a C-contiguous manner.

>>> import numpy as np
>>> a = np.ones((2, 0, 3))
>>> a.strides
(0, 0, 0)
>>> b = a.reshape((3, 0, 2))
>>> b.strides
(16, 16, 8)  # also should be (0, 0, 0)

NumPy/Python version information:

1.23.0 3.10.0 (default, Oct 15 2021, 11:40:42) [GCC 7.5.0]

@eric-wieser
Copy link
Member

eric-wieser commented Jul 4, 2022

As a reminder of why this matters; I think we concluded that it was undefined behavior for an array to contain a pointer to unallocated memory (which I think b[1:] does in your example); while in practice this doesn't seem to matter today, a future GCC optimization would be free to break things in arbitrary and possibly silent ways.

@seberg
Copy link
Member

seberg commented Jul 4, 2022

I would mostly be worried about crashes if some system thinks that e.g. an overflow in pointer arithmetic should send an abort signal?

@takagi is this important for you to be fixed ASAP for the 1.23.x series, and preferably 1.23.1?

@takagi
Copy link
Author

takagi commented Jul 5, 2022

I have no cases this problem immediately makes harm. (I'm in CuPy development, and found this case in its test)

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

No branches or pull requests

3 participants