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

feat: Enable OVEP build with Static OV Libraries #394

Open
wants to merge 2 commits into
base: ovep-develop-lnl-1.1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
12 changes: 8 additions & 4 deletions cmake/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ option(onnxruntime_USE_CUDA_NHWC_OPS "Build CUDA with NHWC op support" OFF)
option(onnxruntime_CUDA_MINIMAL "Build CUDA without any operations apart from memcpy ops. Usefuel for a very minial TRT build" OFF)
option(onnxruntime_ENABLE_CUDA_LINE_NUMBER_INFO "When building with CUDA support, generate device code line number information." OFF)
option(onnxruntime_USE_OPENVINO "Build with OpenVINO support" OFF)
option(onnxruntime_USE_OPENVINO_STATIC_LIBS "Build with OpenVINO support as Static Library" OFF)
option(onnxruntime_USE_OPENVINO_CPU_DEVICE "Build with OpenVINO support as Static Library for CPU Device" OFF)
option(onnxruntime_USE_OPENVINO_GPU_DEVICE "Build with OpenVINO support as Static Library for GPU Device" OFF)
option(onnxruntime_USE_OPENVINO_NPU_DEVICE "Build with OpenVINO support as Static Library for NPU Device" OFF)
option(onnxruntime_USE_COREML "Build with CoreML support" OFF)
option(onnxruntime_USE_NNAPI_BUILTIN "Build with builtin NNAPI lib for Android NNAPI support" OFF)
option(onnxruntime_USE_QNN "Build with QNN support" OFF)
Expand Down Expand Up @@ -140,7 +144,7 @@ option(onnxruntime_ARMNN_RELU_USE_CPU "Use the CPU implementation for the Relu o
option(onnxruntime_ARMNN_BN_USE_CPU "Use the CPU implementation for the Batch Normalization operator for the ArmNN EP" ON)
option(onnxruntime_ENABLE_INSTRUMENT "Enable Instrument with Event Tracing for Windows (ETW)" OFF)
option(onnxruntime_USE_TELEMETRY "Build with Telemetry" OFF)
cmake_dependent_option(onnxruntime_USE_MIMALLOC "Override new/delete and arena allocator with mimalloc" OFF "WIN32;NOT onnxruntime_USE_CUDA;NOT onnxruntime_USE_OPENVINO" OFF)
cmake_dependent_option(onnxruntime_USE_MIMALLOC "Override new/delete and arena allocator with mimalloc" OFF "WIN32;NOT onnxruntime_USE_CUDA;NOT onnxruntime_USE_OPENVINO; NOT onnxruntime_USE_OPENVINO_STATIC_LIBS" OFF)
option(onnxruntime_USE_CANN "Build with CANN support" OFF)
option(onnxruntime_USE_ROCM "Build with AMD GPU support" OFF)
option(onnxruntime_USE_TVM "Build with TVM support" OFF)
Expand Down Expand Up @@ -606,7 +610,7 @@ if (WIN32)
# structure was padded due to __declspec(align())
list(APPEND ORT_WARNING_FLAGS "/wd4324")
# warning C4800: Implicit conversion from 'X' to bool. Possible information loss
if (onnxruntime_USE_OPENVINO)
if (onnxruntime_USE_OPENVINO OR onnxruntime_USE_OPENVINO_STATIC_LIBS)
list(APPEND ORT_WARNING_FLAGS "/wd4800")
endif()
# operator 'operator-name': deprecated between enumerations of different types
Expand Down Expand Up @@ -782,7 +786,7 @@ if (onnxruntime_USE_DNNL)
list(APPEND ONNXRUNTIME_PROVIDER_NAMES dnnl)
list(APPEND ORT_PROVIDER_CMAKE_FLAGS -Donnxruntime_USE_DNNL=1)
endif()
if (onnxruntime_USE_OPENVINO)
if (onnxruntime_USE_OPENVINO OR onnxruntime_USE_OPENVINO_STATIC_LIBS)
list(APPEND ORT_PROVIDER_FLAGS -DUSE_OPENVINO=1)
list(APPEND ONNXRUNTIME_PROVIDER_NAMES openvino)
list(APPEND ORT_PROVIDER_CMAKE_FLAGS -Donnxruntime_USE_OPENVINO=1)
Expand Down Expand Up @@ -1343,7 +1347,7 @@ if (onnxruntime_ENABLE_TRAINING_APIS)
)
endif()

