[ONNX Importer] Onnx importer support for brocker names (#4087)

* ONNX Importer changes

* Unit tests

* Style

* Don't set tensor names for result output tensor

* Review fx I

* fix

* Style

Co-authored-by: Vladislav Volkov <vladislav.volkov@intel.com>
This commit is contained in:
Tomasz Socha
2021-02-04 04:53:27 +01:00
committed by GitHub
parent ff4227cf53
commit 4563101c5a
6 changed files with 163 additions and 4 deletions

View File

@@ -20,6 +20,7 @@
#include <sstream>
#include "core/graph.hpp"
#include "core/null_node.hpp"
#include "exceptions.hpp"
#include "ngraph/log.hpp"
#include "ngraph/node.hpp"
@@ -272,7 +273,21 @@ namespace ngraph
break;
}
ng_node_vector[i].get_node()->set_friendly_name(onnx_node.output(i));
auto onnx_node_name = onnx_node.get_name();
if (onnx_node_name.empty())
{
ng_node_vector[i].get_node()->set_friendly_name(onnx_node.output(i));
}
else
{
ng_node_vector[i].get_node()->set_friendly_name(onnx_node.get_name());
}
// null node does not have tensor
if (!ngraph::op::is_null(ng_node_vector[i]))
{
ng_node_vector[i].get_tensor().set_names({onnx_node.output(i)});
}
}
}

View File

@@ -104,6 +104,7 @@ namespace ngraph
auto parameter =
std::make_shared<ngraph::op::Parameter>(get_element_type(), get_shape());
parameter->set_friendly_name(get_name());
parameter->get_output_tensor(0).set_names({get_name()});
return parameter;
}

View File

@@ -367,7 +367,8 @@ if (NGRAPH_ONNX_IMPORT_ENABLE AND NOT NGRAPH_USE_PROTOBUF_LITE)
list(APPEND SRC
onnx/onnx_import_exceptions.cpp
onnx/onnx_import_library.cpp
onnx/onnx_editor.cpp)
onnx/onnx_editor.cpp
onnx/onnx_tensor_names.cpp)
endif()
foreach(BACKEND_NAME ${ACTIVE_BACKEND_LIST})

View File

@@ -0,0 +1,58 @@
ir_version: 7
producer_name: "test_model"
graph {
node {
input: "input"
output: "relu_t"
op_type: "Relu"
name: "relu"
}
node {
input: "relu_t"
output: "final_output"
name: "ident"
op_type: "Identity"
}
name: "test_model"
input {
name: "input"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 1
}
dim {
dim_value: 50
}
dim {
dim_value: 50
}
}
}
}
}
output {
name: "final_output"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 1
}
dim {
dim_value: 50
}
dim {
dim_value: 50
}
}
}
}
}
}
opset_import {
version: 13
}

View File

@@ -116,8 +116,12 @@ NGRAPH_TEST(${BACKEND_NAME}, onnx_node_names_check)
[](std::shared_ptr<Node> op) { return std::string(op->get_type_name()) == "Add"; });
EXPECT_EQ(additions.size(), 2);
EXPECT_EQ(additions.at(0)->get_friendly_name(), "X");
EXPECT_EQ(additions.at(1)->get_friendly_name(), "Y");
EXPECT_EQ(additions.at(0)->get_friendly_name(), "add_node1");
EXPECT_EQ(additions.at(0)->get_output_tensor(0).get_names(),
std::unordered_set<std::string>{"X"});
EXPECT_EQ(additions.at(1)->get_friendly_name(), "add_node2");
EXPECT_EQ(additions.at(1)->get_output_tensor(0).get_names(),
std::unordered_set<std::string>{"Y"});
}
NGRAPH_TEST(${BACKEND_NAME}, onnx_model_add_abc)

View File

@@ -0,0 +1,80 @@
//*****************************************************************************
// Copyright 2017-2021 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//*****************************************************************************
#include "gtest/gtest.h"
#include "ngraph/ngraph.hpp"
#include "onnx_import/onnx.hpp"
#include "onnx_import/onnx_utils.hpp"
#include "util/test_case.hpp"
#include "util/test_control.hpp"
NGRAPH_SUPPRESS_DEPRECATED_START
using namespace ngraph;
static std::string s_manifest = "${MANIFEST}";
using Inputs = std::vector<std::vector<float>>;
using Outputs = std::vector<std::vector<float>>;
NGRAPH_TEST(onnx_tensor_names, simple_model)
{
auto function = onnx_import::import_onnx_model(
file_util::path_join(SERIALIZED_ZOO, "onnx/tensor_names.prototxt"));
auto ops = function->get_ordered_ops();
ASSERT_EQ(ops[0]->get_friendly_name(), "input");
ASSERT_EQ(ops[0]->get_output_tensor(0).get_names(), std::unordered_set<std::string>{"input"});
ASSERT_EQ(ops[1]->get_friendly_name(), "relu");
ASSERT_EQ(ops[1]->get_output_tensor(0).get_names(), std::unordered_set<std::string>{"relu_t"});
// ops[2] is a constant created in the ONNX importer as part of Identity operator
ASSERT_EQ(ops[3]->get_friendly_name(), "ident");
ASSERT_EQ(ops[3]->get_output_tensor(0).get_names(),
std::unordered_set<std::string>{"final_output"});
ASSERT_EQ(ops[4]->get_friendly_name(), "final_output");
ASSERT_EQ(function->get_result()->get_input_tensor(0).get_names(),
std::unordered_set<std::string>{"final_output"});
ASSERT_EQ(function->get_result()->input_value(0).get_tensor().get_names(),
std::unordered_set<std::string>{"final_output"});
}
NGRAPH_TEST(onnx_tensor_names, node_multiple_outputs)
{
auto function =
onnx_import::import_onnx_model(file_util::path_join(SERIALIZED_ZOO, "onnx/top_k.prototxt"));
auto ops = function->get_ordered_ops();
ASSERT_EQ(ops[0]->get_friendly_name(), "x");
ASSERT_EQ(ops[0]->get_output_tensor(0).get_names(), std::unordered_set<std::string>{"x"});
// ops[1] is a constant created in the ONNX importer as part of TopK operator(K value)
ASSERT_EQ(ops[2]->get_friendly_name(), "indices");
ASSERT_EQ(ops[2]->get_output_tensor(0).get_names(), std::unordered_set<std::string>{"values"});
ASSERT_EQ(ops[2]->get_output_tensor(1).get_names(), std::unordered_set<std::string>{"indices"});
// result nodes are generated in different order than function results.
ASSERT_EQ(ops[3]->get_friendly_name(), "indices");
ASSERT_EQ(ops[4]->get_friendly_name(), "values");
ASSERT_EQ(function->get_results()[0]->get_input_tensor(0).get_names(),
std::unordered_set<std::string>{"values"});
ASSERT_EQ(function->get_results()[1]->get_input_tensor(0).get_names(),
std::unordered_set<std::string>{"indices"});
ASSERT_EQ(function->get_results()[0]->input_value(0).get_tensor().get_names(),
std::unordered_set<std::string>{"values"});
ASSERT_EQ(function->get_results()[1]->input_value(0).get_tensor().get_names(),
std::unordered_set<std::string>{"indices"});
}