Fast conversion from numpy ndarray to vector#38834
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?) |
tscrim
left a comment
There was a problem hiding this comment.
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.) |
tscrim
left a comment
There was a problem hiding this comment.
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. |
tscrim
left a comment
There was a problem hiding this comment.
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. |
sagemathgh-38834: Fast conversion from numpy ndarray to vector 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 ```python Traceback (most recent call last): File "/sage/src/bin/sage-ipython", line 10, in <module> banner() File "/sage/src/sage/misc/banner.py", line 114, in banner print(banner_text(full=True)) ^^^^^^^^^^^^^^^^^^^^^^ File "/sage/src/sage/misc/banner.py", line 69, in banner_text import sage.all File "/sage/src/sage/all.py", line 91, in <module> from sage.algebras.all import * File "/sage/src/sage/algebras/all.py", line 25, in <module> from sage.algebras.quatalg.all import * File "/sage/src/sage/algebras/quatalg/all.py", line 1, in <module> from sage.algebras.quatalg.quaternion_algebra import QuaternionAlgebra File "/sage/src/sage/algebras/quatalg/quaternion_algebra.py", line 76, in <module> from . import quaternion_algebra_cython File "sage/algebras/quatalg/quaternion_algebra_cython.pyx", line 1, in init sage.algebras.quatalg.quaternion_algebra_cython File "sage/matrix/matrix_integer_dense.pyx", line 1, in init sage.matrix.matrix_integer_dense File "sage/modules/vector_integer_dense.pyx", line 1, in init sage.modules.vector_integer_dense AttributeError: type object 'sage.modules.vector_integer_dense.array' has no attribute '__getattr__'. Did you mean: '__setattr__'? ``` Using the [old buffer syntax](https://cython.readthedocs.io/en/0.29.x/sr c/tutorial/numpy.html#working-numpy) is a bit slower. ### 📝 Checklist <!-- Put an `x` in all the boxes that apply. --> - [x] The title is concise and informative. - [x] The description explains in detail what this PR is about. - [ ] I have linked a relevant issue or discussion. - [x] I have created tests covering the changes. - [x] I have updated the documentation and checked the documentation preview. URL: sagemath#38834 Reported by: user202729 Reviewer(s): Travis Scrimshaw
sagemathgh-38834: Fast conversion from numpy ndarray to vector 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 ```python Traceback (most recent call last): File "/sage/src/bin/sage-ipython", line 10, in <module> banner() File "/sage/src/sage/misc/banner.py", line 114, in banner print(banner_text(full=True)) ^^^^^^^^^^^^^^^^^^^^^^ File "/sage/src/sage/misc/banner.py", line 69, in banner_text import sage.all File "/sage/src/sage/all.py", line 91, in <module> from sage.algebras.all import * File "/sage/src/sage/algebras/all.py", line 25, in <module> from sage.algebras.quatalg.all import * File "/sage/src/sage/algebras/quatalg/all.py", line 1, in <module> from sage.algebras.quatalg.quaternion_algebra import QuaternionAlgebra File "/sage/src/sage/algebras/quatalg/quaternion_algebra.py", line 76, in <module> from . import quaternion_algebra_cython File "sage/algebras/quatalg/quaternion_algebra_cython.pyx", line 1, in init sage.algebras.quatalg.quaternion_algebra_cython File "sage/matrix/matrix_integer_dense.pyx", line 1, in init sage.matrix.matrix_integer_dense File "sage/modules/vector_integer_dense.pyx", line 1, in init sage.modules.vector_integer_dense AttributeError: type object 'sage.modules.vector_integer_dense.array' has no attribute '__getattr__'. Did you mean: '__setattr__'? ``` Using the [old buffer syntax](https://cython.readthedocs.io/en/0.29.x/sr c/tutorial/numpy.html#working-numpy) is a bit slower. ### 📝 Checklist <!-- Put an `x` in all the boxes that apply. --> - [x] The title is concise and informative. - [x] The description explains in detail what this PR is about. - [ ] I have linked a relevant issue or discussion. - [x] I have created tests covering the changes. - [x] I have updated the documentation and checked the documentation preview. URL: sagemath#38834 Reported by: user202729 Reviewer(s): Travis Scrimshaw
sagemathgh-39219: Faster conversion from numpy array to matrix mod 2 As in the title. Estimated 50× speedup. ### 📝 Checklist <!-- Put an `x` in all the boxes that apply. --> - [x] The title is concise and informative. - [x] The description explains in detail what this PR is about. - [x] I have linked a relevant issue or discussion. - [x] I have created tests covering the changes. - [x] I have updated the documentation and checked the documentation preview. (Reuses the `numpy_util` module from sagemath#38834 for the utility function) Reverse direction: sagemath#39152 URL: sagemath#39219 Reported by: user202729 Reviewer(s): Travis Scrimshaw
sagemathgh-39219: Faster conversion from numpy array to matrix mod 2 As in the title. Estimated 50× speedup. ### 📝 Checklist <!-- Put an `x` in all the boxes that apply. --> - [x] The title is concise and informative. - [x] The description explains in detail what this PR is about. - [x] I have linked a relevant issue or discussion. - [x] I have created tests covering the changes. - [x] I have updated the documentation and checked the documentation preview. (Reuses the `numpy_util` module from sagemath#38834 for the utility function) Reverse direction: sagemath#39152 URL: sagemath#39219 Reported by: user202729 Reviewer(s): Travis Scrimshaw
As explained, this allows fast conversion from numpy ndarray to some types of vector.
Side note: is
cimport numpy as npsafe? 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