Skip to content

Commit abccf23

Browse files
committed
update: Enable OVEP build with Static OV Libraries for different devices
1 parent 5ce07b8 commit abccf23

File tree

3 files changed

+56
-9
lines changed

3 files changed

+56
-9
lines changed

cmake/CMakeLists.txt

+3
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ option(onnxruntime_CUDA_MINIMAL "Build CUDA without any operations apart from me
8888
option(onnxruntime_ENABLE_CUDA_LINE_NUMBER_INFO "When building with CUDA support, generate device code line number information." OFF)
8989
option(onnxruntime_USE_OPENVINO "Build with OpenVINO support" OFF)
9090
option(onnxruntime_USE_OPENVINO_STATIC_LIBS "Build with OpenVINO support as Static Library" OFF)
91+
option(onnxruntime_USE_OPENVINO_CPU_DEVICE "Build with OpenVINO support as Static Library for CPU Device" OFF)
92+
option(onnxruntime_USE_OPENVINO_GPU_DEVICE "Build with OpenVINO support as Static Library for GPU Device" OFF)
93+
option(onnxruntime_USE_OPENVINO_NPU_DEVICE "Build with OpenVINO support as Static Library for NPU Device" OFF)
9194
option(onnxruntime_USE_COREML "Build with CoreML support" OFF)
9295
option(onnxruntime_USE_NNAPI_BUILTIN "Build with builtin NNAPI lib for Android NNAPI support" OFF)
9396
option(onnxruntime_USE_QNN "Build with QNN support" OFF)

cmake/onnxruntime_providers_openvino.cmake

+32-4
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@
1818
# Header paths
1919
find_package(OpenVINO REQUIRED COMPONENTS Runtime ONNX)
2020
if(OpenVINO_VERSION VERSION_LESS 2023.0)
21-
message(FATAL_ERROR "OpenVINO 2023.0 and newer are supported. Please, latest OpenVINO release")
21+
message(FATAL_ERROR "OpenVINO 2023.0 and newer are supported. Please, latest OpenVINO release")
2222
endif()
2323

2424
if (WIN32)
25-
unset(CMAKE_MAP_IMPORTED_CONFIG_RELWITHDEBINFO)
25+
unset(CMAKE_MAP_IMPORTED_CONFIG_RELWITHDEBINFO)
2626
endif()
2727

2828
if(onnxruntime_USE_OPENVINO_STATIC_LIBS)
@@ -50,13 +50,41 @@
5050
# Use GLOB_RECURSE to find all static library files in the specified directory based on the OS
5151
file(GLOB_RECURSE OPENVINO_POSSIBLE_LIBS "${OPENVINO_STATIC_LIB_DIR}/${OPENVINO_STATIC_LIB_EXT}")
5252

53-
# Iterate over each possible library and check if it exists before appending
54-
foreach(lib ${OPENVINO_POSSIBLE_LIBS})
53+
# Copy all libraries to OPENVINO_COMMON_LIBS
54+
set(OPENVINO_COMMON_LIBS ${OPENVINO_POSSIBLE_LIBS})
55+
56+
# Define the patterns for device-specific libraries
57+
set(OPENVINO_DEVICE_PATTERNS "cpu" "gpu" "npu")
58+
59+
# Filter out device-specific libraries from OPENVINO_COMMON_LIBS
60+
foreach(device_pattern IN LISTS OPENVINO_DEVICE_PATTERNS)
61+
foreach(lib IN LISTS OPENVINO_COMMON_LIBS)
62+
string(FIND "${lib}" "${device_pattern}" device_pos)
63+
if(NOT device_pos EQUAL -1)
64+
list(REMOVE_ITEM OPENVINO_COMMON_LIBS "${lib}")
65+
endif()
66+
endforeach()
67+
endforeach()
68+
69+
# Iterate over each possible common library and check if it exists before appending
70+
foreach(lib ${OPENVINO_COMMON_LIBS})
5571
if(EXISTS "${lib}")
5672
list(APPEND OPENVINO_FOUND_STATIC_LIBS "${lib}")
5773
endif()
5874
endforeach()
5975

76+
# Iterate over each possible library for the specified device and check if it exists before appending
77+
foreach(lib ${OPENVINO_POSSIBLE_LIBS})
78+
if(EXISTS "${lib}")
79+
# Check device-specific variables and append only the required libraries
80+
if((DEFINED onnxruntime_USE_OPENVINO_CPU_DEVICE AND onnxruntime_USE_OPENVINO_CPU_DEVICE AND lib MATCHES ".*cpu.*") OR
81+
(DEFINED onnxruntime_USE_OPENVINO_GPU_DEVICE AND onnxruntime_USE_OPENVINO_GPU_DEVICE AND lib MATCHES ".*gpu.*") OR
82+
(DEFINED onnxruntime_USE_OPENVINO_NPU_DEVICE AND onnxruntime_USE_OPENVINO_NPU_DEVICE AND lib MATCHES ".*npu.*"))
83+
list(APPEND OPENVINO_FOUND_STATIC_LIBS "${lib}")
84+
endif()
85+
endif()
86+
endforeach()
87+
6088
# Append the found static library files to the OPENVINO_LIB_LIST
6189
list(APPEND OPENVINO_LIB_LIST ${OPENVINO_FOUND_STATIC_LIBS})
6290
else()

tools/ci_build/build.py

+21-5
Original file line numberDiff line numberDiff line change
@@ -547,8 +547,9 @@ def convert_arg_line_to_args(self, arg_line):
547547
type=_openvino_verify_device_type,
548548
help="Build with OpenVINO for specific hardware.",
549549
)
550-
parser.add_argument("--use_openvino_static_libs", action="store_true",
551-
help="Build with OpenVINO built as Static Library.")
550+
parser.add_argument("--use_openvino_static_libs", type=str, default="",
551+
help="Build with OpenVINO built as Static Library."
552+
"Specify the device(s) to use in the format [CPU,GPU,NPU]")
552553
parser.add_argument(
553554
"--dnnl_aarch64_runtime", action="store", default="", type=str.lower, help="e.g. --dnnl_aarch64_runtime acl"
554555
)
@@ -1229,10 +1230,9 @@ def generate_build_tree(
12291230

12301231
if args.winml_root_namespace_override:
12311232
cmake_args += ["-Donnxruntime_WINML_NAMESPACE_OVERRIDE=" + args.winml_root_namespace_override]
1232-
if args.use_openvino or args.use_openvino_static_libs:
1233+
if args.use_openvino:
12331234
cmake_args += [
12341235
"-Donnxruntime_USE_OPENVINO=ON",
1235-
"-Donnxruntime_USE_OPENVINO_STATIC_LIBS=" + ("ON" if args.use_openvino_static_libs else "OFF"),
12361236
"-Donnxruntime_NPU_NO_FALLBACK=" + ("ON" if args.use_openvino == "NPU_NO_CPU_FALLBACK" else "OFF"),
12371237
"-Donnxruntime_USE_OPENVINO_GPU=" + ("ON" if args.use_openvino == "GPU" else "OFF"),
12381238
"-Donnxruntime_USE_OPENVINO_CPU=" + ("ON" if args.use_openvino == "CPU" else "OFF"),
@@ -1245,9 +1245,25 @@ def generate_build_tree(
12451245
"-Donnxruntime_USE_OPENVINO_MULTI=" + ("ON" if args.use_openvino.startswith("MULTI") else "OFF"),
12461246
"-Donnxruntime_USE_OPENVINO_AUTO=" + ("ON" if args.use_openvino.startswith("AUTO") else "OFF"),
12471247
]
1248+
if args.use_openvino_static_libs:
1249+
cmake_args += ["-Donnxruntime_USE_OPENVINO_STATIC_LIBS=ON"]
1250+
devices_str = args.use_openvino_static_libs.strip('[]')
1251+
devices = re.split(r',\s*', devices_str)
1252+
1253+
valid_devices = {'CPU', 'GPU', 'NPU'}
1254+
if not all(device in valid_devices for device in devices):
1255+
raise ValueError("Invalid device specified. Valid devices are: CPU, GPU, NPU")
1256+
1257+
for device in devices:
1258+
if device == 'CPU':
1259+
cmake_args.append("-Donnxruntime_USE_OPENVINO_CPU_DEVICE=ON")
1260+
if device == 'GPU':
1261+
cmake_args.append("-Donnxruntime_USE_OPENVINO_GPU_DEVICE=ON")
1262+
if device == 'NPU':
1263+
cmake_args.append("-Donnxruntime_USE_OPENVINO_NPU_DEVICE=ON")
12481264

12491265
# VitisAI and OpenVINO providers currently only support full_protobuf option.
1250-
if args.use_full_protobuf or args.use_openvino or args.use_openvino_static_libs or args.use_vitisai or args.gen_doc:
1266+
if args.use_full_protobuf or args.use_openvino or args.use_vitisai or args.gen_doc:
12511267
cmake_args += ["-Donnxruntime_USE_FULL_PROTOBUF=ON", "-DProtobuf_USE_STATIC_LIBS=ON"]
12521268

12531269
if args.use_tvm and args.llvm_path is not None:

0 commit comments

Comments
 (0)