-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Pybind11 vectorization vs numpy broadcasting #89
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
Comments
The vectorization in NumPy is just a nice syntax -- the actual loop executes in Python, so it's super-slow. The vectorization in Pybind11 is on the C++ side. |
Actually in both cases of the Python API ( The former case is slow because of the overhead of the python function calls (to the scalar function), but the latter case (the C API to create Ufuncs) should be fast. |
np.vectorize run on a function bound using Pybind11 would involve passing through pybind11 for each function invocation (lots of python API calls), hence the slowness. pybind11::vectorize is there to avoid that. PyUFunc_FromFuncAndData is an alternative, but this does not have some of the amenities of stateful lambda closures -- take a look at the examples, and I'm sure you will agree that the approach in pybind11 is much more powerful. |
Yes, that is really cool. Thanks |
I have a problem with broadcasting in pybind11. Here is a code sample to reproduce the problem: import test_pybind as tp
import numpy as np
x = np.array([1, 1, 1])
y = np.array([1, 1])[:, np.newaxis]
# Arrays are compatible for broadcasting
print(x + y)
# It also works with numpy ufuncs
print(np.logaddexp(x, y))
# But not with the pybind11 vectorize function
print(tp.vec_add(x, y)) |
This would require a rewrite of pybind11::vectorize. It's not on my TODO list but patches are welcomed. (BTW: It's probably better if you create a separate ticket) |
Ok, I will open an new issue and probably start working on a patch tonight |
@jmabille Any news on this? |
@wjakob I have a fix on a private repo in my company, I'm waiting for an authorization to push it on my public fork |
great -- please keep me posted. |
@JohanMabille following the links here - did you contrib this or is |
For what I know |
* chore(deps): bump pypa/cibuildwheel from 2.8.1 to 2.9.0 Bumps [pypa/cibuildwheel](https://github.com/pypa/cibuildwheel) from 2.8.1 to 2.9.0. - [Release notes](https://github.com/pypa/cibuildwheel/releases) - [Changelog](https://github.com/pypa/cibuildwheel/blob/main/docs/changelog.md) - [Commits](pypa/cibuildwheel@v2.8.1...v2.9.0) --- updated-dependencies: - dependency-name: pypa/cibuildwheel dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <[email protected]> * chore: include Python 3.11 Signed-off-by: dependabot[bot] <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Henry Schreiner <[email protected]>
What is the reasoning behind implementing a custom vectorization in pybind11 over the numpy API and helpers to create vectorized functions?
(See http://docs.scipy.org/doc/numpy-1.10.0/reference/c-api.ufunc.html and http://docs.scipy.org/doc/numpy-1.10.1/user/c-info.ufunc-tutorial.html).
The text was updated successfully, but these errors were encountered: