-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add CMake for onnx app #8707
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
Merged
Merged
Add CMake for onnx app #8707
Changes from 4 commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
0bb58bf
Use py::array::request().itemsize instead of py::array::itemsize()
vawale 531314f
Add CMake build for onnx app
vawale 4207cbd
Format apps/onnx/CMakeLists.txt
alexreinking b3240d2
Merge branch 'main' into fork/vawale/add-cmake-for-onnx-app
alexreinking 687009e
Fix unused struct member issue
alexreinking fd3abb5
Pass Adams2019 via MODEL_AUTOSCHEDULER env-var
alexreinking 98cf3c8
Add pytest to dependencies, which was missing from onnx
alexreinking d9847dc
halide_as_onnx_backend_test seems to run forever
alexreinking de4e6d9
Replace raw download with FetchContent
alexreinking a771088
Add PRIVATE to target_link_libraries
alexreinking 83fb3c2
Lock onnx to 1.4.1
alexreinking b090854
Add DEPENDS argument to add_halide_library
alexreinking 808e8e1
Skip GIT_SHALLOW
alexreinking b9ff026
Formatting...
alexreinking 2425cdf
Add header sources to model_cpp
alexreinking a39ed23
Merge set_tests_properties calls
alexreinking a1f799e
Simplify protobuf generation and dependencies
alexreinking f4fbb8f
Try using ONNX 1.18.0
alexreinking f0faabe
Try another way of silencing unused member warnings
alexreinking 18a4b78
Fix project name
alexreinking d0ca03e
Merge branch 'main' into fork/vawale/add-cmake-for-onnx-app
alexreinking 05dc7eb
Update requirements.txt
alexreinking fec3d67
Fix --proto_path
alexreinking d9b4014
Update dependency specs
alexreinking 72e1d1a
Improve search for pybind11 in ONNX app.
alexreinking 011847f
Update exclude patterns for halide_as_onnx_backend_test.py
alexreinking File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,162 @@ | ||
| cmake_minimum_required(VERSION 3.28) | ||
| project(onnx) | ||
|
|
||
| enable_testing() | ||
|
|
||
| # Set up language settings | ||
| set(CMAKE_CXX_STANDARD 17) | ||
| set(CMAKE_CXX_STANDARD_REQUIRED YES) | ||
| set(CMAKE_CXX_EXTENSIONS NO) | ||
| set(CMAKE_POSITION_INDEPENDENT_CODE ON) | ||
|
|
||
| find_package(Protobuf) | ||
| if (NOT Protobuf_FOUND) | ||
| message(WARNING "Could NOT find Protobuf") | ||
| return() | ||
| endif () | ||
|
|
||
| find_package(Halide REQUIRED) | ||
|
|
||
| # Download onnx.proto | ||
| set(ONNX_PROTO_FILE ${CMAKE_CURRENT_BINARY_DIR}/onnx.proto) | ||
| if (NOT EXISTS ${ONNX_PROTO_FILE}) | ||
| file( | ||
| DOWNLOAD | ||
| "https://raw.githubusercontent.com/onnx/onnx/v1.4.1/onnx/onnx.proto" | ||
| ${ONNX_PROTO_FILE} | ||
| SHOW_PROGRESS | ||
| STATUS download_status | ||
| ) | ||
| if (NOT download_status EQUAL 0) | ||
| message(FATAL_ERROR "Failed to download onnx.proto: ${download_status}") | ||
| endif () | ||
|
|
||
| file(READ ${ONNX_PROTO_FILE} onnx_proto_content) | ||
| string(REPLACE "package onnx;" "package onnx;option optimize_for = LITE_RUNTIME;" onnx_proto_content "${onnx_proto_content}") | ||
| file(WRITE ${ONNX_PROTO_FILE} "${onnx_proto_content}") | ||
| endif () | ||
|
|
||
| # Generate sources from onnx.proto | ||
| add_custom_target(onnx_proto) | ||
| protobuf_generate( | ||
| TARGET onnx_proto | ||
| LANGUAGE cpp | ||
| PROTOC_OUT_DIR ${CMAKE_CURRENT_BINARY_DIR}/onnx | ||
| PROTOC_OUT_VAR onnx_proto_generated_sources | ||
| PROTOS ${ONNX_PROTO_FILE} | ||
| IMPORT_DIRS ${CMAKE_CURRENT_BINARY_DIR} | ||
| ) | ||
|
|
||
| # Add library that converts ONNX models to Halide operators | ||
| add_library(oclib STATIC onnx_converter.cc ${CMAKE_CURRENT_BINARY_DIR}/onnx/onnx.pb.cc) | ||
| target_include_directories( | ||
| oclib | ||
| PUBLIC | ||
| ${CMAKE_CURRENT_BINARY_DIR} | ||
| ${Protobuf_INCLUDE_DIRS} | ||
| ) | ||
|
alexreinking marked this conversation as resolved.
Outdated
|
||
| target_link_libraries(oclib PUBLIC Halide::Halide ${Protobuf_LITE_LIBRARIES}) | ||
| target_compile_definitions(oclib PUBLIC GOOGLE_PROTOBUF_NO_RTTI) | ||
|
|
||
| add_executable(onnx_converter_test onnx_converter_test.cc) | ||
| target_link_libraries(onnx_converter_test oclib) | ||
|
|
||
| add_test(NAME onnx_converter_test COMMAND onnx_converter_test) | ||
| set_tests_properties( | ||
| onnx_converter_test PROPERTIES | ||
| LABELS onnx | ||
| PASS_REGULAR_EXPRESSION "Success!" | ||
| SKIP_REGULAR_EXPRESSION "\\[SKIP\\]" | ||
| ) | ||
|
|
||
| # Generator | ||
| add_halide_generator( | ||
| onnx_converter.generator | ||
| SOURCES onnx_converter_generator.cc | ||
| LINK_LIBRARIES oclib | ||
| ) | ||
|
|
||
| # Generate test onnx model | ||
| add_custom_command( | ||
| OUTPUT test_model.onnx | ||
| COMMAND | ||
| ${CMAKE_COMMAND} -E cat ${CMAKE_CURRENT_SOURCE_DIR}/test_model_proto.txt | ||
| | protoc --encode=onnx.ModelProto --proto_path=${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_BINARY_DIR}/onnx.proto | ||
| > test_model.onnx | ||
| DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/onnx.proto | ||
| COMMENT "Generating test ONNX model from proto content" | ||
| MAIN_DEPENDENCY ${CMAKE_CURRENT_SOURCE_DIR}/test_model_proto.txt | ||
| VERBATIM | ||
| ) | ||
| add_custom_target(test_model_onnx DEPENDS test_model.onnx) | ||
|
alexreinking marked this conversation as resolved.
Outdated
|
||
| add_dependencies(test_model_onnx onnx_proto) | ||
|
|
||
| # Generate static library using halide generator for test onnx model | ||
| add_halide_library( | ||
| test_model FROM onnx_converter.generator | ||
| GENERATOR onnx_model_generator | ||
| PARAMS model_file_path=${CMAKE_CURRENT_BINARY_DIR}/test_model.onnx | ||
| AUTOSCHEDULER Halide::Adams2019 | ||
| ) | ||
| add_dependencies(test_model test_model_onnx) | ||
|
|
||
| # Test the generated static library | ||
| add_executable(onnx_converter_generator_test onnx_converter_generator_test.cc) | ||
| target_link_libraries(onnx_converter_generator_test PRIVATE Halide::Runtime test_model) | ||
|
|
||
| add_test(NAME onnx_converter_generator_test COMMAND onnx_converter_generator_test) | ||
| set_tests_properties( | ||
| onnx_converter_generator_test | ||
| PROPERTIES | ||
| LABELS onnx | ||
| PASS_REGULAR_EXPRESSION "Success!" | ||
| SKIP_REGULAR_EXPRESSION "\\[SKIP\\]" | ||
| ) | ||
|
|
||
| # Python bindings to convert onnx models to Halide model | ||
| find_package(Python3 COMPONENTS Interpreter Development) | ||
| if (NOT Python3_FOUND) | ||
| message(WARNING "Could NOT find Python3") | ||
| return() | ||
| endif () | ||
|
|
||
| find_package(pybind11) | ||
| if (NOT pybind11_FOUND) | ||
| message(WARNING "Could NOT find pybind11") | ||
| return() | ||
| endif () | ||
|
|
||
| pybind11_add_module(model_cpp model.cpp) | ||
| target_link_libraries(model_cpp PRIVATE Halide::Halide oclib) | ||
| if (UNIX) | ||
| target_link_libraries(model_cpp PRIVATE -Wl,--no-as-needed Halide::Adams2019 -Wl,--as-needed) | ||
| endif () | ||
|
alexreinking marked this conversation as resolved.
Outdated
|
||
| if (WIN32) | ||
| target_link_libraries(model_cpp PRIVATE Halide::Adams2019) | ||
| endif () | ||
|
|
||
| add_test( | ||
| NAME model_test | ||
| COMMAND ${Python3_EXECUTABLE} -m unittest ${CMAKE_CURRENT_SOURCE_DIR}/model_test.py -v | ||
| WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} | ||
| ) | ||
| set_tests_properties( | ||
| model_test PROPERTIES | ||
| LABELS onnx | ||
| PASS_REGULAR_EXPRESSION "OK" | ||
| SKIP_REGULAR_EXPRESSION "\\[SKIP\\]" | ||
| ENVIRONMENT "PYTHONPATH=$<TARGET_FILE_DIR:model_cpp>" | ||
| ) | ||
|
|
||
| add_test( | ||
| NAME halide_as_onnx_backend_test | ||
| COMMAND ${Python3_EXECUTABLE} -m unittest ${CMAKE_CURRENT_SOURCE_DIR}/halide_as_onnx_backend_test.py -v | ||
| WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} | ||
| ) | ||
| set_tests_properties( | ||
| halide_as_onnx_backend_test PROPERTIES | ||
| LABELS onnx | ||
| PASS_REGULAR_EXPRESSION "OK" | ||
| SKIP_REGULAR_EXPRESSION "\\[SKIP\\]" | ||
| ENVIRONMENT "PYTHONPATH=$<TARGET_FILE_DIR:model_cpp>" | ||
| ) | ||
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.