if (onnxruntime_USE_OPENVINO)
if (onnxruntime_USE_OPENVINO OR onnxruntime_USE_OPENVINO_STATIC_LIBS)

add_definitions(-DUSE_OPENVINO=1)

Expand Down
69 changes: 69 additions & 0 deletions cmake/onnxruntime_providers_openvino.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,73 @@
unset(CMAKE_MAP_IMPORTED_CONFIG_RELWITHDEBINFO)
endif()

if(onnxruntime_USE_OPENVINO_STATIC_LIBS)
# Get the INTEL_OPENVINO_DIR environment variable
file(TO_CMAKE_PATH "$ENV{INTEL_OPENVINO_DIR}" OpenVINO_BASE_DIR)

# Define the suffix path
set(OPENVINO_SUFFIX_PATH "runtime/lib/intel64/Release")

# Combine the base directory with the suffix path
file(TO_CMAKE_PATH "${OpenVINO_BASE_DIR}/${OPENVINO_SUFFIX_PATH}" OPENVINO_STATIC_LIB_DIR)

# Check if the combined directory exists, If the directory exists, proceed with setting up the static libraries
if(IS_DIRECTORY "${OPENVINO_STATIC_LIB_DIR}")
# Initialize an empty list to hold the found static libraries
set(OPENVINO_FOUND_STATIC_LIBS)

# Use the appropriate file extension for static libraries based on the host operating system
if(CMAKE_HOST_WIN32)
set(OPENVINO_STATIC_LIB_EXT "*.lib")
elseif(CMAKE_HOST_UNIX)
set(OPENVINO_STATIC_LIB_EXT "*.a")
endif()

# Use GLOB_RECURSE to find all static library files in the specified directory based on the OS
file(GLOB_RECURSE OPENVINO_POSSIBLE_LIBS "${OPENVINO_STATIC_LIB_DIR}/${OPENVINO_STATIC_LIB_EXT}")

# Copy all libraries to OPENVINO_COMMON_LIBS
set(OPENVINO_COMMON_LIBS ${OPENVINO_POSSIBLE_LIBS})

# Define the patterns for device-specific libraries
set(OPENVINO_DEVICE_PATTERNS "cpu" "gpu" "npu")

# Filter out device-specific libraries from OPENVINO_COMMON_LIBS
foreach(device_pattern IN LISTS OPENVINO_DEVICE_PATTERNS)
foreach(lib IN LISTS OPENVINO_COMMON_LIBS)
string(FIND "${lib}" "${device_pattern}" device_pos)
if(NOT device_pos EQUAL -1)
list(REMOVE_ITEM OPENVINO_COMMON_LIBS "${lib}")
endif()
endforeach()
endforeach()

# Iterate over each possible common library and check if it exists before appending
foreach(lib ${OPENVINO_COMMON_LIBS})
if(EXISTS "${lib}")
list(APPEND OPENVINO_FOUND_STATIC_LIBS "${lib}")
endif()
endforeach()

