Skip to content

Commit 053caee

Browse files
Merge pull request #3944 from akodanka:intel_ov_options
LiteRT-PiperOrigin-RevId: 820757648
2 parents 4199154 + ce20b6a commit 053caee

33 files changed

+1906
-42
lines changed

litert/c/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,7 @@ cc_library(
613613
srcs = ["litert_opaque_options.cc"],
614614
hdrs = ["litert_opaque_options.h"],
615615
deps = [":litert_common"],
616+
alwayslink = 1,
616617
)
617618

618619
cc_test(

litert/c/options/BUILD

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,21 @@ cc_library(
131131
],
132132
)
133133

134+
# INTEL OPENVINO ##############################################################################
135+
136+
cc_library(
137+
name = "litert_intel_openvino_options",
138+
srcs = ["litert_intel_openvino_options.cc"],
139+
hdrs = ["litert_intel_openvino_options.h"],
140+
deps = [
141+
"//litert/c:litert_common",
142+
"//litert/c:litert_opaque_options",
143+
"//litert/cc:litert_macros",
144+
"//litert/core/cache:hash_util",
145+
"@com_google_absl//absl/strings:string_view",
146+
],
147+
)
148+
134149
cc_library(
135150
name = "litert_gpu_options",
136151
srcs = ["litert_gpu_options.cc"],

litert/c/options/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ add_library(litert_c_options STATIC
2121
litert_cpu_options.cc
2222
litert_google_tensor_options.cc
2323
litert_gpu_options.cc
24+
litert_intel_openvino_options.cc
2425
litert_mediatek_options.cc
2526
litert_qualcomm_options.cc
2627
litert_runtime_options.cc
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
// Copyright 2025 Google LLC.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
//
15+
// Copyright (c) Qualcomm Innovation Center, Inc. All Rights Reserved.
16+
// SPDX-License-Identifier: Apache-2.0
17+
18+
#include "litert/c/options/litert_intel_openvino_options.h"
19+
20+
#include <cstdint>
21+
#include <memory>
22+
#include <string>
23+
#include <utility>
24+
#include <vector>
25+
26+
#include "absl/strings/string_view.h" // from @com_google_absl
27+
#include "litert/c/litert_common.h"
28+
#include "litert/c/litert_opaque_options.h"
29+
#include "litert/cc/litert_macros.h"
30+
#include "litert/core/cache/hash_util.h"
31+
32+
struct LiteRtIntelOpenVinoOptionsT {
33+
LiteRtIntelOpenVinoDeviceType device_type = kLiteRtIntelOpenVinoDeviceTypeNPU;
34+
LiteRtIntelOpenVinoPerformanceMode performance_mode =
35+
kLiteRtIntelOpenVinoPerformanceModeLatency;
36+
// Store custom configuration options as key-value pairs
37+
std::vector<std::pair<std::string, std::string>> configs_map_options;
38+
};
39+
40+
LiteRtStatus LiteRtIntelOpenVinoOptionsCreate(LiteRtOpaqueOptions* options) {
41+
if (options == nullptr) {
42+
return kLiteRtStatusErrorInvalidArgument;
43+
}
44+
45+
auto options_data = std::make_unique<LiteRtIntelOpenVinoOptionsT>();
46+
47+
LITERT_RETURN_IF_ERROR(LiteRtCreateOpaqueOptions(
48+
LiteRtIntelOpenVinoOptionsGetIdentifier(), options_data.get(),
49+
[](void* payload) {
50+
delete reinterpret_cast<LiteRtIntelOpenVinoOptions>(payload);
51+
},
52+
options));
53+
54+
auto intel_openvino_hash = [](const void* payload) -> uint64_t {
55+
const LiteRtIntelOpenVinoOptionsT* opts =
56+
reinterpret_cast<const LiteRtIntelOpenVinoOptionsT*>(payload);
57+
uint64_t ans = 0;
58+
litert::HashCombine(ans, opts->device_type, opts->performance_mode);
59+
// Hash the configs_map_options
60+
for (const auto& pair : opts->configs_map_options) {
61+
litert::HashCombine(ans, pair.first, pair.second);
62+
}
63+
return ans;
64+
};
65+
LITERT_RETURN_IF_ERROR(
66+
LiteRtSetOpaqueOptionsHash(*options, intel_openvino_hash));
67+
68+
options_data.release();
69+
return kLiteRtStatusOk;
70+
}
71+
72+
const char* LiteRtIntelOpenVinoOptionsGetIdentifier() {
73+
return "intel_openvino";
74+
}
75+
76+
LiteRtStatus LiteRtIntelOpenVinoOptionsGet(
77+
LiteRtOpaqueOptions options, LiteRtIntelOpenVinoOptions* options_data) {
78+
if (options_data == nullptr || options == nullptr) {
79+
return kLiteRtStatusErrorInvalidArgument;
80+
}
81+
const char* identifier;
82+
LITERT_RETURN_IF_ERROR(
83+
LiteRtGetOpaqueOptionsIdentifier(options, &identifier));
84+
if (absl::NullSafeStringView(identifier) !=
85+
LiteRtIntelOpenVinoOptionsGetIdentifier()) {
86+
return kLiteRtStatusErrorInvalidArgument;
87+
}
88+
void* payload;
89+
LITERT_RETURN_IF_ERROR(LiteRtGetOpaqueOptionsData(options, &payload));
90+
*options_data = reinterpret_cast<LiteRtIntelOpenVinoOptionsT*>(payload);
91+
return kLiteRtStatusOk;
92+
}
93+
94+
// COMPILATION OPTIONS /////////////////////////////////////////////////////////
95+
96+
// device_type ----------------------------------------------------------------
97+
LiteRtStatus LiteRtIntelOpenVinoOptionsSetDeviceType(
98+
LiteRtIntelOpenVinoOptions options,
99+
LiteRtIntelOpenVinoDeviceType device_type) {
100+
if (options == nullptr) {
101+
return kLiteRtStatusErrorInvalidArgument;
102+
}
103+
options->device_type = device_type;
104+
return kLiteRtStatusOk;
105+
}
106+
107+
LiteRtStatus LiteRtIntelOpenVinoOptionsGetDeviceType(
108+
LiteRtIntelOpenVinoOptions options,
109+
LiteRtIntelOpenVinoDeviceType* device_type) {
110+
if (options == nullptr || device_type == nullptr) {
111+
return kLiteRtStatusErrorInvalidArgument;
112+
}
113+
*device_type = options->device_type;
114+
return kLiteRtStatusOk;
115+
}
116+
117+
// performance_mode -----------------------------------------------------------
118+
LiteRtStatus LiteRtIntelOpenVinoOptionsSetPerformanceMode(
119+
LiteRtIntelOpenVinoOptions options,
120+
LiteRtIntelOpenVinoPerformanceMode performance_mode) {
121+
if (options == nullptr) {
122+
return kLiteRtStatusErrorInvalidArgument;
123+
}
124+
options->performance_mode = performance_mode;
125+
return kLiteRtStatusOk;
126+
}
127+
128+
LiteRtStatus LiteRtIntelOpenVinoOptionsGetPerformanceMode(
129+
LiteRtIntelOpenVinoOptions options,
130+
LiteRtIntelOpenVinoPerformanceMode* performance_mode) {
131+
if (options == nullptr || performance_mode == nullptr) {
132+
return kLiteRtStatusErrorInvalidArgument;
133+
}
134+
*performance_mode = options->performance_mode;
135+
return kLiteRtStatusOk;
136+
}
137+
138+
// configs_map_options --------------------------------------------------------
139+
LiteRtStatus LiteRtIntelOpenVinoOptionsSetConfigsMapOption(
140+
LiteRtIntelOpenVinoOptions options, const char* key, const char* value) {
141+
if (options == nullptr || key == nullptr || value == nullptr) {
142+
return kLiteRtStatusErrorInvalidArgument;
143+
}
144+
// Check if the key already exists and update it, otherwise add new
145+
for (auto& pair : options->configs_map_options) {
146+
if (pair.first == key) {
147+
pair.second = value;
148+
return kLiteRtStatusOk;
149+
}
150+
}
151+
options->configs_map_options.emplace_back(key, value);
152+
return kLiteRtStatusOk;
153+
}
154+
155+
LiteRtStatus LiteRtIntelOpenVinoOptionsGetNumConfigsMapOptions(
156+
LiteRtIntelOpenVinoOptions options, int* num_options) {
157+
if (options == nullptr || num_options == nullptr) {
158+
return kLiteRtStatusErrorInvalidArgument;
159+
}
160+
*num_options = static_cast<int>(options->configs_map_options.size());
161+
return kLiteRtStatusOk;
162+
}
163+
164+
LiteRtStatus LiteRtIntelOpenVinoOptionsGetConfigsMapOption(
165+
LiteRtIntelOpenVinoOptions options, int index, const char** key,
166+
const char** value) {
167+
if (options == nullptr || key == nullptr || value == nullptr) {
168+
return kLiteRtStatusErrorInvalidArgument;
169+
}
170+
if (index < 0 ||
171+
index >= static_cast<int>(options->configs_map_options.size())) {
172+
return kLiteRtStatusErrorIndexOOB;
173+
}
174+
const auto& pair = options->configs_map_options[index];
175+
*key = pair.first.c_str();
176+
*value = pair.second.c_str();
177+
return kLiteRtStatusOk;
178+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
// Copyright 2025 Google LLC.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
//
15+
// Copyright (c) Qualcomm Innovation Center, Inc. All Rights Reserved.
16+
// SPDX-License-Identifier: Apache-2.0
17+
18+
#ifndef THIRD_PARTY_ODML_LITERT_LITERT_C_OPTIONS_LITERT_INTEL_OPENVINO_OPTIONS_H_
19+
#define THIRD_PARTY_ODML_LITERT_LITERT_C_OPTIONS_LITERT_INTEL_OPENVINO_OPTIONS_H_
20+
#include "litert/c/litert_common.h"
21+
22+
#ifdef __cplusplus
23+
extern "C" {
24+
#endif // __cplusplus
25+
26+
// Create an Intel OpenVINO options object that is type erased. The actual
27+
// option data can be accessed from the payload.
28+
LiteRtStatus LiteRtIntelOpenVinoOptionsCreate(LiteRtOpaqueOptions* options);
29+
LITERT_DEFINE_HANDLE(LiteRtIntelOpenVinoOptions);
30+
31+
// The string identifier that discriminates Intel OpenVINO options within
32+
// type erased options.
33+
const char* LiteRtIntelOpenVinoOptionsGetIdentifier();
34+
35+
// Attempt to retrieve Intel OpenVINO options from the opaque options. Fails
36+
// if the opaque options are of another type.
37+
LiteRtStatus LiteRtIntelOpenVinoOptionsGet(
38+
LiteRtOpaqueOptions options, LiteRtIntelOpenVinoOptions* options_data);
39+
40+
// COMPILATION OPTIONS /////////////////////////////////////////////////////////
41+
42+
// device_type ----------------------------------------------------------------
43+
typedef enum LiteRtIntelOpenVinoDeviceType {
44+
kLiteRtIntelOpenVinoDeviceTypeCPU = 0,
45+
kLiteRtIntelOpenVinoDeviceTypeGPU = 1,
46+
kLiteRtIntelOpenVinoDeviceTypeNPU = 2,
47+
kLiteRtIntelOpenVinoDeviceTypeAUTO = 3,
48+
} LiteRtIntelOpenVinoDeviceType;
49+
50+
LiteRtStatus LiteRtIntelOpenVinoOptionsSetDeviceType(
51+
LiteRtIntelOpenVinoOptions options,
52+
enum LiteRtIntelOpenVinoDeviceType device_type);
53+
54+
LiteRtStatus LiteRtIntelOpenVinoOptionsGetDeviceType(
55+
LiteRtIntelOpenVinoOptions options,
56+
enum LiteRtIntelOpenVinoDeviceType* device_type);
57+
58+
// performance_mode -----------------------------------------------------------
59+
60+
// Configures OpenVINO devices to optimize for performance or efficiency.
61+
// See ov::hint::PerformanceMode in OpenVINO. By default, it
62+
// will use LATENCY mode.
63+
64+
typedef enum LiteRtIntelOpenVinoPerformanceMode {
65+
/* Optimize for low latency */
66+
kLiteRtIntelOpenVinoPerformanceModeLatency = 0,
67+
/* Optimize for high throughput */
68+
kLiteRtIntelOpenVinoPerformanceModeThroughput = 1,
69+
/* Optimize for cumulative throughput */
70+
kLiteRtIntelOpenVinoPerformanceModeCumulativeThroughput = 2,
71+
} LiteRtIntelOpenVinoPerformanceMode;
72+
73+
LiteRtStatus LiteRtIntelOpenVinoOptionsSetPerformanceMode(
74+
LiteRtIntelOpenVinoOptions options,
75+
LiteRtIntelOpenVinoPerformanceMode performance_mode);
76+
77+
LiteRtStatus LiteRtIntelOpenVinoOptionsGetPerformanceMode(
78+
LiteRtIntelOpenVinoOptions options,
79+
LiteRtIntelOpenVinoPerformanceMode* performance_mode);
80+
81+
// configs_map ----------------------------------------------------------------
82+
83+
// Set a custom configuration option with a string key-value pair.
84+
// The key and value strings are copied internally, so their lifetime does not
85+
// need to extend beyond this function call.
86+
LiteRtStatus LiteRtIntelOpenVinoOptionsSetConfigsMapOption(
87+
LiteRtIntelOpenVinoOptions options, const char* key, const char* value);
88+
89+
// Get the number of custom configuration options
90+
LiteRtStatus LiteRtIntelOpenVinoOptionsGetNumConfigsMapOptions(
91+
LiteRtIntelOpenVinoOptions options, int* num_options);
92+
93+
// Get a custom configuration option by index.
94+
// The returned key and value pointers point to internal string data
95+
// and are valid for the lifetime of the options object.
96+
// The caller should not free these pointers.
97+
LiteRtStatus LiteRtIntelOpenVinoOptionsGetConfigsMapOption(
98+
LiteRtIntelOpenVinoOptions options, int index, const char** key,
99+
const char** value);
100+
101+
#ifdef __cplusplus
102+
103+
} // extern "C"
104+
105+
#endif // __cplusplus
106+
#endif // THIRD_PARTY_ODML_LITERT_LITERT_C_OPTIONS_LITERT_INTEL_OPENVINO_OPTIONS_H_

litert/cc/BUILD

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1287,6 +1287,7 @@ cc_library(
12871287
"//litert/cc/options:litert_mediatek_options.h",
12881288
"//litert/cc/options:litert_qualcomm_options.h",
12891289
"//litert/cc/options:litert_runtime_options.h",
1290+
"//litert/cc/options:litert_intel_openvino_options.h",
12901291
# Other headers needed by the C++ API headers.
12911292
"//litert/cc/internal:litert_c_types_printing.h",
12921293
"//litert/cc/internal:litert_consts.h",
@@ -1328,6 +1329,7 @@ cc_library(
13281329
"//litert/cc/dynamic_runtime/options:litert_cpu_options",
13291330
"//litert/cc/dynamic_runtime/options:litert_gpu_options",
13301331
"//litert/cc/dynamic_runtime/options:litert_qualcomm_options",
1332+
"//litert/cc/dynamic_runtime/options:litert_intel_openvino_options",
13311333
"//litert/cc/dynamic_runtime/options:litert_runtime_options",
13321334
# Other (static) C++ API targets.
13331335
":litert_any",

litert/cc/dynamic_runtime/options/BUILD

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ LITERT_C_API_DEPS = [
3535
"//litert/c/options:litert_darwinn_runtime_options",
3636
"//litert/c/options:litert_google_tensor_options",
3737
"//litert/c/options:litert_gpu_options",
38+
"//litert/c/options:litert_intel_openvino_options",
3839
"//litert/c/options:litert_mediatek_options",
3940
"//litert/c/options:litert_qualcomm_options",
4041
"//litert/c/options:litert_runtime_options",
@@ -168,6 +169,27 @@ cc_library(
168169
"//litert/c:litert_runtime_c_api_shared_lib",
169170
],
170171
}),
172+
alwayslink = 1,
173+
)
174+
175+
cc_library(
176+
name = "litert_intel_openvino_options",
177+
srcs = ["//litert/cc/options:litert_intel_openvino_options.cc"],
178+
hdrs = ["//litert/cc/options:litert_intel_openvino_options.h"],
179+
deps = [
180+
"//litert/c/options:litert_intel_openvino_options",
181+
"//litert/cc:litert_expected",
182+
"//litert/cc:litert_handle",
183+
"//litert/cc:litert_macros",
184+
"//litert/cc/dynamic_runtime:litert_opaque_options",
185+
"@com_google_absl//absl/strings:string_view",
186+
] + select({
187+
"@org_tensorflow//tensorflow:ios": LITERT_C_API_DEPS,
188+
"@org_tensorflow//tensorflow:emscripten": LITERT_C_API_DEPS,
189+
"//conditions:default": [
190+
"//litert/c:litert_runtime_c_api_shared_lib",
191+
],
192+
}),
171193
)
172194

173195
cc_library(

0 commit comments

Comments
 (0)