Skip to content

Commit

Permalink
update: Enable OVEP build with Static OV Libraries for different devices
Browse files Browse the repository at this point in the history
  • Loading branch information
ankitm3k committed Jul 9, 2024
1 parent 5ce07b8 commit 5f745d2
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 8 deletions.
3 changes: 3 additions & 0 deletions cmake/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ option(onnxruntime_CUDA_MINIMAL "Build CUDA without any operations apart from me
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
34 changes: 31 additions & 3 deletions cmake/onnxruntime_providers_openvino.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
endif()

if (WIN32)
unset(CMAKE_MAP_IMPORTED_CONFIG_RELWITHDEBINFO)
unset(CMAKE_MAP_IMPORTED_CONFIG_RELWITHDEBINFO)
endif()

if(onnxruntime_USE_OPENVINO_STATIC_LIBS)
Expand Down Expand Up @@ -50,13 +50,41 @@
# 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}")

# Iterate over each possible library and check if it exists before appending
foreach(lib ${OPENVINO_POSSIBLE_LIBS})
# 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()
Expand Down
26 changes: 21 additions & 5 deletions tools/ci_build/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -547,8 +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", action="store_true",
help="Build with OpenVINO built as Static Library.")
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 @@ -1229,10 +1230,9 @@ def generate_build_tree(

if args.winml_root_namespace_override:
cmake_args += ["-Donnxruntime_WINML_NAMESPACE_OVERRIDE=" + args.winml_root_namespace_override]
if args.use_openvino or args.use_openvino_static_libs:
if args.use_openvino:
cmake_args += [
"-Donnxruntime_USE_OPENVINO=ON",
"-Donnxruntime_USE_OPENVINO_STATIC_LIBS=" + ("ON" if args.use_openvino_static_libs else "OFF"),
"-Donnxruntime_NPU_NO_FALLBACK=" + ("ON" if args.use_openvino == "NPU_NO_CPU_FALLBACK" else "OFF"),
"-Donnxruntime_USE_OPENVINO_GPU=" + ("ON" if args.use_openvino == "GPU" else "OFF"),
"-Donnxruntime_USE_OPENVINO_CPU=" + ("ON" if args.use_openvino == "CPU" else "OFF"),
Expand All @@ -1245,9 +1245,25 @@ 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_openvino_static_libs or args.use_vitisai or args.gen_doc:
if args.use_full_protobuf or args.use_openvino or args.use_vitisai or args.gen_doc:
cmake_args += ["-Donnxruntime_USE_FULL_PROTOBUF=ON", "-DProtobuf_USE_STATIC_LIBS=ON"]

if args.use_tvm and args.llvm_path is not None:
Expand Down

0 comments on commit 5f745d2

Please sign in to comment.