-
Notifications
You must be signed in to change notification settings - Fork 626
Add NumPy 2.0 support #2599
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
Add NumPy 2.0 support #2599
Conversation
|
Ah, wheel builds are failing, which I did not test. It looks like maybe the NumPy 2.0-compatible wheels will need to be Python 3.9+. |
|
Do we build 3.8? I'm ok only doing 3.9+ |
|
Yes, as of 0.14.2 there are 3.8 wheels. The first thing I noticed though was the sdist was apparently being built with 3.7 😮. |
|
I'm marking this ready for review, as I was able to run napari (+tests) without noticing any (vispy-related) issues. It would be great to have this in before the numpy 2.0 release on June 16, but of course I want to make sure it's actually ready so any review is appreciated! |
|
Running tests in napari does reveal some deprecation warnings coming through vispy: There is some discussion here about what should be used instead: |
|
Looks like we should wait for |
|
I'm not sure we'd want to use it, as that would make it 2.0-only, right? |
|
True... I guess we should use their example like you did in the napari PR: cross2d = arr1[..., 0] * arr2[..., 1] - arr1[..., 1] * arr2[..., 0]A bit ugly to lose the explicit "cross", but oh well |
|
Yeah, it's unfortunate but I understand why it's changing. My concern is if any of these functions can take 2D or 3D input, but the behavior is quite different so I doubt it. I guess we could make our own |
👆 this is the right way I think. Abstract away the unreadability and make clear the intent of the code. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great, I like the new function, much more readable.
| # see https://numpy.org/devdocs/dev/depending_on_numpy.html#numpy-2-0-specific-advice | ||
| "numpy>=2.0.0rc2", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the link they suggest adding a test against the oldest supported numpy version. Am I understanding correctly that before we were getting this "for free" because we compiled with the oldest numpy, but now we don't anymore and so maybe we should add this test?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we were getting that for free before either, as I believe this should only affect the build configuration. It seems like they're recommending new tests added to test against the minimum supported version of numpy. Do you think we should add that here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's happening in this branch now is that we're building against numpy 2.0.0rc2 and testing against the latest released version (1.26.4).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got it... No need to add them here then I guess :) We can do in a follow up!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we'll need to do some testing to figure out what the minimum supported version of numpy should even be. Right now it's left unspecified in setup.py.
| from libc.math cimport sqrt | ||
| cimport cython | ||
|
|
||
| np.import_array() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is also from ruff fixes?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually no, this is from a warning. I thought it was related to numpy 2.0, but now that I look maybe not.
See this example in the Cython docs:
# "cimport" is used to import special compile-time information
# about the numpy module (this is stored in a file numpy.pxd which is
# distributed with Numpy).
# Here we've used the name "cnp" to make it easier to understand what
# comes from the cimported module and what comes from the imported module,
# however you can use the same name for both if you wish.
cimport numpy as cnp
# It's necessary to call "import_array" if you use any part of the
# numpy PyArray_* API. From Cython 3, accessing attributes like
# ".shape" on a typed Numpy array use this API. Therefore we recommend
# always calling "import_array" whenever you "cimport numpy"
cnp.import_array()
|
Since tests are passing, we should probably merge and release this sooner rather than later since NumPy 2 is going to be out in a couple of days... 💣 😅 |
|
Yep, seems good to me. Let's go! |
|
Released |
|
Thanks! Fingers are crossed 😬 |
Alternative to: #2549 This PR drops the monkeypatch because: - The fix in ctypes for cdlls in bigsur is out in python >3.8: https://bugs.python.org/issue41100 - The remaining issue was python 3.8, which has been dropped in #2599 Also, changes the name of the library to the proper `Quartz`, as per: https://developer.apple.com/library/archive/documentation/MacOSX/Conceptual/OSX_Technology_Overview/SystemFrameworks/SystemFrameworks.html The lower case may work due with case insensitive file system, but as noted by Ashley, with caching, it may fail.
This introduces numpy 2.0 support. napari has started looking into support, and ran into a few small issues using vispy testing against it. See the napari zulip thread for some discussion and relevant links.
The process for adding support in this PR was:
copykwarg innp.arrayvispy.util.np_copy_if_neededvalue (Falsefor numpy < 1.28,Nonefor numpy >= 2.0)copy=Falseused inno.arrayand remove itcopy=copyused innp.array, replace withcopy=vispy.util.np_copy_if_neededforward compatibility. This was accomplished by depending on
oldest-supported-numpy.
numpy>=2.0.0rc1, and the build will be compatible with both 1.xand 2.x.
[build-system]section ofpyproject.tomlI think this is ready for review, but I'm going to try creating a wheel and using to run napari tests with numpy 2.0 as well. I'll mark it non-draft after I do that.