Commit 3fd2743f authored by Artur Wojcik's avatar Artur Wojcik Committed by Scott Cyphers

[ONNX] Fix build for CentOS 7 (#1449)

* onnx: fix build for CentOS 7.4
Signed-off-by: 's avatarArtur Wojcik <artur.wojcik@intel.com>

* onnx: fix build for CentOS 7.4
Signed-off-by: 's avatarArtur Wojcik <artur.wojcik@intel.com>

* onnx: compile fix for CentOS 7
Signed-off-by: 's avatarArtur Wojcik <artur.wojcik@intel.com>

* onnx: unit-test compilation fixes for CentOS 7
Signed-off-by: 's avatarArtur Wojcik <artur.wojcik@intel.com>

* onnx: unit-test compilation fixes for CentOS 7
Signed-off-by: 's avatarArtur Wojcik <artur.wojcik@intel.com>

* onnx: fix 'add abc initializer' unit test
Signed-off-by: 's avatarArtur Wojcik <artur.wojcik@intel.com>

* onnx: fix code style
Signed-off-by: 's avatarArtur Wojcik <artur.wojcik@intel.com>
parent 822aa81d
...@@ -50,7 +50,7 @@ namespace ngraph ...@@ -50,7 +50,7 @@ namespace ngraph
{ {
m_nodes.emplace_back(node_proto, this); m_nodes.emplace_back(node_proto, this);
const Node& node{m_nodes.back()}; const Node& node{m_nodes.back()};
const auto& ng_nodes{node.get_ng_nodes()}; NodeVector ng_nodes{node.get_ng_nodes()};
for (int i = 0; i < ng_nodes.size(); i++) for (int i = 0; i < ng_nodes.size(); i++)
{ {
m_ng_node_cache[node.output(i)] = ng_nodes[i]; m_ng_node_cache[node.output(i)] = ng_nodes[i];
......
...@@ -82,10 +82,10 @@ namespace ngraph ...@@ -82,10 +82,10 @@ namespace ngraph
template <typename T> template <typename T>
T get_attribute_value(const std::string& name, T default_value) const T get_attribute_value(const std::string& name, T default_value) const
{ {
auto it{std::find_if( auto it = std::find_if(
std::begin(m_attributes), std::begin(m_attributes),
std::end(m_attributes), std::end(m_attributes),
[&](const Attribute& attribute) { return attribute.get_name() == name; })}; [&](const Attribute& attribute) { return attribute.get_name() == name; });
if (it == std::end(m_attributes)) if (it == std::end(m_attributes))
{ {
return default_value; return default_value;
...@@ -96,10 +96,10 @@ namespace ngraph ...@@ -96,10 +96,10 @@ namespace ngraph
template <typename T> template <typename T>
T get_attribute_value(const std::string& name) const T get_attribute_value(const std::string& name) const
{ {
auto it{std::find_if( auto it = std::find_if(
std::begin(m_attributes), std::begin(m_attributes),
std::end(m_attributes), std::end(m_attributes),
[&](const Attribute& attribute) { return attribute.get_name() == name; })}; [&](const Attribute& attribute) { return attribute.get_name() == name; });
if (it == std::end(m_attributes)) if (it == std::end(m_attributes))
{ {
throw error::node::UnknownAttribute{get_name(), name}; throw error::node::UnknownAttribute{get_name(), name};
......
...@@ -39,7 +39,7 @@ namespace ngraph ...@@ -39,7 +39,7 @@ namespace ngraph
struct unspecified_element_type : ngraph_error struct unspecified_element_type : ngraph_error
{ {
unspecified_element_type() unspecified_element_type()
: ngraph_error{"value info has not element type specified"} : ngraph_error{"value info has no element type specified"}
{ {
} }
}; };
...@@ -108,7 +108,7 @@ namespace ngraph ...@@ -108,7 +108,7 @@ namespace ngraph
get_ng_node(op::ParameterVector& parameters, get_ng_node(op::ParameterVector& parameters,
const std::map<std::string, Tensor>& initializers) const const std::map<std::string, Tensor>& initializers) const
{ {
const auto it{initializers.find(get_name())}; const auto it = initializers.find(get_name());
if (it != std::end(initializers)) if (it != std::end(initializers))
{ {
return get_ng_constant(it->second); return get_ng_constant(it->second);
......
...@@ -26,136 +26,123 @@ ...@@ -26,136 +26,123 @@
using namespace ngraph; using namespace ngraph;
using Inputs = std::vector<std::vector<float>>;
using Outputs = std::vector<std::vector<float>>;
using Model = std::vector<std::shared_ptr<Function>>;
TEST(onnx, model_add_abc) TEST(onnx, model_add_abc)
{ {
auto model{ngraph::onnx_import::load_onnx_model( auto function = onnx_import::import_onnx_function(
ngraph::file_util::path_join(SERIALIZED_ZOO, "onnx/add_abc.onnx"))}; file_util::path_join(SERIALIZED_ZOO, "onnx/add_abc.onnx"));
auto backend{ngraph::runtime::Backend::create("INTERPRETER")};
Inputs inputs{{1}, {2}, {3}};
ngraph::Shape shape{1}; Outputs expected_outputs{{6}};
auto a{backend->create_tensor(ngraph::element::f32, shape)};
copy_data(a, std::vector<float>{1}); Outputs outputs{execute(function, inputs, "INTERPRETER")};
auto b{backend->create_tensor(ngraph::element::f32, shape)}; EXPECT_TRUE(test::all_close_f(expected_outputs.front(), outputs.front()));
copy_data(b, std::vector<float>{2});
auto c{backend->create_tensor(ngraph::element::f32, shape)};
copy_data(c, std::vector<float>{3});
auto r{backend->create_tensor(ngraph::element::f32, shape)};
backend->call(model.front(), {r}, {a, b, c});
EXPECT_TRUE(test::all_close_f((std::vector<float>{6}), read_vector<float>(r)));
} }
TEST(onnx, model_add_abc_initializers) TEST(onnx, model_add_abc_initializers)
{ {
auto model{ngraph::onnx_import::load_onnx_model( auto function = onnx_import::import_onnx_function(
ngraph::file_util::path_join(SERIALIZED_ZOO, "onnx/add_abc_initializers.onnx"))}; file_util::path_join(SERIALIZED_ZOO, "onnx/add_abc_initializers.onnx"));
auto backend{ngraph::runtime::Backend::create("INTERPRETER")};
ngraph::Shape shape{2, 2};
auto c{backend->create_tensor(ngraph::element::f32, shape)};
copy_data(c, std::vector<float>{1, 2, 3, 4});
auto r{backend->create_tensor(ngraph::element::f32, shape)}; Inputs inputs{{1, 2, 3, 4}};
Outputs expected_outputs{{3, 6, 9, 12}};
backend->call(model.front(), {r}, {c}); Outputs outputs{execute(function, inputs, "INTERPRETER")};
EXPECT_TRUE(test::all_close_f((std::vector<float>{3, 6, 9, 12}), read_vector<float>(r))); EXPECT_TRUE(test::all_close_f(expected_outputs.front(), outputs.front()));
} }
TEST(onnx, model_split_equal_parts_default) TEST(onnx, model_split_equal_parts_default)
{ {
auto model{ngraph::onnx_import::load_onnx_model( Model model{onnx_import::load_onnx_model(
ngraph::file_util::path_join(SERIALIZED_ZOO, "onnx/split_equal_parts_default.onnx"))}; file_util::path_join(SERIALIZED_ZOO, "onnx/split_equal_parts_default.onnx"))};
auto args = std::vector<std::vector<float>>{{1, 2, 3, 4, 5, 6}}; Inputs inputs{{1, 2, 3, 4, 5, 6}};
auto expected_output = std::vector<std::vector<float>>{{1, 2}, {3, 4}, {5, 6}}; Outputs expected_outputs{{1, 2}, {3, 4}, {5, 6}};
for (std::size_t i = 0; i < expected_output.size(); ++i) for (std::size_t i = 0; i < expected_outputs.size(); ++i)
{ {
auto result_vectors = execute(model[i], args, "INTERPRETER"); Outputs outputs{execute(model[i], inputs, "INTERPRETER")};
EXPECT_EQ(result_vectors.size(), 1); EXPECT_EQ(outputs.size(), 1);
EXPECT_TRUE(test::all_close_f(expected_output[i], result_vectors.front())); EXPECT_TRUE(test::all_close_f(expected_outputs[i], outputs.front()));
} }
} }
TEST(onnx, model_split_equal_parts_2d) TEST(onnx, model_split_equal_parts_2d)
{ {
// Split into 2 equal parts along axis=1 // Split into 2 equal parts along axis=1
auto model{ngraph::onnx_import::load_onnx_model( Model model{onnx_import::load_onnx_model(
ngraph::file_util::path_join(SERIALIZED_ZOO, "onnx/split_equal_parts_2d.onnx"))}; file_util::path_join(SERIALIZED_ZOO, "onnx/split_equal_parts_2d.onnx"))};
auto args = std::vector<std::vector<float>>{{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}}; Inputs inputs{{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}};
// each output we get as a flattened vector Outputs expected_outputs{{0, 1, 2, 6, 7, 8}, {3, 4, 5, 9, 10, 11}};
auto expected_output =
std::vector<std::vector<float>>{{0, 1, 2, 6, 7, 8}, {3, 4, 5, 9, 10, 11}};
for (std::size_t i = 0; i < expected_output.size(); ++i) for (std::size_t i = 0; i < expected_outputs.size(); ++i)
{ {
auto result_vectors = execute(model[i], args, "INTERPRETER"); Outputs outputs{execute(model[i], inputs, "INTERPRETER")};
EXPECT_EQ(result_vectors.size(), 1); EXPECT_EQ(outputs.size(), 1);
EXPECT_TRUE(test::all_close_f(expected_output[i], result_vectors[0])); EXPECT_TRUE(test::all_close_f(expected_outputs[i], outputs.front()));
} }
} }
TEST(onnx, model_split_variable_parts_2d) TEST(onnx, model_split_variable_parts_2d)
{ {
// Split into variable parts {2, 4} along axis=1 // Split into variable parts {2, 4} along axis=1
auto model{ngraph::onnx_import::load_onnx_model( Model model{onnx_import::load_onnx_model(
ngraph::file_util::path_join(SERIALIZED_ZOO, "onnx/split_variable_parts_2d.onnx"))}; file_util::path_join(SERIALIZED_ZOO, "onnx/split_variable_parts_2d.onnx"))};
auto args = std::vector<std::vector<float>>{{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}}; Inputs inputs{{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}};
// each output we get as a flattened vector Outputs expected_outputs{{0, 1, 6, 7}, {2, 3, 4, 5, 8, 9, 10, 11}};
auto expected_output =
std::vector<std::vector<float>>{{0, 1, 6, 7}, {2, 3, 4, 5, 8, 9, 10, 11}};
for (std::size_t i = 0; i < expected_output.size(); ++i) for (std::size_t i = 0; i < expected_outputs.size(); ++i)
{ {
auto result_vectors = execute(model[i], args, "INTERPRETER"); Outputs outputs{execute(model[i], inputs, "INTERPRETER")};
EXPECT_EQ(result_vectors.size(), 1); EXPECT_EQ(outputs.size(), 1);
EXPECT_TRUE(test::all_close_f(expected_output[i], result_vectors[0])); EXPECT_TRUE(test::all_close_f(expected_outputs[i], outputs.front()));
} }
} }
TEST(onnx, model_batchnorm_default) TEST(onnx, model_batchnorm_default)
{ {
// Batch Normalization with default parameters // Batch Normalization with default parameters
auto function{ngraph::onnx_import::import_onnx_function( Model model{onnx_import::import_onnx_function(
ngraph::file_util::path_join(SERIALIZED_ZOO, "onnx/batchnorm_default.onnx"))}; file_util::path_join(SERIALIZED_ZOO, "onnx/batchnorm_default.onnx"))};
std::vector<std::vector<float>> inputs; Inputs inputs;
// input data shape (1, 2, 1, 3) // input data shape (1, 2, 1, 3)
inputs.emplace_back( inputs.push_back(
ngraph::test::NDArray<float, 4>({{{{-1., 0., 1.}}, {{2., 3., 4.}}}}).get_vector()); test::NDArray<float, 4>({{{{-1.f, 0.f, 1.f}}, {{2.f, 3.f, 4.f}}}}).get_vector());
// scale (3) // scale (3)
inputs.emplace_back(std::vector<float>{1., 1.5}); inputs.emplace_back(std::vector<float>{1.f, 1.5f});
// bias (3) // bias (3)
inputs.emplace_back(std::vector<float>{0., 1.}); inputs.emplace_back(std::vector<float>{0.f, 1.f});
// mean (3) // mean (3)
inputs.emplace_back(std::vector<float>{0., 3}); inputs.emplace_back(std::vector<float>{0.f, 3.f});
// var (3) // var (3)
inputs.emplace_back(std::vector<float>{1., 1.5}); inputs.emplace_back(std::vector<float>{1.f, 1.5f});
// shape (1, 2, 1, 3) // shape (1, 2, 1, 3)
auto expected_output = ngraph::test::NDArray<float, 4>({{{{-0.999995f, 0.f, 0.999995f}}, Outputs expected_outputs{test::NDArray<float, 4>{
{{-0.22474074f, 1.f, 2.2247407f}}}}) {{{{-0.999995f, 0.f, 0.999995f}}, {{-0.22474074f, 1.f, 2.2247407f}}}}}
.get_vector(); .get_vector()};
auto result_vectors = execute(function, inputs, "INTERPRETER"); Outputs outputs{execute(model.front(), inputs, "INTERPRETER")};
EXPECT_TRUE(test::all_close_f(expected_output, result_vectors.front())); EXPECT_TRUE(test::all_close_f(expected_outputs.front(), outputs.front()));
} }
TEST(onnx, model_relu) TEST(onnx, model_relu)
{ {
// Simple ReLU test // Simple ReLU test
auto function{ngraph::onnx_import::import_onnx_function( auto function = ngraph::onnx_import::import_onnx_function(
ngraph::file_util::path_join(SERIALIZED_ZOO, "onnx/relu.onnx"))}; ngraph::file_util::path_join(SERIALIZED_ZOO, "onnx/relu.onnx"));
auto inputs = std::vector<std::vector<float>>{{-1, -2, 0, 1, 2, 3}}; Inputs inputs{{-1, -2, 0, 1, 2, 3}};
auto expected_output = std::vector<std::vector<float>>{{0, 0, 0, 1, 2, 3}}; Outputs expected_outputs{{0, 0, 0, 1, 2, 3}};
auto result_vectors = execute(function, inputs, "INTERPRETER"); Outputs outputs{execute(function, inputs, "INTERPRETER")};
EXPECT_TRUE(test::all_close_f(expected_output.front(), result_vectors.front())); EXPECT_TRUE(test::all_close_f(expected_outputs.front(), outputs.front()));
} }
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