halide / Halide

a language for fast, portable data-parallel computation
https://halide-lang.org
Other
5.83k stars 1.07k forks source link

Consider upgrading pybind11 to nanobind #6662

Open steven-johnson opened 2 years ago

steven-johnson commented 2 years ago

Per https://github.com/wjakob/nanobind:

TLDR: nanobind bindings compile ~2-3× faster, producing ~3× smaller binaries, with up to ~8× lower overheads on runtime performance (when comparing to pybind11 with -Os size optimizations).

steven-johnson commented 2 years ago

Update: reading further, there are at least two issues with the current (Mar 2022) version of nanobind:

We rely (heavily) on numpy and buffer protocol; the notes currently say it's "not clear" whether they will be reintroduced.

rootjalex commented 2 years ago

Looks like they’ve been reintroduced! https://github.com/wjakob/nanobind/blob/master/docs/tensor.md

alexreinking commented 2 years ago

This does seem worth evaluating now that the buffer protocol is back.

steven-johnson commented 2 years ago

Agreed, but availability could be an issue -- pybind11 is widely available on many systems already, and (more importantly to me personally), nanobind isn't available inside Google yet, so we'd have to get it imported and updated on a regular basis if we adopted it.)

steven-johnson commented 1 year ago

FYI, I took a stab at doing a basic adaptation, but it looks like there are a number of API changes that aren't well-documented yet (eg buffer->tensor) and other things that we use that are (still) missing in nanobind -- see https://github.com/wjakob/nanobind/discussions/77 for a handful of things I noticed. I pushed my (very very very rough) work-in-progress to the srj/nanobind branch if anyone is interested, but right now it looks like nanobind probably needs more work to be ready for us (and we'd probably have to contribute that work).

steven-johnson commented 1 year ago

(One part of nanobind that is better than pybind11 is that it appears to have support for dlpack built in, which is something we really want/need, not sure if it will be backported to pybind11)

alexreinking commented 3 weeks ago

It's been almost two years. Is nanobind ready? It now provides a nb::ndarray argument type that is supposedly even more powerful than the buffer protocol binding was. They claim support with "NumPy, PyTorch, TensorFlow, JAX, etc."

I am particularly interested in nanobind for its support for the Python 3.12+ Stable ABI, which will dramatically reduce the number of wheels we'll need to publish.

https://nanobind.readthedocs.io/en/latest/porting.html#removed

https://nanobind.readthedocs.io/en/latest/ndarray.html

steven-johnson commented 3 weeks ago

I talked with a Xoogler who until recently maintained PyBind for Google; he strongly discouraged us from moving to nanobind; he considered it a 'hobby' project, and PyBind 3.x (on the way) will contain most of the improvements we want.