-
-
Notifications
You must be signed in to change notification settings - Fork 734
test_serialize_numba: Workaround issue with np.empty_like in NumPy 1.23 #7089
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
Conversation
In NumPy 1.23, the strides of empty arrays are 0 instead of the item size, due to numpy/numpy#21477 - however, `np.empty_like` seems to create non-zero-strided arrays from a zero-strided empty array, and copying to the host from a device array with zero strides fails a compatibility check in Numba. This commit works around the issue by calling `copy_to_host()` with no arguments, allowing Numba to create an array on the host that is compatible with the device array - the resulting implementation is functionally equivalent and slightly simpler, so I believe this change could remain permanant rather than requiring a revert later.
Can one of the admins verify this patch? Admins can comment |
add to allowlist |
ok to test |
Just to confirm, we expect this to also work with NumPy 1.22.X as well, correct ? |
Yes. |
Presumably the fails here are unrelated to this PR?
|
Unit Test ResultsSee test report for an extended history of previous test failures. This is useful for diagnosing flaky tests. 15 files ±0 15 suites ±0 6h 29m 8s ⏱️ - 5m 20s Results for commit 84d776a. ± Comparison against base commit ee43309. ♻️ This comment has been updated with latest results. |
I think this is ok to merge in but I will wait for until morning to do so. In the mean time I filed to #7092 to track the unrelated test failure we see here: |
All tests are passing now. Merging in. |
Related Numba issue: numba/numba#8477 |
Since NumPy 1.23, it is possible for zero-length ndarrays to have different strides (e.g. `(0,)`, and `(8,)`). If we attempt to copy from a zero-length device array to a zero-length host array where the strides differ, our compatibility check fails because it compares strides. This commit fixes the issue by only considering strides when checking compatibility of nonzero-length arrays. I believe this to be valid because the following works normally with NumPy 1.23: ```python import numpy as np ary1 = np.arange(0) ary2 = np.ndarray((0,), buffer=ary1.data) ary3 = np.empty_like(ary2) ary3[:] = ary2 ary3[...] = ary2 np.copyto(ary2, ary3) ``` i.e. copying zero-length arrays with different strides generally works as expected. The included test is written in such a way that it should test this change in behaviour regardless of the installed NumPy version - we explicitly construct zero-length device and host arrays with differing strides. The additional sanity check ensures that the host array has the strides we expect, just in case there is some version of NumPy in which setting the strides explicitly didn't result in the expected strides - I have observed that requesting nonzero strides for a zero length array can result still in zero strides (a separate but related behaviour), so this sanity check is provided to account for any other unexpected behaviour of this nature. I have tested locally with NumPy 1.22 and 1.23 (pre- and post-changes to strides). See also dask/distributed#7089 where a workaround for an observation of this issue was needed. This would not be needed with the fix in this commit.
…ask#7089) In NumPy 1.23, the strides of empty arrays are 0 instead of the item size, due to numpy/numpy#21477 - however, `np.empty_like` seems to create non-zero-strided arrays from a zero-strided empty array, and copying to the host from a device array with zero strides fails a compatibility check in Numba. This commit works around the issue by calling `copy_to_host()` with no arguments, allowing Numba to create an array on the host that is compatible with the device array - the resulting implementation is functionally equivalent and slightly simpler, so I believe this change could remain permanant rather than requiring a revert later.
In NumPy 1.23, the strides of empty arrays are 0 instead of the item size, due to numpy/numpy#21477 - however,
np.empty_like
seems to create non-zero-strided arrays from a zero-strided empty array, and copying to the host from a device array with zero strides fails a compatibility check in Numba.This PR works around the issue by calling
copy_to_host()
with no arguments, allowing Numba to create an array on the host that is compatible with the device array - the resulting implementation is functionally equivalent and slightly simpler, so I believe this change could remain permanant rather than requiring a revert later.pre-commit run --all-files