Commit e8c0282c authored by Ewa Tusień's avatar Ewa Tusień Committed by Scott Cyphers

[ONNX] Update Min, Max, Mean and Sum ops to use v1 version of ops (set_1 namespace) (#4157)

* Changes version from v0 to default for Min and Max ops.

* Added broadcast option in variadic function.

* Added tests.

* Added test for opset8.

* Undo changes for Mean op.
Co-authored-by: 's avatarTomasz Dołbniak <tomasz.dolbniak@intel.com>
Co-authored-by: 's avatarScott Cyphers <diyessi@users.noreply.github.com>
parent f66fd5a9
......@@ -19,7 +19,6 @@
#include "core/node.hpp"
#include "default_opset.hpp"
#include "ngraph/node.hpp"
#include "ngraph/opsets/opset0.hpp"
#include "utils/variadic.hpp"
namespace ngraph
......@@ -32,7 +31,8 @@ namespace ngraph
{
inline NodeVector max(const Node& node)
{
return variadic::make_ng_variadic_op<ngraph::opset0::Maximum>(node);
return variadic::make_ng_variadic_op<default_opset::Maximum>(
node, ngraph::op::AutoBroadcastSpec::NONE);
}
} // namespace set_1
......
......@@ -19,7 +19,7 @@
#include "core/node.hpp"
#include "default_opset.hpp"
#include "ngraph/node.hpp"
#include "ngraph/opsets/opset0.hpp"
#include "ngraph/op/util/broadcasting.hpp"
#include "utils/variadic.hpp"
namespace ngraph
......@@ -32,7 +32,8 @@ namespace ngraph
{
inline NodeVector min(const Node& node)
{
return variadic::make_ng_variadic_op<ngraph::opset0::Minimum>(node);
return variadic::make_ng_variadic_op<default_opset::Minimum>(
node, ngraph::op::AutoBroadcastSpec::NONE);
}
} // namespace set_1
......
......@@ -19,6 +19,7 @@
#include "core/node.hpp"
#include "default_opset.hpp"
#include "ngraph/node.hpp"
#include "ngraph/op/util/broadcasting.hpp"
#include "ngraph/opsets/opset0.hpp"
#include "utils/variadic.hpp"
......@@ -32,7 +33,8 @@ namespace ngraph
{
inline NodeVector sum(const Node& node)
{
return variadic::make_ng_variadic_op<ngraph::opset0::Add>(node);
return variadic::make_ng_variadic_op<default_opset::Add>(
node, ngraph::op::AutoBroadcastSpec::NONE);
}
} // namespace set_1
......
......@@ -40,18 +40,22 @@ namespace ngraph
///
/// \return nGraph node equivalent of the ONNX operation
template <class T>
inline NodeVector make_ng_variadic_op(const Node& node)
inline NodeVector
make_ng_variadic_op(const Node& node,
const ngraph::op::AutoBroadcastSpec& auto_broadcast =
ngraph::op::AutoBroadcastSpec::NUMPY)
{
NodeVector ng_inputs{node.get_ng_inputs()};
const NodeVector ng_inputs{node.get_ng_inputs()};
// Templated binary operation - Creates Add, Minimum, Maximum, etc.
auto binary_operation = [](const std::shared_ptr<ngraph::Node>& arg0,
const auto binary_operation =
[&auto_broadcast](const std::shared_ptr<ngraph::Node>& arg0,
const std::shared_ptr<ngraph::Node>& arg1) {
return std::make_shared<T>(arg0, arg1);
return std::make_shared<T>(arg0, arg1, auto_broadcast);
};
// Create a result node as a series of binary operations
auto result = std::accumulate(
const auto result = std::accumulate(
std::next(std::begin(ng_inputs)), // First operand value - the second input
std::end(ng_inputs), // Last value - final input
ng_inputs.front(), // Initial value - first input
......
......@@ -29,7 +29,7 @@ graph {
elem_type: 1
shape {
dim {
dim_value: 3
dim_value: 1
}
}
}
......@@ -63,5 +63,5 @@ graph {
}
}
opset_import {
version: 7
version: 8
}
ir_version: 3
producer_name: "nGraph ONNX Importer"
graph {
node {
input: "data_0"
input: "data_1"
input: "data_2"
output: "result"
op_type: "Max"
}
name: "test_max_example"
input {
name: "data_0"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 3
}
}
}
}
}
input {
name: "data_1"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 3
}
}
}
}
}
input {
name: "data_2"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 3
}
}
}
}
}
output {
name: "result"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 3
}
}
}
}
}
}
opset_import {
version: 1
}
......@@ -16,7 +16,7 @@ graph {
elem_type: 1
shape {
dim {
dim_value: 3
dim_value: 1
}
}
}
......@@ -63,5 +63,5 @@ graph {
}
}
opset_import {
version: 7
version: 8
}
ir_version: 3
producer_name: "nGraph ONNX Importer"
graph {
node {
input: "data_0"
input: "data_1"
input: "data_2"
output: "result"
op_type: "Mean"
}
name: "test_mean_example"
input {
name: "data_0"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 3
}
}
}
}
}
input {
name: "data_1"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 3
}
}
}
}
}
input {
name: "data_2"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 3
}
}
}
}
}
output {
name: "result"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 3
}
}
}
}
}
}
opset_import {
version: 1
}
......@@ -15,7 +15,7 @@ graph {
elem_type: 1
shape {
dim {
dim_value: 3
dim_value: 1
}
}
}
......@@ -49,5 +49,5 @@ graph {
}
}
opset_import {
version: 7
version: 8
}
ir_version: 3
producer_name: "nGraph ONNX Importer"
graph {
node {
input: "data_0"
input: "data_1"
output: "result"
op_type: "Min"
}
name: "test_min_two_inputs"
input {
name: "data_0"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 3
}
}
}
}
}
input {
name: "data_1"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 3
}
}
}
}
}
output {
name: "result"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 3
}
}
}
}
}
}
opset_import {
version: 1
}
......@@ -16,7 +16,7 @@ graph {
elem_type: 1
shape {
dim {
dim_value: 3
dim_value: 1
}
}
}
......@@ -63,5 +63,5 @@ graph {
}
}
opset_import {
version: 7
version: 8
}
ir_version: 3
producer_name: "nGraph ONNX Importer"
graph {
node {
input: "data_0"
input: "data_1"
input: "data_2"
output: "result"
op_type: "Sum"
}
name: "test_sum_example"
input {
name: "data_0"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 3
}
}
}
}
}
input {
name: "data_1"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 3
}
}
}
}
}
input {
name: "data_2"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 3
}
}
}
}
}
output {
name: "result"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 3
}
}
}
}
}
}
opset_import {
version: 1
}
......@@ -411,17 +411,31 @@ NGRAPH_TEST(onnx_${BACKEND_NAME}, model_relu)
EXPECT_TRUE(test::all_close_f(expected_outputs.front(), outputs.front()));
}
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_sum_opset1)
{
// Simple Sum test for opset1.
auto function = onnx_import::import_onnx_model(
file_util::path_join(SERIALIZED_ZOO, "onnx/sum_opset1.prototxt"));
auto test_case = ngraph::test::NgraphTestCase(function, "${BACKEND_NAME}");
test_case.add_input<float>({3.f, 0.f, 2.f});
test_case.add_input<float>({1.f, 3.f, 4.f});
test_case.add_input<float>({2.f, 6.f, 6.f});
test_case.add_expected_output<float>(Shape{3}, {6.f, 9.f, 12.f});
test_case.run();
}
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_sum)
{
// Simple Sum test
// Simple Sum test for opset8.
auto function =
onnx_import::import_onnx_model(file_util::path_join(SERIALIZED_ZOO, "onnx/sum.prototxt"));
auto test_case = ngraph::test::NgraphTestCase(function, "${BACKEND_NAME}");
test_case.add_input<float>({3.f, 0.f, 2.f});
test_case.add_input<float>({3.f});
test_case.add_input<float>({1.f, 3.f, 4.f});
test_case.add_input<float>({2.f, 6.f, 6.f});
test_case.add_expected_output<float>(Shape{3}, {6.f, 9.f, 12.f});
test_case.add_expected_output<float>(Shape{3}, {6.f, 12.f, 13.f});
test_case.run();
}
......@@ -486,10 +500,10 @@ NGRAPH_TEST(onnx_${BACKEND_NAME}, model_cum_sum_3d_exclusive_reverse)
test_case.run();
}
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_min_two_inputs)
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_min_two_inputs_opset1)
{
auto function = onnx_import::import_onnx_model(
file_util::path_join(SERIALIZED_ZOO, "onnx/min_two_inputs.prototxt"));
file_util::path_join(SERIALIZED_ZOO, "onnx/min_two_inputs_opset1.prototxt"));
// input data shape (3, )
Inputs inputs;
......@@ -501,6 +515,37 @@ NGRAPH_TEST(onnx_${BACKEND_NAME}, model_min_two_inputs)
EXPECT_TRUE(test::all_close_f(expected_outputs.front(), outputs.front()));
}
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_min_two_inputs)
{
auto function = onnx_import::import_onnx_model(
file_util::path_join(SERIALIZED_ZOO, "onnx/min_two_inputs.prototxt"));
// input data shape (3, )
Inputs inputs;
inputs.emplace_back(std::vector<float>{2.f});
inputs.emplace_back(std::vector<float>{1.f, 4.f, 4.f});
Outputs expected_outputs{{1.f, 2.f, 2.f}};
Outputs outputs{execute(function, inputs, "${BACKEND_NAME}")};
EXPECT_TRUE(test::all_close_f(expected_outputs.front(), outputs.front()));
}
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_max_opset1)
{
auto function = onnx_import::import_onnx_model(
file_util::path_join(SERIALIZED_ZOO, "onnx/max_opset1.prototxt"));
// input data shape (3, )
Inputs inputs;
inputs.emplace_back(std::vector<float>{3.f, 2.f, 1.f});
inputs.emplace_back(std::vector<float>{1.f, 4.f, 4.f});
inputs.emplace_back(std::vector<float>{2.f, 5.f, 3.f});
Outputs expected_outputs{{3.f, 5.f, 4.f}};
Outputs outputs{execute(function, inputs, "${BACKEND_NAME}")};
EXPECT_TRUE(test::all_close_f(expected_outputs.front(), outputs.front()));
}
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_max)
{
auto function =
......@@ -508,8 +553,9 @@ NGRAPH_TEST(onnx_${BACKEND_NAME}, model_max)
// input data shape (3, )
Inputs inputs;
inputs.emplace_back(std::vector<float>{3.f, 2.f, 1.f});
inputs.emplace_back(std::vector<float>{1.f, 4.f, 4.f});
inputs.emplace_back(std::vector<float>{3.f});
inputs.emplace_back(std::vector<float>{2.f, 5.f, 3.f});
Outputs expected_outputs{{3.f, 5.f, 4.f}};
......@@ -517,10 +563,10 @@ NGRAPH_TEST(onnx_${BACKEND_NAME}, model_max)
EXPECT_TRUE(test::all_close_f(expected_outputs.front(), outputs.front()));
}
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_mean)
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_mean_opset1)
{
auto function =
onnx_import::import_onnx_model(file_util::path_join(SERIALIZED_ZOO, "onnx/mean.prototxt"));
auto function = onnx_import::import_onnx_model(
file_util::path_join(SERIALIZED_ZOO, "onnx/mean_opset1.prototxt"));
// input data shape (3, )
Inputs inputs;
......@@ -533,6 +579,22 @@ NGRAPH_TEST(onnx_${BACKEND_NAME}, model_mean)
EXPECT_TRUE(test::all_close_f(expected_outputs.front(), outputs.front()));
}
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_mean)
{
auto function =
onnx_import::import_onnx_model(file_util::path_join(SERIALIZED_ZOO, "onnx/mean.prototxt"));
// input data shape (3, )
Inputs inputs;
inputs.emplace_back(std::vector<float>{3.f});
inputs.emplace_back(std::vector<float>{1.f, 2.f, 5.f});
inputs.emplace_back(std::vector<float>{2.f, 7.f, 7.f});
Outputs expected_outputs{{2.f, 4.f, 5.f}};
Outputs outputs{execute(function, inputs, "${BACKEND_NAME}")};
EXPECT_TRUE(test::all_close_f(expected_outputs.front(), outputs.front()));
}
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_gemm_abc)
{
auto function = onnx_import::import_onnx_model(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment