-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Define a Generator framework in Python #6764
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
Changes from all commits
300c1a5
e673674
9de3610
17d9f7e
8267757
d36e4f3
802fe80
8deff69
8fa1c8b
e8621c8
b32f347
988ae06
46fefdb
d686a96
b690801
fc9e685
d210731
fc17dc2
42ba36b
4ac278c
d452955
5081cc1
e8638e7
028367f
abadedf
87b2e1d
e8782d4
118acc2
fde327f
7ab869f
6f2d878
e822000
14c720b
dd79a82
14638e2
3af76bc
33d471e
a927717
57468c3
e7229b3
d4fa840
59fc488
a29fd0b
96fec07
d899cc4
1959ae0
b96e972
93a437d
7379375
1dcff43
3a1d480
d7949f3
406ade5
7a84863
1b19311
f85be0a
202e8bf
07ea6c2
b789450
f613271
1c570cb
a2964a3
d040964
dbe82c8
f10329e
06e22d1
a01fcc9
5ebd375
04e1314
9b06df8
2c5d927
83a23b3
8314468
d2fe8d9
95f023f
ebba00d
3284b0f
f3608db
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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.") | ||
|
|
@@ -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) | ||
|
|
@@ -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}>) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (1) How could it be anything else?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| 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) | ||
|
|
@@ -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}" | ||
|
|
@@ -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}/") | ||
|
|
||
| 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 |
Uh oh!
There was an error while loading. Please reload this page.