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

ENH: ascontiguousarray leaves negative strides for length-1 dimensions #23349

Open
shangke1988 opened this issue Mar 6, 2023 · 3 comments
Open

Comments

@shangke1988
Copy link

Describe the issue:

np.ascontiguousarray don't process negative strides of single channle CWH data

Reproduce the code example:

import numpy as np
import torch
a=np.zeros((1,10,20))[::-1]
b=np.ascontiguousarray(a)
b.strides
c=torch.from_numpy(b)

Error message:

>>> a=np.zeros((1,10,20))[::-1]
>>> b=np.ascontiguousarray(a)
>>> b.strides
(-1600, 160, 8)
>>> c=torch.from_numpy(b)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: At least one stride in the given numpy array is negative, and tensors with negative strides are not currently supported. (You can probably work around this by making a 
copy of your array  with array.copy().)

Runtime information:

import sys, numpy; print(numpy.version); print(sys.version)
1.23.5
3.10.9 | packaged by Anaconda, Inc. | (main, Mar 1 2023, 18:18:15) [MSC v.1916 64 bit (AMD64)]

Context for the issue:

No response

@rkern
Copy link
Member

rkern commented Mar 6, 2023

While I do think it would be a good idea for ascontiguousarray() to normalize the stride in such cases, torch.from_numpy() could probably check for this case, too. With a 1-length dimension, that dimension's stride can be anything, and it would still validly describe the same contiguous chunk of memory.

@rkern rkern changed the title BUG: <Please write a comprehensive title after the 'BUG: ' prefix> BUG: ascontiguousarray leaves negative strides for length-1 dimensions Mar 6, 2023
@seberg seberg changed the title BUG: ascontiguousarray leaves negative strides for length-1 dimensions ENH: ascontiguousarray leaves negative strides for length-1 dimensions Apr 25, 2023
@mattip
Copy link
Member

mattip commented May 31, 2023

@Adesoji1 I don't think that relates to the question at hand. Clearly copy can be used. The issue is a bit more subtle as captured by the comment above. Note that using dlpack does normalize the strides, but the resulting tensor is read-only, where torch.from_numpy creates a read-write tensor.

@seberg
Copy link
Member

seberg commented May 31, 2023

Note that using dlpack does normalize the strides

As does the buffer protocol, but mainly due to the fact that cython needed transition time and I suspect there is little reason to remove the normalization at this point.

I don't mind a PR to create a new view in NumPy, but really I think this should be fixed in torch. NumPy fixed the definition of contiguous on this one long before torch even existed.

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

No branches or pull requests

4 participants