From 8a5abed9f944b14dc75c120d99c550c0fdb08a5a Mon Sep 17 00:00:00 2001 From: ynimmaga Date: Sun, 9 Feb 2025 14:42:44 -0800 Subject: [PATCH 1/2] cmake updates for openvino --- backends/openvino/CMakeLists.txt | 67 ++++++++-------- .../scripts/{build.sh => openvino_build.sh} | 0 examples/openvino/CMakeLists.txt | 77 ++++++++----------- ...ino_build.sh => openvino_build_example.sh} | 4 +- 4 files changed, 65 insertions(+), 83 deletions(-) rename backends/openvino/scripts/{build.sh => openvino_build.sh} (100%) rename examples/openvino/{openvino_build.sh => openvino_build_example.sh} (95%) diff --git a/backends/openvino/CMakeLists.txt b/backends/openvino/CMakeLists.txt index 4df2015a8d7..3f2199b634d 100644 --- a/backends/openvino/CMakeLists.txt +++ b/backends/openvino/CMakeLists.txt @@ -4,74 +4,69 @@ # except in compliance with the License. See the license file in the root # directory of this source tree for more details. +# Set minimum required CMake version +cmake_minimum_required(VERSION 3.19) + +# Set project name +project(openvino_backend_project) + # Set C++ standard set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) -# Ensure compile_commands are generated +# Ensure compile_commands.json is generated set(CMAKE_EXPORT_COMPILE_COMMANDS ON) -# Define common include directories -set(COMMON_INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/../../..) - -# Include common directories before others to ensure proper order -include_directories(BEFORE ${COMMON_INCLUDE_DIRS}) - # Set up EXECUTORCH_ROOT if not already set if(NOT EXECUTORCH_ROOT) set(EXECUTORCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../..) endif() -# Include utility cmake script from the executorch repository -include(${EXECUTORCH_ROOT}/build/Utils.cmake) - -# Update common include directory for ExecuteTorch +# Define common include directories set(COMMON_INCLUDE_DIRS ${EXECUTORCH_ROOT}/..) -# Set OpenVINO directory and include directories from environment variable +# Include utility CMake scripts from ExecuteTorch +include(${EXECUTORCH_ROOT}/build/Utils.cmake) + +# Set OpenVINO directory from environment variable set(OPENVINO_DIR "$ENV{INTEL_OPENVINO_DIR}") if(NOT OPENVINO_DIR) - message(FATAL_ERROR "INTEL_OPENVINO_DIR environment variable is not set.") + message(FATAL_ERROR "ERROR: INTEL_OPENVINO_DIR environment variable is not set.") endif() +# Set OpenVINO include directories set(OPENVINO_INCLUDE_DIRS - ${OPENVINO_DIR}/deployment_tools/inference_engine/include ${OPENVINO_DIR}/runtime/include + ${OPENVINO_DIR}/deployment_tools/inference_engine/include ) -# Define OpenVINO library path +# Set OpenVINO library path set(OPENVINO_LIB_PATH ${OPENVINO_DIR}/runtime/lib/intel64) -# Define OpenVINO libraries -set(OPENVINO_LIB ${OPENVINO_LIB_PATH}/libopenvino.so) +# Try to locate OpenVINO automatically +find_library(OPENVINO_LIB NAMES openvino PATHS ${OPENVINO_LIB_PATH} NO_DEFAULT_PATH) +if(NOT OPENVINO_LIB) + message(FATAL_ERROR "ERROR: OpenVINO library (libopenvino.so) not found in ${OPENVINO_LIB_PATH}") +endif() -# Add the OpenVINO backend library as a shared library +# Define OpenVINO backend as a shared library add_library(openvino_backend SHARED) # Enable exceptions and RTTI for OpenVINO backend -target_compile_options(openvino_backend PRIVATE "-frtti" "-fexceptions") +target_compile_options(openvino_backend PRIVATE -frtti -fexceptions) -# Include directories for ExecuteTorch and OpenVINO -target_include_directories( - openvino_backend PUBLIC - ${COMMON_INCLUDE_DIRS} - ${OPENVINO_INCLUDE_DIRS} -) +# Include ExecuteTorch and OpenVINO directories +target_include_directories(openvino_backend PUBLIC ${COMMON_INCLUDE_DIRS} ${OPENVINO_INCLUDE_DIRS}) -# Link OpenVINO libraries and executorch core to the backend -target_link_libraries(openvino_backend PRIVATE - ${OPENVINO_LIB} - executorch_core -) +# Link OpenVINO and ExecuteTorch core libraries +target_link_libraries(openvino_backend PRIVATE ${OPENVINO_LIB} executorch_core) -# Add source files to the OpenVINO backend library -target_sources(openvino_backend PRIVATE - ${CMAKE_CURRENT_LIST_DIR}/runtime/OpenvinoBackend.cpp -) +# Add source files for OpenVINO backend +target_sources(openvino_backend PRIVATE ${CMAKE_CURRENT_LIST_DIR}/runtime/OpenvinoBackend.cpp) -# Set additional link options for shared library +# Set runtime library path for OpenVINO target_link_options(openvino_backend PRIVATE -Wl,-rpath=${OPENVINO_LIB_PATH}) -# Install the OpenVINO backend library to the lib directory +# Install OpenVINO backend library to the lib directory install(TARGETS openvino_backend DESTINATION lib) diff --git a/backends/openvino/scripts/build.sh b/backends/openvino/scripts/openvino_build.sh similarity index 100% rename from backends/openvino/scripts/build.sh rename to backends/openvino/scripts/openvino_build.sh diff --git a/examples/openvino/CMakeLists.txt b/examples/openvino/CMakeLists.txt index 64f1e8d5463..761de51cf28 100644 --- a/examples/openvino/CMakeLists.txt +++ b/examples/openvino/CMakeLists.txt @@ -4,11 +4,13 @@ # except in compliance with the License. See the license file in the root # directory of this source tree for more details. -set(CMAKE_CXX_STANDARD 17) - cmake_minimum_required(VERSION 3.19) project(openvino_runner_example) +set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD_REQUIRED ON) +set(CMAKE_CXX_EXTENSIONS OFF) + # Source root directory for executorch. if(NOT EXECUTORCH_ROOT) set(EXECUTORCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../..) @@ -26,31 +28,20 @@ if(NOT CMAKE_BUILD_TYPE) endif() set(_common_compile_options -Wno-deprecated-declarations -fPIC) - -# Let files say "include ". set(_common_include_directories ${EXECUTORCH_ROOT}/..) -# -# The `__srcs` lists are defined by including ${EXECUTORCH_SRCS_FILE}. -# -set(EXECUTORCH_SRCS_FILE - "${CMAKE_CURRENT_BINARY_DIR}/../../../build/executorch_srcs.cmake" -) +set(EXECUTORCH_SRCS_FILE "${CMAKE_CURRENT_BINARY_DIR}/../../../build/executorch_srcs.cmake") extract_sources(${EXECUTORCH_SRCS_FILE}) include(${EXECUTORCH_SRCS_FILE}) -set(_openvino_executor_runner__srcs ${CMAKE_CURRENT_LIST_DIR}/../openvino/executor_runner/openvino_executor_runner.cpp) - -# preprocess executor runner src files -list(PREPEND _openvino_executor_runner__srcs - ${CMAKE_CURRENT_LIST_DIR}/../openvino/executor_runner/openvino_executor_runner.cpp +set(_openvino_executor_runner__srcs + ${CMAKE_CURRENT_LIST_DIR}/../openvino/executor_runner/openvino_executor_runner.cpp ) find_package(executorch CONFIG REQUIRED) -target_include_directories(executorch INTERFACE ${_common_include_directories}) -target_compile_options(executorch INTERFACE ${_common_compile_options}) +include_directories(${EXECUTORCH_INCLUDE_DIRS}) -# portable_ops_lib +# Portable Ops Library gen_selected_ops(LIB_NAME "openvino_portable_ops_lib" INCLUDE_ALL_OPS "ON") generate_bindings_for_kernels( LIB_NAME "openvino_portable_ops_lib" FUNCTIONS_YAML @@ -59,43 +50,39 @@ generate_bindings_for_kernels( gen_operators_lib( LIB_NAME "openvino_portable_ops_lib" KERNEL_LIBS portable_kernels DEPS executorch ) -target_compile_options( - openvino_portable_ops_lib INTERFACE -DET_EVENT_TRACER_ENABLED -) -target_include_directories( - openvino_portable_ops_lib PUBLIC ${_common_include_directories} -) - +target_compile_options(openvino_portable_ops_lib INTERFACE -DET_EVENT_TRACER_ENABLED) +target_include_directories(openvino_portable_ops_lib PUBLIC ${_common_include_directories}) -# build executor runner +# Build Executor Runner add_executable(openvino_executor_runner ${_openvino_executor_runner__srcs}) target_include_directories( - openvino_executor_runner PUBLIC ${_common_include_directories} + openvino_executor_runner PUBLIC ${_common_include_directories} ${EXECUTORCH_ROOT}/third-party/gflags/include ) -# Set the path to the library directory +# Set Library Directory set(LIBRARY_DIR "${CMAKE_CURRENT_LIST_DIR}/../../cmake-openvino-out/lib/") +message(STATUS "Library directory path: ${LIBRARY_DIR}") -# List the libraries you want to link (without the 'lib' prefix and file extension) -set(LIBRARIES_TO_LINK ${LIBRARY_DIR}/libopenvino_backend.so - ${LIBRARY_DIR}/libexecutorch.a - ${LIBRARY_DIR}/libexecutorch_core.a - ${EXECUTORCH_ROOT}/third-party/gflags/build/lib/libgflags_nothreads.a - ${LIBRARY_DIR}/libpthreadpool.a - ${LIBRARY_DIR}/libextension_data_loader.a - ${LIBRARY_DIR}/libextension_runner_util.a -) - -# Add the library directory to the link search path -link_directories(${LIBRARY_DIR}) - -# Link all libraries at once -target_link_libraries(openvino_executor_runner PRIVATE ${LIBRARIES_TO_LINK} openvino_portable_ops_lib) +# Locate OpenVINO Backend Library +find_library(OPENVINO_BACKEND_LIB NAMES openvino_backend PATHS ${LIBRARY_DIR} NO_DEFAULT_PATH) +if(NOT OPENVINO_BACKEND_LIB) + message(FATAL_ERROR "OpenVINO backend library not found in ${LIBRARY_DIR}") +endif() -set_target_properties( - openvino_executor_runner PROPERTIES LINK_FLAGS "-Wl,-rpath='$ORIGIN'" +# Link Libraries +target_link_libraries(openvino_executor_runner PRIVATE + ${OPENVINO_BACKEND_LIB} + executorch + executorch_core + openvino_portable_ops_lib + extension_data_loader + extension_runner_util + gflags + pthreadpool ) +# Ensure Proper RPATH Handling +set_target_properties(openvino_executor_runner PROPERTIES INSTALL_RPATH "$ORIGIN") get_filename_component( EXECUTORCH_SOURCE_DIR "${CMAKE_CURRENT_LIST_DIR}/../.." ABSOLUTE diff --git a/examples/openvino/openvino_build.sh b/examples/openvino/openvino_build_example.sh similarity index 95% rename from examples/openvino/openvino_build.sh rename to examples/openvino/openvino_build_example.sh index 0d2703e5646..de6e585a1ab 100755 --- a/examples/openvino/openvino_build.sh +++ b/examples/openvino/openvino_build_example.sh @@ -27,7 +27,7 @@ main() { # Build the project - cmake --build cmake-openvino-out --target install --config Release -j5 + cmake --build cmake-openvino-out --target install --config Release -j$(nproc) ## Build example local example_dir=examples/openvino @@ -41,7 +41,7 @@ main() { -B"${example_build_dir}" \ $EXECUTORCH_ROOT/$example_dir - cmake --build "${example_build_dir}" -j5 + cmake --build "${example_build_dir}" -j$(nproc) # Switch back to the original directory cd - > /dev/null From 81460f395ccc351411c5d441b235f392125f1a80 Mon Sep 17 00:00:00 2001 From: ynimmaga Date: Tue, 11 Feb 2025 14:46:07 -0800 Subject: [PATCH 2/2] Updated cmake and build scripts to link against gflags --- backends/openvino/scripts/openvino_build.sh | 1 + examples/openvino/CMakeLists.txt | 12 +++++++++--- examples/openvino/openvino_build_example.sh | 1 + 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/backends/openvino/scripts/openvino_build.sh b/backends/openvino/scripts/openvino_build.sh index 0c07a5bb729..2a8a25511ac 100755 --- a/backends/openvino/scripts/openvino_build.sh +++ b/backends/openvino/scripts/openvino_build.sh @@ -18,6 +18,7 @@ main() { # Configure the project with CMake # Note: Add any additional configuration options you need here cmake -DCMAKE_INSTALL_PREFIX="${build_dir}" \ + -DCMAKE_BUILD_TYPE=Release \ -DEXECUTORCH_BUILD_OPENVINO=ON \ -DEXECUTORCH_BUILD_EXTENSION_DATA_LOADER=ON \ -DEXECUTORCH_BUILD_EXTENSION_MODULE=ON \ diff --git a/examples/openvino/CMakeLists.txt b/examples/openvino/CMakeLists.txt index 761de51cf28..4a1917fa3af 100644 --- a/examples/openvino/CMakeLists.txt +++ b/examples/openvino/CMakeLists.txt @@ -56,11 +56,11 @@ target_include_directories(openvino_portable_ops_lib PUBLIC ${_common_include_di # Build Executor Runner add_executable(openvino_executor_runner ${_openvino_executor_runner__srcs}) target_include_directories( - openvino_executor_runner PUBLIC ${_common_include_directories} ${EXECUTORCH_ROOT}/third-party/gflags/include + openvino_executor_runner PUBLIC ${_common_include_directories} ${EXECUTORCH_ROOT}/cmake-openvino-out/third-party/gflags/include ) # Set Library Directory -set(LIBRARY_DIR "${CMAKE_CURRENT_LIST_DIR}/../../cmake-openvino-out/lib/") +set(LIBRARY_DIR "${CMAKE_CURRENT_LIST_DIR}/../../cmake-openvino-out/lib/;${CMAKE_CURRENT_LIST_DIR}/../../cmake-openvino-out/third-party/gflags") message(STATUS "Library directory path: ${LIBRARY_DIR}") # Locate OpenVINO Backend Library @@ -69,15 +69,21 @@ if(NOT OPENVINO_BACKEND_LIB) message(FATAL_ERROR "OpenVINO backend library not found in ${LIBRARY_DIR}") endif() +# Locate OpenVINO Backend Library +find_library(GFLAGS_LIB NAMES gflags_nothreads PATHS ${LIBRARY_DIR} NO_DEFAULT_PATH) +if(NOT GFLAGS_LIB) + message(FATAL_ERROR "Gflags library not found in ${LIBRARY_DIR}") +endif() + # Link Libraries target_link_libraries(openvino_executor_runner PRIVATE ${OPENVINO_BACKEND_LIB} + ${GFLAGS_LIB} executorch executorch_core openvino_portable_ops_lib extension_data_loader extension_runner_util - gflags pthreadpool ) diff --git a/examples/openvino/openvino_build_example.sh b/examples/openvino/openvino_build_example.sh index de6e585a1ab..ee16658941d 100755 --- a/examples/openvino/openvino_build_example.sh +++ b/examples/openvino/openvino_build_example.sh @@ -18,6 +18,7 @@ main() { # Configure the project with CMake # Note: Add any additional configuration options you need here cmake -DCMAKE_INSTALL_PREFIX="${build_dir}" \ + -DCMAKE_BUILD_TYPE=Release \ -DEXECUTORCH_BUILD_OPENVINO=ON \ -DEXECUTORCH_BUILD_EXTENSION_DATA_LOADER=ON \ -DEXECUTORCH_BUILD_EXTENSION_MODULE=ON \