-
-
Notifications
You must be signed in to change notification settings - Fork 481
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
Fast conversion from numpy ndarray to vector #38834
base: develop
Are you sure you want to change the base?
Conversation
bd5f9f8
to
52d7c0c
Compare
Now use old buffer syntax, it should work. Similar changes could be applied to |
52d7c0c
to
dff5068
Compare
Documentation preview for this PR (built with commit 7c54e21; changes) is ready! 🎉 |
What am I supposed to do with the test? Move all numpy-related functions to a separate file and lazily import it? (but then won't the construction of any vector import numpy anyway?) |
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 believe this Cython module gets imported at startup, which since you now have a global cimport of numpy causes the test to fail. Indeed, if I add a cimport numpy
to integer.pyx
(which is 100% imported at startup), then that test fails. So either you need to make everything local to that method or make sure this file is not loaded at startup.
dff5068
to
90053d4
Compare
Lazy import appears to be difficult. I end up with a hacky solution, where I make the function in Python, and cast the pointer to I tried to use Annoyingly enough there doesn't seem to be It may be interesting to test if precompute the word and use the combination to set the whole word in at once would be faster. (Maybe it will, if for nothing other than loop unrolling.) |
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.
Thank you. This is okay for me.
For your optimizations, that is beyond my technical abilities and knowledge. Although adding such functions would need to be done upstream I believe.
In https://github.com/sagemath/sage/actions/runs/11736415975/job/32695629971, it gives Maybe the test failure is caused by the pxd file not exist… not sure, it works locally for me. (After all we're importing from Python instead of from C, why is the pxd needed?) I'll just push the commit to test it out, don't merge yet (until tests pass). |
Should be done now. Test failures are:
First one #38940 , the rest are clearly written as unrelated. |
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 feel like there should also be a .pxd
file, right? Well, I guess that is not strictly necessary given the setup. I forget exactly when they are needed.
If I understood correctly you need it when you want I can add for consistency though. (though it won't be tested so is also dangerous) The alternative is to leave documentation in the pyx explaining what's going on with the pxd being missing and to add it when useful. |
Let's add it for consistency and in case someone down the road has a use case for it. Thank you. |
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.
Thank you. LGTM.
As explained, this allows fast conversion from numpy ndarray to some types of vector.
Side note: is
cimport numpy as np
safe? what happens if Sage is built without numpy?Extra context: I originally tried to use memory view, but for some reason it doesn't work: cython/cython#6442
Using the old buffer syntax is a bit slower.
📝 Checklist