Skip to content

Commit af29be7

Browse files
committed
Add function to query external initializer file name
1 parent 7401335 commit af29be7

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

onnxruntime/core/providers/openvino/backend_utils.cc

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,51 @@ void DestroyOVTensors(SharedContext::SharedWeights::Metadata::Map& metadata_map)
436436
metadata_map.clear();
437437
}
438438

439+
std::optional<std::string> GetExternalWeightFilename(const GraphViewer& graph) {
440+
auto get_external_location = [](const ONNX_NAMESPACE::TensorProto& proto) -> std::optional<std::string> {
441+
using mutable_proto_t = ONNX_NAMESPACE::TensorProto*;
442+
auto& mutable_proto = *const_cast<mutable_proto_t>(&proto);
443+
auto* entry_protos = mutable_proto.mutable_external_data();
444+
445+
if (proto.has_data_location() && proto.data_location() == ONNX_NAMESPACE::TensorProto_DataLocation_EXTERNAL) {
446+
for (int i = 0; i < entry_protos->size(); i++) {
447+
auto& string_entry_proto{entry_protos->at(i)};
448+
const auto& pb_key{*(string_entry_proto.mutable_key())};
449+
const auto& pb_value{*(string_entry_proto.mutable_value())};
450+
if (pb_key == "location") {
451+
return std::make_optional<std::string>(pb_value);
452+
}
453+
}
454+
}
455+
456+
return std::nullopt;
457+
};
458+
459+
// Handle constant initializers
460+
auto& initializers = graph.GetAllInitializedTensors();
461+
for (const auto& it : initializers) {
462+
if (auto result = get_external_location(*it.second)) {
463+
return result;
464+
}
465+
}
466+
467+
// Handle outer-scope constant initializers
468+
for (auto& node_idx : graph.GetNodesInTopologicalOrder()) {
469+
const auto& node = graph.GetNode(node_idx);
470+
for (const auto& input : node->InputDefs()) {
471+
if (graph.IsConstantInitializer(input->Name(), true)) {
472+
const auto& initializer_tensor = *graph.GetConstantInitializer(input->Name(), true);
473+
474+
if (auto result = get_external_location(initializer_tensor)) {
475+
return result;
476+
}
477+
}
478+
}
479+
}
480+
481+
return std::nullopt;
482+
}
483+
439484
} // namespace backend_utils
440485
} // namespace openvino_ep
441486
} // namespace onnxruntime

onnxruntime/core/providers/openvino/backend_utils.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ void printPerformanceCounts(const std::vector<OVProfilingInfo>& performanceMap,
7676

7777
void printPerformanceCounts(OVInferRequestPtr request, std::ostream& stream, std::string deviceName);
7878

79+
// Returns the location string from the first external initializer nodes found or nullopt if none found
80+
std::optional<std::string> GetExternalWeightFilename(const GraphViewer& graph);
81+
7982
} // namespace backend_utils
8083
} // namespace openvino_ep
8184
} // namespace onnxruntime

0 commit comments

Comments
 (0)