Skip to content

Commit

Permalink
[ONNX] Update external data location in Constant nodes (#12992)
Browse files Browse the repository at this point in the history
Ticket: 91271
  • Loading branch information
mateusztabaka committed Sep 9, 2022
1 parent c076284 commit dcc8f92
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
ir_version: 3
producer_name: "nGraph ONNX Importer"
graph {
node {
output: "B"
op_type: "Constant"
attribute {
name: "value"
t {
dims: 2
dims: 2
data_type: 1
name: "const_tensor"
external_data {
key: "location",
value: "tensors_data/tensor.data"
}
data_location: 1
}
type: TENSOR
}
}
node {
input: "A"
input: "B"
output: "X"
name: "add_node1"
op_type: "Add"
}
name: "test_graph"
input {
name: "A"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 2
}
dim {
dim_value: 2
}
}
}
}
}
output {
name: "X"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 2
}
dim {
dim_value: 2
}
}
}
}
}
}
opset_import {
version: 4
}
35 changes: 27 additions & 8 deletions src/frontends/onnx/frontend/src/core/transform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,12 @@ void ngraph::onnx_import::transform::update_external_data_paths(ONNX_NAMESPACE::
if (model_path.empty()) {
return;
}
const auto model_dir_path = file_util::get_directory(model_path);
auto graph_proto = model_proto.mutable_graph();
for (auto& initializer_tensor : *graph_proto->mutable_initializer()) {

auto update_tensor_path = [](ONNX_NAMESPACE::TensorProto& tensor, const std::string& model_dir_path) {
const auto location_key_value_index = 0;
if (initializer_tensor.has_data_location() &&
initializer_tensor.data_location() ==
ONNX_NAMESPACE::TensorProto_DataLocation::TensorProto_DataLocation_EXTERNAL) {
const auto external_data_relative_path = initializer_tensor.external_data(location_key_value_index).value();
if (tensor.has_data_location() &&
tensor.data_location() == ONNX_NAMESPACE::TensorProto_DataLocation::TensorProto_DataLocation_EXTERNAL) {
const auto external_data_relative_path = tensor.external_data(location_key_value_index).value();
const auto santized_external_data_relative_path = file_util::sanitize_path(external_data_relative_path);
auto external_data_full_path = file_util::path_join(model_dir_path, santized_external_data_relative_path);

Expand All @@ -128,9 +126,30 @@ void ngraph::onnx_import::transform::update_external_data_paths(ONNX_NAMESPACE::
#endif

// Set full paths to the external file
initializer_tensor.mutable_external_data(location_key_value_index)->set_value(external_data_full_path);
tensor.mutable_external_data(location_key_value_index)->set_value(external_data_full_path);
}
};

const auto model_dir_path = file_util::get_directory(model_path);
auto graph_proto = model_proto.mutable_graph();
for (auto& initializer_tensor : *graph_proto->mutable_initializer()) {
update_tensor_path(initializer_tensor, model_dir_path);
}

for (auto& node : *graph_proto->mutable_node()) {
if (node.op_type() != "Constant") {
continue;
}

for (auto& attribute : *node.mutable_attribute()) {
if (attribute.type() != ONNX_NAMESPACE::AttributeProto_AttributeType_TENSOR) {
continue;
}
auto tensor = attribute.mutable_t();
update_tensor_path(*tensor, model_dir_path);
}
}

NGRAPH_SUPPRESS_DEPRECATED_END
}

Expand Down
11 changes: 11 additions & 0 deletions src/frontends/tests/onnx/onnx_import_external_data.in.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,14 @@ NGRAPH_TEST(${BACKEND_NAME}, onnx_external_data_sanitize_path) {

test_case.run();
}

NGRAPH_TEST(${BACKEND_NAME}, onnx_external_data_in_constant_node) {
const auto function = onnx_import::import_onnx_model(
file_util::path_join(SERIALIZED_ZOO, "onnx/external_data/external_data_in_constant_node.onnx"));

auto test_case = test::TestCase(function, s_device);
test_case.add_input<float>({3.f, 5.f, 8.f, 13.f});
test_case.add_expected_output<float>(Shape{2, 2}, {4.f, 7.f, 11.f, 17.f});

test_case.run();
}

0 comments on commit dcc8f92

Please sign in to comment.