Skip to content

Commit fa68db1

Browse files
CVS-176081: Add support for nested maps to load_config parsing (#844)
* openvino_provider_factory: Add nested map support to load_config parsing * ParseInnerMap: Add warning that unsupported json types will become fatal in the future * ParseInnerMap: address review comments * load_config: Throw error for unsupported JSON types --------- Co-authored-by: MayureshV1 <[email protected]>
1 parent 7aa5363 commit fa68db1

File tree

1 file changed

+31
-13
lines changed

1 file changed

+31
-13
lines changed

onnxruntime/core/providers/openvino/openvino_provider_factory.cc

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,36 @@ std::string ParseDeviceType(std::shared_ptr<OVCore> ov_core, const ProviderOptio
188188

189189
void ParseProviderOptions([[maybe_unused]] ProviderInfo& result, [[maybe_unused]] const ProviderOptions& config_options) {}
190190

191+
static void ParseInnerMap(const nlohmann::json& json_map, ov::AnyMap& inner_map, size_t level = 0) {
192+
const size_t max_levels = 8;
193+
if (level >= max_levels) {
194+
ORT_THROW("ParseInnerMap: load_config can have only up to " + std::to_string(max_levels) +
195+
" levels of nested maps. Current level = " + std::to_string(level));
196+
}
197+
198+
if (!json_map.is_object()) {
199+
ORT_THROW("ParseInnerMap: Expected an object as input");
200+
}
201+
202+
for (auto& [inner_key, inner_value] : json_map.items()) {
203+
if (inner_value.is_string()) {
204+
inner_map[inner_key] = ov::Any(inner_value.get<std::string>());
205+
} else if (inner_value.is_number_integer()) {
206+
inner_map[inner_key] = ov::Any(inner_value.get<int64_t>());
207+
} else if (inner_value.is_number_float()) {
208+
inner_map[inner_key] = ov::Any(inner_value.get<double>());
209+
} else if (inner_value.is_boolean()) {
210+
inner_map[inner_key] = ov::Any(inner_value.get<bool>());
211+
} else if (inner_value.is_object()) {
212+
auto inner_inner_map = ov::AnyMap();
213+
ParseInnerMap(inner_value, inner_inner_map, level + 1);
214+
inner_map[inner_key] = std::move(inner_inner_map);
215+
} else {
216+
ORT_THROW("load_config: unsupported JSON value type=" + std::string(inner_value.type_name()) + ", for key=" + inner_key);
217+
}
218+
}
219+
}
220+
191221
// Initializes a ProviderInfo struct from a ProviderOptions map and a ConfigOptions map.
192222
static void ParseProviderInfo(const ProviderOptions& provider_options,
193223
const ConfigOptions* config_options,
@@ -267,19 +297,7 @@ static void ParseProviderInfo(const ProviderOptions& provider_options,
267297
ORT_THROW("Invalid JSON structure: Expected an object for device properties.");
268298
}
269299

270-
for (auto& [inner_key, inner_value] : value.items()) {
271-
if (inner_value.is_string()) {
272-
inner_map[inner_key] = inner_value.get<std::string>();
273-
} else if (inner_value.is_number_integer()) {
274-
inner_map[inner_key] = inner_value.get<int64_t>();
275-
} else if (inner_value.is_number_float()) {
276-
inner_map[inner_key] = inner_value.get<double>();
277-
} else if (inner_value.is_boolean()) {
278-
inner_map[inner_key] = inner_value.get<bool>();
279-
} else {
280-
LOGS_DEFAULT(WARNING) << "Unsupported JSON value type for key: " << inner_key << ". Skipping key.";
281-
}
282-
}
300+
ParseInnerMap(value, inner_map);
283301
target_map[key] = std::move(inner_map);
284302
}
285303
} catch (const nlohmann::json::parse_error& e) {

0 commit comments

Comments
 (0)