Skip to content

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

Closed
SylvainCorlay opened this issue Jan 29, 2016 · 12 comments
Closed

Pybind11 vectorization vs numpy broadcasting #89

SylvainCorlay opened this issue Jan 29, 2016 · 12 comments

Comments

@SylvainCorlay
Copy link
Member

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).

@wjakob
Copy link
Member

wjakob commented Jan 29, 2016

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.

@wjakob wjakob closed this as completed Jan 29, 2016
@SylvainCorlay
Copy link
Member Author

Actually in both cases of the Python API (np.vectorize) and the C API (PyUFunc_FromFuncAndData), the loop is done in C.

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.

@wjakob
Copy link
Member

wjakob commented Jan 30, 2016

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.

@SylvainCorlay
Copy link
Member Author

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

@JohanMabille
Copy link
Contributor

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))

@wjakob
Copy link
Member

wjakob commented Feb 9, 2016

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)

@JohanMabille
Copy link
Contributor

Ok, I will open an new issue and probably start working on a patch tonight

@wjakob
Copy link
Member

wjakob commented Feb 16, 2016

@jmabille Any news on this?

@JohanMabille
Copy link
Contributor

@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

@wjakob
Copy link
Member

wjakob commented Feb 16, 2016

great -- please keep me posted.

@ax3l
Copy link
Collaborator

ax3l commented Jul 27, 2023

@JohanMabille following the links here - did you contrib this or is py::vectorize still the way to go? :)

@JohanMabille
Copy link
Contributor

JohanMabille commented Jul 28, 2023

For what I know py::vectorize is still the way to go; I remember contributing to this part of the code (i.e. numpy related) but it has been rewritten many times, so I don't know if the issue is still here.

sschnug pushed a commit to sschnug/pybind11 that referenced this issue Aug 2, 2024
* 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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants