Skip to content

Migrating binding to nanobind support#2289

Open
themason2011 wants to merge 1 commit into
mainfrom
masoncorey/nanobind-stubgen-upgrade
Open

Migrating binding to nanobind support#2289
themason2011 wants to merge 1 commit into
mainfrom
masoncorey/nanobind-stubgen-upgrade

Conversation

@themason2011

Copy link
Copy Markdown

Migrating binding to nanobind support. Also includes hover types to link python methods to their C_API equivalent methods

…ink python methods to their C_API equivalent methods
Copilot AI review requested due to automatic review settings July 10, 2026 18:33
@themason2011 themason2011 requested a review from a team as a code owner July 10, 2026 18:33

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.typed during wheel builds.
  • Remove the pybind11 FetchContent dependency and add nanobind to 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 thread src/python/python.cpp
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).
Comment thread src/python/python.cpp
.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"));
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

Successfully merging this pull request may close these issues.

2 participants