-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace pybind11 with nanobind in frontend (#1173)
**Context:** The Catalyst frontend contains a small Python extension module at [frontend/catalyst/utils/wrapper.cpp](https://github.com/PennyLaneAI/catalyst/blob/main/frontend/catalyst/utils/wrapper.cpp), which is an importable Python module written in C/C++ used to wrap the entry point of compiled programs. The Python-C++ bindings were originally implemented using pybind11. This PR is part of a larger effort to replace all pybind11 code with nanobind. **Description of the Change:** This change replaces all the pybind11 code in the frontend with the equivalent nanobind objects and operations. It was also necessary to modify the frontend build system to build the `wrapper` module using CMake for compatibility with nanobind, rather than in `setup.py` with the `intree_extensions` setuptools utility included with pybind11. **Benefits:** See Epic [68472](https://app.shortcut.com/xanaduai/epic/68472) for a list of nanobind's benefits. ----- [[sc-72837](https://app.shortcut.com/xanaduai/story/72837/replace-pybind11-with-nanobind-in-the-frontend)] --------- Co-authored-by: Lee James O'Riordan <[email protected]>
- Loading branch information
1 parent
24cd67d
commit 143564a
Showing
12 changed files
with
257 additions
and
81 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
cmake_minimum_required(VERSION 3.26) | ||
|
||
project(catalyst_frontend LANGUAGES CXX) | ||
|
||
add_subdirectory(catalyst) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
add_subdirectory(utils) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
set(CMAKE_CXX_STANDARD 17) | ||
set(CMAKE_CXX_STANDARD_REQUIRED ON) | ||
|
||
find_package(Python 3 | ||
REQUIRED COMPONENTS Interpreter Development.Module | ||
OPTIONAL_COMPONENTS Development.SABIModule) | ||
|
||
# nanobind suggests including these lines to configure CMake to perform an optimized release build | ||
# by default unless another build type is specified. Without this addition, binding code may run | ||
# slowly and produce large binaries. | ||
# See https://nanobind.readthedocs.io/en/latest/building.html#preliminaries | ||
if (NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES) | ||
set(CMAKE_BUILD_TYPE Release CACHE STRING "Choose the type of build." FORCE) | ||
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release" "MinSizeRel" "RelWithDebInfo") | ||
endif() | ||
|
||
# Detect the installed nanobind package and import it into CMake | ||
execute_process( | ||
COMMAND "${Python_EXECUTABLE}" -m nanobind --cmake_dir | ||
OUTPUT_VARIABLE nanobind_ROOT OUTPUT_STRIP_TRAILING_WHITESPACE) | ||
|
||
find_package(nanobind CONFIG REQUIRED) | ||
|
||
# Get the NumPy include directory | ||
execute_process( | ||
COMMAND "${Python_EXECUTABLE}" -c "import numpy; print(numpy.get_include())" | ||
OUTPUT_VARIABLE NUMPY_INCLUDE_DIR | ||
OUTPUT_STRIP_TRAILING_WHITESPACE | ||
) | ||
|
||
# Source file list for `wrapper` module | ||
set(WRAPPER_SRC_FILES | ||
wrapper.cpp | ||
) | ||
|
||
# Create the Python `wrapper` module | ||
# Target the stable ABI for Python 3.12+, which reduces the number of binary wheels that must be | ||
# built (`STABLE_ABI` does nothing on older Python versions). | ||
nanobind_add_module(wrapper STABLE_ABI ${WRAPPER_SRC_FILES}) | ||
|
||
# Add the NumPy include directory to the library's include paths | ||
target_include_directories(wrapper PRIVATE ${NUMPY_INCLUDE_DIR}) | ||
|
||
# Use suffix ".so" rather than ".abi3.so" for library file using Stable ABI | ||
# This is necessary for compatibility with setuptools build extensions | ||
set_target_properties(wrapper PROPERTIES SUFFIX ".so") |
This file contains 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
This file contains 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
Oops, something went wrong.