# Iterate over each possible library for the specified device and check if it exists before appending
foreach(lib ${OPENVINO_POSSIBLE_LIBS})
if(EXISTS "${lib}")
# Check device-specific variables and append only the required libraries
if((DEFINED onnxruntime_USE_OPENVINO_CPU_DEVICE AND onnxruntime_USE_OPENVINO_CPU_DEVICE AND lib MATCHES ".*cpu.*") OR
(DEFINED onnxruntime_USE_OPENVINO_GPU_DEVICE AND onnxruntime_USE_OPENVINO_GPU_DEVICE AND lib MATCHES ".*gpu.*") OR
(DEFINED onnxruntime_USE_OPENVINO_NPU_DEVICE AND onnxruntime_USE_OPENVINO_NPU_DEVICE AND lib MATCHES ".*npu.*"))
list(APPEND OPENVINO_FOUND_STATIC_LIBS "${lib}")
endif()
endif()
endforeach()

# Append the found static library files to the OPENVINO_LIB_LIST
list(APPEND OPENVINO_LIB_LIST ${OPENVINO_FOUND_STATIC_LIBS})
else()
message(FATAL_ERROR "The specified OpenVINO static library directory does not exist: ${OPENVINO_STATIC_LIB_DIR}")
endif()
endif()

list(APPEND OPENVINO_LIB_LIST openvino::frontend::onnx openvino::runtime ${PYTHON_LIBRARIES})
if ((DEFINED ENV{OPENCL_LIBS}) AND (DEFINED ENV{OPENCL_INCS}))
add_definitions(-DIO_BUFFER_ENABLED=1)
Expand All @@ -38,6 +105,8 @@
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/onnxruntime/)
set_target_properties(onnxruntime_providers_openvino PROPERTIES LINKER_LANGUAGE CXX)
set_target_properties(onnxruntime_providers_openvino PROPERTIES FOLDER "ONNXRuntime")
set_target_properties(onnxruntime_providers_openvino PROPERTIES INTERPROCEDURAL_OPTIMIZATION_RELEASE ON INTERPROCEDURAL_OPTIMIZATION_DEBUG ON INTERPROCEDURAL_OPTIMIZATION_RELWITHDEBINFO ON)

if(NOT MSVC)
target_compile_options(onnxruntime_providers_openvino PRIVATE "-Wno-parentheses")
endif()
Expand Down
19 changes: 19 additions & 0 deletions tools/ci_build/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,9 @@ def convert_arg_line_to_args(self, arg_line):
type=_openvino_verify_device_type,
help="Build with OpenVINO for specific hardware.",
)
parser.add_argument("--use_openvino_static_libs", type=str, default="",
help="Build with OpenVINO built as Static Library."
"Specify the device(s) to use in the format [CPU,GPU,NPU]")
parser.add_argument(
"--dnnl_aarch64_runtime", action="store", default="", type=str.lower, help="e.g. --dnnl_aarch64_runtime acl"
)
Expand Down Expand Up @@ -1242,6 +1245,22 @@ def generate_build_tree(
"-Donnxruntime_USE_OPENVINO_MULTI=" + ("ON" if args.use_openvino.startswith("MULTI") else "OFF"),
"-Donnxruntime_USE_OPENVINO_AUTO=" + ("ON" if args.use_openvino.startswith("AUTO") else "OFF"),
]
if args.use_openvino_static_libs:
cmake_args += ["-Donnxruntime_USE_OPENVINO_STATIC_LIBS=ON"]
devices_str = args.use_openvino_static_libs.strip('[]')
devices = re.split(r',\s*', devices_str)

valid_devices = {'CPU', 'GPU', 'NPU'}
if not all(device in valid_devices for device in devices):
raise ValueError("Invalid device specified. Valid devices are: CPU, GPU, NPU")

for device in devices:
if device == 'CPU':
cmake_args.append("-Donnxruntime_USE_OPENVINO_CPU_DEVICE=ON")
if device == 'GPU':
cmake_args.append("-Donnxruntime_USE_OPENVINO_GPU_DEVICE=ON")
if device == 'NPU':
cmake_args.append("-Donnxruntime_USE_OPENVINO_NPU_DEVICE=ON")

# VitisAI and OpenVINO providers currently only support full_protobuf option.
if args.use_full_protobuf or args.use_openvino or args.use_vitisai or args.gen_doc:
Expand Down
Loading