Skip to content
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

Halide pip package for python bindings #6815

Closed
wants to merge 25 commits into from
Closed
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
77c8f38
Scikit build for python bindings
lukastruemper Jun 1, 2022
f7fb197
Merge branch 'main' into users/lukas/python-pip
alexreinking Jun 28, 2022
fd46aa5
Spoof find_package(Halide) for internal builds
alexreinking Jun 28, 2022
e2f3c81
Correct dependencies
alexreinking Jun 28, 2022
6f530dc
Make setup.py inject pybind11 paths
alexreinking Jun 28, 2022
4c27754
Format setup.py
alexreinking Jun 28, 2022
508dd36
Restore trailing newline
alexreinking Jun 28, 2022
76fa110
Add PyPI preset
alexreinking Jun 28, 2022
8074e24
Add gitignore rules for Python builds
alexreinking Jun 28, 2022
cb59dbd
Check that exceptions are RTTI are enabled
alexreinking Jun 28, 2022
5a492d1
Restore robust target names and install rules
alexreinking Jun 28, 2022
92a1497
Trim trailing whitespace
alexreinking Jun 28, 2022
036b4f3
Install imported libHalide.so with Python bindings
alexreinking Jun 29, 2022
2f55c5a
Set python bindings' RPATH to $ORIGIN
alexreinking Jun 29, 2022
7c1dbb0
Remove version.py
alexreinking Jun 29, 2022
b3956cc
Detect Halide version in setup.py
alexreinking Jun 29, 2022
ffb77ea
De-duplicate libHalide.so.15.0.0 and SONAME link in wheel
alexreinking Jun 29, 2022
cc04fdb
Suppress spurious unused variable warnings
alexreinking Jun 29, 2022
985515a
Ignore MANIFEST.in
alexreinking Jun 29, 2022
b360124
Re-enable Python bindings in main build
alexreinking Jun 29, 2022
02659eb
Make CMake install the same layout as setuptools
alexreinking Jun 29, 2022
7e31535
Move more packaging logic into python_bindings
alexreinking Jun 29, 2022
17382ee
Merge branch 'main' into users/lukas/python-pip
alexreinking Jun 30, 2022
8b60387
Merge branch 'main' into users/lukas/python-pip
alexreinking Jul 10, 2022
36db99d
Merge branch 'main' into users/lukas/python-pip
alexreinking Jul 11, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 0 additions & 18 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -94,24 +94,6 @@ if (CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
message(STATUS "Building tests disabled")
endif ()

option(WITH_PYTHON_BINDINGS "Build Python bindings" ON)
if (WITH_PYTHON_BINDINGS)
if (Halide_ENABLE_RTTI AND Halide_ENABLE_EXCEPTIONS)
message(STATUS "Building Python bindings enabled")
add_subdirectory(python_bindings)
else ()
if (NOT Halide_ENABLE_RTTI)
message(WARNING "Building Python bindings disabled: must compile with RTTI")
endif ()
if (NOT Halide_ENABLE_EXCEPTIONS)
message(WARNING "Building Python bindings disabled: must compile with exceptions")
endif ()
set(WITH_PYTHON_BINDINGS OFF CACHE BOOL "Build Python bindings" FORCE)
endif ()
else ()
message(STATUS "Building Python bindings disabled")
endif ()

option(WITH_TUTORIALS "Build tutorials" ON)
if (WITH_TUTORIALS)
message(STATUS "Building tutorials enabled")
Expand Down
2 changes: 2 additions & 0 deletions cmake/FindHalide.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
message(VERBOSE " Spoofing find_package(Halide) since in-tree builds already have Halide available.")
alexreinking marked this conversation as resolved.
Show resolved Hide resolved
set(Halide_FOUND 1)
11 changes: 0 additions & 11 deletions packaging/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,6 @@ foreach (dep IN ITEMS Halide_LLVM Halide_wabt)
endif ()
endforeach ()

##
# Python bindings
##

if (WITH_PYTHON_BINDINGS)
set(Halide_INSTALL_PYTHONDIR "${CMAKE_INSTALL_LIBDIR}/python3/site-packages"
CACHE STRING "Path to Halide Python bindings folder")
install(TARGETS Halide_Python
LIBRARY DESTINATION ${Halide_INSTALL_PYTHONDIR} COMPONENT Halide_Python
NAMELINK_COMPONENT Halide_Python)
endif ()

##
# Library-type-agnostic interface targets
Expand Down
27 changes: 10 additions & 17 deletions python_bindings/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
##
# Load Python dependencies, including external pybind11
##
cmake_minimum_required(VERSION 3.16...3.23)
project(Halide_Python)

find_package(Python3 REQUIRED COMPONENTS Interpreter Development)
set(CMAKE_CXX_STANDARD 17 CACHE STRING "The minimum C++ standard to use")
option(CMAKE_CXX_STANDARD_REQUIRED "Prevent CMake C++ standard selection decay" ON)
option(CMAKE_CXX_EXTENSIONS "Enable C++ vendor extensions (e.g. GNU)" OFF)

set(PYBIND11_VER 2.6.2 CACHE STRING "The pybind11 version to use")

set(PYBIND11_VER 2.6.2)
find_package(Halide REQUIRED)
find_package(Python3 REQUIRED COMPONENTS Interpreter Development)
find_package(pybind11 ${PYBIND11_VER} QUIET)

if (NOT pybind11_FOUND)
include(FetchContent)
FetchContent_Declare(pybind11
Expand All @@ -14,16 +19,4 @@ if (NOT pybind11_FOUND)
FetchContent_MakeAvailable(pybind11)
endif ()

##
# Add our sources to this sub-tree.
##

add_subdirectory(src)
add_subdirectory(stub)

option(WITH_TEST_PYTHON "Build Python tests" ON)
if (WITH_TESTS AND WITH_TEST_PYTHON)
add_subdirectory(apps)
add_subdirectory(correctness)
add_subdirectory(tutorial)
endif ()
10 changes: 10 additions & 0 deletions python_bindings/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[build-system]
requires = [
"setuptools>=42",
"wheel",
"scikit-build",
"pybind11==2.6.2",
"cmake>=3.22",
"ninja; platform_system!='Windows'"
]
build-backend = "setuptools.build_meta"
29 changes: 29 additions & 0 deletions python_bindings/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from skbuild import setup
from setuptools import find_packages
import pybind11

with open("version.py", "r") as f:
version = f.read().strip().split(" ")[-1][1:-1]

with open("readme.md", "r") as fp:
long_description = fp.read()

setup(
name="halide",
version=version,
author="The Halide team",
author_email="",
description="",
long_description=long_description,
python_requires=">=3.6",
packages=find_packages(where="src"),
package_dir={"": "src"},
cmake_install_dir="src/halide",
cmake_args=[
f"-Dpybind11_ROOT={pybind11.get_cmake_dir()}",
"-DCMAKE_REQUIRE_FIND_PACKAGE_pybind11=YES",
],
include_package_data=True,
extras_require={"test": ["pytest>=6.0"]},
zip_safe=False,
)
23 changes: 4 additions & 19 deletions python_bindings/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,7 @@ set(SOURCES
PyVar.cpp
PyVarOrRVar.cpp
)

pybind11_add_module(Halide_Python MODULE ${SOURCES})
add_library(Halide::Python ALIAS Halide_Python)
set_target_properties(Halide_Python
PROPERTIES
LIBRARY_OUTPUT_NAME halide
EXPORT_NAME Python)
target_link_libraries(Halide_Python PRIVATE Halide::Halide)

if (WIN32 AND BUILD_SHARED_LIBS)
# There's precious little information about why Python only sometimes prevents DLLs from loading from the PATH on Windows.
# This workaround places a copy of Halide.dll next to our Python module.
# Ref: https://stackoverflow.com/questions/59860465/pybind11-importerror-dll-not-found-when-trying-to-import-pyd-in-python-int
# Ref: https://bugs.python.org/issue36085
# Ref: https://docs.python.org/3/whatsnew/3.8.html#bpo-36085-whatsnew
add_custom_command(TARGET Halide_Python POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:Halide::Halide> $<TARGET_FILE_DIR:Halide::Python>
VERBATIM)
endif ()

pybind11_add_module(halide_ MODULE ${SOURCES})
target_link_libraries(halide_ PRIVATE Halide::Halide)
install(TARGETS halide_ DESTINATION .)
2 changes: 1 addition & 1 deletion python_bindings/src/PyHalide.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ static_assert(PY_VERSION_HEX >= 0x03000000,
"We appear to be compiling against Python 2.x rather than 3.x, which is not supported.");

#ifndef HALIDE_PYBIND_MODULE_NAME
#define HALIDE_PYBIND_MODULE_NAME halide
#define HALIDE_PYBIND_MODULE_NAME halide_
alexreinking marked this conversation as resolved.
Show resolved Hide resolved
#endif

PYBIND11_MODULE(HALIDE_PYBIND_MODULE_NAME, m) {
Expand Down
1 change: 1 addition & 0 deletions python_bindings/src/halide/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .halide_ import *
alexreinking marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions python_bindings/version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__version__ = '1.0.0'