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
{
m_nodes.emplace_back(node_proto, this);
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++)
{
m_ng_node_cache[node.output(i)] = ng_nodes[i];
......
......@@ -82,10 +82,10 @@ namespace ngraph
template <typename T>
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::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))
{
return default_value;
......@@ -96,10 +96,10 @@ namespace ngraph
template <typename T>
T get_attribute_value(const std::string& name) const
{
auto it{std::find_if(
auto it = std::find_if(
std::begin(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))
{
throw error::node::UnknownAttribute{get_name(), name};
......
......@@ -39,7 +39,7 @@ namespace ngraph
struct unspecified_element_type : ngraph_error
{
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
get_ng_node(op::ParameterVector& parameters,
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))
{
return get_ng_constant(it->second);
......
......@@ -26,136 +26,123 @@
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)
{
auto model{ngraph::onnx_import::load_onnx_model(
ngraph::file_util::path_join(SERIALIZED_ZOO, "onnx/add_abc.onnx"))};
auto backend{ngraph::runtime::Backend::create("INTERPRETER")};
ngraph::Shape shape{1};
auto a{backend->create_tensor(ngraph::element::f32, shape)};
copy_data(a, std::vector<float>{1});
auto b{backend->create_tensor(ngraph::element::f32, shape)};
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)));
auto function = onnx_import::import_onnx_function(
file_util::path_join(SERIALIZED_ZOO, "onnx/add_abc.onnx"));
Inputs inputs{{1}, {2}, {3}};
Outputs expected_outputs{{6}};
Outputs outputs{execute(function, inputs, "INTERPRETER")};
EXPECT_TRUE(test::all_close_f(expected_outputs.front(), outputs.front()));
}
TEST(onnx, model_add_abc_initializers)
{
auto model{ngraph::onnx_import::load_onnx_model(
ngraph::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 function = onnx_import::import_onnx_function(
file_util::path_join(SERIALIZED_ZOO, "onnx/add_abc_initializers.onnx"));
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});
EXPECT_TRUE(test::all_close_f((std::vector<float>{3, 6, 9, 12}), read_vector<float>(r)));
Outputs outputs{execute(function, inputs, "INTERPRETER")};
EXPECT_TRUE(test::all_close_f(expected_outputs.front(), outputs.front()));
}
TEST(onnx, model_split_equal_parts_default)
{
auto model{ngraph::onnx_import::load_onnx_model(
ngraph::file_util::path_join(SERIALIZED_ZOO, "onnx/split_equal_parts_default.onnx"))};
Model model{onnx_import::load_onnx_model(
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}};
auto expected_output = std::vector<std::vector<float>>{{1, 2}, {3, 4}, {5, 6}};
Inputs inputs{{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");
EXPECT_EQ(result_vectors.size(), 1);
EXPECT_TRUE(test::all_close_f(expected_output[i], result_vectors.front()));
Outputs outputs{execute(model[i], inputs, "INTERPRETER")};
EXPECT_EQ(outputs.size(), 1);
EXPECT_TRUE(test::all_close_f(expected_outputs[i], outputs.front()));
}
}
TEST(onnx, model_split_equal_parts_2d)
{
// Split into 2 equal parts along axis=1
auto model{ngraph::onnx_import::load_onnx_model(
ngraph::file_util::path_join(SERIALIZED_ZOO, "onnx/split_equal_parts_2d.onnx"))};
Model model{onnx_import::load_onnx_model(
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}};
// each output we get as a flattened vector
auto expected_output =
std::vector<std::vector<float>>{{0, 1, 2, 6, 7, 8}, {3, 4, 5, 9, 10, 11}};
Inputs inputs{{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}};
Outputs expected_outputs{{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");
EXPECT_EQ(result_vectors.size(), 1);
EXPECT_TRUE(test::all_close_f(expected_output[i], result_vectors[0]));
Outputs outputs{execute(model[i], inputs, "INTERPRETER")};
EXPECT_EQ(outputs.size(), 1);
EXPECT_TRUE(test::all_close_f(expected_outputs[i], outputs.front()));
}
}
TEST(onnx, model_split_variable_parts_2d)
{
// Split into variable parts {2, 4} along axis=1
auto model{ngraph::onnx_import::load_onnx_model(
ngraph::file_util::path_join(SERIALIZED_ZOO, "onnx/split_variable_parts_2d.onnx"))};
Model model{onnx_import::load_onnx_model(
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}};
// each output we get as a flattened vector
auto expected_output =
std::vector<std::vector<float>>{{0, 1, 6, 7}, {2, 3, 4, 5, 8, 9, 10, 11}};
Inputs inputs{{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}};
Outputs expected_outputs{{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");
EXPECT_EQ(result_vectors.size(), 1);
EXPECT_TRUE(test::all_close_f(expected_output[i], result_vectors[0]));
Outputs outputs{execute(model[i], inputs, "INTERPRETER")};
EXPECT_EQ(outputs.size(), 1);
EXPECT_TRUE(test::all_close_f(expected_outputs[i], outputs.front()));
}
}
TEST(onnx, model_batchnorm_default)
{
// Batch Normalization with default parameters
auto function{ngraph::onnx_import::import_onnx_function(
ngraph::file_util::path_join(SERIALIZED_ZOO, "onnx/batchnorm_default.onnx"))};
Model model{onnx_import::import_onnx_function(
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)
inputs.emplace_back(
ngraph::test::NDArray<float, 4>({{{{-1., 0., 1.}}, {{2., 3., 4.}}}}).get_vector());
inputs.push_back(
test::NDArray<float, 4>({{{{-1.f, 0.f, 1.f}}, {{2.f, 3.f, 4.f}}}}).get_vector());
// scale (3)
inputs.emplace_back(std::vector<float>{1., 1.5});
inputs.emplace_back(std::vector<float>{1.f, 1.5f});
// bias (3)
inputs.emplace_back(std::vector<float>{0., 1.});
inputs.emplace_back(std::vector<float>{0.f, 1.f});
// mean (3)
inputs.emplace_back(std::vector<float>{0., 3});
inputs.emplace_back(std::vector<float>{0.f, 3.f});
// 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)
auto expected_output = ngraph::test::NDArray<float, 4>({{{{-0.999995f, 0.f, 0.999995f}},
{{-0.22474074f, 1.f, 2.2247407f}}}})
.get_vector();
Outputs expected_outputs{test::NDArray<float, 4>{
{{{{-0.999995f, 0.f, 0.999995f}}, {{-0.22474074f, 1.f, 2.2247407f}}}}}
.get_vector()};
auto result_vectors = execute(function, inputs, "INTERPRETER");
EXPECT_TRUE(test::all_close_f(expected_output, result_vectors.front()));
Outputs outputs{execute(model.front(), inputs, "INTERPRETER")};
EXPECT_TRUE(test::all_close_f(expected_outputs.front(), outputs.front()));
}
TEST(onnx, model_relu)
{
// Simple ReLU test
auto function{ngraph::onnx_import::import_onnx_function(
ngraph::file_util::path_join(SERIALIZED_ZOO, "onnx/relu.onnx"))};
auto function = ngraph::onnx_import::import_onnx_function(
ngraph::file_util::path_join(SERIALIZED_ZOO, "onnx/relu.onnx"));
auto inputs = std::vector<std::vector<float>>{{-1, -2, 0, 1, 2, 3}};
auto expected_output = std::vector<std::vector<float>>{{0, 0, 0, 1, 2, 3}};
Inputs inputs{{-1, -2, 0, 1, 2, 3}};
Outputs expected_outputs{{0, 0, 0, 1, 2, 3}};
auto result_vectors = execute(function, inputs, "INTERPRETER");
EXPECT_TRUE(test::all_close_f(expected_output.front(), result_vectors.front()));
Outputs outputs{execute(function, inputs, "INTERPRETER")};
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