Migrating binding to nanobind support#2289
Open
themason2011 wants to merge 1 commit into
Open
Conversation
…ink python methods to their C_API equivalent methods
Contributor
There was a problem hiding this comment.
Pull request overview
This PR migrates the Python extension module from pybind11 to nanobind, and adds C-API cross-reference strings to bindings so IDE hovers and generated stubs can link Python methods back to their underlying C API entry points.
Changes:
- Replace pybind11-based bindings with nanobind equivalents in
src/python/python.cpp, including ndarray conversions and updated binding definitions. - Update the Python CMake build to discover nanobind from the active Python environment and generate a typed stub (
.pyi) +py.typedduring wheel builds. - Remove the pybind11 FetchContent dependency and add
nanobindto Python test requirements.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| test/python/requirements.txt | Adds nanobind as a Python test/build dependency. |
| src/python/python.cpp | Migrates the Python binding layer to nanobind and adds C-API hover/doc annotations. |
| src/python/CMakeLists.txt | Switches to nanobind_add_module and generates a .pyi stub + py.typed in wheel builds. |
| cmake/external/onnxruntime_external_deps.cmake | Removes pybind11 FetchContent and documents nanobind discovery via pip. |
Comment on lines
+28
to
+31
| // This mapping is hand-written: nanobind cannot derive it, since a binding is just a | ||
| // lambda/function pointer from its point of view. To keep it from rotting as new | ||
| // bindings are added, tools/python/check_python_bindings_capi.py fails if any binding | ||
| // lacks one of these annotations (run as part of the Python test suite). |
| .def("shape", &OgaTensor::Shape, OGA_CAPI("OgaTensorGetShape")) | ||
| .def("type", [](OgaTensor& t) { return static_cast<int>(t.Type()); }, OGA_CAPI("OgaTensorGetType")) | ||
| .def("data", [](OgaTensor& t) { return reinterpret_cast<uintptr_t>(t.Data()); }, OGA_CAPI("OgaTensorGetData")) | ||
| .def("as_numpy", [](OgaTensor& t) { return ToNumpy(t); }, OGA_CAPI("OgaTensorGetData")); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Migrating binding to nanobind support. Also includes hover types to link python methods to their C_API equivalent methods