diff --git a/docs/template_plugin/src/template_executable_network.cpp b/docs/template_plugin/src/template_executable_network.cpp index 84feb424927..9819d76ba7c 100644 --- a/docs/template_plugin/src/template_executable_network.cpp +++ b/docs/template_plugin/src/template_executable_network.cpp @@ -13,10 +13,10 @@ #include "ie_icore.hpp" #include "ie_ngraph_utils.hpp" #include "openvino/core/except.hpp" +#include "openvino/pass/serialize.hpp" #include "template/template_config.hpp" #include "template_itt.hpp" #include "template_plugin.hpp" -#include "transformations/serialize.hpp" #include "transformations/utils/utils.hpp" using namespace TemplatePlugin; @@ -205,7 +205,7 @@ void TemplatePlugin::ExecutableNetwork::Export(std::ostream& modelStream) { // Note: custom ngraph extensions are not supported std::map custom_opsets; std::stringstream xmlFile, binFile; - ngraph::pass::Serialize serializer(xmlFile, binFile, custom_opsets); + ov::pass::Serialize serializer(xmlFile, binFile, custom_opsets); serializer.run_on_function(_function); auto m_constants = binFile.str(); diff --git a/inference-engine/src/hetero_plugin/hetero_executable_network.cpp b/inference-engine/src/hetero_plugin/hetero_executable_network.cpp index 85a932f2ca0..c7b171f65cc 100644 --- a/inference-engine/src/hetero_plugin/hetero_executable_network.cpp +++ b/inference-engine/src/hetero_plugin/hetero_executable_network.cpp @@ -29,7 +29,7 @@ #include #include -#include "transformations/serialize.hpp" +#include "openvino/pass/serialize.hpp" #include "ie_ngraph_utils.hpp" #include "ie_plugin_config.hpp" #include "ie_algorithm.hpp" @@ -714,8 +714,8 @@ void HeteroExecutableNetwork::Export(std::ostream& heteroModel) { // Note: custom ngraph extensions are not supported std::stringstream xmlFile, binFile; - ngraph::pass::Serialize serializer(xmlFile, binFile, - ngraph::pass::Serialize::Version::IR_V10); + ov::pass::Serialize serializer(xmlFile, binFile, + ov::pass::Serialize::Version::IR_V10); serializer.run_on_function(subnet.getFunction()); auto m_constants = binFile.str(); diff --git a/inference-engine/src/inference_engine/src/cnn_network_ngraph_impl.cpp b/inference-engine/src/inference_engine/src/cnn_network_ngraph_impl.cpp index a89bc529d7a..24484c2f8f2 100644 --- a/inference-engine/src/inference_engine/src/cnn_network_ngraph_impl.cpp +++ b/inference-engine/src/inference_engine/src/cnn_network_ngraph_impl.cpp @@ -23,7 +23,7 @@ #include "ngraph/pass/constant_folding.hpp" #include "ngraph/pass/manager.hpp" #include "openvino/core/except.hpp" -#include "transformations/serialize.hpp" +#include "openvino/pass/serialize.hpp" #include "transformations/smart_reshape/set_batch_size.hpp" #include "transformations/smart_reshape/smart_reshape.hpp" #include "transformations/utils/utils.hpp" @@ -506,10 +506,10 @@ StatusCode CNNNetworkNGraphImpl::serialize(const std::string& xmlPath, custom_opsets.insert(begin(opset), end(opset)); } ngraph::pass::Manager manager; - manager.register_pass(xmlPath, - binPath, - custom_opsets, - ngraph::pass::Serialize::Version::IR_V10); + manager.register_pass(xmlPath, + binPath, + custom_opsets, + ov::pass::Serialize::Version::IR_V10); manager.run_passes(_ngraph_function); } catch (const Exception& e) { return DescriptionBuffer(GENERAL_ERROR, resp) << e.what(); @@ -530,10 +530,7 @@ StatusCode CNNNetworkNGraphImpl::serialize(std::ostream& xmlBuf, std::ostream& b custom_opsets.insert(begin(opset), end(opset)); } ngraph::pass::Manager manager; - manager.register_pass(xmlBuf, - binBuf, - custom_opsets, - ngraph::pass::Serialize::Version::IR_V10); + manager.register_pass(xmlBuf, binBuf, custom_opsets, ov::pass::Serialize::Version::IR_V10); manager.run_passes(_ngraph_function); } catch (const Exception& e) { return DescriptionBuffer(GENERAL_ERROR, resp) << e.what(); @@ -556,10 +553,7 @@ StatusCode CNNNetworkNGraphImpl::serialize(std::ostream& xmlBuf, Blob::Ptr& binB std::stringstream binBuf; ngraph::pass::Manager manager; - manager.register_pass(xmlBuf, - binBuf, - custom_opsets, - ngraph::pass::Serialize::Version::IR_V10); + manager.register_pass(xmlBuf, binBuf, custom_opsets, ov::pass::Serialize::Version::IR_V10); manager.run_passes(_ngraph_function); std::streambuf* pbuf = binBuf.rdbuf(); diff --git a/inference-engine/src/inference_engine/src/compilation_context.cpp b/inference-engine/src/inference_engine/src/compilation_context.cpp index b1a7c74ec4a..4d78374d12e 100644 --- a/inference-engine/src/inference_engine/src/compilation_context.cpp +++ b/inference-engine/src/inference_engine/src/compilation_context.cpp @@ -18,9 +18,9 @@ #include "ie_itt.hpp" #include "ngraph/opsets/opset6.hpp" #include "ngraph/variant.hpp" +#include "openvino/pass/serialize.hpp" #include "transformations/rt_info/fused_names_attribute.hpp" #include "transformations/rt_info/primitives_priority_attribute.hpp" -#include "transformations/serialize.hpp" #ifdef _WIN32 # define stat _stat @@ -97,7 +97,7 @@ std::string NetworkCompilationContext::computeHash(const CNNNetwork& network, // 1. Serialize CNNNetwork net(network); - ngraph::pass::Serialize serializer(xml, bin); + ov::pass::Serialize serializer(xml, bin); serializer.run_on_function(net.getFunction()); // 2. Compute hash on serialized data and options diff --git a/inference-engine/src/mkldnn_plugin/mkldnn_graph_dumper.cpp b/inference-engine/src/mkldnn_plugin/mkldnn_graph_dumper.cpp index 4f695741bda..4ffa1845f6c 100644 --- a/inference-engine/src/mkldnn_plugin/mkldnn_graph_dumper.cpp +++ b/inference-engine/src/mkldnn_plugin/mkldnn_graph_dumper.cpp @@ -12,7 +12,7 @@ #include #include "ngraph/ngraph.hpp" #include -#include +#include #include #include @@ -228,9 +228,9 @@ void serializeToXML(const MKLDNNGraph &graph, const std::string& path) { std::string binPath; ngraph::pass::Manager manager; - manager.register_pass(path, - binPath, - ngraph::pass::Serialize::Version::IR_V10); + manager.register_pass(path, + binPath, + ov::pass::Serialize::Version::IR_V10); manager.run_passes(graph.dump()); } diff --git a/inference-engine/src/mkldnn_plugin/mkldnn_plugin.cpp b/inference-engine/src/mkldnn_plugin/mkldnn_plugin.cpp index 9d70c0478bf..6fb925d9595 100644 --- a/inference-engine/src/mkldnn_plugin/mkldnn_plugin.cpp +++ b/inference-engine/src/mkldnn_plugin/mkldnn_plugin.cpp @@ -68,7 +68,6 @@ #include #include #include -#include #include #include diff --git a/inference-engine/src/mkldnn_plugin/mkldnn_serialize.cpp b/inference-engine/src/mkldnn_plugin/mkldnn_serialize.cpp index feb6a6aa257..ae4044279a3 100644 --- a/inference-engine/src/mkldnn_plugin/mkldnn_serialize.cpp +++ b/inference-engine/src/mkldnn_plugin/mkldnn_serialize.cpp @@ -3,7 +3,7 @@ // #include "mkldnn_serialize.h" -#include +#include #include @@ -118,7 +118,7 @@ void CNNNetworkSerializer::operator << (const CNNNetwork & network) { }; // Serialize to old representation in case of old API - ngraph::pass::StreamSerialize serializer(_ostream, getCustomOpSets(), serializeInputsAndOutputs); + ov::pass::StreamSerialize serializer(_ostream, getCustomOpSets(), serializeInputsAndOutputs); serializer.run_on_function(std::const_pointer_cast(network.getFunction())); } @@ -128,7 +128,7 @@ CNNNetworkDeserializer::CNNNetworkDeserializer(std::istream & istream, cnn_netwo } void CNNNetworkDeserializer::operator >> (InferenceEngine::CNNNetwork & network) { - using namespace ngraph::pass; + using namespace ov::pass; std::string xmlString, xmlInOutString; InferenceEngine::Blob::Ptr dataBlob; diff --git a/inference-engine/src/snippets/src/op/subgraph.cpp b/inference-engine/src/snippets/src/op/subgraph.cpp index f114b49e4e1..355d52698b8 100644 --- a/inference-engine/src/snippets/src/op/subgraph.cpp +++ b/inference-engine/src/snippets/src/op/subgraph.cpp @@ -12,7 +12,7 @@ #include "snippets/pass/assign_registers.hpp" #include -#include +#include #include #include @@ -348,7 +348,7 @@ void snippets::op::Subgraph::print_statistics(bool verbose) { void snippets::op::Subgraph::serialize() const { std::stringstream xmlFile, binFile; - ngraph::pass::Serialize serializer(xmlFile, xmlFile, ngraph::pass::Serialize::Version::IR_V10); + ov::pass::Serialize serializer(xmlFile, xmlFile, ov::pass::Serialize::Version::IR_V10); serializer.run_on_function(get_body()); auto m_constants = binFile.str(); auto m_model = xmlFile.str(); diff --git a/inference-engine/src/transformations/CMakeLists.txt b/inference-engine/src/transformations/CMakeLists.txt index 21d27abf18e..8f9129a6e28 100644 --- a/inference-engine/src/transformations/CMakeLists.txt +++ b/inference-engine/src/transformations/CMakeLists.txt @@ -28,7 +28,7 @@ ie_add_vs_version_file(NAME ${TARGET_NAME} FILEDESCRIPTION "Inference Engine Transformations library") target_link_libraries(${TARGET_NAME} PUBLIC ngraph - PRIVATE ngraph_reference openvino::itt ngraph::builder pugixml::static) + PRIVATE ngraph_reference openvino::itt ngraph::builder) target_include_directories(${TARGET_NAME} PUBLIC $ PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/src") diff --git a/inference-engine/src/transformations/include/transformations/rt_info/attributes.hpp b/inference-engine/src/transformations/include/transformations/rt_info/attributes.hpp index 1207c3bd278..f8bd5493a6f 100644 --- a/inference-engine/src/transformations/include/transformations/rt_info/attributes.hpp +++ b/inference-engine/src/transformations/include/transformations/rt_info/attributes.hpp @@ -7,38 +7,28 @@ #include #include #include -#include -#include - -#include - -#include -#include #include - +#include +#include +#include #include #include #include #include #include #include +#include +#include namespace ov { namespace pass { class TRANSFORMATIONS_API Attributes { public: - Attributes() { - register_factory>(); - register_factory(); - register_factory(); - register_factory(); - register_factory(); - register_factory(); - } + Attributes(); + ~Attributes(); + + Variant* create_by_type_info(const ov::DiscreteTypeInfo& type_info); - Variant * create_by_type_info(const ov::DiscreteTypeInfo & type_info) { - return m_factory_registry.create(type_info); - } private: template void register_factory() { @@ -47,5 +37,5 @@ private: ngraph::FactoryRegistry m_factory_registry; }; -} // namespace pass -} // namespace ov +} // namespace pass +} // namespace ov diff --git a/inference-engine/src/transformations/include/transformations/rt_info/disable_constant_folding.hpp b/inference-engine/src/transformations/include/transformations/rt_info/disable_constant_folding.hpp index 6a03ba2b6ee..62f227219cb 100644 --- a/inference-engine/src/transformations/include/transformations/rt_info/disable_constant_folding.hpp +++ b/inference-engine/src/transformations/include/transformations/rt_info/disable_constant_folding.hpp @@ -13,25 +13,13 @@ #include #include #include - +#include "openvino/pass/constant_folding.hpp" namespace ov { -TRANSFORMATIONS_API void disable_constant_folding(const std::shared_ptr& node); - -TRANSFORMATIONS_API void enable_constant_folding(const std::shared_ptr& node); - -TRANSFORMATIONS_API bool constant_folding_is_disabled(const std::shared_ptr& node); - -class TRANSFORMATIONS_API DisableConstantFolding : public VariantImpl { -public: - OPENVINO_RTTI("disabled_constant_folding", "0"); - - DisableConstantFolding() = default; - - DisableConstantFolding(const value_type &value) : VariantImpl(value) {} - - bool is_copyable() const override { return false; } -}; +using pass::disable_constant_folding; +using pass::enable_constant_folding; +using pass::constant_folding_is_disabled; +using pass::DisableConstantFolding; } // namespace ov diff --git a/inference-engine/src/transformations/include/transformations/rt_info/fused_names_attribute.hpp b/inference-engine/src/transformations/include/transformations/rt_info/fused_names_attribute.hpp index 7ae7646fc4c..c80a4b1536b 100644 --- a/inference-engine/src/transformations/include/transformations/rt_info/fused_names_attribute.hpp +++ b/inference-engine/src/transformations/include/transformations/rt_info/fused_names_attribute.hpp @@ -102,11 +102,4 @@ public: bool visit_attributes(AttributeVisitor & visitor) override; }; - -template <> -class NGRAPH_API AttributeAdapter> : public DirectValueAccessor> { -public: - OPENVINO_RTTI("AttributeAdapter>"); - AttributeAdapter(std::set& value) : DirectValueAccessor>(value) {} -}; } // namespace ov diff --git a/inference-engine/src/transformations/include/transformations/serialize.hpp b/inference-engine/src/transformations/include/transformations/serialize.hpp index 1f676548986..cea06cb8aeb 100644 --- a/inference-engine/src/transformations/include/transformations/serialize.hpp +++ b/inference-engine/src/transformations/include/transformations/serialize.hpp @@ -4,91 +4,4 @@ #pragma once -#include -#include - -#include "ngraph/opsets/opset.hpp" -#include "ngraph/pass/pass.hpp" -#include "transformations_visibility.hpp" - -namespace ngraph { -namespace pass { - -class TRANSFORMATIONS_API Serialize; -class TRANSFORMATIONS_API StreamSerialize; - -} // namespace pass -} // namespace ngraph - -/** - * @ingroup ie_transformation_common_api - * @brief Serialize transformation converts ngraph::Function into IR files - * @attention - * - dynamic shapes are not supported - * - order of generated layers in xml file is ngraph specific (given by - * get_ordered_ops()); MO generates file with different order, but they are - * logically equivalent - */ -class ngraph::pass::Serialize : public ngraph::pass::FunctionPass { -public: - enum class Version : uint8_t { - UNSPECIFIED = 0, // Use the latest or function version - IR_V10 = 10, // v10 IR - IR_V11 = 11 // v11 IR - }; - NGRAPH_RTTI_DECLARATION; - bool run_on_function(std::shared_ptr f) override; - - Serialize(std::ostream& xmlFile, std::ostream& binFile, - std::map custom_opsets, - Version version = Version::UNSPECIFIED); - Serialize(std::ostream& xmlFile, std::ostream& binFile, - Version version = Version::UNSPECIFIED); - - Serialize(const std::string& xmlPath, const std::string& binPath, - std::map custom_opsets, - Version version = Version::UNSPECIFIED); - Serialize(const std::string& xmlPath, const std::string& binPath, - Version version = Version::UNSPECIFIED); - -private: - std::ostream * m_xmlFile; - std::ostream * m_binFile; - const std::string m_xmlPath; - const std::string m_binPath; - const Version m_version; - const std::map m_custom_opsets; -}; - -/** - * @ingroup ie_transformation_common_api - * @brief StreamSerialize transformation converts ngraph::Function into single binary stream - * @attention - * - dynamic shapes are not supported - */ -class ngraph::pass::StreamSerialize : public ngraph::pass::FunctionPass { -public: - NGRAPH_RTTI_DECLARATION; - - struct DataHeader { - size_t custom_data_offset; - size_t custom_data_size; - size_t consts_offset; - size_t consts_size; - size_t model_offset; - size_t model_size; - }; - - bool run_on_function(std::shared_ptr f) override; - - StreamSerialize(std::ostream& stream, - std::map && custom_opsets = {}, - const std::function & custom_data_serializer = {}, - Serialize::Version version = Serialize::Version::UNSPECIFIED); - -private: - std::ostream & m_stream; - std::map m_custom_opsets; - std::function m_custom_data_serializer; - const Serialize::Version m_version; -}; +#include diff --git a/inference-engine/src/transformations/src/transformations/rt_info/attributes.cpp b/inference-engine/src/transformations/src/transformations/rt_info/attributes.cpp new file mode 100644 index 00000000000..17d552d19a0 --- /dev/null +++ b/inference-engine/src/transformations/src/transformations/rt_info/attributes.cpp @@ -0,0 +1,20 @@ +// Copyright (C) 2021 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#include "transformations/rt_info/attributes.hpp" + +ov::pass::Attributes::Attributes() { + register_factory>(); + register_factory(); + register_factory(); + register_factory(); + register_factory(); + register_factory(); +} + +ov::Variant* ov::pass::Attributes::create_by_type_info(const ov::DiscreteTypeInfo& type_info) { + return m_factory_registry.create(type_info); +} + +ov::pass::Attributes::~Attributes() = default; diff --git a/inference-engine/src/transformations/src/transformations/rt_info/disable_constant_folding.cpp b/inference-engine/src/transformations/src/transformations/rt_info/disable_constant_folding.cpp deleted file mode 100644 index e97627c1505..00000000000 --- a/inference-engine/src/transformations/src/transformations/rt_info/disable_constant_folding.cpp +++ /dev/null @@ -1,20 +0,0 @@ -// Copyright (C) 2021 Intel Corporation -// SPDX-License-Identifier: Apache-2.0 -// - -#include "transformations/rt_info/disable_constant_folding.hpp" - -void ov::disable_constant_folding(const std::shared_ptr& node) { - auto & rt_info = node->get_rt_info(); - rt_info[DisableConstantFolding::get_type_info_static()] = std::make_shared(true); -} - -void ov::enable_constant_folding(const std::shared_ptr& node) { - auto & rt_info = node->get_rt_info(); - rt_info.erase(DisableConstantFolding::get_type_info_static()); -} - -bool ov::constant_folding_is_disabled(const std::shared_ptr &node) { - const auto & rt_info = node->get_rt_info(); - return rt_info.count(DisableConstantFolding::get_type_info_static()); -} diff --git a/inference-engine/tests/functional/inference_engine/ir_serialization/const_compression.cpp b/inference-engine/tests/functional/inference_engine/ir_serialization/const_compression.cpp deleted file mode 100644 index 4ab43af6536..00000000000 --- a/inference-engine/tests/functional/inference_engine/ir_serialization/const_compression.cpp +++ /dev/null @@ -1,246 +0,0 @@ -// Copyright (C) 2021 Intel Corporation -// SPDX-License-Identifier: Apache-2.0 -// - -#include - -#include "common_test_utils/ngraph_test_utils.hpp" -#include "ie_core.hpp" -#include "gtest/gtest.h" - -#include -#include - -class SerializatioConstantCompressionTest : public ::testing::Test { -protected: - std::string test_name = - ::testing::UnitTest::GetInstance()->current_test_info()->name(); - std::string m_out_xml_path_1 = test_name + "1" + ".xml"; - std::string m_out_bin_path_1 = test_name + "1" + ".bin"; - - void TearDown() override { - std::remove(m_out_xml_path_1.c_str()); - std::remove(m_out_bin_path_1.c_str()); - } - - std::uintmax_t file_size(std::ifstream &f) { - // get length of file: - const auto pos_to_restore = f.tellg(); - f.seekg(0, f.end); - std::uintmax_t length = f.tellg(); - f.seekg(pos_to_restore, f.beg); - return length; - } -}; - -TEST_F(SerializatioConstantCompressionTest, IdenticalConstantsI32) { - constexpr int unique_const_count = 1; - const ngraph::Shape shape{2, 2, 2}; - - auto A = ngraph::op::Constant::create(ngraph::element::i32, shape, - {1, 2, 3, 4, 5, 6, 7, 8}); - auto B = ngraph::op::Constant::create(ngraph::element::i32, shape, - {1, 2, 3, 4, 5, 6, 7, 8}); - - auto ngraph_a = std::make_shared(ngraph::NodeVector{A, B}, - ngraph::ParameterVector{}); - - ngraph::pass::Serialize(m_out_xml_path_1, m_out_bin_path_1).run_on_function(ngraph_a); - - std::ifstream xml_1(m_out_xml_path_1, std::ios::binary); - std::ifstream bin_1(m_out_bin_path_1, std::ios::binary); - - ASSERT_TRUE(file_size(bin_1) == unique_const_count * ngraph::shape_size(shape) * sizeof(int32_t)); -} - -TEST_F(SerializatioConstantCompressionTest, IdenticalConstantsI64) { - constexpr int unique_const_count = 1; - const ngraph::Shape shape{2, 2, 2}; - - auto A = ngraph::op::Constant::create(ngraph::element::i64, shape, - {1, 2, 3, 4, 5, 6, 7, 8}); - auto B = ngraph::op::Constant::create(ngraph::element::i64, shape, - {1, 2, 3, 4, 5, 6, 7, 8}); - - auto ngraph_a = std::make_shared(ngraph::NodeVector{A, B}, - ngraph::ParameterVector{}); - - ngraph::pass::Serialize(m_out_xml_path_1, m_out_bin_path_1).run_on_function(ngraph_a); - - std::ifstream xml_1(m_out_xml_path_1, std::ios::binary); - std::ifstream bin_1(m_out_bin_path_1, std::ios::binary); - - ASSERT_TRUE(file_size(bin_1) == unique_const_count * ngraph::shape_size(shape) * sizeof(int64_t)); -} - -TEST_F(SerializatioConstantCompressionTest, IdenticalConstantsFP16) { - constexpr int unique_const_count = 1; - const ngraph::Shape shape{2, 2, 2}; - - auto A = ngraph::op::Constant::create(ngraph::element::f16, shape, - {1, 2, 3, 4, 5, 6, 7, 8}); - auto B = ngraph::op::Constant::create(ngraph::element::f16, shape, - {1, 2, 3, 4, 5, 6, 7, 8}); - - auto ngraph_a = std::make_shared(ngraph::NodeVector{A, B}, - ngraph::ParameterVector{}); - - ngraph::pass::Serialize(m_out_xml_path_1, m_out_bin_path_1).run_on_function(ngraph_a); - - std::ifstream xml_1(m_out_xml_path_1, std::ios::binary); - std::ifstream bin_1(m_out_bin_path_1, std::ios::binary); - - ASSERT_TRUE(file_size(bin_1) == unique_const_count * ngraph::shape_size(shape) * sizeof(ngraph::float16)); -} - -TEST_F(SerializatioConstantCompressionTest, IdenticalConstantsFP32) { - constexpr int unique_const_count = 1; - const ngraph::Shape shape{2, 2, 2}; - - auto A = ngraph::op::Constant::create(ngraph::element::f32, shape, - {1, 2, 3, 4, 5, 6, 7, 8}); - auto B = ngraph::op::Constant::create(ngraph::element::f32, shape, - {1, 2, 3, 4, 5, 6, 7, 8}); - - auto ngraph_a = std::make_shared(ngraph::NodeVector{A, B}, - ngraph::ParameterVector{}); - - ngraph::pass::Serialize(m_out_xml_path_1, m_out_bin_path_1).run_on_function(ngraph_a); - - std::ifstream xml_1(m_out_xml_path_1, std::ios::binary); - std::ifstream bin_1(m_out_bin_path_1, std::ios::binary); - - ASSERT_TRUE(file_size(bin_1) == unique_const_count * ngraph::shape_size(shape) * sizeof(float)); -} - -TEST_F(SerializatioConstantCompressionTest, NonIdenticalConstantsI64) { - constexpr int unique_const_count = 2; - const ngraph::Shape shape{2}; - - // hash_combine returns the same hash for this two constants so we also check the content of arrays - auto A = ngraph::op::Constant::create(ngraph::element::i64, shape, {2, 2}); - auto B = ngraph::op::Constant::create(ngraph::element::i64, shape, {0, 128}); - - auto ngraph_a = std::make_shared(ngraph::NodeVector{A, B}, - ngraph::ParameterVector{}); - - ngraph::pass::Serialize(m_out_xml_path_1, m_out_bin_path_1).run_on_function(ngraph_a); - - std::ifstream xml_1(m_out_xml_path_1, std::ios::binary); - std::ifstream bin_1(m_out_bin_path_1, std::ios::binary); - - ASSERT_TRUE(file_size(bin_1) == unique_const_count * ngraph::shape_size(shape) * sizeof(int64_t)); -} - -TEST_F(SerializatioConstantCompressionTest, IdenticalConstantsTimesTwo) { - constexpr int unique_const_count = 2; - const ngraph::Shape shape{2, 2, 2}; - - auto A = ngraph::op::Constant::create(ngraph::element::i32, shape, - {1, 2, 3, 4, 5, 6, 7, 8}); - auto B = ngraph::op::Constant::create(ngraph::element::i32, shape, - {1, 2, 3, 4, 5, 6, 7, 8}); - auto C = ngraph::op::Constant::create(ngraph::element::i32, shape, - {0, 3, 1, 2, 5, 6, 25, 3}); - auto D = ngraph::op::Constant::create(ngraph::element::i32, shape, - {0, 3, 1, 2, 5, 6, 25, 3}); - - auto ngraph_a = std::make_shared(ngraph::NodeVector{A, B, C, D}, - ngraph::ParameterVector{}); - - ngraph::pass::Serialize(m_out_xml_path_1, m_out_bin_path_1).run_on_function(ngraph_a); - - std::ifstream xml_1(m_out_xml_path_1, std::ios::binary); - std::ifstream bin_1(m_out_bin_path_1, std::ios::binary); - - ASSERT_TRUE(file_size(bin_1) == unique_const_count * ngraph::shape_size(shape) * sizeof(int32_t)); -} - -TEST_F(SerializatioConstantCompressionTest, IdenticalConstantsTimesTwoMultipleOccurences) { - constexpr int unique_const_count = 2; - const ngraph::Shape shape{2, 2, 2}; - - auto A = ngraph::op::Constant::create(ngraph::element::i32, shape, - {1, 2, 3, 4, 5, 6, 7, 8}); - auto B = ngraph::op::Constant::create(ngraph::element::i32, shape, - {0, 3, 1, 2, 5, 6, 25, 3}); - auto C = ngraph::op::Constant::create(ngraph::element::i32, shape, - {1, 2, 3, 4, 5, 6, 7, 8}); - auto D = ngraph::op::Constant::create(ngraph::element::i32, shape, - {0, 3, 1, 2, 5, 6, 25, 3}); - auto E = ngraph::op::Constant::create(ngraph::element::i32, shape, - {1, 2, 3, 4, 5, 6, 7, 8}); - auto F = ngraph::op::Constant::create(ngraph::element::i32, shape, - {0, 3, 1, 2, 5, 6, 25, 3}); - - auto ngraph_a = std::make_shared(ngraph::NodeVector{A, B, C, D, E, F}, - ngraph::ParameterVector{}); - - ngraph::pass::Serialize(m_out_xml_path_1, m_out_bin_path_1).run_on_function(ngraph_a); - - std::ifstream xml_1(m_out_xml_path_1, std::ios::binary); - std::ifstream bin_1(m_out_bin_path_1, std::ios::binary); - - ASSERT_TRUE(file_size(bin_1) == unique_const_count * ngraph::shape_size(shape) * sizeof(int32_t)); -} - -TEST_F(SerializatioConstantCompressionTest, NonIdenticalConstants) { - constexpr int unique_const_count = 2; - const ngraph::Shape shape{2, 2, 2}; - - auto A = ngraph::op::Constant::create(ngraph::element::i32, shape, - {1, 2, 3, 4, 5, 6, 7, 8}); - auto B = ngraph::op::Constant::create(ngraph::element::i32, shape, - {2, 2, 3, 4, 5, 6, 7, 8}); - - auto ngraph_a = std::make_shared(ngraph::NodeVector{A, B}, - ngraph::ParameterVector{}); - - ngraph::pass::Serialize(m_out_xml_path_1, m_out_bin_path_1).run_on_function(ngraph_a); - - std::ifstream xml_1(m_out_xml_path_1, std::ios::binary); - std::ifstream bin_1(m_out_bin_path_1, std::ios::binary); - - ASSERT_TRUE(file_size(bin_1) == unique_const_count * ngraph::shape_size(shape) * sizeof(int32_t)); -} - -TEST_F(SerializatioConstantCompressionTest, IdenticalConstantsDifferentTypesI32I64) { - constexpr int unique_const_count = 1; - const ngraph::Shape shape{2, 2, 2}; - - auto A = ngraph::op::Constant::create(ngraph::element::i32, shape, - {1, 0, 2, 0, 3, 0, 4, 0}); - auto B = ngraph::op::Constant::create(ngraph::element::i64, ngraph::Shape({1, 2, 2}), - {1, 2, 3, 4}); - - auto ngraph_a = std::make_shared(ngraph::NodeVector{A, B}, - ngraph::ParameterVector{}); - - ngraph::pass::Serialize(m_out_xml_path_1, m_out_bin_path_1).run_on_function(ngraph_a); - - std::ifstream xml_1(m_out_xml_path_1, std::ios::binary); - std::ifstream bin_1(m_out_bin_path_1, std::ios::binary); - - ASSERT_TRUE(file_size(bin_1) == unique_const_count * ngraph::shape_size(shape) * sizeof(int32_t)); -} - -TEST_F(SerializatioConstantCompressionTest, IdenticalConstantsDifferentTypesI32I8) { - constexpr int unique_const_count = 1; - const ngraph::Shape shape{1, 1, 2}; - - auto A = ngraph::op::Constant::create(ngraph::element::i32, shape, - {1, 2}); - auto B = ngraph::op::Constant::create(ngraph::element::i8, ngraph::Shape({1, 2, 4}), - {1, 0, 0, 0, - 2, 0, 0, 0}); - - auto ngraph_a = std::make_shared(ngraph::NodeVector{A, B}, - ngraph::ParameterVector{}); - - ngraph::pass::Serialize(m_out_xml_path_1, m_out_bin_path_1).run_on_function(ngraph_a); - - std::ifstream xml_1(m_out_xml_path_1, std::ios::binary); - std::ifstream bin_1(m_out_bin_path_1, std::ios::binary); - - ASSERT_TRUE(file_size(bin_1) == unique_const_count * ngraph::shape_size(shape) * sizeof(int32_t)); -} diff --git a/inference-engine/tests/functional/inference_engine/ir_serialization/deterministicity.cpp b/inference-engine/tests/functional/inference_engine/ir_serialization/deterministicity.cpp index ddcc0149532..c22afcac38d 100644 --- a/inference-engine/tests/functional/inference_engine/ir_serialization/deterministicity.cpp +++ b/inference-engine/tests/functional/inference_engine/ir_serialization/deterministicity.cpp @@ -10,13 +10,12 @@ #include "ie_core.hpp" #ifndef IR_SERIALIZATION_MODELS_PATH // should be already defined by cmake -# error "IR_SERIALIZATION_MODELS_PATH is not defined" +# error "IR_SERIALIZATION_MODELS_PATH is not defined" #endif class SerializationDeterministicityTest : public ::testing::Test { protected: - std::string test_name = - ::testing::UnitTest::GetInstance()->current_test_info()->name(); + std::string test_name = ::testing::UnitTest::GetInstance()->current_test_info()->name(); std::string m_out_xml_path_1 = test_name + "1" + ".xml"; std::string m_out_bin_path_1 = test_name + "1" + ".bin"; std::string m_out_xml_path_2 = test_name + "2" + ".xml"; @@ -30,8 +29,10 @@ protected: } bool files_equal(std::ifstream& f1, std::ifstream& f2) { - if (!f1.good()) return false; - if (!f2.good()) return false; + if (!f1.good()) + return false; + if (!f2.good()) + return false; while (!f1.eof() && !f2.eof()) { if (f1.get() != f2.get()) { @@ -47,91 +48,11 @@ protected: } }; -#ifdef NGRAPH_ONNX_FRONTEND_ENABLE - -TEST_F(SerializationDeterministicityTest, BasicModel) { - const std::string model = CommonTestUtils::getModelFromTestModelZoo( - IR_SERIALIZATION_MODELS_PATH "add_abc.onnx"); - - InferenceEngine::Core ie; - auto expected = ie.ReadNetwork(model); - expected.serialize(m_out_xml_path_1, m_out_bin_path_1); - expected.serialize(m_out_xml_path_2, m_out_bin_path_2); - - std::ifstream xml_1(m_out_xml_path_1, std::ios::in | std::ios::binary); - std::ifstream bin_1(m_out_bin_path_1, std::ios::in | std::ios::binary); - std::ifstream xml_2(m_out_xml_path_2, std::ios::in | std::ios::binary); - std::ifstream bin_2(m_out_bin_path_2, std::ios::in | std::ios::binary); - - ASSERT_TRUE(files_equal(xml_1, xml_2)); - ASSERT_TRUE(files_equal(bin_1, bin_2)); -} - -TEST_F(SerializationDeterministicityTest, ModelWithMultipleLayers) { - const std::string model = CommonTestUtils::getModelFromTestModelZoo( - IR_SERIALIZATION_MODELS_PATH "addmul_abc.onnx"); - - InferenceEngine::Core ie; - auto expected = ie.ReadNetwork(model); - expected.serialize(m_out_xml_path_1, m_out_bin_path_1); - expected.serialize(m_out_xml_path_2, m_out_bin_path_2); - - std::ifstream xml_1(m_out_xml_path_1, std::ios::in | std::ios::binary); - std::ifstream bin_1(m_out_bin_path_1, std::ios::in | std::ios::binary); - std::ifstream xml_2(m_out_xml_path_2, std::ios::in | std::ios::binary); - std::ifstream bin_2(m_out_bin_path_2, std::ios::in | std::ios::binary); - - ASSERT_TRUE(files_equal(xml_1, xml_2)); - ASSERT_TRUE(files_equal(bin_1, bin_2)); -} - -#endif - -TEST_F(SerializationDeterministicityTest, ModelWithMultipleOutputs) { - const std::string model = CommonTestUtils::getModelFromTestModelZoo( - IR_SERIALIZATION_MODELS_PATH "split_equal_parts_2d.xml"); - const std::string weights = CommonTestUtils::getModelFromTestModelZoo( - IR_SERIALIZATION_MODELS_PATH "split_equal_parts_2d.bin"); - - InferenceEngine::Core ie; - auto expected = ie.ReadNetwork(model, weights); - expected.serialize(m_out_xml_path_1, m_out_bin_path_1); - expected.serialize(m_out_xml_path_2, m_out_bin_path_2); - - std::ifstream xml_1(m_out_xml_path_1, std::ios::in | std::ios::binary); - std::ifstream bin_1(m_out_bin_path_1, std::ios::in | std::ios::binary); - std::ifstream xml_2(m_out_xml_path_2, std::ios::in | std::ios::binary); - std::ifstream bin_2(m_out_bin_path_2, std::ios::in | std::ios::binary); - - ASSERT_TRUE(files_equal(xml_1, xml_2)); - ASSERT_TRUE(files_equal(bin_1, bin_2)); -} - -TEST_F(SerializationDeterministicityTest, ModelWithConstants) { - const std::string model = CommonTestUtils::getModelFromTestModelZoo( - IR_SERIALIZATION_MODELS_PATH "add_abc_initializers.xml"); - const std::string weights = CommonTestUtils::getModelFromTestModelZoo( - IR_SERIALIZATION_MODELS_PATH "add_abc_initializers.bin"); - - InferenceEngine::Core ie; - auto expected = ie.ReadNetwork(model, weights); - expected.serialize(m_out_xml_path_1, m_out_bin_path_1); - expected.serialize(m_out_xml_path_2, m_out_bin_path_2); - - std::ifstream xml_1(m_out_xml_path_1, std::ios::in | std::ios::binary); - std::ifstream bin_1(m_out_bin_path_1, std::ios::in | std::ios::binary); - std::ifstream xml_2(m_out_xml_path_2, std::ios::in | std::ios::binary); - std::ifstream bin_2(m_out_bin_path_2, std::ios::in | std::ios::binary); - - ASSERT_TRUE(files_equal(xml_1, xml_2)); - ASSERT_TRUE(files_equal(bin_1, bin_2)); -} - TEST_F(SerializationDeterministicityTest, SerializeToStream) { - const std::string model = CommonTestUtils::getModelFromTestModelZoo( - IR_SERIALIZATION_MODELS_PATH "add_abc_initializers.xml"); - const std::string weights = CommonTestUtils::getModelFromTestModelZoo( - IR_SERIALIZATION_MODELS_PATH "add_abc_initializers.bin"); + const std::string model = + CommonTestUtils::getModelFromTestModelZoo(IR_SERIALIZATION_MODELS_PATH "add_abc_initializers.xml"); + const std::string weights = + CommonTestUtils::getModelFromTestModelZoo(IR_SERIALIZATION_MODELS_PATH "add_abc_initializers.bin"); std::stringstream m_out_xml_buf, m_out_bin_buf; InferenceEngine::Blob::Ptr binBlob; @@ -143,8 +64,7 @@ TEST_F(SerializationDeterministicityTest, SerializeToStream) { std::streambuf* pbuf = m_out_bin_buf.rdbuf(); unsigned long bufSize = m_out_bin_buf.tellp(); - InferenceEngine::TensorDesc tensorDesc(InferenceEngine::Precision::U8, - { bufSize }, InferenceEngine::Layout::C); + InferenceEngine::TensorDesc tensorDesc(InferenceEngine::Precision::U8, {bufSize}, InferenceEngine::Layout::C); binBlob = InferenceEngine::make_shared_blob(tensorDesc); binBlob->allocate(); pbuf->sgetn(binBlob->buffer(), bufSize); @@ -156,10 +76,10 @@ TEST_F(SerializationDeterministicityTest, SerializeToStream) { } TEST_F(SerializationDeterministicityTest, SerializeToBlob) { - const std::string model = CommonTestUtils::getModelFromTestModelZoo( - IR_SERIALIZATION_MODELS_PATH "add_abc_initializers.xml"); - const std::string weights = CommonTestUtils::getModelFromTestModelZoo( - IR_SERIALIZATION_MODELS_PATH "add_abc_initializers.bin"); + const std::string model = + CommonTestUtils::getModelFromTestModelZoo(IR_SERIALIZATION_MODELS_PATH "add_abc_initializers.xml"); + const std::string weights = + CommonTestUtils::getModelFromTestModelZoo(IR_SERIALIZATION_MODELS_PATH "add_abc_initializers.bin"); std::stringstream m_out_xml_buf; InferenceEngine::Blob::Ptr m_out_bin_buf; diff --git a/inference-engine/tests/functional/inference_engine/ir_serialization/rt_info_deserialization.cpp b/inference-engine/tests/functional/inference_engine/ir_serialization/rt_info_deserialization.cpp index d75e636b88f..5e8f101c024 100644 --- a/inference-engine/tests/functional/inference_engine/ir_serialization/rt_info_deserialization.cpp +++ b/inference-engine/tests/functional/inference_engine/ir_serialization/rt_info_deserialization.cpp @@ -2,7 +2,6 @@ // SPDX-License-Identifier: Apache-2.0 // -#include #include #include @@ -10,9 +9,11 @@ #include #include +#include +#include +#include #include #include -#include #include "frontend_manager/frontend_manager.hpp" #include "graph_comparator.hpp" #include "ie_blob.h" @@ -25,7 +26,6 @@ #include "ngraph/variant.hpp" #include "ngraph/pass/manager.hpp" #include "openvino/runtime/core.hpp" -#include using namespace ngraph; @@ -115,11 +115,11 @@ TEST_F(RTInfoDeserialization, NodeV10) { auto f = getWithIRFrontend(model); ASSERT_NE(nullptr, f); - auto check_rt_info = [](const RTMap & info) { - const std::string & key = VariantWrapper::get_type_info_static(); + auto check_rt_info = [](const RTMap& info) { + const std::string& key = VariantWrapper::get_type_info_static(); EXPECT_FALSE(info.count(key)); - const std::string & key_old_api = ov::OldApiMap::get_type_info_static(); + const std::string& key_old_api = ov::OldApiMap::get_type_info_static(); EXPECT_FALSE(info.count(key_old_api)); }; @@ -173,8 +173,8 @@ TEST_F(RTInfoDeserialization, NodeV10) { auto result = std::make_shared(convert_result); result->set_friendly_name("output"); - auto f_10_ref = std::make_shared(ngraph::ResultVector{result}, - ngraph::ParameterVector{param}); + auto f_10_ref = + std::make_shared(ngraph::ResultVector{result}, ngraph::ParameterVector{param}); f_10_ref->set_friendly_name("Network"); ov::runtime::Core core; @@ -184,11 +184,11 @@ TEST_F(RTInfoDeserialization, NodeV10) { check_version(f_10_core, 10); const auto fc = FunctionsComparator::with_default() - .enable(FunctionsComparator::ATTRIBUTES) - .enable(FunctionsComparator::PRECISIONS) - .enable(FunctionsComparator::RUNTIME_KEYS) - .enable(FunctionsComparator::NAMES) - .enable(FunctionsComparator::CONST_VALUES); + .enable(FunctionsComparator::ATTRIBUTES) + .enable(FunctionsComparator::PRECISIONS) + .enable(FunctionsComparator::RUNTIME_KEYS) + .enable(FunctionsComparator::NAMES) + .enable(FunctionsComparator::CONST_VALUES); auto res = fc.compare(f_10_core, f_10_ref); EXPECT_TRUE(res.valid) << res.message; } @@ -269,8 +269,8 @@ TEST_F(RTInfoDeserialization, InputAndOutputV10) { auto f = getWithIRFrontend(model); ASSERT_NE(nullptr, f); - auto check_rt_info = [](const RTMap & info) { - const std::string & key = VariantWrapper::get_type_info_static(); + auto check_rt_info = [](const RTMap& info) { + const std::string& key = VariantWrapper::get_type_info_static(); ASSERT_FALSE(info.count(key)); }; @@ -325,8 +325,8 @@ TEST_F(RTInfoDeserialization, InputAndOutputV10) { auto result = std::make_shared(convert_result); result->set_friendly_name("output"); - auto f_10_ref = std::make_shared(ngraph::ResultVector{result}, - ngraph::ParameterVector{param}); + auto f_10_ref = + std::make_shared(ngraph::ResultVector{result}, ngraph::ParameterVector{param}); f_10_ref->set_friendly_name("Network"); ov::runtime::Core core; @@ -335,11 +335,11 @@ TEST_F(RTInfoDeserialization, InputAndOutputV10) { check_version(f_10_core, 10); const auto fc = FunctionsComparator::with_default() - .enable(FunctionsComparator::ATTRIBUTES) - .enable(FunctionsComparator::PRECISIONS) - .enable(FunctionsComparator::RUNTIME_KEYS) - .enable(FunctionsComparator::NAMES) - .enable(FunctionsComparator::CONST_VALUES); + .enable(FunctionsComparator::ATTRIBUTES) + .enable(FunctionsComparator::PRECISIONS) + .enable(FunctionsComparator::RUNTIME_KEYS) + .enable(FunctionsComparator::NAMES) + .enable(FunctionsComparator::CONST_VALUES); auto res = fc.compare(f_10_core, f_10_ref); EXPECT_TRUE(res.valid) << res.message; } @@ -409,8 +409,8 @@ TEST_F(RTInfoDeserialization, NodeV11) { auto f = getWithIRFrontend(model); ASSERT_NE(nullptr, f); - auto check_fused_names = [](const RTMap & info, const std::string & names) { - const std::string & key = VariantWrapper::get_type_info_static(); + auto check_fused_names = [](const RTMap& info, const std::string& names) { + const std::string& key = VariantWrapper::get_type_info_static(); ASSERT_TRUE(info.count(key)); auto fused_names_attr = std::dynamic_pointer_cast>(info.at(key)); ASSERT_TRUE(fused_names_attr); @@ -437,14 +437,10 @@ TEST_F(RTInfoDeserialization, NodeV11) { auto param = f->get_parameters()[0]; check_fused_names(param->get_rt_info(), "in1"); - check_old_api_map(param->get_rt_info(), - std::vector({0, 2, 3, 1}), - ngraph::element::Type_t::f16); + check_old_api_map(param->get_rt_info(), std::vector({0, 2, 3, 1}), ngraph::element::Type_t::f16); auto result = f->get_result(); - check_old_api_map(result->get_rt_info(), - std::vector({0, 3, 1, 2}), - ngraph::element::Type_t::f16); + check_old_api_map(result->get_rt_info(), std::vector({0, 3, 1, 2}), ngraph::element::Type_t::f16); auto round = result->get_input_node_ptr(0); check_fused_names(round->get_rt_info(), "Round1,Round2"); @@ -478,8 +474,9 @@ TEST_F(RTInfoDeserialization, NodeV11) { auto convert_param = std::make_shared(param, ngraph::element::f32); - auto constant_param = std::make_shared(ngraph::element::i64, ngraph::Shape{4}, - std::vector{0, 2, 3, 1}); + auto constant_param = std::make_shared(ngraph::element::i64, + ngraph::Shape{4}, + std::vector{0, 2, 3, 1}); auto transpose_param = std::make_shared(convert_param, constant_param); auto round = std::make_shared(transpose_param, @@ -488,8 +485,9 @@ TEST_F(RTInfoDeserialization, NodeV11) { round->get_rt_info()[VariantWrapper::get_type_info_static()] = std::make_shared>(ngraph::FusedNames("Round1,Round2")); - auto constant_result = std::make_shared(ngraph::element::i64, ngraph::Shape{4}, - std::vector{0, 3, 1, 2}); + auto constant_result = std::make_shared(ngraph::element::i64, + ngraph::Shape{4}, + std::vector{0, 3, 1, 2}); auto transpose_result = std::make_shared(round, constant_result); auto convert_result = std::make_shared(transpose_result, type); @@ -499,8 +497,8 @@ TEST_F(RTInfoDeserialization, NodeV11) { auto result = std::make_shared(convert_result); result->set_friendly_name("output"); - auto f_10_ref = std::make_shared(ngraph::ResultVector{result}, - ngraph::ParameterVector{param}); + auto f_10_ref = + std::make_shared(ngraph::ResultVector{result}, ngraph::ParameterVector{param}); f_10_ref->set_friendly_name("Network"); InferenceEngine::Core core; @@ -514,11 +512,11 @@ TEST_F(RTInfoDeserialization, NodeV11) { EXPECT_EQ(InferenceEngine::Precision::FP32, cnn_core.getOutputsInfo()["Round"]->getPrecision()); const auto fc = FunctionsComparator::with_default() - .enable(FunctionsComparator::ATTRIBUTES) - .enable(FunctionsComparator::PRECISIONS) - .enable(FunctionsComparator::RUNTIME_KEYS) - .enable(FunctionsComparator::NAMES) - .enable(FunctionsComparator::CONST_VALUES); + .enable(FunctionsComparator::ATTRIBUTES) + .enable(FunctionsComparator::PRECISIONS) + .enable(FunctionsComparator::RUNTIME_KEYS) + .enable(FunctionsComparator::NAMES) + .enable(FunctionsComparator::CONST_VALUES); auto res = fc.compare(f_10_core, f_10_ref); EXPECT_TRUE(res.valid) << res.message; @@ -634,16 +632,16 @@ TEST_F(RTInfoDeserialization, InputAndOutputV11) { }; check_version(f, 11); - auto check_fused_names = [](const RTMap & info, const std::string & names) { - const std::string & key = VariantWrapper::get_type_info_static(); + auto check_fused_names = [](const RTMap& info, const std::string& names) { + const std::string& key = VariantWrapper::get_type_info_static(); ASSERT_TRUE(info.count(key)); auto fused_names_attr = std::dynamic_pointer_cast>(info.at(key)); ASSERT_TRUE(fused_names_attr); ASSERT_EQ(fused_names_attr->get().getNames(), names); }; - auto check_old_api_map = [](const RTMap & info, const std::vector & order, ngraph::element::Type type) { - const std::string & old_api_map_key = ov::OldApiMap::get_type_info_static(); + auto check_old_api_map = [](const RTMap& info, const std::vector& order, ngraph::element::Type type) { + const std::string& old_api_map_key = ov::OldApiMap::get_type_info_static(); ASSERT_TRUE(info.count(old_api_map_key)); auto old_api_map_attr = std::dynamic_pointer_cast(info.at(old_api_map_key)); ASSERT_TRUE(old_api_map_attr); @@ -654,15 +652,11 @@ TEST_F(RTInfoDeserialization, InputAndOutputV11) { auto param = f->get_parameters()[0]; check_fused_names(param->output(0).get_rt_info(), "test1,test2"); - check_old_api_map(param->get_rt_info(), - std::vector({}), - ngraph::element::Type_t::undefined); + check_old_api_map(param->get_rt_info(), std::vector({}), ngraph::element::Type_t::undefined); auto result = f->get_result(); check_fused_names(result->input(0).get_rt_info(), "test5,test6"); - check_old_api_map(result->get_rt_info(), - std::vector({}), - ngraph::element::Type_t::undefined); + check_old_api_map(result->get_rt_info(), std::vector({}), ngraph::element::Type_t::undefined); auto add = result->get_input_node_ptr(0); check_fused_names(add->input(0).get_rt_info(), "test2,test3"); @@ -680,8 +674,8 @@ TEST_F(RTInfoDeserialization, InputAndOutputV11) { EXPECT_EQ(InferenceEngine::Precision::FP32, cnn.getOutputsInfo()["sum"]->getPrecision()); // check that old api map is removed once applied - auto check_old_api_rt_info = [](const RTMap & info) { - const std::string & key = ov::OldApiMap::get_type_info_static(); + auto check_old_api_rt_info = [](const RTMap& info) { + const std::string& key = ov::OldApiMap::get_type_info_static(); EXPECT_FALSE(info.count(key)); }; diff --git a/inference-engine/tests/functional/inference_engine/ir_serialization/serialize.cpp b/inference-engine/tests/functional/inference_engine/ir_serialization/serialize.cpp deleted file mode 100644 index 8f1e29d4d89..00000000000 --- a/inference-engine/tests/functional/inference_engine/ir_serialization/serialize.cpp +++ /dev/null @@ -1,91 +0,0 @@ -// Copyright (C) 2018-2021 Intel Corporation -// SPDX-License-Identifier: Apache-2.0 -// - -#include - -#include "common_test_utils/ngraph_test_utils.hpp" -#include "common_test_utils/file_utils.hpp" -#include "gtest/gtest.h" -#include "ie_core.hpp" - -#ifndef IR_SERIALIZATION_MODELS_PATH // should be already defined by cmake -# error "IR_SERIALIZATION_MODELS_PATH is not defined" -#endif - -typedef std::tuple SerializationParams; - -class SerializationTest: public CommonTestUtils::TestsCommon, - public testing::WithParamInterface { -public: - std::string m_model_path; - std::string m_binary_path; - std::string m_out_xml_path; - std::string m_out_bin_path; - - void SetUp() override { - m_model_path = CommonTestUtils::getModelFromTestModelZoo( - IR_SERIALIZATION_MODELS_PATH + std::get<0>(GetParam())); - if (!std::get<1>(GetParam()).empty()) { - m_binary_path = CommonTestUtils::getModelFromTestModelZoo( - IR_SERIALIZATION_MODELS_PATH + std::get<1>(GetParam())); - } - - const std::string test_name = GetTestName() + "_" + GetTimestamp(); - m_out_xml_path = test_name + ".xml"; - m_out_bin_path = test_name + ".bin"; - } - - void TearDown() override { - std::remove(m_out_xml_path.c_str()); - std::remove(m_out_bin_path.c_str()); - } -}; - -TEST_P(SerializationTest, CompareFunctions) { - InferenceEngine::Core ie; - InferenceEngine::CNNNetwork expected; - - expected = ie.ReadNetwork(m_model_path, m_binary_path); - expected.serialize(m_out_xml_path, m_out_bin_path); - auto result = ie.ReadNetwork(m_out_xml_path, m_out_bin_path); - - const auto fc = FunctionsComparator::with_default() - .enable(FunctionsComparator::ATTRIBUTES) - .enable(FunctionsComparator::CONST_VALUES); - const auto res = fc.compare(result.getFunction(), expected.getFunction()); - EXPECT_TRUE(res.valid) << res.message; -} - -INSTANTIATE_TEST_SUITE_P(IRSerialization, SerializationTest, - testing::Values(std::make_tuple("add_abc.xml", "add_abc.bin"), - std::make_tuple("add_abc_f64.xml", ""), - std::make_tuple("add_abc_bin.xml", ""), - std::make_tuple("split_equal_parts_2d.xml", "split_equal_parts_2d.bin"), - std::make_tuple("addmul_abc.xml", "addmul_abc.bin"), - std::make_tuple("add_abc_initializers.xml", "add_abc_initializers.bin"), - std::make_tuple("add_abc_initializers.xml", "add_abc_initializers_f32_nan_const.bin"), - std::make_tuple("add_abc_initializers_nan_const.xml", "add_abc_initializers_nan_const.bin"), - std::make_tuple("add_abc_initializers_u1_const.xml", "add_abc_initializers_u1_const.bin"), - std::make_tuple("experimental_detectron_roi_feature_extractor.xml", ""), - std::make_tuple("experimental_detectron_roi_feature_extractor_opset6.xml", ""), - std::make_tuple("experimental_detectron_detection_output.xml", ""), - std::make_tuple("experimental_detectron_detection_output_opset6.xml", ""), - std::make_tuple("nms5.xml", "nms5.bin"), - std::make_tuple("shape_of.xml", ""), - std::make_tuple("dynamic_input_shape.xml", ""), - std::make_tuple("pad_with_shape_of.xml", ""), - std::make_tuple("conv_with_rt_info.xml", ""), - std::make_tuple("loop_2d_add.xml", "loop_2d_add.bin"), - std::make_tuple("nms5_dynamism.xml", "nms5_dynamism.bin"), - std::make_tuple("if_diff_case.xml", "if_diff_case.bin"))); - -#ifdef NGRAPH_ONNX_FRONTEND_ENABLE - -INSTANTIATE_TEST_SUITE_P(ONNXSerialization, SerializationTest, - testing::Values(std::make_tuple("add_abc.onnx", ""), - std::make_tuple("split_equal_parts_2d.onnx", ""), - std::make_tuple("addmul_abc.onnx", ""), - std::make_tuple("add_abc_initializers.onnx", ""))); - -#endif diff --git a/inference-engine/tests/functional/inference_engine/transformations/disable_shapeof_constant_folding_tests.cpp b/inference-engine/tests/functional/inference_engine/transformations/disable_shapeof_constant_folding_tests.cpp index 13ccc254a4a..59c6a753302 100644 --- a/inference-engine/tests/functional/inference_engine/transformations/disable_shapeof_constant_folding_tests.cpp +++ b/inference-engine/tests/functional/inference_engine/transformations/disable_shapeof_constant_folding_tests.cpp @@ -4,21 +4,17 @@ #include -#include #include -#include - #include #include -#include #include - +#include +#include +#include #include -#include #include "common_test_utils/ngraph_test_utils.hpp" - using namespace testing; using namespace ngraph; @@ -75,4 +71,4 @@ TEST(TransformationTests, ShapeOfShapeOfConstantFolding) { auto res = compare_functions(f, f_ref); ASSERT_TRUE(res.first) << res.second; -} \ No newline at end of file +} diff --git a/inference-engine/tests/functional/inference_engine/transformations/low_latency_v2_test.cpp b/inference-engine/tests/functional/inference_engine/transformations/low_latency_v2_test.cpp index 5c66e8109ef..cb143088c7c 100644 --- a/inference-engine/tests/functional/inference_engine/transformations/low_latency_v2_test.cpp +++ b/inference-engine/tests/functional/inference_engine/transformations/low_latency_v2_test.cpp @@ -15,7 +15,6 @@ #include #include #include -#include #include "common_test_utils/ngraph_test_utils.hpp" diff --git a/inference-engine/tests/functional/plugin/shared/include/behavior/ov_exec_network.hpp b/inference-engine/tests/functional/plugin/shared/include/behavior/ov_exec_network.hpp index 0df07245dae..bae04081f6b 100644 --- a/inference-engine/tests/functional/plugin/shared/include/behavior/ov_exec_network.hpp +++ b/inference-engine/tests/functional/plugin/shared/include/behavior/ov_exec_network.hpp @@ -10,6 +10,7 @@ #include #include "base/ov_behavior_test_utils.hpp" +#include #include "common_test_utils/test_constants.hpp" #include "cpp/ie_cnn_network.h" #include "ie_core.hpp" @@ -18,7 +19,6 @@ #include "openvino/core/except.hpp" #include "openvino/opsets/opset8.hpp" #include "openvino/runtime/runtime.hpp" -#include "transformations/serialize.hpp" #include "functional_test_utils/plugin_cache.hpp" namespace ov { @@ -167,7 +167,7 @@ TEST_P(OVExecNetwork, precisionsAsInOriginalIR) { const std::string m_out_xml_path_1 = "precisionsAsInOriginalIR.xml"; const std::string m_out_bin_path_1 = "precisionsAsInOriginalIR.bin"; - ngraph::pass::Serialize(m_out_xml_path_1, m_out_bin_path_1).run_on_function(function); + ov::pass::Serialize(m_out_xml_path_1, m_out_bin_path_1).run_on_function(function); ov::runtime::ExecutableNetwork execNet; execNet = ie->compile_model(m_out_xml_path_1, targetDevice, configuration); diff --git a/inference-engine/tests/functional/shared_test_classes/src/base/layer_test_utils.cpp b/inference-engine/tests/functional/shared_test_classes/src/base/layer_test_utils.cpp index fe618a1ef72..8fad3196f25 100644 --- a/inference-engine/tests/functional/shared_test_classes/src/base/layer_test_utils.cpp +++ b/inference-engine/tests/functional/shared_test_classes/src/base/layer_test_utils.cpp @@ -11,7 +11,7 @@ #include "pugixml.hpp" -#include +#include #include #include "ngraph/variant.hpp" @@ -103,7 +103,7 @@ void LayerTestsCommon::Serialize() { std::string out_bin_path = output_name + ".bin"; ngraph::pass::Manager manager; - manager.register_pass(out_xml_path, out_bin_path); + manager.register_pass(out_xml_path, out_bin_path); manager.run_passes(function); function->validate_nodes_and_infer_types(); diff --git a/inference-engine/tests/functional/shared_test_classes/src/single_layer/memory.cpp b/inference-engine/tests/functional/shared_test_classes/src/single_layer/memory.cpp index 874ae4823db..d24e579906b 100644 --- a/inference-engine/tests/functional/shared_test_classes/src/single_layer/memory.cpp +++ b/inference-engine/tests/functional/shared_test_classes/src/single_layer/memory.cpp @@ -5,7 +5,6 @@ #include #include #include -#include #include #include "ngraph/opsets/opset7.hpp" #include "ngraph_functions/builders.hpp" diff --git a/inference-engine/tests/ie_test_utils/functional_test_utils/src/test_model/test_model.cpp b/inference-engine/tests/ie_test_utils/functional_test_utils/src/test_model/test_model.cpp index f9c908e8913..33abbf92997 100644 --- a/inference-engine/tests/ie_test_utils/functional_test_utils/src/test_model/test_model.cpp +++ b/inference-engine/tests/ie_test_utils/functional_test_utils/src/test_model/test_model.cpp @@ -12,7 +12,7 @@ #include "functional_test_utils/precision_utils.hpp" #include #include -#include "transformations/serialize.hpp" +#include "openvino/pass/serialize.hpp" #include "ie_ngraph_utils.hpp" namespace FuncTestUtils { @@ -31,7 +31,7 @@ void generateTestModel(const std::string &modelPath, const InferenceEngine::Precision &netPrc, const InferenceEngine::SizeVector &inputDims) { ngraph::pass::Manager manager; - manager.register_pass(modelPath, weightsPath); + manager.register_pass(modelPath, weightsPath); manager.run_passes(ngraph::builder::subgraph::makeConvPoolRelu( inputDims, InferenceEngine::details::convertPrecision(netPrc))); } diff --git a/ngraph/core/CMakeLists.txt b/ngraph/core/CMakeLists.txt index e60f5637705..892ffbb5114 100644 --- a/ngraph/core/CMakeLists.txt +++ b/ngraph/core/CMakeLists.txt @@ -61,7 +61,7 @@ endif() addVersionDefines(src/version.cpp CI_BUILD_NUMBER) -target_link_libraries(ngraph PRIVATE ngraph::builder ngraph::reference openvino::util ov_shape_inference) +target_link_libraries(ngraph PRIVATE ngraph::builder ngraph::reference openvino::util pugixml::static ov_shape_inference) ie_mark_target_as_cc(ngraph) diff --git a/ngraph/core/include/ngraph/pass/serialize.hpp b/ngraph/core/include/ngraph/pass/serialize.hpp new file mode 100644 index 00000000000..7cc233b6ded --- /dev/null +++ b/ngraph/core/include/ngraph/pass/serialize.hpp @@ -0,0 +1,15 @@ +// Copyright (C) 2018-2021 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#pragma once + +#include "ngraph/pass/pass.hpp" +#include "openvino/pass/serialize.hpp" + +namespace ngraph { +namespace pass { +using ov::pass::Serialize; +using ov::pass::StreamSerialize; +} // namespace pass +} // namespace ngraph diff --git a/ngraph/core/include/openvino/core/attribute_adapter.hpp b/ngraph/core/include/openvino/core/attribute_adapter.hpp index 1113f9c7c05..fdf15baa7ae 100644 --- a/ngraph/core/include/openvino/core/attribute_adapter.hpp +++ b/ngraph/core/include/openvino/core/attribute_adapter.hpp @@ -4,6 +4,7 @@ #pragma once +#include #include #include #include @@ -422,4 +423,11 @@ public: OPENVINO_RTTI("AttributeAdapter"); BWDCMP_RTTI_DECLARATION; }; + +template <> +class OPENVINO_API AttributeAdapter> : public DirectValueAccessor> { +public: + OPENVINO_RTTI("AttributeAdapter>"); + AttributeAdapter(std::set& value) : DirectValueAccessor>(value) {} +}; } // namespace ov diff --git a/ngraph/core/include/openvino/core/variant.hpp b/ngraph/core/include/openvino/core/variant.hpp index 450a517d456..2337f063990 100644 --- a/ngraph/core/include/openvino/core/variant.hpp +++ b/ngraph/core/include/openvino/core/variant.hpp @@ -60,6 +60,7 @@ protected: extern template class OPENVINO_API VariantImpl; extern template class OPENVINO_API VariantImpl; +extern template class OPENVINO_API VariantImpl; template class VariantWrapper {}; diff --git a/ngraph/core/include/openvino/pass/constant_folding.hpp b/ngraph/core/include/openvino/pass/constant_folding.hpp index d1eb93022ad..462d9916cb5 100644 --- a/ngraph/core/include/openvino/pass/constant_folding.hpp +++ b/ngraph/core/include/openvino/pass/constant_folding.hpp @@ -4,6 +4,7 @@ #pragma once +#include "openvino/core/variant.hpp" #include "openvino/pass/pass.hpp" namespace ov { @@ -24,5 +25,24 @@ private: /// upper estimations are equal. Traverses graph backwards starting from the results. bool pre_calculated_values_folding(const std::shared_ptr& f); }; + +OPENVINO_API void disable_constant_folding(const std::shared_ptr& node); + +OPENVINO_API void enable_constant_folding(const std::shared_ptr& node); + +OPENVINO_API bool constant_folding_is_disabled(const std::shared_ptr& node); + +class OPENVINO_API DisableConstantFolding : public VariantImpl { +public: + OPENVINO_RTTI("disabled_constant_folding", "0"); + + DisableConstantFolding() = default; + + DisableConstantFolding(const value_type& value) : VariantImpl(value) {} + + bool is_copyable() const override { + return false; + } +}; } // namespace pass } // namespace ov diff --git a/ngraph/core/include/openvino/pass/serialize.hpp b/ngraph/core/include/openvino/pass/serialize.hpp new file mode 100644 index 00000000000..c964d74c5f2 --- /dev/null +++ b/ngraph/core/include/openvino/pass/serialize.hpp @@ -0,0 +1,90 @@ +// Copyright (C) 2018-2021 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#pragma once + +#include +#include + +#include "ngraph/opsets/opset.hpp" +#include "openvino/core/function.hpp" +#include "openvino/pass/pass.hpp" + +namespace ov { +namespace pass { + +/** + * @brief Serialize transformation converts ngraph::Function into IR files + * @attention + * - dynamic shapes are not supported + * - order of generated layers in xml file is ngraph specific (given by + * get_ordered_ops()); MO generates file with different order, but they are + * logically equivalent + */ +class OPENVINO_API Serialize : public ov::pass::FunctionPass { +public: + OPENVINO_RTTI("Serialize"); + + enum class Version : uint8_t { + UNSPECIFIED = 0, // Use the latest or function version + IR_V10 = 10, // v10 IR + IR_V11 = 11 // v11 IR + }; + bool run_on_function(std::shared_ptr f) override; + + Serialize(std::ostream& xmlFile, + std::ostream& binFile, + std::map custom_opsets, + Version version = Version::UNSPECIFIED); + Serialize(std::ostream& xmlFile, std::ostream& binFile, Version version = Version::UNSPECIFIED); + + Serialize(const std::string& xmlPath, + const std::string& binPath, + std::map custom_opsets, + Version version = Version::UNSPECIFIED); + Serialize(const std::string& xmlPath, const std::string& binPath, Version version = Version::UNSPECIFIED); + +private: + std::ostream* m_xmlFile; + std::ostream* m_binFile; + const std::string m_xmlPath; + const std::string m_binPath; + const Version m_version; + const std::map m_custom_opsets; +}; + +/** + * @brief StreamSerialize transformation converts ngraph::Function into single binary stream + * @attention + * - dynamic shapes are not supported + */ +class OPENVINO_API StreamSerialize : public ov::pass::FunctionPass { +public: + OPENVINO_RTTI("StreamSerialize"); + + struct DataHeader { + size_t custom_data_offset; + size_t custom_data_size; + size_t consts_offset; + size_t consts_size; + size_t model_offset; + size_t model_size; + }; + + bool run_on_function(std::shared_ptr f) override; + + StreamSerialize(std::ostream& stream, + std::map&& custom_opsets = {}, + const std::function& custom_data_serializer = {}, + Serialize::Version version = Serialize::Version::UNSPECIFIED); + +private: + std::ostream& m_stream; + std::map m_custom_opsets; + std::function m_custom_data_serializer; + const Serialize::Version m_version; +}; + +} // namespace pass +} // namespace ov diff --git a/ngraph/core/src/pass/constant_folding.cpp b/ngraph/core/src/pass/constant_folding.cpp index 9ef0c28d327..8101f25dc12 100644 --- a/ngraph/core/src/pass/constant_folding.cpp +++ b/ngraph/core/src/pass/constant_folding.cpp @@ -119,3 +119,18 @@ bool ngraph::pass::ConstantFolding::pre_calculated_values_folding(const std::sha } return rewritten; } + +void ov::pass::disable_constant_folding(const std::shared_ptr& node) { + auto& rt_info = node->get_rt_info(); + rt_info[DisableConstantFolding::get_type_info_static()] = std::make_shared(true); +} + +void ov::pass::enable_constant_folding(const std::shared_ptr& node) { + auto& rt_info = node->get_rt_info(); + rt_info.erase(DisableConstantFolding::get_type_info_static()); +} + +bool ov::pass::constant_folding_is_disabled(const std::shared_ptr& node) { + const auto& rt_info = node->get_rt_info(); + return rt_info.count(DisableConstantFolding::get_type_info_static()); +} diff --git a/inference-engine/src/transformations/src/transformations/serialize.cpp b/ngraph/core/src/pass/serialize.cpp similarity index 71% rename from inference-engine/src/transformations/src/transformations/serialize.cpp rename to ngraph/core/src/pass/serialize.cpp index eb109e14483..92b224f0c38 100644 --- a/inference-engine/src/transformations/src/transformations/serialize.cpp +++ b/ngraph/core/src/pass/serialize.cpp @@ -2,30 +2,26 @@ // SPDX-License-Identifier: Apache-2.0 // -#include "itt.hpp" +#include "openvino/pass/serialize.hpp" + #include #include #include #include +#include #include #include -#include -#include +#include "itt.hpp" #include "ngraph/ops.hpp" #include "ngraph/opsets/opset.hpp" #include "ngraph/opsets/opset1.hpp" #include "openvino/op/util/framework_node.hpp" -#include "ngraph_ops/type_relaxed.hpp" +#include "openvino/pass/constant_folding.hpp" #include "pugixml.hpp" -#include "transformations/serialize.hpp" -#include "transformations/rt_info/attributes.hpp" using namespace ngraph; -NGRAPH_RTTI_DEFINITION(ngraph::pass::Serialize, "Serialize", 0); -NGRAPH_RTTI_DEFINITION(ngraph::pass::StreamSerialize, "StreamSerialize", 0); - namespace { // helpers template std::string join(const Container& c, const char* glue = ", ") { @@ -49,11 +45,10 @@ struct Edge { // convention. Most of them are the same, but there are exceptions, e.g // Constant (ngraph name) and Const (IR name). If there will be more // discrepancies discovered, translations needs to be added here. -const std::unordered_map translate_type_name_translator = { - {"Constant", "Const"}, - {"PRelu", "PReLU"}, - {"Relu", "ReLU"}, - {"Softmax", "SoftMax"}}; +const std::unordered_map translate_type_name_translator = {{"Constant", "Const"}, + {"PRelu", "PReLU"}, + {"Relu", "ReLU"}, + {"Softmax", "SoftMax"}}; std::string translate_type_name(const std::string& name) { auto found = translate_type_name_translator.find(name); @@ -84,13 +79,12 @@ class ConstantWriter { public: using FilePosition = int64_t; using HashValue = size_t; - using ConstWritePositions = std::unordered_map>; + using ConstWritePositions = std::unordered_map>; ConstantWriter(std::ostream& bin_data, bool enable_compression = true) - : m_binary_output(bin_data) - , m_enable_compression(enable_compression) - , m_blob_offset(bin_data.tellp()) { - } + : m_binary_output(bin_data), + m_enable_compression(enable_compression), + m_blob_offset(bin_data.tellp()) {} FilePosition write(const char* ptr, size_t size) { const FilePosition write_pos = m_binary_output.tellp(); @@ -111,7 +105,7 @@ public: } m_binary_output.write(ptr, size); - m_hash_to_file_positions.insert({hash, {offset, static_cast(ptr)}}); + m_hash_to_file_positions.insert({hash, {offset, static_cast(ptr)}}); return offset; } @@ -120,7 +114,7 @@ private: ConstWritePositions m_hash_to_file_positions; std::ostream& m_binary_output; bool m_enable_compression; - FilePosition m_blob_offset; // blob offset inside output stream + FilePosition m_blob_offset; // blob offset inside output stream }; void ngfunction_2_ir(pugi::xml_node& node, @@ -133,8 +127,8 @@ void ngfunction_2_ir(pugi::xml_node& node, // that allows such operators to be serialized with proper opsets. // If new operators are discovered that have the same problem, the mapping // needs to be updated here. The keys contain op name and version in NodeTypeInfo. -const std::unordered_map - special_operator_to_opset_assignments = {{ngraph::Node::type_info_t("ShuffleChannels", 0), "opset3"}}; +const std::unordered_map special_operator_to_opset_assignments = { + {ngraph::Node::type_info_t("ShuffleChannels", 0), "opset3"}}; std::string get_special_opset_for_op(const ngraph::Node::type_info_t& type_info) { auto found = special_operator_to_opset_assignments.find(type_info); @@ -145,20 +139,18 @@ std::string get_special_opset_for_op(const ngraph::Node::type_info_t& type_info) } namespace rt_info { -const std::vector list_of_names { +const std::vector list_of_names{ "PrimitivesPriority", "alt_width", }; class XmlSerializer { public: - explicit XmlSerializer(pugi::xml_node &xml_node) - : m_xml_node(xml_node) { - } + explicit XmlSerializer(pugi::xml_node& xml_node) : m_xml_node(xml_node) {} void serialize(const ngraph::Node::RTMap& rt_info) { for (const auto& rt_info_name : list_of_names) { - const auto &found_rt_info = rt_info.find(rt_info_name); + const auto& found_rt_info = rt_info.find(rt_info_name); if (found_rt_info != rt_info.end()) { xml_node_append_attribute(rt_info_name, found_rt_info->second); } @@ -166,10 +158,9 @@ public: } private: - template - void xml_node_append_attribute(const std::string& name, - const std::shared_ptr& variant) { - if ( auto v = std::dynamic_pointer_cast>(variant) ) { + template + void xml_node_append_attribute(const std::string& name, const std::shared_ptr& variant) { + if (auto v = std::dynamic_pointer_cast>(variant)) { const auto& value = v->get(); m_xml_node.append_attribute(name.c_str()).set_value(value.c_str()); } @@ -184,77 +175,77 @@ class RTInfoSerializer : public ngraph::AttributeVisitor { public: RTInfoSerializer(const pugi::xml_node node) : m_node(node) {} - void on_adapter(const std::string &name, ngraph::ValueAccessor &adapter) override { + void on_adapter(const std::string& name, ngraph::ValueAccessor& adapter) override { check_attribute_name(name); - if (auto a = ngraph::as_type>>(&adapter)) { - const auto & value = join(a->get()); + if (auto a = ov::as_type>>(&adapter)) { + const auto& value = join(a->get()); m_node.append_attribute(name.c_str()).set_value(value.c_str()); } else { throw ngraph_error("Unsupported attribute type for serialization: " + name); } } - void on_adapter(const std::string &name, ngraph::ValueAccessor &adapter) override { + void on_adapter(const std::string& name, ngraph::ValueAccessor& adapter) override { check_attribute_name(name); m_node.append_attribute(name.c_str()).set_value(adapter.get()); } - void on_adapter(const std::string &name, ngraph::ValueAccessor &adapter) override { + void on_adapter(const std::string& name, ngraph::ValueAccessor& adapter) override { check_attribute_name(name); m_node.append_attribute(name.c_str()).set_value(adapter.get().c_str()); } - void on_adapter(const std::string &name, ngraph::ValueAccessor &adapter) override { + void on_adapter(const std::string& name, ngraph::ValueAccessor& adapter) override { check_attribute_name(name); m_node.append_attribute(name.c_str()).set_value(adapter.get()); } - void on_adapter(const std::string &name, ngraph::ValueAccessor &adapter) override { + void on_adapter(const std::string& name, ngraph::ValueAccessor& adapter) override { check_attribute_name(name); m_node.append_attribute(name.c_str()).set_value(adapter.get()); } - void on_adapter(const std::string &name, ngraph::ValueAccessor> &adapter) override { + void on_adapter(const std::string& name, ngraph::ValueAccessor>& adapter) override { check_attribute_name(name); - const auto & value = join(adapter.get()); + const auto& value = join(adapter.get()); m_node.append_attribute(name.c_str()).set_value(value.c_str()); } - void on_adapter(const std::string &name, ngraph::ValueAccessor> &adapter) override { + void on_adapter(const std::string& name, ngraph::ValueAccessor>& adapter) override { check_attribute_name(name); - const auto & value = join(adapter.get()); + const auto& value = join(adapter.get()); m_node.append_attribute(name.c_str()).set_value(value.c_str()); } - void on_adapter(const std::string &name, ngraph::ValueAccessor> &adapter) override { + void on_adapter(const std::string& name, ngraph::ValueAccessor>& adapter) override { check_attribute_name(name); - const auto & value = join(adapter.get()); + const auto& value = join(adapter.get()); m_node.append_attribute(name.c_str()).set_value(value.c_str()); } - void on_adapter(const std::string &name, ngraph::ValueAccessor> &adapter) override { + void on_adapter(const std::string& name, ngraph::ValueAccessor>& adapter) override { check_attribute_name(name); - const auto & value = join(adapter.get()); + const auto& value = join(adapter.get()); m_node.append_attribute(name.c_str()).set_value(value.c_str()); } - void on_adapter(const std::string &name, ngraph::ValueAccessor> &adapter) override { + void on_adapter(const std::string& name, ngraph::ValueAccessor>& adapter) override { check_attribute_name(name); - const auto & value = join(adapter.get()); + const auto& value = join(adapter.get()); m_node.append_attribute(name.c_str()).set_value(value.c_str()); } - void on_adapter(const std::string &name, ngraph::ValueAccessor> &adapter) override { + void on_adapter(const std::string& name, ngraph::ValueAccessor>& adapter) override { throw ngraph_error("Function type is unsupported for rt info serialization"); } - void check_attribute_name(const std::string & name) const { + void check_attribute_name(const std::string& name) const { if (name == "name" || name == "version") { throw ngraph_error("Attribute key with name: " + name + " is not allowed. Please use another name"); } } }; -} // namespace rt_info +} // namespace rt_info class XmlSerializer : public ngraph::AttributeVisitor { pugi::xml_node& m_xml_node; @@ -264,13 +255,13 @@ class XmlSerializer : public ngraph::AttributeVisitor { int64_t m_version; template - std::string create_atribute_list( - ngraph::ValueAccessor>& adapter) { + std::string create_atribute_list(ngraph::ValueAccessor>& adapter) { return join(adapter.get()); } std::vector map_type_from_body(const pugi::xml_node& xml_node, - const std::string& map_type, const std::string& body_name = "body") { + const std::string& map_type, + const std::string& body_name = "body") { std::vector output; for (pugi::xml_node node : xml_node.child(body_name.c_str()).child("layers")) { if (!map_type.compare(node.attribute("type").value())) { @@ -284,11 +275,12 @@ class XmlSerializer : public ngraph::AttributeVisitor { return output; } - void input_descriptions_on_adapter(const std::vector>& input_descriptions, - const std::vector& parameter_mapping, - const std::vector& result_mapping, - pugi::xml_node& port_map, const std::string& portmap_name) { + void input_descriptions_on_adapter( + const std::vector>& input_descriptions, + const std::vector& parameter_mapping, + const std::vector& result_mapping, + pugi::xml_node& port_map, + const std::string& portmap_name) { NGRAPH_CHECK(!parameter_mapping.empty(), "No parameters found in body Function."); if (!m_xml_node.parent().child(portmap_name.c_str())) { @@ -298,31 +290,36 @@ class XmlSerializer : public ngraph::AttributeVisitor { for (const auto& input_description : input_descriptions) { pugi::xml_node input = port_map.append_child("input"); input.append_attribute("external_port_id").set_value(input_description->m_input_index); - input.append_attribute("internal_layer_id").set_value(parameter_mapping[input_description->m_body_parameter_index].c_str()); + input.append_attribute("internal_layer_id") + .set_value(parameter_mapping[input_description->m_body_parameter_index].c_str()); - if (auto slice_input = ov::as_type_ptr(input_description)) { + if (auto slice_input = + ov::as_type_ptr(input_description)) { input.prepend_attribute("axis").set_value(slice_input->m_axis); input.append_attribute("start").set_value(slice_input->m_start); input.append_attribute("end").set_value(slice_input->m_end); input.append_attribute("stride").set_value(slice_input->m_stride); input.append_attribute("part_size").set_value(slice_input->m_part_size); - } else if (auto merged_input = ov::as_type_ptr(input_description)) { + } else if (auto merged_input = + ov::as_type_ptr(input_description)) { pugi::xml_node back_edges = m_xml_node.parent().child("back_edges"); if (!back_edges) { back_edges = m_xml_node.parent().insert_child_after("back_edges", port_map); } pugi::xml_node edge = back_edges.append_child("edge"); edge.append_attribute("from-layer").set_value(result_mapping[merged_input->m_body_value_index].c_str()); - edge.append_attribute("to-layer").set_value(parameter_mapping[merged_input->m_body_parameter_index].c_str()); + edge.append_attribute("to-layer") + .set_value(parameter_mapping[merged_input->m_body_parameter_index].c_str()); } } } - void output_descriptions_on_adapter(const std::vector>& output_descriptions, - const uint32_t& input_count, - const std::vector& result_mapping, - pugi::xml_node& port_map, const std::string& portmap_name) { + void output_descriptions_on_adapter( + const std::vector>& output_descriptions, + const uint32_t& input_count, + const std::vector& result_mapping, + pugi::xml_node& port_map, + const std::string& portmap_name) { NGRAPH_CHECK(!result_mapping.empty(), "No results found in body Function."); if (!port_map) { @@ -332,9 +329,11 @@ class XmlSerializer : public ngraph::AttributeVisitor { for (const auto& output_description : output_descriptions) { pugi::xml_node output = port_map.append_child("output"); output.append_attribute("external_port_id").set_value(input_count + output_description->m_output_index); - output.append_attribute("internal_layer_id").set_value(result_mapping[output_description->m_body_value_index].c_str()); + output.append_attribute("internal_layer_id") + .set_value(result_mapping[output_description->m_body_value_index].c_str()); - if (auto concat_output = ov::as_type_ptr(output_description)) { + if (auto concat_output = + ov::as_type_ptr(output_description)) { output.prepend_attribute("axis").set_value(concat_output->m_axis); output.append_attribute("start").set_value(concat_output->m_start); output.append_attribute("end").set_value(concat_output->m_end); @@ -345,22 +344,24 @@ class XmlSerializer : public ngraph::AttributeVisitor { } void special_body_ports_on_adapter(const ngraph::op::v5::Loop::SpecialBodyPorts& special_body_ports, - const std::vector& parameter_mapping, - const std::vector& result_mapping, - pugi::xml_node& port_map) { + const std::vector& parameter_mapping, + const std::vector& result_mapping, + pugi::xml_node& port_map) { NGRAPH_CHECK(port_map, "port_map section not found, purpose attribute cannot be added."); if (special_body_ports.current_iteration_input_idx != -1) { pugi::xml_node iter_input = port_map.append_child("input"); iter_input.append_attribute("external_port_id").set_value("-1"); - iter_input.append_attribute("internal_layer_id").set_value(parameter_mapping[special_body_ports.current_iteration_input_idx].c_str()); + iter_input.append_attribute("internal_layer_id") + .set_value(parameter_mapping[special_body_ports.current_iteration_input_idx].c_str()); iter_input.append_attribute("purpose").set_value("current_iteration"); } if (special_body_ports.body_condition_output_idx != -1) { pugi::xml_node exec_output = port_map.append_child("output"); exec_output.append_attribute("external_port_id").set_value("-1"); - exec_output.append_attribute("internal_layer_id").set_value(result_mapping[special_body_ports.body_condition_output_idx].c_str()); + exec_output.append_attribute("internal_layer_id") + .set_value(result_mapping[special_body_ports.body_condition_output_idx].c_str()); exec_output.append_attribute("purpose").set_value("execution_condition"); } } @@ -371,20 +372,19 @@ public: const std::map& custom_opsets, ConstantWriter& constant_write_handler, int64_t version) - : m_xml_node(data) - , m_node_type_name(node_type_name) - , m_custom_opsets(custom_opsets) - , m_constant_write_handler(constant_write_handler) - , m_version(version) { - } + : m_xml_node(data), + m_node_type_name(node_type_name), + m_custom_opsets(custom_opsets), + m_constant_write_handler(constant_write_handler), + m_version(version) {} void on_adapter(const std::string& name, ngraph::ValueAccessor& adapter) override { using BodyTargetNames = std::tuple>; const std::vector body_names = { - BodyTargetNames{"body", "port_map", {"input_descriptions", "output_descriptions", "special_body_ports"}}, - BodyTargetNames{"then_body", "then_port_map", {"then_inputs", "then_outputs"}}, - BodyTargetNames{"else_body", "else_port_map", {"else_inputs", "else_outputs"}} }; + BodyTargetNames{"body", "port_map", {"input_descriptions", "output_descriptions", "special_body_ports"}}, + BodyTargetNames{"then_body", "then_port_map", {"then_inputs", "then_outputs"}}, + BodyTargetNames{"else_body", "else_port_map", {"else_inputs", "else_outputs"}}}; BodyTargetNames bnames; bool is_body_target = false; for (const auto& _body_target : body_names) { @@ -402,40 +402,48 @@ public: auto body_name = std::get<0>(bnames); auto portmap_name = std::get<1>(bnames); std::vector result_mapping = map_type_from_body(m_xml_node.parent(), "Result", body_name); - std::vector parameter_mapping = map_type_from_body(m_xml_node.parent(), "Parameter", body_name); + std::vector parameter_mapping = + map_type_from_body(m_xml_node.parent(), "Parameter", body_name); pugi::xml_node port_map = m_xml_node.parent().child(portmap_name.c_str()); - NGRAPH_CHECK(!parameter_mapping.empty() || !result_mapping.empty(), "No parameters or results found in body Function."); + NGRAPH_CHECK(!parameter_mapping.empty() || !result_mapping.empty(), + "No parameters or results found in body Function."); // TI, Loop do not have attributtes as regular ops, it is necessary to append "port_map" and // "back_edges" to layer above (m_xml_node.parent()) as in ngfunction_2_ir() layer (here "m_xml_node") // with empty attributes is removed. - if (const auto& a = ngraph::as_type>>>(&adapter)) { + if (const auto& a = ngraph::as_type>>>(&adapter)) { input_descriptions_on_adapter(a->get(), parameter_mapping, result_mapping, port_map, portmap_name); - } else if (const auto& a = ngraph::as_type>>>(&adapter)) { + } else if (const auto& a = ngraph::as_type>>>( + &adapter)) { uint32_t op_input_count = 0; for (auto c = m_xml_node.parent().child("input").first_child(); !c.empty(); c = c.next_sibling()) { op_input_count++; } output_descriptions_on_adapter(a->get(), op_input_count, result_mapping, port_map, portmap_name); - } else if (const auto& a = ngraph::as_type>(&adapter)) { + } else if (const auto& a = + ngraph::as_type>( + &adapter)) { special_body_ports_on_adapter(a->get(), parameter_mapping, result_mapping, port_map); } - } else if (const auto& a = ngraph::as_type>>(&adapter)) { - m_xml_node.append_attribute(name.c_str()).set_value(a->get()->get_info().variable_id.c_str()); - } else if (const auto& a = ngraph::as_type>>(&adapter)) { - if (name == "value" && translate_type_name(m_node_type_name) == "Const") { + } else if (const auto& a = + ngraph::as_type>>(&adapter)) { + m_xml_node.append_attribute(name.c_str()).set_value(a->get()->get_info().variable_id.c_str()); + } else if (const auto& a = + ngraph::as_type>>( + &adapter)) { + if (name == "value" && translate_type_name(m_node_type_name) == "Const") { const int64_t size = a->get()->size(); - int64_t offset = m_constant_write_handler.write( - static_cast(a->get()->get_ptr()), size); + int64_t offset = m_constant_write_handler.write(static_cast(a->get()->get_ptr()), size); m_xml_node.append_attribute("offset").set_value(offset); m_xml_node.append_attribute("size").set_value(size); } - } else if (const auto& a = ngraph::as_type>(&adapter)) { - const auto & attrs = a->get(); + } else if (const auto& a = + ngraph::as_type>(&adapter)) { + const auto& attrs = a->get(); // Update type and version attributes pugi::xml_node layer = m_xml_node.parent(); @@ -452,67 +460,45 @@ public: } // Update node attributes in data field - for (const auto & attr : attrs) { + for (const auto& attr : attrs) { m_xml_node.append_attribute(attr.first.c_str()).set_value(attr.second.c_str()); } } else if (const auto& a = ngraph::as_type>(&adapter)) { - const auto & attrs = a->get(); + const auto& attrs = a->get(); m_xml_node.append_attribute(name.c_str()).set_value(join(attrs).c_str()); } else { throw ngraph_error("Unsupported attribute type for serialization: " + name); } } - void on_adapter(const std::string& name, - ngraph::ValueAccessor& adapter) override { + void on_adapter(const std::string& name, ngraph::ValueAccessor& adapter) override { m_xml_node.append_attribute(name.c_str()).set_value(adapter.get()); } - void on_adapter(const std::string& name, - ngraph::ValueAccessor& adapter) override { - m_xml_node.append_attribute(name.c_str()) - .set_value(adapter.get().c_str()); + void on_adapter(const std::string& name, ngraph::ValueAccessor& adapter) override { + m_xml_node.append_attribute(name.c_str()).set_value(adapter.get().c_str()); } - void on_adapter(const std::string& name, - ngraph::ValueAccessor& adapter) override { + void on_adapter(const std::string& name, ngraph::ValueAccessor& adapter) override { m_xml_node.append_attribute(name.c_str()).set_value(adapter.get()); } - void on_adapter(const std::string& name, - ngraph::ValueAccessor& adapter) override { + void on_adapter(const std::string& name, ngraph::ValueAccessor& adapter) override { m_xml_node.append_attribute(name.c_str()).set_value(adapter.get()); } - void on_adapter( - const std::string& name, - ngraph::ValueAccessor>& adapter) override { - m_xml_node.append_attribute(name.c_str()) - .set_value(create_atribute_list(adapter).c_str()); + void on_adapter(const std::string& name, ngraph::ValueAccessor>& adapter) override { + m_xml_node.append_attribute(name.c_str()).set_value(create_atribute_list(adapter).c_str()); } - void on_adapter( - const std::string& name, - ngraph::ValueAccessor>& adapter) override { - m_xml_node.append_attribute(name.c_str()) - .set_value(create_atribute_list(adapter).c_str()); + void on_adapter(const std::string& name, ngraph::ValueAccessor>& adapter) override { + m_xml_node.append_attribute(name.c_str()).set_value(create_atribute_list(adapter).c_str()); } - void on_adapter( - const std::string& name, - ngraph::ValueAccessor>& adapter) override { - m_xml_node.append_attribute(name.c_str()) - .set_value(create_atribute_list(adapter).c_str()); + void on_adapter(const std::string& name, ngraph::ValueAccessor>& adapter) override { + m_xml_node.append_attribute(name.c_str()).set_value(create_atribute_list(adapter).c_str()); } - void on_adapter( - const std::string& name, - ngraph::ValueAccessor>& adapter) override { - m_xml_node.append_attribute(name.c_str()) - .set_value(create_atribute_list(adapter).c_str()); + void on_adapter(const std::string& name, ngraph::ValueAccessor>& adapter) override { + m_xml_node.append_attribute(name.c_str()).set_value(create_atribute_list(adapter).c_str()); } - void on_adapter( - const std::string& name, - ngraph::ValueAccessor>& adapter) override { - m_xml_node.append_attribute(name.c_str()) - .set_value(create_atribute_list(adapter).c_str()); + void on_adapter(const std::string& name, ngraph::ValueAccessor>& adapter) override { + m_xml_node.append_attribute(name.c_str()).set_value(create_atribute_list(adapter).c_str()); } - void on_adapter( - const std::string& name, - ngraph::ValueAccessor>& adapter) override { + void on_adapter(const std::string& name, ngraph::ValueAccessor>& adapter) override { if (name == "body" || name == "then_body" || name == "else_body") { // TI, Loop do not have attributtes as regular ops, it is necessary to append "body" // to layer above (m_xml_node.parent()) as in ngfunction_2_ir() layer (m_xml_node) with empty attributes @@ -531,8 +517,7 @@ public: } }; -const std::unordered_map create_layer_ids( - const ngraph::Function& f) { +const std::unordered_map create_layer_ids(const ngraph::Function& f) { std::unordered_map layer_ids; int id = 0; for (const auto& node : f.get_ordered_ops()) { @@ -541,9 +526,8 @@ const std::unordered_map create_layer_ids( return layer_ids; } -const std::vector create_edge_mapping( - const std::unordered_map& layer_ids, - const ngraph::Function& f) { +const std::vector create_edge_mapping(const std::unordered_map& layer_ids, + const ngraph::Function& f) { std::vector edges; for (const auto& node : f.get_ordered_ops()) { if (ngraph::op::is_parameter(node)) { @@ -555,45 +539,43 @@ const std::vector create_edge_mapping( auto source_node = source_output.get_node(); auto current_node = i.get_node(); - NGRAPH_CHECK(layer_ids.find(source_node) != layer_ids.end(), - "Internal error"); - NGRAPH_CHECK(layer_ids.find(current_node) != layer_ids.end(), - "Internal error"); + NGRAPH_CHECK(layer_ids.find(source_node) != layer_ids.end(), "Internal error"); + NGRAPH_CHECK(layer_ids.find(current_node) != layer_ids.end(), "Internal error"); Edge e{}; e.from_layer = layer_ids.find(source_node)->second; - e.from_port = - source_node->get_input_size() + source_output.get_index(); + e.from_port = source_node->get_input_size() + source_output.get_index(); e.to_layer = layer_ids.find(current_node)->second; e.to_port = i.get_index(); edges.push_back(e); } } - std::sort(begin(edges), end(edges), - [](const Edge& a, const Edge& b) -> bool { - return a.from_layer < b.from_layer; - }); + std::sort(begin(edges), end(edges), [](const Edge& a, const Edge& b) -> bool { + return a.from_layer < b.from_layer; + }); return edges; } -std::string get_opset_name( - const ngraph::Node* n, - const std::map& custom_opsets) { +std::string get_opset_name(const ngraph::Node* n, const std::map& custom_opsets) { // Try to find opset name from RT info auto opset_it = n->get_rt_info().find("opset"); if (opset_it != n->get_rt_info().end()) { if (auto variant = std::dynamic_pointer_cast>(opset_it->second)) { - const std::string & opset_name = variant->get(); + const std::string& opset_name = variant->get(); if (custom_opsets.find(opset_name) != custom_opsets.end()) { return opset_name; } } } - auto opsets = std::array, 8>{ - ngraph::get_opset1(), ngraph::get_opset2(), ngraph::get_opset3(), - ngraph::get_opset4(), ngraph::get_opset5(), ngraph::get_opset6(), - ngraph::get_opset7(), ngraph::get_opset8()}; + auto opsets = std::array, 8>{ngraph::get_opset1(), + ngraph::get_opset2(), + ngraph::get_opset3(), + ngraph::get_opset4(), + ngraph::get_opset5(), + ngraph::get_opset6(), + ngraph::get_opset7(), + ngraph::get_opset8()}; auto special_opset = get_special_opset_for_op(n->get_type_info()); if (!special_opset.empty()) { @@ -617,7 +599,7 @@ std::string get_opset_name( return "experimental"; } -std::string get_precision_name(const ngraph::element::Type & elem_type) { +std::string get_precision_name(const ngraph::element::Type& elem_type) { switch (elem_type) { case ::ngraph::element::Type_t::undefined: case ::ngraph::element::Type_t::dynamic: @@ -672,9 +654,9 @@ std::string escape_delim(const std::string& name, const char delim = ',') { return result_name; } -std::string generate_unique_name( - const std::unordered_set& unique_names, const std::string& base_name, - int suffix) { +std::string generate_unique_name(const std::unordered_set& unique_names, + const std::string& base_name, + int suffix) { std::string new_name = base_name + std::to_string(suffix); if (unique_names.find(new_name) == unique_names.end()) { return new_name; @@ -685,8 +667,7 @@ std::string generate_unique_name( } // TODO: remove when CNNNetwork will be supporting not-unique names -std::string get_node_unique_name(std::unordered_set& unique_names, - const ngraph::Node* n) { +std::string get_node_unique_name(std::unordered_set& unique_names, const ngraph::Node* n) { std::string name = n->get_friendly_name(); if (unique_names.find(name) != unique_names.end()) { name = generate_unique_name(unique_names, name, 0); @@ -699,8 +680,8 @@ void visit_exec_graph_node(pugi::xml_node& layer, const ngraph::Node* n) { auto data = layer.child("data"); for (const auto& param : n->get_rt_info()) { if (auto variant = std::dynamic_pointer_cast>(param.second)) { - const std::string & name = param.first; - const std::string & value = variant->get(); + const std::string& name = param.first; + const std::string& value = variant->get(); if (name == "layerType") { layer.attribute("type").set_value(value.c_str()); @@ -733,21 +714,21 @@ bool has_dynamic_output(const std::shared_ptr& n) { } bool resolve_dynamic_shapes(const ngraph::Function& f) { - const auto & f_ops = f.get_ordered_ops(); - if (std::all_of(f_ops.begin(), f_ops.end(), - [](const std::shared_ptr& results) { - return !results->is_dynamic() && !has_dynamic_output(results); })) { + const auto& f_ops = f.get_ordered_ops(); + if (std::all_of(f_ops.begin(), f_ops.end(), [](const std::shared_ptr& results) { + return !results->is_dynamic() && !has_dynamic_output(results); + })) { return false; } auto f_clone = ngraph::clone_function(f); - const auto & f_clone_ops = f_clone->get_ordered_ops(); + const auto& f_clone_ops = f_clone->get_ordered_ops(); NGRAPH_CHECK(f_ops.size() == f_clone_ops.size(), "Unexpected get_ordered_ops method behaviour"); for (size_t id = 0; id < f_ops.size(); ++id) { - auto & op = f_ops[id]; - auto & clone_op = f_clone_ops[id]; - enable_constant_folding(clone_op); // to be able to fold ShapeOfs + auto& op = f_ops[id]; + auto& clone_op = f_clone_ops[id]; + ov::pass::enable_constant_folding(clone_op); // to be able to fold ShapeOfs if (auto op_subgraph = std::dynamic_pointer_cast(op)) { resolve_dynamic_shapes(*op_subgraph->get_function()); } @@ -757,12 +738,13 @@ bool resolve_dynamic_shapes(const ngraph::Function& f) { // dynamic_to_static function converts dynamic dimensions to static using // upperbound (get_max_length) dimension value. - auto dynamic_to_static = [&op](const PartialShape & shape) -> PartialShape { + auto dynamic_to_static = [&op](const PartialShape& shape) -> PartialShape { if (shape.is_static() || shape.rank().is_dynamic()) { return shape; } std::vector out_shape; - std::transform(std::begin(shape), std::end(shape), + std::transform(std::begin(shape), + std::end(shape), std::back_inserter(out_shape), [](const Dimension& d) -> Dimension { return d.get_max_length(); @@ -773,15 +755,18 @@ bool resolve_dynamic_shapes(const ngraph::Function& f) { OutputVector replacements(clone_op->get_output_size()); if (!clone_op->constant_fold(replacements, clone_op->input_values())) { for (size_t output_id = 0; output_id < clone_op->get_output_size(); ++output_id) { - clone_op->set_output_type(output_id, clone_op->output(output_id).get_element_type(), - dynamic_to_static(clone_op->output(output_id).get_partial_shape())); - op->set_output_type(output_id, clone_op->output(output_id).get_element_type(), - clone_op->output(output_id).get_partial_shape()); + clone_op->set_output_type(output_id, + clone_op->output(output_id).get_element_type(), + dynamic_to_static(clone_op->output(output_id).get_partial_shape())); + op->set_output_type(output_id, + clone_op->output(output_id).get_element_type(), + clone_op->output(output_id).get_partial_shape()); } } else { for (size_t output_id = 0; output_id < clone_op->get_output_size(); ++output_id) { - op->set_output_type(output_id, replacements[output_id].get_element_type(), - replacements[output_id].get_partial_shape()); + op->set_output_type(output_id, + replacements[output_id].get_element_type(), + replacements[output_id].get_partial_shape()); } for (size_t i = 0; i < replacements.size(); ++i) { @@ -798,10 +783,10 @@ bool resolve_dynamic_shapes(const ngraph::Function& f) { void auto_pad_resolving(ov::Node* node) { const std::set pad_agnostic_types = { - ov::op::PadType::SAME_LOWER, - ov::op::PadType::SAME_UPPER, - ov::op::PadType::VALID, - ov::op::PadType::AUTO, + ov::op::PadType::SAME_LOWER, + ov::op::PadType::SAME_UPPER, + ov::op::PadType::VALID, + ov::op::PadType::AUTO, }; if (auto op = as_type(node)) { if (pad_agnostic_types.count(op->get_auto_pad())) { @@ -855,8 +840,7 @@ void ngfunction_2_ir(pugi::xml_node& netXml, netXml.append_attribute("version").set_value(version); pugi::xml_node layers = netXml.append_child("layers"); - const std::unordered_map layer_ids = - create_layer_ids(f); + const std::unordered_map layer_ids = create_layer_ids(f); std::unordered_set unique_names; // TODO remove resolve_dynamic_shapes function completely when support for -1 will be implemented in the MO @@ -889,7 +873,7 @@ void ngfunction_2_ir(pugi::xml_node& netXml, for (const auto& n : sorted_ops) { ngraph::Node* node = n.get(); - const std::string & node_type_name{node->get_type_name()}; + const std::string& node_type_name{node->get_type_name()}; NGRAPH_CHECK(layer_ids.find(node) != layer_ids.end(), "Internal error"); // @@ -904,13 +888,14 @@ void ngfunction_2_ir(pugi::xml_node& netXml, // general attributes pugi::xml_node data = layer.append_child("data"); - auto append_runtime_info = [](pugi::xml_node & node, const RTMap& attributes) { + auto append_runtime_info = [](pugi::xml_node& node, const RTMap& attributes) { pugi::xml_node rt_node = node.append_child("rt_info"); bool has_attrs = false; - for (const auto &item : attributes) { + for (const auto& item : attributes) { auto attribute_node = rt_node.append_child("attribute"); attribute_node.append_attribute("name").set_value(item.second->get_type_info().name); - attribute_node.append_attribute("version").set_value(item.second->get_type_info().get_version().c_str()); + attribute_node.append_attribute("version").set_value( + item.second->get_type_info().get_version().c_str()); rt_info::RTInfoSerializer serializer(attribute_node); if (!item.second->visit_attributes(serializer)) { rt_node.remove_child(attribute_node); @@ -931,24 +916,23 @@ void ngfunction_2_ir(pugi::xml_node& netXml, // if (node->get_input_size() > 0) { pugi::xml_node input = layer.append_child("input"); - for (const auto & i : node->inputs()) { + for (const auto& i : node->inputs()) { // WA for LSTMCellv0, peephole input shall not be serialized - if (i.get_index() == 6 && dynamic_cast(node)) { + if (i.get_index() == 6 && dynamic_cast(node)) { port_id++; continue; } pugi::xml_node port = input.append_child("port"); port.append_attribute("id").set_value(port_id++); - port.append_attribute("precision") - .set_value(get_precision_name(i.get_element_type()).c_str()); + port.append_attribute("precision").set_value(get_precision_name(i.get_element_type()).c_str()); for (auto d : i.get_partial_shape()) { pugi::xml_node dim = port.append_child("dim"); if (d.is_dynamic()) { dim.append_child(pugi::xml_node_type::node_pcdata).set_value("-1"); } else { dim.append_child(pugi::xml_node_type::node_pcdata) - .set_value(std::to_string(d.get_length()).c_str()); + .set_value(std::to_string(d.get_length()).c_str()); } } if (version >= 11) @@ -962,11 +946,10 @@ void ngfunction_2_ir(pugi::xml_node& netXml, // if ((node->get_output_size() > 0) && !ngraph::op::is_output(node)) { pugi::xml_node output = layer.append_child("output"); - for (const auto & o : node->outputs()) { + for (const auto& o : node->outputs()) { pugi::xml_node port = output.append_child("port"); port.append_attribute("id").set_value(port_id++); - port.append_attribute("precision") - .set_value(get_precision_name(o.get_element_type()).c_str()); + port.append_attribute("precision").set_value(get_precision_name(o.get_element_type()).c_str()); // Sort tensor names // Skip autogenerated names @@ -976,7 +959,7 @@ void ngfunction_2_ir(pugi::xml_node& netXml, return false; return true; }; - const auto & tensor_names = o.get_tensor().get_names(); + const auto& tensor_names = o.get_tensor().get_names(); std::vector vector_names; for (const auto& name : tensor_names) { if (!autogenerated_name(name)) @@ -1000,7 +983,7 @@ void ngfunction_2_ir(pugi::xml_node& netXml, dim.append_child(pugi::xml_node_type::node_pcdata).set_value("-1"); } else { dim.append_child(pugi::xml_node_type::node_pcdata) - .set_value(std::to_string(d.get_length()).c_str()); + .set_value(std::to_string(d.get_length()).c_str()); } } if (version >= 11) @@ -1012,7 +995,7 @@ void ngfunction_2_ir(pugi::xml_node& netXml, } // fill general attributes - auto_pad_resolving(node); // Backward compatibility: clear padding values for nodes with auto_pad + auto_pad_resolving(node); // Backward compatibility: clear padding values for nodes with auto_pad XmlSerializer visitor(data, node_type_name, custom_opsets, constant_node_write_handler, version); NGRAPH_CHECK(node->visit_attributes(visitor), "Visitor API is not supported in ", node); rt_info::XmlSerializer{data}.serialize(node->get_rt_info()); @@ -1021,8 +1004,7 @@ void ngfunction_2_ir(pugi::xml_node& netXml, visit_exec_graph_node(layer, node); } - const bool data_attr_size = - data.attributes().begin() == data.attributes().end(); + const bool data_attr_size = data.attributes().begin() == data.attributes().end(); if (data_attr_size) { layer.remove_child(data); } @@ -1050,24 +1032,23 @@ void ngfunction_2_ir(pugi::xml_node& netXml, } } -std::string valid_xml_path(const std::string &path) { +std::string valid_xml_path(const std::string& path) { NGRAPH_CHECK(path.length() > 4, "Path for xml file is to short: \"" + path + "\""); - const char *const extension = ".xml"; + const char* const extension = ".xml"; const bool has_xml_extension = path.rfind(extension) == path.size() - std::strlen(extension); NGRAPH_CHECK(has_xml_extension, - "Path for xml file doesn't contains file name with 'xml' extension: \"" + - path + "\""); + "Path for xml file doesn't contains file name with 'xml' extension: \"" + path + "\""); return path; } -std::string provide_bin_path(const std::string &xmlPath, const std::string &binPath) { +std::string provide_bin_path(const std::string& xmlPath, const std::string& binPath) { if (!binPath.empty()) { return binPath; } - assert(xmlPath.size() > 4); // should be check by valid_xml_path + assert(xmlPath.size() > 4); // should be check by valid_xml_path std::string bestPath = xmlPath; - const char *const extension = "bin"; + const char* const extension = "bin"; const auto ext_size = std::strlen(extension); bestPath.replace(bestPath.size() - ext_size, ext_size, extension); return bestPath; @@ -1075,11 +1056,9 @@ std::string provide_bin_path(const std::string &xmlPath, const std::string &binP } // namespace -namespace ngraph { +namespace ov { bool pass::Serialize::run_on_function(std::shared_ptr f) { - RUN_ON_FUNCTION_SCOPE(Serialize); - - auto serializeFunc = [&] (std::ostream & xml_file, std::ostream & bin_file) { + auto serializeFunc = [&](std::ostream& xml_file, std::ostream& bin_file) { auto version = static_cast(m_version); auto& rt_info = f->get_rt_info(); @@ -1088,7 +1067,6 @@ bool pass::Serialize::run_on_function(std::shared_ptr f) { version = version_var->get(); } - if (version != static_cast(m_version) && m_version != Serialize::Version::UNSPECIFIED) throw ngraph_error("Cannot serialize function to incompatible IR version"); @@ -1141,53 +1119,46 @@ bool pass::Serialize::run_on_function(std::shared_ptr f) { pass::Serialize::Serialize(std::ostream& xmlFile, std::ostream& binFile, - std::map custom_opsets, + std::map custom_opsets, pass::Serialize::Version version) - : m_xmlFile{&xmlFile} - , m_binFile{&binFile} - , m_xmlPath{} - , m_binPath{} - , m_version{version} - , m_custom_opsets{custom_opsets} -{ -} -pass::Serialize::Serialize(std::ostream& xmlFile, - std::ostream& binFile, - pass::Serialize::Version version) - : pass::Serialize::Serialize(xmlFile, binFile, std::map{}, version) {} + : m_xmlFile{&xmlFile}, + m_binFile{&binFile}, + m_xmlPath{}, + m_binPath{}, + m_version{version}, + m_custom_opsets{custom_opsets} {} +pass::Serialize::Serialize(std::ostream& xmlFile, std::ostream& binFile, pass::Serialize::Version version) + : pass::Serialize::Serialize(xmlFile, binFile, std::map{}, version) {} pass::Serialize::Serialize(const std::string& xmlPath, const std::string& binPath, - std::map custom_opsets, + std::map custom_opsets, pass::Serialize::Version version) - : m_xmlFile{nullptr} - , m_binFile{nullptr} - , m_xmlPath{valid_xml_path(xmlPath)} - , m_binPath{provide_bin_path(xmlPath, binPath)} - , m_version{version} - , m_custom_opsets{custom_opsets} -{ -} -pass::Serialize::Serialize(const std::string& xmlPath, - const std::string& binPath, - pass::Serialize::Version version) - : pass::Serialize::Serialize(xmlPath, binPath, std::map{}, version) {} + : m_xmlFile{nullptr}, + m_binFile{nullptr}, + m_xmlPath{valid_xml_path(xmlPath)}, + m_binPath{provide_bin_path(xmlPath, binPath)}, + m_version{version}, + m_custom_opsets{custom_opsets} {} -ngraph::pass::StreamSerialize::StreamSerialize(std::ostream & stream, - std::map && custom_opsets, - const std::function & custom_data_serializer, - Serialize::Version version) - : m_stream(stream) - , m_custom_opsets(std::move(custom_opsets)) - , m_custom_data_serializer(custom_data_serializer) - , m_version(version) { +pass::Serialize::Serialize(const std::string& xmlPath, const std::string& binPath, pass::Serialize::Version version) + : pass::Serialize::Serialize(xmlPath, binPath, std::map{}, version) {} + +pass::StreamSerialize::StreamSerialize(std::ostream& stream, + std::map&& custom_opsets, + const std::function& custom_data_serializer, + Serialize::Version version) + : m_stream(stream), + m_custom_opsets(std::move(custom_opsets)), + m_custom_data_serializer(custom_data_serializer), + m_version(version) { if (version != Serialize::Version::UNSPECIFIED && version != Serialize::Version::IR_V10 && version != Serialize::Version::IR_V11) { throw ngraph_error("Unsupported version"); } } -bool ngraph::pass::StreamSerialize::run_on_function(std::shared_ptr f) { +bool pass::StreamSerialize::run_on_function(std::shared_ptr f) { /* Format: [ DataHeader ] @@ -1197,7 +1168,7 @@ bool ngraph::pass::StreamSerialize::run_on_function(std::shared_ptr(m_version); @@ -1252,4 +1223,4 @@ bool ngraph::pass::StreamSerialize::run_on_function(std::shared_ptr; template class ngraph::VariantImpl; +template class ngraph::VariantImpl; diff --git a/ngraph/test/CMakeLists.txt b/ngraph/test/CMakeLists.txt index 2d7f88f0b7a..4e35aba468d 100644 --- a/ngraph/test/CMakeLists.txt +++ b/ngraph/test/CMakeLists.txt @@ -83,6 +83,11 @@ set(SRC partial_shape.cpp pass_config.cpp pass_manager.cpp + pass/serialization/cleanup.cpp + pass/serialization/const_compression.cpp + pass/serialization/deterministicity.cpp + pass/serialization/rt_info_serialization.cpp + pass/serialization/serialize.cpp pattern.cpp preprocess.cpp provenance.cpp @@ -603,6 +608,8 @@ endforeach() add_executable(unit-test ${SRC}) +add_dependencies(unit-test template_extension) + target_include_directories(unit-test PRIVATE ".") target_include_directories(unit-test PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/runtime) @@ -619,6 +626,8 @@ endif() target_link_libraries(unit-test PRIVATE ngraph_test_util engines_test_util ngraph::builder + openvino::util + pugixml::static ${CMAKE_DL_LIBS} ie_backend interpreter_backend @@ -638,6 +647,7 @@ if (NGRAPH_ONNX_FRONTEND_ENABLE) ${Protobuf_INCLUDE_DIRS}) target_compile_definitions(unit-test PRIVATE $) + target_compile_definitions(unit-test PRIVATE NGRAPH_ONNX_FRONTEND_ENABLE) endif() if (OV_COMPILER_IS_CLANG) diff --git a/inference-engine/tests/functional/inference_engine/ir_serialization/models/add_abc.prototxt b/ngraph/test/models/ir/add_abc.prototxt similarity index 100% rename from inference-engine/tests/functional/inference_engine/ir_serialization/models/add_abc.prototxt rename to ngraph/test/models/ir/add_abc.prototxt diff --git a/ngraph/test/models/ir/add_abc.xml b/ngraph/test/models/ir/add_abc.xml index bb9c0b07c6f..607c9673c4c 100644 --- a/ngraph/test/models/ir/add_abc.xml +++ b/ngraph/test/models/ir/add_abc.xml @@ -18,7 +18,6 @@ - 1 @@ -42,7 +41,6 @@ - 1 diff --git a/inference-engine/tests/functional/inference_engine/ir_serialization/models/add_abc_bin.xml b/ngraph/test/models/ir/add_abc_bin.xml similarity index 100% rename from inference-engine/tests/functional/inference_engine/ir_serialization/models/add_abc_bin.xml rename to ngraph/test/models/ir/add_abc_bin.xml diff --git a/inference-engine/tests/functional/inference_engine/ir_serialization/models/add_abc_f64.xml b/ngraph/test/models/ir/add_abc_f64.xml similarity index 100% rename from inference-engine/tests/functional/inference_engine/ir_serialization/models/add_abc_f64.xml rename to ngraph/test/models/ir/add_abc_f64.xml diff --git a/ngraph/test/models/ir/add_abc_initializers.bin b/ngraph/test/models/ir/add_abc_initializers.bin new file mode 100644 index 00000000000..e3389a807c2 Binary files /dev/null and b/ngraph/test/models/ir/add_abc_initializers.bin differ diff --git a/inference-engine/tests/functional/inference_engine/ir_serialization/models/add_abc_initializers.prototxt b/ngraph/test/models/ir/add_abc_initializers.prototxt similarity index 100% rename from inference-engine/tests/functional/inference_engine/ir_serialization/models/add_abc_initializers.prototxt rename to ngraph/test/models/ir/add_abc_initializers.prototxt diff --git a/ngraph/test/models/ir/add_abc_initializers.xml b/ngraph/test/models/ir/add_abc_initializers.xml new file mode 100644 index 00000000000..cfaebc7d2f9 --- /dev/null +++ b/ngraph/test/models/ir/add_abc_initializers.xml @@ -0,0 +1,93 @@ + + + + + + + + 2 + 2 + + + + + + + + 2 + 2 + + + + + + + 2 + 2 + + + 2 + 2 + + + + + 2 + 2 + + + + + + + 2 + 2 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/inference-engine/tests/functional/inference_engine/ir_serialization/models/add_abc_initializers_f32_nan_const.bin b/ngraph/test/models/ir/add_abc_initializers_f32_nan_const.bin similarity index 100% rename from inference-engine/tests/functional/inference_engine/ir_serialization/models/add_abc_initializers_f32_nan_const.bin rename to ngraph/test/models/ir/add_abc_initializers_f32_nan_const.bin diff --git a/inference-engine/tests/functional/inference_engine/ir_serialization/models/add_abc_initializers_nan_const.bin b/ngraph/test/models/ir/add_abc_initializers_nan_const.bin similarity index 100% rename from inference-engine/tests/functional/inference_engine/ir_serialization/models/add_abc_initializers_nan_const.bin rename to ngraph/test/models/ir/add_abc_initializers_nan_const.bin diff --git a/inference-engine/tests/functional/inference_engine/ir_serialization/models/add_abc_initializers_nan_const.xml b/ngraph/test/models/ir/add_abc_initializers_nan_const.xml similarity index 100% rename from inference-engine/tests/functional/inference_engine/ir_serialization/models/add_abc_initializers_nan_const.xml rename to ngraph/test/models/ir/add_abc_initializers_nan_const.xml diff --git a/inference-engine/tests/functional/inference_engine/ir_serialization/models/add_abc_initializers_u1_const.bin b/ngraph/test/models/ir/add_abc_initializers_u1_const.bin similarity index 100% rename from inference-engine/tests/functional/inference_engine/ir_serialization/models/add_abc_initializers_u1_const.bin rename to ngraph/test/models/ir/add_abc_initializers_u1_const.bin diff --git a/inference-engine/tests/functional/inference_engine/ir_serialization/models/add_abc_initializers_u1_const.xml b/ngraph/test/models/ir/add_abc_initializers_u1_const.xml similarity index 100% rename from inference-engine/tests/functional/inference_engine/ir_serialization/models/add_abc_initializers_u1_const.xml rename to ngraph/test/models/ir/add_abc_initializers_u1_const.xml diff --git a/inference-engine/tests/functional/inference_engine/ir_serialization/models/addmul_abc.bin b/ngraph/test/models/ir/addmul_abc.bin similarity index 100% rename from inference-engine/tests/functional/inference_engine/ir_serialization/models/addmul_abc.bin rename to ngraph/test/models/ir/addmul_abc.bin diff --git a/inference-engine/tests/functional/inference_engine/ir_serialization/models/addmul_abc.prototxt b/ngraph/test/models/ir/addmul_abc.prototxt similarity index 100% rename from inference-engine/tests/functional/inference_engine/ir_serialization/models/addmul_abc.prototxt rename to ngraph/test/models/ir/addmul_abc.prototxt diff --git a/inference-engine/tests/functional/inference_engine/ir_serialization/models/addmul_abc.xml b/ngraph/test/models/ir/addmul_abc.xml similarity index 100% rename from inference-engine/tests/functional/inference_engine/ir_serialization/models/addmul_abc.xml rename to ngraph/test/models/ir/addmul_abc.xml diff --git a/inference-engine/tests/functional/inference_engine/ir_serialization/models/conv_with_rt_info.bin b/ngraph/test/models/ir/conv_with_rt_info.bin similarity index 100% rename from inference-engine/tests/functional/inference_engine/ir_serialization/models/conv_with_rt_info.bin rename to ngraph/test/models/ir/conv_with_rt_info.bin diff --git a/inference-engine/tests/functional/inference_engine/ir_serialization/models/conv_with_rt_info.xml b/ngraph/test/models/ir/conv_with_rt_info.xml similarity index 100% rename from inference-engine/tests/functional/inference_engine/ir_serialization/models/conv_with_rt_info.xml rename to ngraph/test/models/ir/conv_with_rt_info.xml diff --git a/inference-engine/tests/functional/inference_engine/ir_serialization/models/dynamic_input_shape.xml b/ngraph/test/models/ir/dynamic_input_shape.xml similarity index 100% rename from inference-engine/tests/functional/inference_engine/ir_serialization/models/dynamic_input_shape.xml rename to ngraph/test/models/ir/dynamic_input_shape.xml diff --git a/inference-engine/tests/functional/inference_engine/ir_serialization/models/experimental_detectron_detection_output.xml b/ngraph/test/models/ir/experimental_detectron_detection_output.xml similarity index 100% rename from inference-engine/tests/functional/inference_engine/ir_serialization/models/experimental_detectron_detection_output.xml rename to ngraph/test/models/ir/experimental_detectron_detection_output.xml diff --git a/inference-engine/tests/functional/inference_engine/ir_serialization/models/experimental_detectron_detection_output_opset6.xml b/ngraph/test/models/ir/experimental_detectron_detection_output_opset6.xml similarity index 100% rename from inference-engine/tests/functional/inference_engine/ir_serialization/models/experimental_detectron_detection_output_opset6.xml rename to ngraph/test/models/ir/experimental_detectron_detection_output_opset6.xml diff --git a/inference-engine/tests/functional/inference_engine/ir_serialization/models/experimental_detectron_roi_feature_extractor.xml b/ngraph/test/models/ir/experimental_detectron_roi_feature_extractor.xml similarity index 100% rename from inference-engine/tests/functional/inference_engine/ir_serialization/models/experimental_detectron_roi_feature_extractor.xml rename to ngraph/test/models/ir/experimental_detectron_roi_feature_extractor.xml diff --git a/inference-engine/tests/functional/inference_engine/ir_serialization/models/experimental_detectron_roi_feature_extractor_opset6.xml b/ngraph/test/models/ir/experimental_detectron_roi_feature_extractor_opset6.xml similarity index 100% rename from inference-engine/tests/functional/inference_engine/ir_serialization/models/experimental_detectron_roi_feature_extractor_opset6.xml rename to ngraph/test/models/ir/experimental_detectron_roi_feature_extractor_opset6.xml diff --git a/inference-engine/tests/functional/inference_engine/ir_serialization/models/if_diff_case.bin b/ngraph/test/models/ir/if_diff_case.bin similarity index 100% rename from inference-engine/tests/functional/inference_engine/ir_serialization/models/if_diff_case.bin rename to ngraph/test/models/ir/if_diff_case.bin diff --git a/inference-engine/tests/functional/inference_engine/ir_serialization/models/if_diff_case.xml b/ngraph/test/models/ir/if_diff_case.xml similarity index 100% rename from inference-engine/tests/functional/inference_engine/ir_serialization/models/if_diff_case.xml rename to ngraph/test/models/ir/if_diff_case.xml diff --git a/ngraph/test/models/ir/loop_2d_add.bin b/ngraph/test/models/ir/loop_2d_add.bin new file mode 100644 index 00000000000..e76944fa86d Binary files /dev/null and b/ngraph/test/models/ir/loop_2d_add.bin differ diff --git a/ngraph/test/models/ir/loop_2d_add.xml b/ngraph/test/models/ir/loop_2d_add.xml new file mode 100644 index 00000000000..45e796945c7 --- /dev/null +++ b/ngraph/test/models/ir/loop_2d_add.xml @@ -0,0 +1,237 @@ + + + + + + + + 1 + + + + + + + + + + + + + + 1 + 2 + + + + + + + 1 + + + + 1 + 2 + + + + + 1 + 2 + + + 3 + 1 + 2 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1 + 2 + + + + + + + + 1 + 2 + + + + + + + 1 + 2 + + + 1 + 2 + + + + + 1 + 2 + + + + + + + 1 + 2 + + + + + + + + 1 + + + + + + + 1 + 2 + + + 1 + + + + + 3 + 1 + 2 + + + + + + + 3 + 1 + 2 + + + + + + + + + + + + + + + + + + + + + + + + + 1 + 2 + + + + + + 1 + 2 + + + + + + + 1 + 2 + + + + + + + + + + + + + 3 + 1 + 2 + + + + + + 3 + 1 + 2 + + + + + + + 3 + 1 + 2 + + + + + + + + + + + + + + + + diff --git a/inference-engine/tests/functional/inference_engine/ir_serialization/models/nms5.bin b/ngraph/test/models/ir/nms5.bin similarity index 100% rename from inference-engine/tests/functional/inference_engine/ir_serialization/models/nms5.bin rename to ngraph/test/models/ir/nms5.bin diff --git a/inference-engine/tests/functional/inference_engine/ir_serialization/models/nms5.xml b/ngraph/test/models/ir/nms5.xml similarity index 100% rename from inference-engine/tests/functional/inference_engine/ir_serialization/models/nms5.xml rename to ngraph/test/models/ir/nms5.xml diff --git a/inference-engine/tests/functional/inference_engine/ir_serialization/models/nms5_dynamism.bin b/ngraph/test/models/ir/nms5_dynamism.bin similarity index 100% rename from inference-engine/tests/functional/inference_engine/ir_serialization/models/nms5_dynamism.bin rename to ngraph/test/models/ir/nms5_dynamism.bin diff --git a/inference-engine/tests/functional/inference_engine/ir_serialization/models/nms5_dynamism.xml b/ngraph/test/models/ir/nms5_dynamism.xml similarity index 100% rename from inference-engine/tests/functional/inference_engine/ir_serialization/models/nms5_dynamism.xml rename to ngraph/test/models/ir/nms5_dynamism.xml diff --git a/inference-engine/tests/functional/inference_engine/ir_serialization/models/pad_with_shape_of.bin b/ngraph/test/models/ir/pad_with_shape_of.bin similarity index 100% rename from inference-engine/tests/functional/inference_engine/ir_serialization/models/pad_with_shape_of.bin rename to ngraph/test/models/ir/pad_with_shape_of.bin diff --git a/inference-engine/tests/functional/inference_engine/ir_serialization/models/pad_with_shape_of.xml b/ngraph/test/models/ir/pad_with_shape_of.xml similarity index 100% rename from inference-engine/tests/functional/inference_engine/ir_serialization/models/pad_with_shape_of.xml rename to ngraph/test/models/ir/pad_with_shape_of.xml diff --git a/inference-engine/tests/functional/inference_engine/ir_serialization/models/shape_of.xml b/ngraph/test/models/ir/shape_of.xml similarity index 100% rename from inference-engine/tests/functional/inference_engine/ir_serialization/models/shape_of.xml rename to ngraph/test/models/ir/shape_of.xml diff --git a/inference-engine/tests/functional/inference_engine/ir_serialization/models/split_equal_parts_2d.bin b/ngraph/test/models/ir/split_equal_parts_2d.bin similarity index 100% rename from inference-engine/tests/functional/inference_engine/ir_serialization/models/split_equal_parts_2d.bin rename to ngraph/test/models/ir/split_equal_parts_2d.bin diff --git a/inference-engine/tests/functional/inference_engine/ir_serialization/models/split_equal_parts_2d.prototxt b/ngraph/test/models/ir/split_equal_parts_2d.prototxt similarity index 100% rename from inference-engine/tests/functional/inference_engine/ir_serialization/models/split_equal_parts_2d.prototxt rename to ngraph/test/models/ir/split_equal_parts_2d.prototxt diff --git a/inference-engine/tests/functional/inference_engine/ir_serialization/models/split_equal_parts_2d.xml b/ngraph/test/models/ir/split_equal_parts_2d.xml similarity index 100% rename from inference-engine/tests/functional/inference_engine/ir_serialization/models/split_equal_parts_2d.xml rename to ngraph/test/models/ir/split_equal_parts_2d.xml diff --git a/inference-engine/tests/functional/inference_engine/ir_serialization/cleanup.cpp b/ngraph/test/pass/serialization/cleanup.cpp similarity index 60% rename from inference-engine/tests/functional/inference_engine/ir_serialization/cleanup.cpp rename to ngraph/test/pass/serialization/cleanup.cpp index 6cf426ba60b..a51f8432eee 100644 --- a/inference-engine/tests/functional/inference_engine/ir_serialization/cleanup.cpp +++ b/ngraph/test/pass/serialization/cleanup.cpp @@ -2,14 +2,15 @@ // SPDX-License-Identifier: Apache-2.0 // +#include + #include #include "common_test_utils/ngraph_test_utils.hpp" -#include "gtest/gtest.h" -#include "ie_core.hpp" -#include "ngraph_functions/builders.hpp" +#include "openvino/pass/serialize.hpp" +#include "util/test_common.hpp" -class SerializationCleanupTest : public CommonTestUtils::TestsCommon { +class SerializationCleanupTest : public ov::test::TestsCommon { protected: const std::string test_name = GetTestName() + "_" + GetTimestamp(); std::string m_out_xml_path = test_name + ".xml"; @@ -22,23 +23,19 @@ protected: }; namespace { -std::shared_ptr CreateTestFunction( - const std::string& name, const ngraph::PartialShape& ps) { +std::shared_ptr CreateTestFunction(const std::string& name, const ngraph::PartialShape& ps) { using namespace ngraph; const auto param = std::make_shared(element::f16, ps); const auto convert = std::make_shared(param, element::f32); const auto result = std::make_shared(convert); - return std::make_shared(ResultVector{result}, - ParameterVector{param}, name); + return std::make_shared(ResultVector{result}, ParameterVector{param}, name); } } // namespace TEST_F(SerializationCleanupTest, SerializationShouldWork) { - const auto f = - CreateTestFunction("StaticFunction", ngraph::PartialShape{2, 2}); + const auto f = CreateTestFunction("StaticFunction", ngraph::PartialShape{2, 2}); - const InferenceEngine::CNNNetwork net{f}; - net.serialize(m_out_xml_path, m_out_bin_path); + ov::pass::Serialize(m_out_xml_path, m_out_bin_path).run_on_function(f); // .xml & .bin files should be present ASSERT_TRUE(std::ifstream(m_out_xml_path, std::ios::in).good()); @@ -46,12 +43,9 @@ TEST_F(SerializationCleanupTest, SerializationShouldWork) { } TEST_F(SerializationCleanupTest, SerializationShouldWorkWithDynamicFunction) { - const auto f = - CreateTestFunction("DynamicFunction", - ngraph::PartialShape{ngraph::Dimension()}); + const auto f = CreateTestFunction("DynamicFunction", ngraph::PartialShape{ngraph::Dimension()}); - const InferenceEngine::CNNNetwork net{f}; - net.serialize(m_out_xml_path, m_out_bin_path); + ov::pass::Serialize(m_out_xml_path, m_out_bin_path).run_on_function(f); // .xml & .bin files should be present ASSERT_TRUE(std::ifstream(m_out_xml_path, std::ios::in).good()); diff --git a/ngraph/test/pass/serialization/const_compression.cpp b/ngraph/test/pass/serialization/const_compression.cpp new file mode 100644 index 00000000000..29e3126423b --- /dev/null +++ b/ngraph/test/pass/serialization/const_compression.cpp @@ -0,0 +1,209 @@ +// Copyright (C) 2021 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#include + +#include + +#include "openvino/opsets/opset8.hpp" +#include "openvino/pass/serialize.hpp" +#include "util/test_common.hpp" + +class SerializatioConstantCompressionTest : public ov::test::TestsCommon { +protected: + std::string test_name = GetTestName(); + std::string m_out_xml_path_1 = test_name + "1" + ".xml"; + std::string m_out_bin_path_1 = test_name + "1" + ".bin"; + + void TearDown() override { + std::remove(m_out_xml_path_1.c_str()); + std::remove(m_out_bin_path_1.c_str()); + } + + std::uintmax_t file_size(std::ifstream& f) { + // get length of file: + const auto pos_to_restore = f.tellg(); + f.seekg(0, f.end); + std::uintmax_t length = f.tellg(); + f.seekg(pos_to_restore, f.beg); + return length; + } +}; + +TEST_F(SerializatioConstantCompressionTest, IdenticalConstantsI32) { + constexpr int unique_const_count = 1; + const ov::Shape shape{2, 2, 2}; + + auto A = ov::opset8::Constant::create(ov::element::i32, shape, {1, 2, 3, 4, 5, 6, 7, 8}); + auto B = ov::opset8::Constant::create(ov::element::i32, shape, {1, 2, 3, 4, 5, 6, 7, 8}); + + auto ngraph_a = std::make_shared(ov::NodeVector{A, B}, ov::ParameterVector{}); + + ov::pass::Serialize(m_out_xml_path_1, m_out_bin_path_1).run_on_function(ngraph_a); + + std::ifstream xml_1(m_out_xml_path_1, std::ios::binary); + std::ifstream bin_1(m_out_bin_path_1, std::ios::binary); + + ASSERT_TRUE(file_size(bin_1) == unique_const_count * ov::shape_size(shape) * sizeof(int32_t)); +} + +TEST_F(SerializatioConstantCompressionTest, IdenticalConstantsI64) { + constexpr int unique_const_count = 1; + const ov::Shape shape{2, 2, 2}; + + auto A = ov::opset8::Constant::create(ov::element::i64, shape, {1, 2, 3, 4, 5, 6, 7, 8}); + auto B = ov::opset8::Constant::create(ov::element::i64, shape, {1, 2, 3, 4, 5, 6, 7, 8}); + + auto ngraph_a = std::make_shared(ov::NodeVector{A, B}, ov::ParameterVector{}); + + ov::pass::Serialize(m_out_xml_path_1, m_out_bin_path_1).run_on_function(ngraph_a); + + std::ifstream xml_1(m_out_xml_path_1, std::ios::binary); + std::ifstream bin_1(m_out_bin_path_1, std::ios::binary); + + ASSERT_TRUE(file_size(bin_1) == unique_const_count * ov::shape_size(shape) * sizeof(int64_t)); +} + +TEST_F(SerializatioConstantCompressionTest, IdenticalConstantsFP16) { + constexpr int unique_const_count = 1; + const ov::Shape shape{2, 2, 2}; + + auto A = ov::opset8::Constant::create(ov::element::f16, shape, {1, 2, 3, 4, 5, 6, 7, 8}); + auto B = ov::opset8::Constant::create(ov::element::f16, shape, {1, 2, 3, 4, 5, 6, 7, 8}); + + auto ngraph_a = std::make_shared(ov::NodeVector{A, B}, ov::ParameterVector{}); + + ov::pass::Serialize(m_out_xml_path_1, m_out_bin_path_1).run_on_function(ngraph_a); + + std::ifstream xml_1(m_out_xml_path_1, std::ios::binary); + std::ifstream bin_1(m_out_bin_path_1, std::ios::binary); + + ASSERT_TRUE(file_size(bin_1) == unique_const_count * ov::shape_size(shape) * sizeof(ov::float16)); +} + +TEST_F(SerializatioConstantCompressionTest, IdenticalConstantsFP32) { + constexpr int unique_const_count = 1; + const ov::Shape shape{2, 2, 2}; + + auto A = ov::opset8::Constant::create(ov::element::f32, shape, {1, 2, 3, 4, 5, 6, 7, 8}); + auto B = ov::opset8::Constant::create(ov::element::f32, shape, {1, 2, 3, 4, 5, 6, 7, 8}); + + auto ngraph_a = std::make_shared(ov::NodeVector{A, B}, ov::ParameterVector{}); + + ov::pass::Serialize(m_out_xml_path_1, m_out_bin_path_1).run_on_function(ngraph_a); + + std::ifstream xml_1(m_out_xml_path_1, std::ios::binary); + std::ifstream bin_1(m_out_bin_path_1, std::ios::binary); + + ASSERT_TRUE(file_size(bin_1) == unique_const_count * ov::shape_size(shape) * sizeof(float)); +} + +TEST_F(SerializatioConstantCompressionTest, NonIdenticalConstantsI64) { + constexpr int unique_const_count = 2; + const ov::Shape shape{2}; + + // hash_combine returns the same hash for this two constants so we also check the content of arrays + auto A = ov::opset8::Constant::create(ov::element::i64, shape, {2, 2}); + auto B = ov::opset8::Constant::create(ov::element::i64, shape, {0, 128}); + + auto ngraph_a = std::make_shared(ov::NodeVector{A, B}, ov::ParameterVector{}); + + ov::pass::Serialize(m_out_xml_path_1, m_out_bin_path_1).run_on_function(ngraph_a); + + std::ifstream xml_1(m_out_xml_path_1, std::ios::binary); + std::ifstream bin_1(m_out_bin_path_1, std::ios::binary); + + ASSERT_TRUE(file_size(bin_1) == unique_const_count * ov::shape_size(shape) * sizeof(int64_t)); +} + +TEST_F(SerializatioConstantCompressionTest, IdenticalConstantsTimesTwo) { + constexpr int unique_const_count = 2; + const ov::Shape shape{2, 2, 2}; + + auto A = ov::opset8::Constant::create(ov::element::i32, shape, {1, 2, 3, 4, 5, 6, 7, 8}); + auto B = ov::opset8::Constant::create(ov::element::i32, shape, {1, 2, 3, 4, 5, 6, 7, 8}); + auto C = ov::opset8::Constant::create(ov::element::i32, shape, {0, 3, 1, 2, 5, 6, 25, 3}); + auto D = ov::opset8::Constant::create(ov::element::i32, shape, {0, 3, 1, 2, 5, 6, 25, 3}); + + auto ngraph_a = std::make_shared(ov::NodeVector{A, B, C, D}, ov::ParameterVector{}); + + ov::pass::Serialize(m_out_xml_path_1, m_out_bin_path_1).run_on_function(ngraph_a); + + std::ifstream xml_1(m_out_xml_path_1, std::ios::binary); + std::ifstream bin_1(m_out_bin_path_1, std::ios::binary); + + ASSERT_TRUE(file_size(bin_1) == unique_const_count * ov::shape_size(shape) * sizeof(int32_t)); +} + +TEST_F(SerializatioConstantCompressionTest, IdenticalConstantsTimesTwoMultipleOccurences) { + constexpr int unique_const_count = 2; + const ov::Shape shape{2, 2, 2}; + + auto A = ov::opset8::Constant::create(ov::element::i32, shape, {1, 2, 3, 4, 5, 6, 7, 8}); + auto B = ov::opset8::Constant::create(ov::element::i32, shape, {0, 3, 1, 2, 5, 6, 25, 3}); + auto C = ov::opset8::Constant::create(ov::element::i32, shape, {1, 2, 3, 4, 5, 6, 7, 8}); + auto D = ov::opset8::Constant::create(ov::element::i32, shape, {0, 3, 1, 2, 5, 6, 25, 3}); + auto E = ov::opset8::Constant::create(ov::element::i32, shape, {1, 2, 3, 4, 5, 6, 7, 8}); + auto F = ov::opset8::Constant::create(ov::element::i32, shape, {0, 3, 1, 2, 5, 6, 25, 3}); + + auto ngraph_a = std::make_shared(ov::NodeVector{A, B, C, D, E, F}, ov::ParameterVector{}); + + ov::pass::Serialize(m_out_xml_path_1, m_out_bin_path_1).run_on_function(ngraph_a); + + std::ifstream xml_1(m_out_xml_path_1, std::ios::binary); + std::ifstream bin_1(m_out_bin_path_1, std::ios::binary); + + ASSERT_TRUE(file_size(bin_1) == unique_const_count * ov::shape_size(shape) * sizeof(int32_t)); +} + +TEST_F(SerializatioConstantCompressionTest, NonIdenticalConstants) { + constexpr int unique_const_count = 2; + const ov::Shape shape{2, 2, 2}; + + auto A = ov::opset8::Constant::create(ov::element::i32, shape, {1, 2, 3, 4, 5, 6, 7, 8}); + auto B = ov::opset8::Constant::create(ov::element::i32, shape, {2, 2, 3, 4, 5, 6, 7, 8}); + + auto ngraph_a = std::make_shared(ov::NodeVector{A, B}, ov::ParameterVector{}); + + ov::pass::Serialize(m_out_xml_path_1, m_out_bin_path_1).run_on_function(ngraph_a); + + std::ifstream xml_1(m_out_xml_path_1, std::ios::binary); + std::ifstream bin_1(m_out_bin_path_1, std::ios::binary); + + ASSERT_TRUE(file_size(bin_1) == unique_const_count * ov::shape_size(shape) * sizeof(int32_t)); +} + +TEST_F(SerializatioConstantCompressionTest, IdenticalConstantsDifferentTypesI32I64) { + constexpr int unique_const_count = 1; + const ov::Shape shape{2, 2, 2}; + + auto A = ov::opset8::Constant::create(ov::element::i32, shape, {1, 0, 2, 0, 3, 0, 4, 0}); + auto B = ov::opset8::Constant::create(ov::element::i64, ov::Shape({1, 2, 2}), {1, 2, 3, 4}); + + auto ngraph_a = std::make_shared(ov::NodeVector{A, B}, ov::ParameterVector{}); + + ov::pass::Serialize(m_out_xml_path_1, m_out_bin_path_1).run_on_function(ngraph_a); + + std::ifstream xml_1(m_out_xml_path_1, std::ios::binary); + std::ifstream bin_1(m_out_bin_path_1, std::ios::binary); + + ASSERT_TRUE(file_size(bin_1) == unique_const_count * ov::shape_size(shape) * sizeof(int32_t)); +} + +TEST_F(SerializatioConstantCompressionTest, IdenticalConstantsDifferentTypesI32I8) { + constexpr int unique_const_count = 1; + const ov::Shape shape{1, 1, 2}; + + auto A = ov::opset8::Constant::create(ov::element::i32, shape, {1, 2}); + auto B = ov::opset8::Constant::create(ov::element::i8, ov::Shape({1, 2, 4}), {1, 0, 0, 0, 2, 0, 0, 0}); + + auto ngraph_a = std::make_shared(ov::NodeVector{A, B}, ov::ParameterVector{}); + + ov::pass::Serialize(m_out_xml_path_1, m_out_bin_path_1).run_on_function(ngraph_a); + + std::ifstream xml_1(m_out_xml_path_1, std::ios::binary); + std::ifstream bin_1(m_out_bin_path_1, std::ios::binary); + + ASSERT_TRUE(file_size(bin_1) == unique_const_count * ov::shape_size(shape) * sizeof(int32_t)); +} diff --git a/ngraph/test/pass/serialization/deterministicity.cpp b/ngraph/test/pass/serialization/deterministicity.cpp new file mode 100644 index 00000000000..54fa93b3157 --- /dev/null +++ b/ngraph/test/pass/serialization/deterministicity.cpp @@ -0,0 +1,118 @@ +// Copyright (C) 2018-2021 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#include + +#include + +#include "openvino/pass/serialize.hpp" +#include "openvino/util/file_util.hpp" +#include "read_ir.hpp" +#include "util/graph_comparator.hpp" +#include "util/test_common.hpp" + +class SerializationDeterministicityTest : public ov::test::TestsCommon { +protected: + std::string test_name = GetTestName(); + std::string m_out_xml_path_1 = test_name + "1" + ".xml"; + std::string m_out_bin_path_1 = test_name + "1" + ".bin"; + std::string m_out_xml_path_2 = test_name + "2" + ".xml"; + std::string m_out_bin_path_2 = test_name + "2" + ".bin"; + + void TearDown() override { + std::remove(m_out_xml_path_1.c_str()); + std::remove(m_out_xml_path_2.c_str()); + std::remove(m_out_bin_path_1.c_str()); + std::remove(m_out_bin_path_2.c_str()); + } + + bool files_equal(std::ifstream& f1, std::ifstream& f2) { + if (!f1.good()) + return false; + if (!f2.good()) + return false; + + while (!f1.eof() && !f2.eof()) { + if (f1.get() != f2.get()) { + return false; + } + } + + if (f1.eof() != f2.eof()) { + return false; + } + + return true; + } +}; + +#ifdef NGRAPH_ONNX_FRONTEND_ENABLE + +TEST_F(SerializationDeterministicityTest, BasicModel) { + const std::string model = ov::util::path_join({SERIALIZED_ZOO, "ir/add_abc.onnx"}); + + auto expected = ov::test::readIR(model, ""); + ov::pass::Serialize(m_out_xml_path_1, m_out_bin_path_1).run_on_function(expected); + ov::pass::Serialize(m_out_xml_path_2, m_out_bin_path_2).run_on_function(expected); + + std::ifstream xml_1(m_out_xml_path_1, std::ios::in | std::ios::binary); + std::ifstream bin_1(m_out_bin_path_1, std::ios::in | std::ios::binary); + std::ifstream xml_2(m_out_xml_path_2, std::ios::in | std::ios::binary); + std::ifstream bin_2(m_out_bin_path_2, std::ios::in | std::ios::binary); + + ASSERT_TRUE(files_equal(xml_1, xml_2)); + ASSERT_TRUE(files_equal(bin_1, bin_2)); +} + +TEST_F(SerializationDeterministicityTest, ModelWithMultipleLayers) { + const std::string model = ov::util::path_join({SERIALIZED_ZOO, "ir/addmul_abc.onnx"}); + + auto expected = ov::test::readIR(model, ""); + ov::pass::Serialize(m_out_xml_path_1, m_out_bin_path_1).run_on_function(expected); + ov::pass::Serialize(m_out_xml_path_2, m_out_bin_path_2).run_on_function(expected); + + std::ifstream xml_1(m_out_xml_path_1, std::ios::in | std::ios::binary); + std::ifstream bin_1(m_out_bin_path_1, std::ios::in | std::ios::binary); + std::ifstream xml_2(m_out_xml_path_2, std::ios::in | std::ios::binary); + std::ifstream bin_2(m_out_bin_path_2, std::ios::in | std::ios::binary); + + ASSERT_TRUE(files_equal(xml_1, xml_2)); + ASSERT_TRUE(files_equal(bin_1, bin_2)); +} + +#endif + +TEST_F(SerializationDeterministicityTest, ModelWithMultipleOutputs) { + const std::string model = ov::util::path_join({SERIALIZED_ZOO, "ir/split_equal_parts_2d.xml"}); + const std::string weights = ov::util::path_join({SERIALIZED_ZOO, "ir/split_equal_parts_2d.bin"}); + + auto expected = ov::test::readIR(model, weights); + ov::pass::Serialize(m_out_xml_path_1, m_out_bin_path_1).run_on_function(expected); + ov::pass::Serialize(m_out_xml_path_2, m_out_bin_path_2).run_on_function(expected); + + std::ifstream xml_1(m_out_xml_path_1, std::ios::in | std::ios::binary); + std::ifstream bin_1(m_out_bin_path_1, std::ios::in | std::ios::binary); + std::ifstream xml_2(m_out_xml_path_2, std::ios::in | std::ios::binary); + std::ifstream bin_2(m_out_bin_path_2, std::ios::in | std::ios::binary); + + ASSERT_TRUE(files_equal(xml_1, xml_2)); + ASSERT_TRUE(files_equal(bin_1, bin_2)); +} + +TEST_F(SerializationDeterministicityTest, ModelWithConstants) { + const std::string model = ov::util::path_join({SERIALIZED_ZOO, "ir/add_abc_initializers.xml"}); + const std::string weights = ov::util::path_join({SERIALIZED_ZOO, "ir/add_abc_initializers.bin"}); + + auto expected = ov::test::readIR(model, weights); + ov::pass::Serialize(m_out_xml_path_1, m_out_bin_path_1).run_on_function(expected); + ov::pass::Serialize(m_out_xml_path_2, m_out_bin_path_2).run_on_function(expected); + + std::ifstream xml_1(m_out_xml_path_1, std::ios::in | std::ios::binary); + std::ifstream bin_1(m_out_bin_path_1, std::ios::in | std::ios::binary); + std::ifstream xml_2(m_out_xml_path_2, std::ios::in | std::ios::binary); + std::ifstream bin_2(m_out_bin_path_2, std::ios::in | std::ios::binary); + + ASSERT_TRUE(files_equal(xml_1, xml_2)); + ASSERT_TRUE(files_equal(bin_1, bin_2)); +} diff --git a/ngraph/test/pass/serialization/read_ir.hpp b/ngraph/test/pass/serialization/read_ir.hpp new file mode 100644 index 00000000000..ae0f45362e5 --- /dev/null +++ b/ngraph/test/pass/serialization/read_ir.hpp @@ -0,0 +1,52 @@ +// Copyright (C) 2018-2021 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#pragma once + +#include "frontend_manager/frontend_manager.hpp" +#include "openvino/core/function.hpp" + +namespace ov { +namespace test { + +inline std::shared_ptr readIR(const std::string& model_path, const std::string& weights_path) { + static ngraph::frontend::FrontEndManager manager; + ngraph::frontend::FrontEnd::Ptr FE; + ngraph::frontend::InputModel::Ptr inputModel; + + ov::VariantVector params{ov::make_variant(model_path)}; + if (!weights_path.empty()) + params.emplace_back(ov::make_variant(weights_path)); + + FE = manager.load_by_model(params); + if (FE) + inputModel = FE->load(params); + + if (inputModel) + return FE->convert(inputModel); + + return nullptr; +} + +inline std::shared_ptr readIR(const std::string& model) { + static ngraph::frontend::FrontEndManager manager; + ngraph::frontend::FrontEnd::Ptr FE; + ngraph::frontend::InputModel::Ptr inputModel; + std::istringstream modelStringStream(model); + std::istream& modelStream = modelStringStream; + + ov::VariantVector params{ov::make_variant(&modelStream)}; + + FE = manager.load_by_model(params); + if (FE) + inputModel = FE->load(params); + + if (inputModel) + return FE->convert(inputModel); + + return nullptr; +} + +} // namespace test +} // namespace ov diff --git a/inference-engine/tests/functional/inference_engine/ir_serialization/rt_info_serialization.cpp b/ngraph/test/pass/serialization/rt_info_serialization.cpp similarity index 69% rename from inference-engine/tests/functional/inference_engine/ir_serialization/rt_info_serialization.cpp rename to ngraph/test/pass/serialization/rt_info_serialization.cpp index 2ac68b52c48..b8bc19a54a4 100644 --- a/inference-engine/tests/functional/inference_engine/ir_serialization/rt_info_serialization.cpp +++ b/ngraph/test/pass/serialization/rt_info_serialization.cpp @@ -2,23 +2,21 @@ // SPDX-License-Identifier: Apache-2.0 // -#include #include -#include -#include -#include -#include "common_test_utils/ngraph_test_utils.hpp" -#include "ie_core.hpp" -#include "ngraph/ngraph.hpp" -#include "transformations/serialize.hpp" -#include -#include +#include + #include "frontend_manager/frontend_manager.hpp" +#include "openvino/opsets/opset8.hpp" +#include "openvino/pass/manager.hpp" +#include "openvino/pass/serialize.hpp" +#include "read_ir.hpp" +#include "transformations/rt_info/attributes.hpp" +#include "util/test_common.hpp" -using namespace ngraph; +using namespace ov; -class RTInfoSerializationTest : public CommonTestUtils::TestsCommon { +class RTInfoSerializationTest : public ov::test::TestsCommon { protected: std::string test_name = GetTestName() + "_" + GetTimestamp(); std::string m_out_xml_path = test_name + ".xml"; @@ -28,35 +26,17 @@ protected: CommonTestUtils::removeIRFiles(m_out_xml_path, m_out_bin_path); } - std::shared_ptr getWithIRFrontend(const std::string& model_path, - const std::string& weights_path) { - ngraph::frontend::FrontEnd::Ptr FE; - ngraph::frontend::InputModel::Ptr inputModel; - - ov::VariantVector params{ov::make_variant(model_path), ov::make_variant(weights_path)}; - - FE = manager.load_by_model(params); - if (FE) - inputModel = FE->load(params); - - if (inputModel) - return FE->convert(inputModel); - - return nullptr; - } - private: ngraph::frontend::FrontEndManager manager; }; TEST_F(RTInfoSerializationTest, all_attributes_latest) { - auto init_info = [](RTMap & info) { + auto init_info = [](RTMap& info) { info[VariantWrapper::get_type_info_static()] = - std::make_shared>(ngraph::FusedNames("add")); - info[ov::PrimitivesPriority::get_type_info_static()] = - std::make_shared("priority"); + std::make_shared>(ngraph::FusedNames("add")); + info[ov::PrimitivesPriority::get_type_info_static()] = std::make_shared("priority"); info[ov::OldApiMap::get_type_info_static()] = std::make_shared( - ov::OldApiMapAttr(std::vector{0, 2, 3, 1}, ngraph::element::Type_t::f32)); + ov::OldApiMapAttr(std::vector{0, 2, 3, 1}, ngraph::element::Type_t::f32)); }; std::shared_ptr function; @@ -71,26 +51,26 @@ TEST_F(RTInfoSerializationTest, all_attributes_latest) { } pass::Manager m; - m.register_pass(m_out_xml_path, m_out_bin_path); + m.register_pass(m_out_xml_path, m_out_bin_path); m.run_passes(function); - auto f = getWithIRFrontend(m_out_xml_path, m_out_bin_path); + auto f = ov::test::readIR(m_out_xml_path, m_out_bin_path); ASSERT_NE(nullptr, f); - auto check_info = [](const RTMap & info) { - const std::string & key = VariantWrapper::get_type_info_static(); + auto check_info = [](const RTMap& info) { + const std::string& key = VariantWrapper::get_type_info_static(); ASSERT_TRUE(info.count(key)); auto fused_names_attr = std::dynamic_pointer_cast>(info.at(key)); ASSERT_TRUE(fused_names_attr); ASSERT_EQ(fused_names_attr->get().getNames(), "add"); - const std::string & pkey = ov::PrimitivesPriority::get_type_info_static(); + const std::string& pkey = ov::PrimitivesPriority::get_type_info_static(); ASSERT_TRUE(info.count(pkey)); auto primitives_priority_attr = std::dynamic_pointer_cast(info.at(pkey)); ASSERT_TRUE(primitives_priority_attr); ASSERT_EQ(primitives_priority_attr->get(), "priority"); - const std::string & old_api_map_key = ov::OldApiMap::get_type_info_static(); + const std::string& old_api_map_key = ov::OldApiMap::get_type_info_static(); ASSERT_TRUE(info.count(old_api_map_key)); auto old_api_map_attr = std::dynamic_pointer_cast(info.at(old_api_map_key)); ASSERT_TRUE(old_api_map_attr); @@ -107,11 +87,10 @@ TEST_F(RTInfoSerializationTest, all_attributes_latest) { } TEST_F(RTInfoSerializationTest, all_attributes_v10) { - auto init_info = [](RTMap & info) { + auto init_info = [](RTMap& info) { info[VariantWrapper::get_type_info_static()] = - std::make_shared>(ngraph::FusedNames("add")); - info[ov::PrimitivesPriority::get_type_info_static()] = - std::make_shared("priority"); + std::make_shared>(ngraph::FusedNames("add")); + info[ov::PrimitivesPriority::get_type_info_static()] = std::make_shared("priority"); }; std::shared_ptr function; @@ -126,14 +105,14 @@ TEST_F(RTInfoSerializationTest, all_attributes_v10) { } pass::Manager m; - m.register_pass(m_out_xml_path, m_out_bin_path, pass::Serialize::Version::IR_V10); + m.register_pass(m_out_xml_path, m_out_bin_path, ov::pass::Serialize::Version::IR_V10); m.run_passes(function); - auto f = getWithIRFrontend(m_out_xml_path, m_out_bin_path); + auto f = ov::test::readIR(m_out_xml_path, m_out_bin_path); ASSERT_NE(nullptr, f); - auto check_info = [](const RTMap & info) { - const std::string & key = VariantWrapper::get_type_info_static(); + auto check_info = [](const RTMap& info) { + const std::string& key = VariantWrapper::get_type_info_static(); ASSERT_FALSE(info.count(key)); }; @@ -145,11 +124,10 @@ TEST_F(RTInfoSerializationTest, all_attributes_v10) { } TEST_F(RTInfoSerializationTest, all_attributes_v11) { - auto init_info = [](RTMap & info) { + auto init_info = [](RTMap& info) { info[VariantWrapper::get_type_info_static()] = - std::make_shared>(ngraph::FusedNames("add")); - info[ov::PrimitivesPriority::get_type_info_static()] = - std::make_shared("priority"); + std::make_shared>(ngraph::FusedNames("add")); + info[ov::PrimitivesPriority::get_type_info_static()] = std::make_shared("priority"); }; std::shared_ptr function; @@ -164,20 +142,20 @@ TEST_F(RTInfoSerializationTest, all_attributes_v11) { } pass::Manager m; - m.register_pass(m_out_xml_path, m_out_bin_path); + m.register_pass(m_out_xml_path, m_out_bin_path); m.run_passes(function); - auto f = getWithIRFrontend(m_out_xml_path, m_out_bin_path); + auto f = ov::test::readIR(m_out_xml_path, m_out_bin_path); ASSERT_NE(nullptr, f); - auto check_info = [](const RTMap & info) { - const std::string & key = VariantWrapper::get_type_info_static(); + auto check_info = [](const RTMap& info) { + const std::string& key = VariantWrapper::get_type_info_static(); ASSERT_TRUE(info.count(key)); auto fused_names_attr = std::dynamic_pointer_cast>(info.at(key)); ASSERT_TRUE(fused_names_attr); ASSERT_EQ(fused_names_attr->get().getNames(), "add"); - const std::string & pkey = ov::PrimitivesPriority::get_type_info_static(); + const std::string& pkey = ov::PrimitivesPriority::get_type_info_static(); ASSERT_TRUE(info.count(pkey)); auto primitives_priority_attr = std::dynamic_pointer_cast(info.at(pkey)); ASSERT_TRUE(primitives_priority_attr); @@ -216,10 +194,10 @@ TEST_F(RTInfoSerializationTest, parameter_result_v11) { } pass::Manager m; - m.register_pass(m_out_xml_path, m_out_bin_path, pass::Serialize::Version::IR_V11); + m.register_pass(m_out_xml_path, m_out_bin_path, ov::pass::Serialize::Version::IR_V11); m.run_passes(function); - auto f = getWithIRFrontend(m_out_xml_path, m_out_bin_path); + auto f = ov::test::readIR(m_out_xml_path, m_out_bin_path); ASSERT_NE(nullptr, f); ASSERT_EQ(function->get_results().size(), f->get_results().size()); diff --git a/ngraph/test/pass/serialization/serialize.cpp b/ngraph/test/pass/serialization/serialize.cpp new file mode 100644 index 00000000000..2045ccf4fe2 --- /dev/null +++ b/ngraph/test/pass/serialization/serialize.cpp @@ -0,0 +1,88 @@ +// Copyright (C) 2018-2021 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#include "openvino/pass/serialize.hpp" + +#include + +#include + +#include "openvino/util/file_util.hpp" +#include "read_ir.hpp" +#include "util/graph_comparator.hpp" +#include "util/test_common.hpp" + +using SerializationParams = std::tuple; + +class SerializationTest : public ov::test::TestsCommon, public testing::WithParamInterface { +public: + std::string m_model_path; + std::string m_binary_path; + std::string m_out_xml_path; + std::string m_out_bin_path; + + void SetUp() override { + m_model_path = ov::util::path_join({SERIALIZED_ZOO, "ir/", std::get<0>(GetParam())}); + if (!std::get<1>(GetParam()).empty()) { + m_binary_path = ov::util::path_join({SERIALIZED_ZOO, "ir/", std::get<1>(GetParam())}); + } + + const std::string test_name = GetTestName() + "_" + GetTimestamp(); + m_out_xml_path = test_name + ".xml"; + m_out_bin_path = test_name + ".bin"; + } + + void TearDown() override { + std::remove(m_out_xml_path.c_str()); + std::remove(m_out_bin_path.c_str()); + } +}; + +TEST_P(SerializationTest, CompareFunctions) { + auto expected = ov::test::readIR(m_model_path, m_binary_path); + ov::pass::Serialize(m_out_xml_path, m_out_bin_path).run_on_function(expected); + auto result = ov::test::readIR(m_out_xml_path, m_out_bin_path); + + const auto fc = FunctionsComparator::with_default() + .enable(FunctionsComparator::ATTRIBUTES) + .enable(FunctionsComparator::CONST_VALUES); + const auto res = fc.compare(result, expected); + EXPECT_TRUE(res.valid) << res.message; +} + +INSTANTIATE_TEST_SUITE_P( + IRSerialization, + SerializationTest, + testing::Values(std::make_tuple("add_abc.xml", "add_abc.bin"), + std::make_tuple("add_abc_f64.xml", ""), + std::make_tuple("add_abc_bin.xml", ""), + std::make_tuple("split_equal_parts_2d.xml", "split_equal_parts_2d.bin"), + std::make_tuple("addmul_abc.xml", "addmul_abc.bin"), + std::make_tuple("add_abc_initializers.xml", "add_abc_initializers.bin"), + std::make_tuple("add_abc_initializers.xml", "add_abc_initializers_f32_nan_const.bin"), + std::make_tuple("add_abc_initializers_nan_const.xml", "add_abc_initializers_nan_const.bin"), + std::make_tuple("add_abc_initializers_u1_const.xml", "add_abc_initializers_u1_const.bin"), + std::make_tuple("experimental_detectron_roi_feature_extractor.xml", ""), + std::make_tuple("experimental_detectron_roi_feature_extractor_opset6.xml", ""), + std::make_tuple("experimental_detectron_detection_output.xml", ""), + std::make_tuple("experimental_detectron_detection_output_opset6.xml", ""), + std::make_tuple("nms5.xml", "nms5.bin"), + std::make_tuple("shape_of.xml", ""), + std::make_tuple("dynamic_input_shape.xml", ""), + std::make_tuple("pad_with_shape_of.xml", ""), + std::make_tuple("conv_with_rt_info.xml", ""), + std::make_tuple("loop_2d_add.xml", "loop_2d_add.bin"), + std::make_tuple("nms5_dynamism.xml", "nms5_dynamism.bin"), + std::make_tuple("if_diff_case.xml", "if_diff_case.bin"))); + +#ifdef NGRAPH_ONNX_FRONTEND_ENABLE + +INSTANTIATE_TEST_SUITE_P(ONNXSerialization, + SerializationTest, + testing::Values(std::make_tuple("add_abc.onnx", ""), + std::make_tuple("split_equal_parts_2d.onnx", ""), + std::make_tuple("addmul_abc.onnx", ""), + std::make_tuple("add_abc_initializers.onnx", ""))); + +#endif diff --git a/inference-engine/tests/functional/inference_engine/ir_serialization/tensor_names.cpp b/ngraph/test/pass/serialization/tensor_names.cpp similarity index 52% rename from inference-engine/tests/functional/inference_engine/ir_serialization/tensor_names.cpp rename to ngraph/test/pass/serialization/tensor_names.cpp index 0cc47bcf7cc..94131c22712 100644 --- a/inference-engine/tests/functional/inference_engine/ir_serialization/tensor_names.cpp +++ b/ngraph/test/pass/serialization/tensor_names.cpp @@ -4,16 +4,13 @@ #include -#include -#include -#include -#include "common_test_utils/ngraph_test_utils.hpp" -#include "ie_core.hpp" -#include "ngraph/ngraph.hpp" -#include "transformations/serialize.hpp" -#include +#include "openvino/opsets/opset8.hpp" +#include "openvino/pass/serialize.hpp" +#include "read_ir.hpp" +#include "util/graph_comparator.hpp" +#include "util/test_common.hpp" -class TensorNameSerializationTest : public CommonTestUtils::TestsCommon { +class TensorNameSerializationTest : public ov::test::TestsCommon { protected: std::string test_name = GetTestName() + "_" + GetTimestamp(); std::string m_out_xml_path = test_name + ".xml"; @@ -26,32 +23,30 @@ protected: }; TEST_F(TensorNameSerializationTest, SerializeFunctionWithTensorNames) { - InferenceEngine::Core ie; - std::shared_ptr function; { - auto parameter = std::make_shared(ngraph::element::Type_t::f32, ngraph::Shape{1, 3, 10, 10}); + auto parameter = + std::make_shared(ngraph::element::Type_t::f32, ngraph::Shape{1, 3, 10, 10}); parameter->set_friendly_name("parameter"); parameter->get_output_tensor(0).set_names({"input"}); - auto relu_prev = std::make_shared(parameter); + auto relu_prev = std::make_shared(parameter); relu_prev->set_friendly_name("relu_prev"); relu_prev->get_output_tensor(0).set_names({"relu_prev_t", "identity_prev_t"}); - auto relu = std::make_shared(relu_prev); + auto relu = std::make_shared(relu_prev); relu->set_friendly_name("relu"); relu->get_output_tensor(0).set_names({"relu,t", "identity"}); - const ngraph::ResultVector results{std::make_shared(relu)}; + const ngraph::ResultVector results{std::make_shared(relu)}; results[0]->set_friendly_name("out"); ngraph::ParameterVector params{parameter}; function = std::make_shared(results, params, "TensorNames"); } - InferenceEngine::CNNNetwork expected(function); - expected.serialize(m_out_xml_path, m_out_bin_path); - auto result = ie.ReadNetwork(m_out_xml_path, m_out_bin_path); + ov::pass::Serialize(m_out_xml_path, m_out_bin_path).run_on_function(function); + auto result = ov::test::readIR(m_out_xml_path, m_out_bin_path); const auto fc = FunctionsComparator::with_default() - .enable(FunctionsComparator::ATTRIBUTES) - .enable(FunctionsComparator::CONST_VALUES); - const auto res = fc.compare(result.getFunction(), expected.getFunction()); + .enable(FunctionsComparator::ATTRIBUTES) + .enable(FunctionsComparator::CONST_VALUES); + const auto res = fc.compare(result, function); EXPECT_TRUE(res.valid) << res.message; } diff --git a/ngraph/test/util/test_common.cpp b/ngraph/test/util/test_common.cpp new file mode 100644 index 00000000000..e3940f4f00e --- /dev/null +++ b/ngraph/test/util/test_common.cpp @@ -0,0 +1,89 @@ +// Copyright (C) 2018-2021 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#include "test_common.hpp" + +#include +#include +#include + +#ifdef _WIN32 +# ifndef NOMINMAX +# define NOMINMAX +# endif +# define _WINSOCKAPI_ + +# include + +# include "Psapi.h" +#endif + +namespace ov { +namespace test { + +inline size_t getVmSizeInKB() { +#ifdef _WIN32 + PROCESS_MEMORY_COUNTERS pmc; + pmc.cb = sizeof(PROCESS_MEMORY_COUNTERS); + GetProcessMemoryInfo(GetCurrentProcess(), &pmc, pmc.cb); + return pmc.WorkingSetSize; +#else + auto parseLine = [](char* line) { + // This assumes that a digit will be found and the line ends in " Kb". + size_t i = strlen(line); + const char* p = line; + while (*p < '0' || *p > '9') + p++; + line[i - 3] = '\0'; + i = (size_t)atoi(p); + return i; + }; + + FILE* file = fopen("/proc/self/status", "r"); + size_t result = 0; + if (file != nullptr) { + char line[128]; + + while (fgets(line, 128, file) != NULL) { + if (strncmp(line, "VmSize:", 7) == 0) { + result = parseLine(line); + break; + } + } + fclose(file); + } + return result; +#endif +} + +TestsCommon::~TestsCommon() = default; + +TestsCommon::TestsCommon() { + auto memsize = getVmSizeInKB(); + if (memsize != 0) { + std::cout << "\nMEM_USAGE=" << memsize << "KB\n"; + } +} + +std::string TestsCommon::GetTimestamp() { + auto now = std::chrono::system_clock::now(); + auto epoch = now.time_since_epoch(); + auto ns = std::chrono::duration_cast(epoch); + return std::to_string(ns.count()); +} + +std::string TestsCommon::GetTestName() const { + std::string test_name = ::testing::UnitTest::GetInstance()->current_test_info()->name(); + std::replace_if( + test_name.begin(), + test_name.end(), + [](char c) { + return !std::isalnum(c); + }, + '_'); + return test_name; +} + +} // namespace test +} // namespace ov diff --git a/ngraph/test/util/test_common.hpp b/ngraph/test/util/test_common.hpp new file mode 100644 index 00000000000..f01f3ab70b7 --- /dev/null +++ b/ngraph/test/util/test_common.hpp @@ -0,0 +1,25 @@ +// Copyright (C) 2018-2021 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#pragma once + +#include + +#include +#include + +namespace ov { +namespace test { + +class TestsCommon : virtual public ::testing::Test { +protected: + TestsCommon(); + ~TestsCommon() override; + + static std::string GetTimestamp(); + std::string GetTestName() const; +}; + +} // namespace test +} // namespace ov