Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
78 commits
Select commit Hold shift + click to select a range
300c1a5
Define a Generator framework in Python
steven-johnson Jul 18, 2022
e673674
Merge branch 'main' into srj/pygen-three
steven-johnson Jul 21, 2022
9de3610
Fixes
steven-johnson Jul 21, 2022
17d9f7e
Update PyGenerator.cpp
steven-johnson Jul 21, 2022
8267757
fixes
steven-johnson Jul 21, 2022
d36e4f3
Update PyGenerator.cpp
steven-johnson Jul 21, 2022
802fe80
Update PyGenerator.cpp
steven-johnson Jul 21, 2022
8deff69
fixes
steven-johnson Jul 21, 2022
8fa1c8b
Merge branch 'main' into srj/pygen-three
steven-johnson Jul 26, 2022
e8621c8
Merge branch 'main' into srj/pygen-three
steven-johnson Jul 29, 2022
b32f347
Merge branch 'main' into srj/pygen-three
steven-johnson Aug 4, 2022
988ae06
Merge branch 'main' into srj/pygen-three
steven-johnson Aug 8, 2022
46fefdb
Merge branch 'main' into srj/pygen-three
steven-johnson Aug 10, 2022
d686a96
fixes
steven-johnson Aug 10, 2022
b690801
Update CMakeLists.txt
steven-johnson Aug 10, 2022
fc9e685
Fixes
steven-johnson Aug 11, 2022
d210731
fixes
steven-johnson Aug 11, 2022
fc17dc2
Update PyGenerator.cpp
steven-johnson Aug 11, 2022
42ba36b
Update CMakeLists.txt
steven-johnson Aug 11, 2022
4ac278c
Merge branch 'main' into srj/pygen-three
steven-johnson Aug 11, 2022
d452955
Merge branch 'main' into srj/pygen-three
steven-johnson Aug 11, 2022
5081cc1
Revise PyGen API to have implicit GeneratorContext (#6935)
steven-johnson Aug 12, 2022
e8638e7
Merge branch 'main' into srj/pygen-three
steven-johnson Aug 12, 2022
028367f
add hl.active_generator_context()
steven-johnson Aug 12, 2022
abadedf
Fix badly-merged CMakePresets.json file
steven-johnson Aug 12, 2022
87b2e1d
Merge branch 'srj/bad-presets' into srj/pygen-three
steven-johnson Aug 12, 2022
e8782d4
Update __init__.py
steven-johnson Aug 12, 2022
118acc2
Update PyStubImpl.cpp
steven-johnson Aug 12, 2022
fde327f
Merge branch 'main' into srj/pygen-three
steven-johnson Aug 16, 2022
7ab869f
WIP
steven-johnson Aug 17, 2022
6f2d878
Merge branch 'main' into srj/pygen-three
steven-johnson Aug 17, 2022
e822000
Update Generator.cpp
steven-johnson Aug 17, 2022
14c720b
Update README_python.md
steven-johnson Aug 17, 2022
dd79a82
Merge branch 'main' into srj/pygen-three
steven-johnson Aug 18, 2022
14638e2
Update HalideGeneratorHelpers.cmake
steven-johnson Aug 18, 2022
3af76bc
Update HalideGeneratorHelpers.cmake
steven-johnson Aug 18, 2022
33d471e
Merge branch 'main' into srj/pygen-three
steven-johnson Aug 19, 2022
a927717
Don't allow PYSTUB for Python Generators
steven-johnson Aug 22, 2022
57468c3
Merge branch 'main' into srj/pygen-three
steven-johnson Aug 23, 2022
e7229b3
Add internal Generator link
steven-johnson Aug 23, 2022
d4fa840
Fix bullet list
steven-johnson Aug 23, 2022
59fc488
Clarify naming for generators
steven-johnson Aug 23, 2022
a29fd0b
Fill in Argument and ArgumentEstimates
steven-johnson Aug 23, 2022
96fec07
Add _to_argument() convenience fn for Parameter
steven-johnson Aug 23, 2022
d899cc4
add compile_to_callable()
steven-johnson Aug 23, 2022
1959ae0
Update callable.py
steven-johnson Aug 23, 2022
b96e972
fixes
steven-johnson Aug 23, 2022
93a437d
Make GeneratorParam values only settable from ctor
steven-johnson Aug 23, 2022
7379375
Update _generator_helpers.py
steven-johnson Aug 23, 2022
1dcff43
Update Pipeline.cpp
steven-johnson Aug 23, 2022
3a1d480
Merge branch 'main' into srj/pygen-three
steven-johnson Sep 1, 2022
d7949f3
fixes
steven-johnson Sep 1, 2022
406ade5
Merge branch 'main' into srj/pygen-three
steven-johnson Sep 9, 2022
7a84863
PythonCorrectnessGenerators
steven-johnson Sep 9, 2022
1b19311
NoneType -> UnspecifiedType
steven-johnson Sep 9, 2022
f85be0a
README changes
steven-johnson Sep 9, 2022
202e8bf
Update README_python.md
steven-johnson Sep 9, 2022
07ea6c2
Use add_halide_generator()
steven-johnson Sep 9, 2022
b789450
Add error checking for missing Python in add_halide_library()
steven-johnson Sep 9, 2022
f613271
Move argname uniquification stripping into PyCallable
steven-johnson Sep 9, 2022
1c570cb
Fix Python handling of boolean buffers
steven-johnson Sep 9, 2022
a2964a3
Merge branch 'srj/pyext-bool-types' into srj/pygen-three
steven-johnson Sep 9, 2022
d040964
Update bitpy_generator.py
steven-johnson Sep 9, 2022
dbe82c8
Python Generators should have a dep on the source file too (oops)
steven-johnson Sep 9, 2022
f10329e
Allow use of bool|int|float for Generator type args (#7007)
steven-johnson Sep 12, 2022
06e22d1
Merge branch 'main' into srj/pygen-three
steven-johnson Sep 12, 2022
a01fcc9
Merge branch 'main' into srj/pygen-three
steven-johnson Sep 13, 2022
5ebd375
Merge branch 'main' into srj/pygen-three
steven-johnson Sep 14, 2022
04e1314
Update HalideGeneratorHelpers.cmake
steven-johnson Sep 14, 2022
9b06df8
Kill _print
steven-johnson Sep 14, 2022
2c5d927
Use RegEx for _is_valid_name()
steven-johnson Sep 14, 2022
83a23b3
if t is None or t is type(None):
steven-johnson Sep 14, 2022
8314468
super-init
steven-johnson Sep 14, 2022
d2fe8d9
No magic numbers
steven-johnson Sep 14, 2022
95f023f
assert -> _check
steven-johnson Sep 14, 2022
ebba00d
output_types() -> types()
steven-johnson Sep 14, 2022
3284b0f
Update _generator_helpers.py
steven-johnson Sep 15, 2022
f3608db
Allow explicit inheritance for python Generators (#7021)
steven-johnson Sep 16, 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
578 changes: 525 additions & 53 deletions README_python.md

Large diffs are not rendered by default.

102 changes: 71 additions & 31 deletions cmake/HalideGeneratorHelpers.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ define_property(TARGET PROPERTY Halide_GENERATOR_HAS_POST_BUILD
BRIEF_DOCS "On a Halide generator target, true if Halide.dll copy command has already been added."
FULL_DOCS "On a Halide generator target, true if Halide.dll copy command has already been added.")

define_property(TARGET PROPERTY Halide_PYTHON_GENERATOR_SOURCE
BRIEF_DOCS "Used to store the source file(s) for a Python Generator"
FULL_DOCS "Used to store the source file(s) for a Python Generator")

define_property(TARGET PROPERTY Halide_LIBRARY_RUNTIME_TARGET
BRIEF_DOCS "On a Halide library target, the runtime it uses."
FULL_DOCS "On a Halide library target, the runtime it uses.")
Expand Down Expand Up @@ -68,26 +72,47 @@ function(add_halide_generator TARGET)
find_package(Halide REQUIRED)
endif ()

add_executable(${TARGET} ${ARG_SOURCES})
add_executable(${gen} ALIAS ${TARGET})
target_link_libraries(${TARGET} PRIVATE Halide::Generator ${ARG_LINK_LIBRARIES})

if (NOT ARG_NO_DEFAULT_FLAGS AND NOT Halide_NO_DEFAULT_FLAGS)
# For crosscompiling builds, the Halide headers will be included using -isystem,
# which will cause all warnings to be ignored. This is not helpful, since
# we *want* deprecation warnings to be propagated. So we must set
# NO_SYSTEM_FROM_IMPORTED in order for it to be seen.
set_target_properties(${TARGET} PROPERTIES NO_SYSTEM_FROM_IMPORTED YES)
target_compile_options(${TARGET} PRIVATE
$<$<CXX_COMPILER_ID:GNU,Clang,AppleClang>:-Wdeprecated-declarations>
$<$<CXX_COMPILER_ID:MSVC>:/w14996> # 4996: compiler encountered deprecated declaration
)
endif ()
if (ARG_SOURCES MATCHES ".py$")
if (ARG_LINK_LIBRARIES)
message(FATAL_ERROR "You cannot specify LINK_LIBRARIES in conjunction with Python source code.")
endif ()
if (ARG_PYSTUB)
message(FATAL_ERROR "You cannot specify PYSTUB in conjunction with Python source code (only C++ Generators can produce PyStubs).")
endif ()

list(LENGTH ARG_SOURCES len)
if (NOT len EQUAL 1)
message(FATAL_ERROR "Python Generators must specify exactly one source file.")
endif()

# Make a fake target here that we can attach the Python source to,
# so that we can extract 'em in add_halide_library()
add_custom_target(${TARGET} ALL)
set_property(TARGET ${TARGET} PROPERTY Halide_PYTHON_GENERATOR_SOURCE "${CMAKE_CURRENT_SOURCE_DIR}/${ARG_SOURCES}")

# TODO: what do we need to do for PACKAGE_NAME PACKAGE_NAMESPACE EXPORT_FILE in this case?
else ()
add_executable(${TARGET} ${ARG_SOURCES})
add_executable(${gen} ALIAS ${TARGET})
target_link_libraries(${TARGET} PRIVATE Halide::Generator ${ARG_LINK_LIBRARIES})

if (NOT ARG_NO_DEFAULT_FLAGS AND NOT Halide_NO_DEFAULT_FLAGS)
# For crosscompiling builds, the Halide headers will be included using -isystem,
# which will cause all warnings to be ignored. This is not helpful, since
# we *want* deprecation warnings to be propagated. So we must set
# NO_SYSTEM_FROM_IMPORTED in order for it to be seen.
set_target_properties(${TARGET} PROPERTIES NO_SYSTEM_FROM_IMPORTED YES)
target_compile_options(${TARGET} PRIVATE
$<$<CXX_COMPILER_ID:GNU,Clang,AppleClang>:-Wdeprecated-declarations>
$<$<CXX_COMPILER_ID:MSVC>:/w14996> # 4996: compiler encountered deprecated declaration
)
endif ()

add_dependencies("${ARG_PACKAGE_NAME}" ${TARGET})
export(TARGETS ${TARGET}
NAMESPACE ${ARG_PACKAGE_NAMESPACE}
APPEND FILE "${ARG_EXPORT_FILE}")
add_dependencies("${ARG_PACKAGE_NAME}" ${TARGET})
export(TARGETS ${TARGET}
NAMESPACE ${ARG_PACKAGE_NAMESPACE}
APPEND FILE "${ARG_EXPORT_FILE}")
endif ()
endif ()

if (ARG_PYSTUB)
Expand Down Expand Up @@ -201,17 +226,32 @@ function(add_halide_library TARGET)
message(FATAL_ERROR "Missing FROM argument specifying a Halide generator target")
endif ()

if (NOT TARGET ${ARG_FROM})
# FROM is usually an unqualified name; if we are crosscompiling, we might need a
# fully-qualified name, so add the default package name and retry
set(FQ_ARG_FROM "${PROJECT_NAME}::halide_generators::${ARG_FROM}")
if (NOT TARGET ${FQ_ARG_FROM})
message(FATAL_ERROR "Unable to locate FROM as either ${ARG_FROM} or ${FQ_ARG_FROM}")
get_property(py_src TARGET ${ARG_FROM} PROPERTY Halide_PYTHON_GENERATOR_SOURCE)
if (py_src)
# TODO: Python Generators need work to support crosscompiling (https://github.com/halide/Halide/issues/7014)
if (NOT TARGET Halide::Python)
message(FATAL_ERROR "This version of Halide was built without support for Python bindings; rebuild using WITH_PYTHON_BINDINGS=ON to use this rule with Python Generators.")
endif ()
set(ARG_FROM "${FQ_ARG_FROM}")
endif ()

_Halide_place_dll(${ARG_FROM})
if (NOT TARGET Python3::Interpreter)
message(FATAL_ERROR "You must call find_package(Python3) in your CMake code in order to use this rule with Python Generators.")
endif ()
set(PYTHONPATH "$<TARGET_FILE_DIR:Halide::Python>/..")
set(GENERATOR_CMD ${CMAKE_COMMAND} -E env PYTHONPATH=${PYTHONPATH} ${Python3_EXECUTABLE} $<SHELL_PATH:${py_src}>)
Copy link
Member

Choose a reason for hiding this comment

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

I think here is where we'd need to make sure this is host Python.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

(1) How could it be anything else?
(2) How would we detect and/or ensure?

Copy link
Member

Choose a reason for hiding this comment

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

(1) If you're cross-compiling a Python extension, you will need to find a cross-target Python's libraries
(2) I have some ideas for this... but we can defer to a future PR

set(GENERATOR_CMD_DEPS ${ARG_FROM} Halide::Python ${py_src})
else()
if (NOT TARGET ${ARG_FROM})
# FROM is usually an unqualified name; if we are crosscompiling, we might need a
# fully-qualified name, so add the default package name and retry
set(FQ_ARG_FROM "${PROJECT_NAME}::halide_generators::${ARG_FROM}")
if (NOT TARGET ${FQ_ARG_FROM})
message(FATAL_ERROR "Unable to locate FROM as either ${ARG_FROM} or ${FQ_ARG_FROM}")
endif ()
set(ARG_FROM "${FQ_ARG_FROM}")
endif()
set(GENERATOR_CMD "${ARG_FROM}")
set(GENERATOR_CMD_DEPS ${ARG_FROM})
_Halide_place_dll(${ARG_FROM})
endif()

if (ARG_C_BACKEND)
if (ARG_USE_RUNTIME)
Expand Down Expand Up @@ -377,7 +417,7 @@ function(add_halide_library TARGET)
endif ()

add_custom_command(OUTPUT ${generator_output_files}
COMMAND ${ARG_FROM}
COMMAND ${GENERATOR_CMD}
-n "${TARGET}"
-d "${gradient_descent}"
-g "${ARG_GENERATOR}"
Expand All @@ -387,7 +427,7 @@ function(add_halide_library TARGET)
-o .
"target=$<JOIN:${ARG_TARGETS},$<COMMA>>"
${ARG_PARAMS}
DEPENDS "${ARG_FROM}" ${ARG_PLUGINS}
DEPENDS ${GENERATOR_CMD_DEPS} ${ARG_PLUGINS}
VERBATIM)

list(TRANSFORM generator_output_files PREPEND "${CMAKE_CURRENT_BINARY_DIR}/")
Expand Down
4 changes: 2 additions & 2 deletions python_bindings/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ if (Halide_ASAN_ENABLED)
endif ()

function(add_python_test)
cmake_parse_arguments(ARG "" "FILE;LABEL" "PYTHONPATH;ENVIRONMENT" ${ARGN})
cmake_parse_arguments(ARG "" "FILE;LABEL" "PYTHONPATH;ENVIRONMENT;TEST_ARGS" ${ARGN})

list(PREPEND ARG_PYTHONPATH "$<TARGET_FILE_DIR:Halide::Python>/..")
list(TRANSFORM ARG_PYTHONPATH PREPEND "PYTHONPATH=path_list_prepend:")
Expand All @@ -93,7 +93,7 @@ function(add_python_test)

add_test(
NAME "${test_name}"
COMMAND Python3::Interpreter "$<SHELL_PATH:${CMAKE_CURRENT_SOURCE_DIR}/${ARG_FILE}>"
COMMAND Python3::Interpreter "$<SHELL_PATH:${CMAKE_CURRENT_SOURCE_DIR}/${ARG_FILE}>" ${ARG_TEST_ARGS}
)
set_tests_properties(
"${test_name}"
Expand Down
2 changes: 2 additions & 0 deletions python_bindings/src/halide/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ set(native_sources
PyExternFuncArgument.cpp
PyFunc.cpp
PyFuncRef.cpp
PyGenerator.cpp
PyHalide.cpp
PyImageParam.cpp
PyInlineReductions.cpp
Expand All @@ -33,6 +34,7 @@ list(TRANSFORM native_sources PREPEND "halide_/")

set(python_sources
__init__.py
_generator_helpers.py
)

# It is technically still possible for a user to override the LIBRARY_OUTPUT_DIRECTORY by setting
Expand Down
4 changes: 4 additions & 0 deletions python_bindings/src/halide/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
from .halide_ import *
from .halide_ import _, _1, _2, _3, _4, _5, _6, _7, _8, _9
from ._generator_helpers import GeneratorParam, InputBuffer, InputScalar, OutputBuffer, \
OutputScalar, Generator, alias, generator, active_generator_context, \
_get_python_generator_names, _create_python_generator, \
_generatorcontext_enter, _generatorcontext_exit
Loading