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

Check if numpy could automatically set contiguous flag when passing wrong information #56

Open
songjhaha opened this issue Sep 9, 2022 · 2 comments

Comments

@songjhaha
Copy link
Member

Actually, we don't always set the right contiguous flag when constructing PyArrayInterface. For example, Vector should be both C and F contiguous when converted to ndarray, but we set the flag is_c_style to false when constructing a DynamicArray, but after casting it to ndarray, numpy seems to reset this flag to true, which is correct. Not sure if numpy could always do this check in all situation, need to check carefully.

a = rand(3)
ad = CPython.DynamicArray(a)
ad.is_c_style # false

pya = py_cast(Py, a)
pya.flags
# Py(  C_CONTIGUOUS : True
#   F_CONTIGUOUS : True
# ...

# another example with subarray
b = @view rand(3,3)[2:3, :]
bd = CPython.DynamicArray(y)
bd.is_c_style # false
bd.is_f_style # false
# reconstruct a DynamicArray with wrong flag
bd_wrong = CPython.DynamicArray(bd.arr, bd.eltype, bd.shape, bd.strides, bd.ptr, bd.ndim, bd.itemsize, bd.typekind, true, true, false, bd.perm)
pyb = py_coerce(Py, bd_wrong)
pyb.flags
# Py(  C_CONTIGUOUS : False
#   F_CONTIGUOUS : False
# ...
@songjhaha
Copy link
Member Author

If it's true that numpy could set contiguous flag automatically, the next questions is could we just wrap every StridedArray without copy?

normalized_x = if x isa StridedArray
    x
else
    collect(x)
end

It seems numpy could work correctly if the data pointer, strides, shape and ndims are correct, so it should support all StridedArray. But let me check the mechanism behind numpy first.

@songjhaha
Copy link
Member Author

songjhaha commented Sep 9, 2022

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

No branches or pull requests

1 participant