[ONNX FE] Fixed handling duplicates during graph extraction (#17071)

This commit is contained in:
Mateusz Bencer
2023-04-20 13:10:09 +02:00
committed by GitHub
parent f100c36ac9
commit 77a5d1aa03
6 changed files with 132 additions and 23 deletions

View File

@@ -464,8 +464,8 @@ def test_extract_subgraph_2():
model = fe.load("input_model.onnx")
assert model
place1 = model.get_place_by_tensor_name(tensor_name="add_out")
place2 = model.get_place_by_tensor_name(tensor_name="out3")
place1 = model.get_place_by_tensor_name(tensor_name="out3")
place2 = model.get_place_by_tensor_name(tensor_name="add_out")
model.extract_subgraph(inputs=[], outputs=[place1, place2])
result_model = fe.convert(model)
@@ -616,8 +616,8 @@ def test_override_all_outputs():
model = fe.load("input_model.onnx")
assert model
place1 = model.get_place_by_tensor_name(tensor_name="add_out")
place2 = model.get_place_by_tensor_name(tensor_name="out3")
place1 = model.get_place_by_tensor_name(tensor_name="out3")
place2 = model.get_place_by_tensor_name(tensor_name="add_out")
model.override_all_outputs(outputs=[place1, place2])
result_model = fe.convert(model)

View File

@@ -222,9 +222,6 @@ std::pair<bool, std::string> append_new_graph_input(ONNX_NAMESPACE::GraphProto&
/// original model.
void append_new_graph_output(ONNX_NAMESPACE::GraphProto& graph, const OutputEdge& edge) {
const auto tensor_name = get_output_tensor_name(graph, edge);
if (already_exists(graph.output(), tensor_name)) {
return;
}
auto& new_output = *(graph.add_output());
// copy the intermediate tensor's properties to the newly created
new_output.MergeFrom(find_tensor_descriptor(graph, tensor_name));

View File

@@ -445,6 +445,10 @@ void onnx_editor::ONNXModelEditor::extract_subgraph(const std::vector<InputEdge>
return;
}
if (!outputs.empty()) {
m_pimpl->m_model_proto->mutable_graph()->mutable_output()->Clear();
}
InferShapesAutoRelease onnx_shapes(m_pimpl->m_model_proto);
onnx_shapes.infer_shapes();

View File

@@ -0,0 +1,72 @@
ir_version: 3
producer_name: "nGraph ONNX Importer"
graph {
node {
input: "A"
input: "B"
output: "X"
op_type: "Add"
}
node {
input: "X"
input: "X"
output: "Y"
op_type: "Add"
}
name: "test_graph"
input {
name: "A"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 1
}
}
}
}
}
input {
name: "B"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 1
}
}
}
}
}
output {
name: "Y"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 1
}
}
}
}
}
output {
name: "Y"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 1
}
}
}
}
}
}
opset_import {
version: 13
}

View File

@@ -146,6 +146,22 @@ graph {
}
}
}
output {
name: "add2"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 2
}
dim {
dim_value: 2
}
}
}
}
}
output {
name: "mul2"
type {
@@ -168,22 +184,6 @@ graph {
}
}
}
output {
name: "add2"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 2
}
dim {
dim_value: 2
}
}
}
}
}
}
opset_import {
version: 13

View File

@@ -1867,6 +1867,42 @@ NGRAPH_TEST(onnx_editor, subgraph__cut_custom_edges_from_different_sources_and_m
EXPECT_TRUE(result.is_ok) << result.error_message;
}
NGRAPH_TEST(onnx_editor, subgraph__duplicated_output) {
ONNXModelEditor editor{ngraph::file_util::path_join(CommonTestUtils::getExecutableDirectory(),
SERIALIZED_ZOO,
"onnx/model_editor/add_ab_duplicated_output.onnx")};
const auto y_out_edge = editor.find_output_edge("Y");
editor.extract_subgraph({}, {{y_out_edge}});
const auto ref_model = ngraph::file_util::path_join(CommonTestUtils::getExecutableDirectory(),
SERIALIZED_ZOO,
"onnx/model_editor/add_ab.onnx");
const auto result = compare_onnx_models(editor.model_string(), ref_model);
EXPECT_TRUE(result.is_ok) << result.error_message;
}
NGRAPH_TEST(onnx_editor, subgraph__duplicated_output_2) {
ONNXModelEditor editor{ngraph::file_util::path_join(CommonTestUtils::getExecutableDirectory(),
SERIALIZED_ZOO,
"onnx/model_editor/add_ab_duplicated_output.onnx")};
const auto y_out_edge_1 = editor.find_output_edge("Y");
const auto y_out_edge_2 = editor.find_output_edge("Y");
editor.extract_subgraph({}, {{y_out_edge_1, y_out_edge_2}});
const auto ref_model = ngraph::file_util::path_join(CommonTestUtils::getExecutableDirectory(),
SERIALIZED_ZOO,
"onnx/model_editor/add_ab_duplicated_output.onnx");
// Model not changed
const auto result = compare_onnx_models(editor.model_string(), ref_model);
EXPECT_TRUE(result.is_ok) << result.error_message;
}
NGRAPH_TEST(onnx_editor, onnx_shape_infer_exception) {
ONNXModelEditor editor{ngraph::file_util::path_join(CommonTestUtils::getExecutableDirectory(),
SERIALIZED_ZOO,