Moved serialization pass to public (#7862)

* Moved serialization pass to public

* Fixed unit tests

* Fixed code style

* Fixed tests

* Fix link

* Fix kmb

* Fixed build

* Removed InferenceEngine::Core from ngraph tests

* Add missed models

* Restore IR weights

* Fixed code style
This commit is contained in:
Ilya Churaev 2021-10-13 11:17:53 +03:00 committed by GitHub
parent 6d9a7a108e
commit 82f8f19d11
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
80 changed files with 1499 additions and 1044 deletions

View File

@ -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<std::string, ngraph::OpSet> 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();

View File

@ -29,7 +29,7 @@
#include <array>
#include <cstdint>
#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();

View File

@ -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<ngraph::pass::Serialize>(xmlPath,
manager.register_pass<ov::pass::Serialize>(xmlPath,
binPath,
custom_opsets,
ngraph::pass::Serialize::Version::IR_V10);
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<ngraph::pass::Serialize>(xmlBuf,
binBuf,
custom_opsets,
ngraph::pass::Serialize::Version::IR_V10);
manager.register_pass<ov::pass::Serialize>(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<ngraph::pass::Serialize>(xmlBuf,
binBuf,
custom_opsets,
ngraph::pass::Serialize::Version::IR_V10);
manager.register_pass<ov::pass::Serialize>(xmlBuf, binBuf, custom_opsets, ov::pass::Serialize::Version::IR_V10);
manager.run_passes(_ngraph_function);
std::streambuf* pbuf = binBuf.rdbuf();

View File

@ -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

View File

@ -12,7 +12,7 @@
#include <ngraph/variant.hpp>
#include "ngraph/ngraph.hpp"
#include <ngraph/pass/manager.hpp>
#include <transformations/serialize.hpp>
#include <openvino/pass/serialize.hpp>
#include <vector>
#include <string>
@ -228,9 +228,9 @@ void serializeToXML(const MKLDNNGraph &graph, const std::string& path) {
std::string binPath;
ngraph::pass::Manager manager;
manager.register_pass<ngraph::pass::Serialize>(path,
manager.register_pass<ov::pass::Serialize>(path,
binPath,
ngraph::pass::Serialize::Version::IR_V10);
ov::pass::Serialize::Version::IR_V10);
manager.run_passes(graph.dump());
}

View File

@ -68,7 +68,6 @@
#include <transformations/rt_info/fused_names_attribute.hpp>
#include <transformations/op_conversions/fq_decomposition.hpp>
#include <transformations/utils/utils.hpp>
#include <transformations/serialize.hpp>
#include <ngraph/opsets/opset1.hpp>
#include <ngraph/opsets/opset2.hpp>

View File

@ -3,7 +3,7 @@
//
#include "mkldnn_serialize.h"
#include <transformations/serialize.hpp>
#include <openvino/pass/serialize.hpp>
#include <pugixml.hpp>
@ -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<ngraph::Function>(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;

View File

@ -12,7 +12,7 @@
#include "snippets/pass/assign_registers.hpp"
#include <ngraph/pass/manager.hpp>
#include <transformations/serialize.hpp>
#include <openvino/pass/serialize.hpp>
#include <algorithm>
#include <memory>
@ -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();

View File

@ -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 $<BUILD_INTERFACE:${PUBLIC_HEADERS_DIR}>
PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/src")

View File

@ -7,38 +7,28 @@
#include <locale>
#include <map>
#include <mutex>
#include <set>
#include <utility>
#include <transformations_visibility.hpp>
#include <openvino/core/variant.hpp>
#include <ngraph/node.hpp>
#include <ngraph/factory.hpp>
#include <ngraph/node.hpp>
#include <openvino/core/variant.hpp>
#include <set>
#include <transformations/rt_info/disable_constant_folding.hpp>
#include <transformations/rt_info/fused_names_attribute.hpp>
#include <transformations/rt_info/nms_selected_indices.hpp>
#include <transformations/rt_info/old_api_map_attribute.hpp>
#include <transformations/rt_info/primitives_priority_attribute.hpp>
#include <transformations/rt_info/strides_property.hpp>
#include <transformations_visibility.hpp>
#include <utility>
namespace ov {
namespace pass {
class TRANSFORMATIONS_API Attributes {
public:
Attributes() {
register_factory<VariantWrapper<ngraph::FusedNames>>();
register_factory<PrimitivesPriority>();
register_factory<DisableConstantFolding>();
register_factory<NmsSelectedIndices>();
register_factory<StridesPropagation>();
register_factory<OldApiMap>();
}
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 <class T>
void register_factory() {

View File

@ -13,25 +13,13 @@
#include <ngraph/node.hpp>
#include <ngraph/variant.hpp>
#include <transformations_visibility.hpp>
#include "openvino/pass/constant_folding.hpp"
namespace ov {
TRANSFORMATIONS_API void disable_constant_folding(const std::shared_ptr<Node>& node);
TRANSFORMATIONS_API void enable_constant_folding(const std::shared_ptr<Node>& node);
TRANSFORMATIONS_API bool constant_folding_is_disabled(const std::shared_ptr<Node>& node);
class TRANSFORMATIONS_API DisableConstantFolding : public VariantImpl<bool> {
public:
OPENVINO_RTTI("disabled_constant_folding", "0");
DisableConstantFolding() = default;
DisableConstantFolding(const value_type &value) : VariantImpl<value_type>(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

View File

@ -102,11 +102,4 @@ public:
bool visit_attributes(AttributeVisitor & visitor) override;
};
template <>
class NGRAPH_API AttributeAdapter<std::set<std::string>> : public DirectValueAccessor<std::set<std::string>> {
public:
OPENVINO_RTTI("AttributeAdapter<set<string>>");
AttributeAdapter(std::set<std::string>& value) : DirectValueAccessor<std::set<std::string>>(value) {}
};
} // namespace ov

View File

@ -4,91 +4,4 @@
#pragma once
#include <string>
#include <functional>
#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<ngraph::Function> f) override;
Serialize(std::ostream& xmlFile, std::ostream& binFile,
std::map<std::string, ngraph::OpSet> 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<std::string, ngraph::OpSet> 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<std::string, ngraph::OpSet> 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<ngraph::Function> f) override;
StreamSerialize(std::ostream& stream,
std::map<std::string, ngraph::OpSet> && custom_opsets = {},
const std::function<void(std::ostream &)> & custom_data_serializer = {},
Serialize::Version version = Serialize::Version::UNSPECIFIED);
private:
std::ostream & m_stream;
std::map<std::string, ngraph::OpSet> m_custom_opsets;
std::function<void(std::ostream &)> m_custom_data_serializer;
const Serialize::Version m_version;
};
#include <ngraph/pass/serialize.hpp>

View File

@ -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<VariantWrapper<ngraph::FusedNames>>();
register_factory<PrimitivesPriority>();
register_factory<DisableConstantFolding>();
register_factory<NmsSelectedIndices>();
register_factory<StridesPropagation>();
register_factory<OldApiMap>();
}
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;

View File

@ -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>& node) {
auto & rt_info = node->get_rt_info();
rt_info[DisableConstantFolding::get_type_info_static()] = std::make_shared<DisableConstantFolding>(true);
}
void ov::enable_constant_folding(const std::shared_ptr<Node>& 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> &node) {
const auto & rt_info = node->get_rt_info();
return rt_info.count(DisableConstantFolding::get_type_info_static());
}

View File

@ -1,246 +0,0 @@
// Copyright (C) 2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#include <fstream>
#include "common_test_utils/ngraph_test_utils.hpp"
#include "ie_core.hpp"
#include "gtest/gtest.h"
#include <ngraph/function.hpp>
#include <transformations/serialize.hpp>
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::Function>(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::Function>(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::Function>(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::Function>(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::Function>(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::Function>(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::Function>(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::Function>(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::Function>(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::Function>(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));
}

View File

@ -15,8 +15,7 @@
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<uint8_t>(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;

View File

@ -2,7 +2,6 @@
// SPDX-License-Identifier: Apache-2.0
//
#include <cstdint>
#include <gtest/gtest.h>
#include <sstream>
@ -10,9 +9,11 @@
#include <memory>
#include <inference_engine.hpp>
#include <memory>
#include <ngraph/opsets/opset8.hpp>
#include <string>
#include <transformations/rt_info/fused_names_attribute.hpp>
#include <transformations/rt_info/old_api_map_attribute.hpp>
#include <transformations/serialize.hpp>
#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 <ngraph/opsets/opset8.hpp>
using namespace ngraph;
@ -173,8 +173,8 @@ TEST_F(RTInfoDeserialization, NodeV10) {
auto result = std::make_shared<opset8::Result>(convert_result);
result->set_friendly_name("output");
auto f_10_ref = std::make_shared<ngraph::Function>(ngraph::ResultVector{result},
ngraph::ParameterVector{param});
auto f_10_ref =
std::make_shared<ngraph::Function>(ngraph::ResultVector{result}, ngraph::ParameterVector{param});
f_10_ref->set_friendly_name("Network");
ov::runtime::Core core;
@ -325,8 +325,8 @@ TEST_F(RTInfoDeserialization, InputAndOutputV10) {
auto result = std::make_shared<opset8::Result>(convert_result);
result->set_friendly_name("output");
auto f_10_ref = std::make_shared<ngraph::Function>(ngraph::ResultVector{result},
ngraph::ParameterVector{param});
auto f_10_ref =
std::make_shared<ngraph::Function>(ngraph::ResultVector{result}, ngraph::ParameterVector{param});
f_10_ref->set_friendly_name("Network");
ov::runtime::Core core;
@ -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<uint64_t>({0, 2, 3, 1}),
ngraph::element::Type_t::f16);
check_old_api_map(param->get_rt_info(), std::vector<uint64_t>({0, 2, 3, 1}), ngraph::element::Type_t::f16);
auto result = f->get_result();
check_old_api_map(result->get_rt_info(),
std::vector<uint64_t>({0, 3, 1, 2}),
ngraph::element::Type_t::f16);
check_old_api_map(result->get_rt_info(), std::vector<uint64_t>({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,7 +474,8 @@ TEST_F(RTInfoDeserialization, NodeV11) {
auto convert_param = std::make_shared<opset8::Convert>(param, ngraph::element::f32);
auto constant_param = std::make_shared<opset8::Constant>(ngraph::element::i64, ngraph::Shape{4},
auto constant_param = std::make_shared<opset8::Constant>(ngraph::element::i64,
ngraph::Shape{4},
std::vector<int64_t>{0, 2, 3, 1});
auto transpose_param = std::make_shared<opset8::Transpose>(convert_param, constant_param);
@ -488,7 +485,8 @@ TEST_F(RTInfoDeserialization, NodeV11) {
round->get_rt_info()[VariantWrapper<ngraph::FusedNames>::get_type_info_static()] =
std::make_shared<VariantWrapper<ngraph::FusedNames>>(ngraph::FusedNames("Round1,Round2"));
auto constant_result = std::make_shared<opset8::Constant>(ngraph::element::i64, ngraph::Shape{4},
auto constant_result = std::make_shared<opset8::Constant>(ngraph::element::i64,
ngraph::Shape{4},
std::vector<int64_t>{0, 3, 1, 2});
auto transpose_result = std::make_shared<opset8::Transpose>(round, constant_result);
@ -499,8 +497,8 @@ TEST_F(RTInfoDeserialization, NodeV11) {
auto result = std::make_shared<opset8::Result>(convert_result);
result->set_friendly_name("output");
auto f_10_ref = std::make_shared<ngraph::Function>(ngraph::ResultVector{result},
ngraph::ParameterVector{param});
auto f_10_ref =
std::make_shared<ngraph::Function>(ngraph::ResultVector{result}, ngraph::ParameterVector{param});
f_10_ref->set_friendly_name("Network");
InferenceEngine::Core core;
@ -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<uint64_t>({}),
ngraph::element::Type_t::undefined);
check_old_api_map(param->get_rt_info(), std::vector<uint64_t>({}), 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<uint64_t>({}),
ngraph::element::Type_t::undefined);
check_old_api_map(result->get_rt_info(), std::vector<uint64_t>({}), ngraph::element::Type_t::undefined);
auto add = result->get_input_node_ptr(0);
check_fused_names(add->input(0).get_rt_info(), "test2,test3");

View File

@ -1,91 +0,0 @@
// Copyright (C) 2018-2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#include <fstream>
#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<std::string, std::string> SerializationParams;
class SerializationTest: public CommonTestUtils::TestsCommon,
public testing::WithParamInterface<SerializationParams> {
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

View File

@ -4,21 +4,17 @@
#include <gtest/gtest.h>
#include <string>
#include <memory>
#include <queue>
#include <ngraph/function.hpp>
#include <ngraph/opsets/opset6.hpp>
#include <ngraph/pass/manager.hpp>
#include <ngraph/pass/constant_folding.hpp>
#include <ngraph/pass/manager.hpp>
#include <queue>
#include <string>
#include <transformations/common_optimizations/disable_shapeof_constant_folding.hpp>
#include <transformations/serialize.hpp>
#include "common_test_utils/ngraph_test_utils.hpp"
using namespace testing;
using namespace ngraph;

View File

@ -15,7 +15,6 @@
#include <transformations/control_flow/unroll_tensor_iterator.hpp>
#include <transformations/init_node_info.hpp>
#include <transformations/common_optimizations/low_latency.hpp>
#include <transformations/serialize.hpp>
#include "common_test_utils/ngraph_test_utils.hpp"

View File

@ -10,6 +10,7 @@
#include <vector>
#include "base/ov_behavior_test_utils.hpp"
#include <openvino/pass/serialize.hpp>
#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);

View File

@ -11,7 +11,7 @@
#include "pugixml.hpp"
#include <transformations/serialize.hpp>
#include <openvino/pass/serialize.hpp>
#include <ngraph/opsets/opset.hpp>
#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<ngraph::pass::Serialize>(out_xml_path, out_bin_path);
manager.register_pass<ov::pass::Serialize>(out_xml_path, out_bin_path);
manager.run_passes(function);
function->validate_nodes_and_infer_types();

View File

@ -5,7 +5,6 @@
#include <signal.h>
#include <ie_transformations.hpp>
#include <transformations/control_flow/unroll_tensor_iterator.hpp>
#include <transformations/serialize.hpp>
#include <functional_test_utils/core_config.hpp>
#include "ngraph/opsets/opset7.hpp"
#include "ngraph_functions/builders.hpp"

View File

@ -12,7 +12,7 @@
#include "functional_test_utils/precision_utils.hpp"
#include <ngraph_functions/subgraph_builders.hpp>
#include <ngraph/pass/manager.hpp>
#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<ngraph::pass::Serialize>(modelPath, weightsPath);
manager.register_pass<ov::pass::Serialize>(modelPath, weightsPath);
manager.run_passes(ngraph::builder::subgraph::makeConvPoolRelu(
inputDims, InferenceEngine::details::convertPrecision(netPrc)));
}

View File

@ -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)

View File

@ -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

View File

@ -4,6 +4,7 @@
#pragma once
#include <set>
#include <string>
#include <type_traits>
#include <vector>
@ -422,4 +423,11 @@ public:
OPENVINO_RTTI("AttributeAdapter<std::vector<std::string>");
BWDCMP_RTTI_DECLARATION;
};
template <>
class OPENVINO_API AttributeAdapter<std::set<std::string>> : public DirectValueAccessor<std::set<std::string>> {
public:
OPENVINO_RTTI("AttributeAdapter<set<string>>");
AttributeAdapter(std::set<std::string>& value) : DirectValueAccessor<std::set<std::string>>(value) {}
};
} // namespace ov

View File

@ -60,6 +60,7 @@ protected:
extern template class OPENVINO_API VariantImpl<std::string>;
extern template class OPENVINO_API VariantImpl<int64_t>;
extern template class OPENVINO_API VariantImpl<bool>;
template <typename VT>
class VariantWrapper {};

View File

@ -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<ov::Function>& f);
};
OPENVINO_API void disable_constant_folding(const std::shared_ptr<Node>& node);
OPENVINO_API void enable_constant_folding(const std::shared_ptr<Node>& node);
OPENVINO_API bool constant_folding_is_disabled(const std::shared_ptr<Node>& node);
class OPENVINO_API DisableConstantFolding : public VariantImpl<bool> {
public:
OPENVINO_RTTI("disabled_constant_folding", "0");
DisableConstantFolding() = default;
DisableConstantFolding(const value_type& value) : VariantImpl<value_type>(value) {}
bool is_copyable() const override {
return false;
}
};
} // namespace pass
} // namespace ov

View File

@ -0,0 +1,90 @@
// Copyright (C) 2018-2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#pragma once
#include <functional>
#include <string>
#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<ov::Function> f) override;
Serialize(std::ostream& xmlFile,
std::ostream& binFile,
std::map<std::string, ngraph::OpSet> 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<std::string, ngraph::OpSet> 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<std::string, ngraph::OpSet> 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<ov::Function> f) override;
StreamSerialize(std::ostream& stream,
std::map<std::string, ngraph::OpSet>&& custom_opsets = {},
const std::function<void(std::ostream&)>& custom_data_serializer = {},
Serialize::Version version = Serialize::Version::UNSPECIFIED);
private:
std::ostream& m_stream;
std::map<std::string, ngraph::OpSet> m_custom_opsets;
std::function<void(std::ostream&)> m_custom_data_serializer;
const Serialize::Version m_version;
};
} // namespace pass
} // namespace ov

View File

@ -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>& node) {
auto& rt_info = node->get_rt_info();
rt_info[DisableConstantFolding::get_type_info_static()] = std::make_shared<DisableConstantFolding>(true);
}
void ov::pass::enable_constant_folding(const std::shared_ptr<Node>& 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>& node) {
const auto& rt_info = node->get_rt_info();
return rt_info.count(DisableConstantFolding::get_type_info_static());
}

View File

@ -2,30 +2,26 @@
// SPDX-License-Identifier: Apache-2.0
//
#include "itt.hpp"
#include "openvino/pass/serialize.hpp"
#include <array>
#include <cassert>
#include <cstdint>
#include <fstream>
#include <ngraph/variant.hpp>
#include <unordered_map>
#include <unordered_set>
#include <ngraph/variant.hpp>
#include <transformations/rt_info/disable_constant_folding.hpp>
#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 <typename Container>
std::string join(const Container& c, const char* glue = ", ") {
@ -49,8 +45,7 @@ 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<std::string, std::string> translate_type_name_translator = {
{"Constant", "Const"},
const std::unordered_map<std::string, std::string> translate_type_name_translator = {{"Constant", "Const"},
{"PRelu", "PReLU"},
{"Relu", "ReLU"},
{"Softmax", "SoftMax"}};
@ -87,10 +82,9 @@ public:
using ConstWritePositions = std::unordered_map<HashValue, std::pair<FilePosition, void const*>>;
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();
@ -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<ngraph::Node::type_info_t, std::string>
special_operator_to_opset_assignments = {{ngraph::Node::type_info_t("ShuffleChannels", 0), "opset3"}};
const std::unordered_map<ngraph::Node::type_info_t, std::string> 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);
@ -152,9 +146,7 @@ const std::vector<std::string> list_of_names {
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) {
@ -167,8 +159,7 @@ public:
private:
template <typename VariantType>
void xml_node_append_attribute(const std::string& name,
const std::shared_ptr<ngraph::Variant>& variant) {
void xml_node_append_attribute(const std::string& name, const std::shared_ptr<ngraph::Variant>& variant) {
if (auto v = std::dynamic_pointer_cast<ngraph::VariantImpl<VariantType>>(variant)) {
const auto& value = v->get();
m_xml_node.append_attribute(name.c_str()).set_value(value.c_str());
@ -186,7 +177,7 @@ public:
void on_adapter(const std::string& name, ngraph::ValueAccessor<void>& adapter) override {
check_attribute_name(name);
if (auto a = ngraph::as_type<ngraph::AttributeAdapter<std::set<std::string>>>(&adapter)) {
if (auto a = ov::as_type<ov::AttributeAdapter<std::set<std::string>>>(&adapter)) {
const auto& value = join(a->get());
m_node.append_attribute(name.c_str()).set_value(value.c_str());
} else {
@ -264,13 +255,13 @@ class XmlSerializer : public ngraph::AttributeVisitor {
int64_t m_version;
template <typename T>
std::string create_atribute_list(
ngraph::ValueAccessor<std::vector<T>>& adapter) {
std::string create_atribute_list(ngraph::ValueAccessor<std::vector<T>>& adapter) {
return join(adapter.get());
}
std::vector<std::string> 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<std::string> 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<std::shared_ptr<
ngraph::op::util::MultiSubGraphOp::InputDescription>>& input_descriptions,
void input_descriptions_on_adapter(
const std::vector<std::shared_ptr<ngraph::op::util::MultiSubGraphOp::InputDescription>>& input_descriptions,
const std::vector<std::string>& parameter_mapping,
const std::vector<std::string>& result_mapping,
pugi::xml_node& port_map, const std::string& portmap_name) {
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<ngraph::op::util::SubGraphOp::SliceInputDescription>(input_description)) {
if (auto slice_input =
ov::as_type_ptr<ngraph::op::util::SubGraphOp::SliceInputDescription>(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<ngraph::op::util::SubGraphOp::MergedInputDescription>(input_description)) {
} else if (auto merged_input =
ov::as_type_ptr<ngraph::op::util::SubGraphOp::MergedInputDescription>(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<std::shared_ptr<
ngraph::op::util::MultiSubGraphOp::OutputDescription>>& output_descriptions,
void output_descriptions_on_adapter(
const std::vector<std::shared_ptr<ngraph::op::util::MultiSubGraphOp::OutputDescription>>& output_descriptions,
const uint32_t& input_count,
const std::vector<std::string>& result_mapping,
pugi::xml_node& port_map, const std::string& portmap_name) {
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<ngraph::op::util::SubGraphOp::ConcatOutputDescription>(output_description)) {
if (auto concat_output =
ov::as_type_ptr<ngraph::op::util::SubGraphOp::ConcatOutputDescription>(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);
@ -353,14 +352,16 @@ class XmlSerializer : public ngraph::AttributeVisitor {
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,12 +372,11 @@ public:
const std::map<std::string, ngraph::OpSet>& 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<void>& adapter) override {
using BodyTargetNames = std::tuple<std::string, std::string, std::vector<std::string>>;
@ -402,39 +402,47 @@ public:
auto body_name = std::get<0>(bnames);
auto portmap_name = std::get<1>(bnames);
std::vector<std::string> result_mapping = map_type_from_body(m_xml_node.parent(), "Result", body_name);
std::vector<std::string> parameter_mapping = map_type_from_body(m_xml_node.parent(), "Parameter", body_name);
std::vector<std::string> 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<ngraph::AttributeAdapter<std::vector<std::shared_ptr
<ngraph::op::util::MultiSubGraphOp::InputDescription>>>>(&adapter)) {
if (const auto& a = ngraph::as_type<ngraph::AttributeAdapter<
std::vector<std::shared_ptr<ngraph::op::util::MultiSubGraphOp::InputDescription>>>>(&adapter)) {
input_descriptions_on_adapter(a->get(), parameter_mapping, result_mapping, port_map, portmap_name);
} else if (const auto& a = ngraph::as_type<ngraph::AttributeAdapter<std::vector<std::shared_ptr
<ngraph::op::util::MultiSubGraphOp::OutputDescription>>>>(&adapter)) {
} else if (const auto& a = ngraph::as_type<ngraph::AttributeAdapter<
std::vector<std::shared_ptr<ngraph::op::util::MultiSubGraphOp::OutputDescription>>>>(
&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<ngraph::AttributeAdapter<ngraph::op::v5::Loop::SpecialBodyPorts>>(&adapter)) {
} else if (const auto& a =
ngraph::as_type<ngraph::AttributeAdapter<ngraph::op::v5::Loop::SpecialBodyPorts>>(
&adapter)) {
special_body_ports_on_adapter(a->get(), parameter_mapping, result_mapping, port_map);
}
} else if (const auto& a = ngraph::as_type<ngraph::AttributeAdapter<std::shared_ptr<ngraph::Variable>>>(&adapter)) {
} else if (const auto& a =
ngraph::as_type<ngraph::AttributeAdapter<std::shared_ptr<ngraph::Variable>>>(&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<ngraph::AttributeAdapter<std::shared_ptr<ngraph::runtime::AlignedBuffer>>>(&adapter)) {
} else if (const auto& a =
ngraph::as_type<ngraph::AttributeAdapter<std::shared_ptr<ngraph::runtime::AlignedBuffer>>>(
&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<const char *>(a->get()->get_ptr()), size);
int64_t offset = m_constant_write_handler.write(static_cast<const char*>(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<ngraph::AttributeAdapter<ov::op::util::FrameworkNodeAttrs>>(&adapter)) {
} else if (const auto& a =
ngraph::as_type<ngraph::AttributeAdapter<ov::op::util::FrameworkNodeAttrs>>(&adapter)) {
const auto& attrs = a->get();
// Update type and version attributes
@ -463,56 +471,34 @@ public:
}
}
void on_adapter(const std::string& name,
ngraph::ValueAccessor<bool>& adapter) override {
void on_adapter(const std::string& name, ngraph::ValueAccessor<bool>& adapter) override {
m_xml_node.append_attribute(name.c_str()).set_value(adapter.get());
}
void on_adapter(const std::string& name,
ngraph::ValueAccessor<std::string>& 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<std::string>& 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<int64_t>& adapter) override {
void on_adapter(const std::string& name, ngraph::ValueAccessor<int64_t>& adapter) override {
m_xml_node.append_attribute(name.c_str()).set_value(adapter.get());
}
void on_adapter(const std::string& name,
ngraph::ValueAccessor<double>& adapter) override {
void on_adapter(const std::string& name, ngraph::ValueAccessor<double>& adapter) override {
m_xml_node.append_attribute(name.c_str()).set_value(adapter.get());
}
void on_adapter(
const std::string& name,
ngraph::ValueAccessor<std::vector<int>>& 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<std::vector<int>>& 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<std::vector<int64_t>>& 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<std::vector<int64_t>>& 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<std::vector<uint64_t>>& 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<std::vector<uint64_t>>& 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<std::vector<float>>& 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<std::vector<float>>& 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<std::vector<std::string>>& 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<std::vector<std::string>>& 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<std::shared_ptr<Function>>& adapter) override {
void on_adapter(const std::string& name, ngraph::ValueAccessor<std::shared_ptr<Function>>& 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<ngraph::Node*, int> create_layer_ids(
const ngraph::Function& f) {
const std::unordered_map<ngraph::Node*, int> create_layer_ids(const ngraph::Function& f) {
std::unordered_map<ngraph::Node*, int> layer_ids;
int id = 0;
for (const auto& node : f.get_ordered_ops()) {
@ -541,8 +526,7 @@ const std::unordered_map<ngraph::Node*, int> create_layer_ids(
return layer_ids;
}
const std::vector<Edge> create_edge_mapping(
const std::unordered_map<ngraph::Node*, int>& layer_ids,
const std::vector<Edge> create_edge_mapping(const std::unordered_map<ngraph::Node*, int>& layer_ids,
const ngraph::Function& f) {
std::vector<Edge> edges;
for (const auto& node : f.get_ordered_ops()) {
@ -555,30 +539,24 @@ const std::vector<Edge> 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 {
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<std::string, ngraph::OpSet>& custom_opsets) {
std::string get_opset_name(const ngraph::Node* n, const std::map<std::string, ngraph::OpSet>& 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()) {
@ -590,10 +568,14 @@ std::string get_opset_name(
}
}
auto opsets = std::array<std::reference_wrapper<const ngraph::OpSet>, 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<std::reference_wrapper<const ngraph::OpSet>, 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()) {
@ -672,8 +654,8 @@ std::string escape_delim(const std::string& name, const char delim = ',') {
return result_name;
}
std::string generate_unique_name(
const std::unordered_set<std::string>& unique_names, const std::string& base_name,
std::string generate_unique_name(const std::unordered_set<std::string>& 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()) {
@ -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<std::string>& unique_names,
const ngraph::Node* n) {
std::string get_node_unique_name(std::unordered_set<std::string>& 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);
@ -734,9 +715,9 @@ bool has_dynamic_output(const std::shared_ptr<Node>& 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<Node>& results) {
return !results->is_dynamic() && !has_dynamic_output(results); })) {
if (std::all_of(f_ops.begin(), f_ops.end(), [](const std::shared_ptr<Node>& results) {
return !results->is_dynamic() && !has_dynamic_output(results);
})) {
return false;
}
@ -747,7 +728,7 @@ bool resolve_dynamic_shapes(const ngraph::Function& f) {
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
ov::pass::enable_constant_folding(clone_op); // to be able to fold ShapeOfs
if (auto op_subgraph = std::dynamic_pointer_cast<ngraph::op::util::SubGraphOp>(op)) {
resolve_dynamic_shapes(*op_subgraph->get_function());
}
@ -762,7 +743,8 @@ bool resolve_dynamic_shapes(const ngraph::Function& f) {
return shape;
}
std::vector<Dimension> 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,14 +755,17 @@ 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(),
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(),
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(),
op->set_output_type(output_id,
replacements[output_id].get_element_type(),
replacements[output_id].get_partial_shape());
}
@ -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<ngraph::Node*, int> layer_ids =
create_layer_ids(f);
const std::unordered_map<ngraph::Node*, int> layer_ids = create_layer_ids(f);
std::unordered_set<std::string> unique_names;
// TODO remove resolve_dynamic_shapes function completely when support for -1 will be implemented in the MO
@ -910,7 +894,8 @@ void ngfunction_2_ir(pugi::xml_node& netXml,
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);
@ -940,8 +925,7 @@ void ngfunction_2_ir(pugi::xml_node& netXml,
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()) {
@ -965,8 +949,7 @@ void ngfunction_2_ir(pugi::xml_node& netXml,
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
@ -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);
}
@ -1056,8 +1038,7 @@ std::string valid_xml_path(const std::string &path) {
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;
}
@ -1075,10 +1056,8 @@ 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<ngraph::Function> f) {
RUN_ON_FUNCTION_SCOPE(Serialize);
auto serializeFunc = [&](std::ostream& xml_file, std::ostream& bin_file) {
auto version = static_cast<int64_t>(m_version);
@ -1088,7 +1067,6 @@ bool pass::Serialize::run_on_function(std::shared_ptr<ngraph::Function> f) {
version = version_var->get();
}
if (version != static_cast<int64_t>(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<ngraph::Function> f) {
pass::Serialize::Serialize(std::ostream& xmlFile,
std::ostream& binFile,
std::map<std::string, OpSet> custom_opsets,
std::map<std::string, ngraph::OpSet> 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<std::string, OpSet>{}, 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<std::string, ngraph::OpSet>{}, version) {}
pass::Serialize::Serialize(const std::string& xmlPath,
const std::string& binPath,
std::map<std::string, OpSet> custom_opsets,
std::map<std::string, ngraph::OpSet> 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<std::string, OpSet>{}, 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,
pass::Serialize::Serialize(const std::string& xmlPath, const std::string& binPath, pass::Serialize::Version version)
: pass::Serialize::Serialize(xmlPath, binPath, std::map<std::string, ngraph::OpSet>{}, version) {}
pass::StreamSerialize::StreamSerialize(std::ostream& stream,
std::map<std::string, ngraph::OpSet>&& custom_opsets,
const std::function<void(std::ostream&)>& 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) {
: 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<ngraph::Function> f) {
bool pass::StreamSerialize::run_on_function(std::shared_ptr<ngraph::Function> f) {
/*
Format:
[ DataHeader ]
@ -1252,4 +1223,4 @@ bool ngraph::pass::StreamSerialize::run_on_function(std::shared_ptr<ngraph::Func
// Return false because we didn't change nGraph Function
return false;
}
} // namespace ngraph
} // namespace ov

View File

@ -25,3 +25,4 @@ bool Variant::is_copyable() const {
template class ngraph::VariantImpl<std::string>;
template class ngraph::VariantImpl<int64_t>;
template class ngraph::VariantImpl<bool>;

View File

@ -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_PROPERTY:onnx,INTERFACE_COMPILE_DEFINITIONS>)
target_compile_definitions(unit-test PRIVATE NGRAPH_ONNX_FRONTEND_ENABLE)
endif()
if (OV_COMPILER_IS_CLANG)

View File

@ -18,7 +18,6 @@
</output>
</layer>
<layer id="2" name="add_node1" type="Add" version="opset1">
<data auto_broadcast="numpy"/>
<input>
<port id="0">
<dim>1</dim>
@ -42,7 +41,6 @@
</output>
</layer>
<layer id="4" name="Y" type="Add" version="opset1">
<data auto_broadcast="numpy"/>
<input>
<port id="0">
<dim>1</dim>

Binary file not shown.

View File

@ -0,0 +1,93 @@
<?xml version="1.0" ?>
<net name="add_abc_initializers" version="10">
<layers>
<layer id="0" name="add_node1/Output_0/Data__const" type="Const" version="opset1">
<data element_type="f32" offset="0" shape="2,2" size="16"/>
<output>
<port id="0" precision="FP32">
<dim>2</dim>
<dim>2</dim>
</port>
</output>
</layer>
<layer id="1" name="C" type="Parameter" version="opset1">
<data element_type="f32" shape="2,2"/>
<output>
<port id="0" precision="FP32">
<dim>2</dim>
<dim>2</dim>
</port>
</output>
</layer>
<layer id="2" name="Y" type="Add" version="opset1">
<input>
<port id="0">
<dim>2</dim>
<dim>2</dim>
</port>
<port id="1">
<dim>2</dim>
<dim>2</dim>
</port>
</input>
<output>
<port id="2" precision="FP32">
<dim>2</dim>
<dim>2</dim>
</port>
</output>
</layer>
<layer id="3" name="Y/sink_port_0" type="Result" version="opset1">
<input>
<port id="0">
<dim>2</dim>
<dim>2</dim>
</port>
</input>
</layer>
</layers>
<edges>
<edge from-layer="0" from-port="0" to-layer="2" to-port="0"/>
<edge from-layer="1" from-port="0" to-layer="2" to-port="1"/>
<edge from-layer="2" from-port="2" to-layer="3" to-port="0"/>
</edges>
<meta_data>
<MO_version value="custom_master_29f1c38ba0ae51897f47946d79d6bd6be7a494f0"/>
<cli_parameters>
<caffe_parser_path value="DIR"/>
<data_type value="float"/>
<disable_nhwc_to_nchw value="False"/>
<disable_omitting_optional value="False"/>
<disable_resnet_optimization value="False"/>
<disable_weights_compression value="False"/>
<enable_concat_optimization value="False"/>
<enable_flattening_nested_params value="False"/>
<enable_ssd_gluoncv value="False"/>
<extensions value="DIR"/>
<framework value="onnx"/>
<freeze_placeholder_with_value value="{}"/>
<generate_deprecated_IR_V7 value="False"/>
<input_model value="DIR/add_abc_initializers.onnx"/>
<input_model_is_text value="False"/>
<k value="DIR/CustomLayersMapping.xml"/>
<keep_shape_ops value="True"/>
<legacy_mxnet_model value="False"/>
<log_level value="ERROR"/>
<mean_scale_values value="{}"/>
<mean_values value="()"/>
<model_name value="add_abc_initializers"/>
<output_dir value="DIR"/>
<placeholder_data_types value="{}"/>
<progress value="False"/>
<remove_memory value="False"/>
<remove_output_softmax value="False"/>
<reverse_input_channels value="False"/>
<save_params_from_nd value="False"/>
<scale_values value="()"/>
<silent value="False"/>
<static_shape value="False"/>
<stream_output value="False"/>
<unset unset_cli_parameters="batch, counts, disable_fusing, disable_gfusing, finegrain_fusing, input, input_checkpoint, input_meta_graph, input_proto, input_shape, input_symbol, mean_file, mean_file_offsets, move_to_preprocess, nd_prefix_name, output, placeholder_shapes, pretrained_model_name, saved_model_dir, saved_model_tags, scale, tensorboard_logdir, tensorflow_custom_layer_libraries, tensorflow_custom_operations_config_update, tensorflow_object_detection_api_pipeline_config, tensorflow_use_custom_operations_config, transformations_config"/>
</cli_parameters>
</meta_data>
</net>

Binary file not shown.

View File

@ -0,0 +1,237 @@
<?xml version="1.0" ?>
<net name="loop_2d_add" version="10">
<layers>
<layer id="0" name="trip_count/Output_0/Data__const" type="Const" version="opset1">
<data element_type="i64" offset="0" shape="1" size="8"/>
<output>
<port id="1" precision="I64">
<dim>1</dim>
</port>
</output>
</layer>
<layer id="1" name="a_final5/execution_cond/Output_0/Data__const" type="Const" version="opset1">
<data element_type="boolean" offset="8" shape="" size="1"/>
<output>
<port id="1" precision="BOOL"/>
</output>
</layer>
<layer id="2" name="a_init" type="Parameter" version="opset1">
<data element_type="f32" shape="1,2"/>
<output>
<port id="0" precision="FP32">
<dim>1</dim>
<dim>2</dim>
</port>
</output>
</layer>
<layer id="3" name="a_final5" type="Loop" version="opset5">
<input>
<port id="0">
<dim>1</dim>
</port>
<port id="1"/>
<port id="2">
<dim>1</dim>
<dim>2</dim>
</port>
</input>
<output>
<port id="3" precision="FP32">
<dim>1</dim>
<dim>2</dim>
</port>
<port id="4" precision="FP32">
<dim>3</dim>
<dim>1</dim>
<dim>2</dim>
</port>
</output>
<port_map>
<input external_port_id="1" internal_layer_id="0"/>
<input external_port_id="2" internal_layer_id="2"/>
<output external_port_id="-1" internal_layer_id="1" purpose="execution_condition"/>
<output external_port_id="3" internal_layer_id="5"/>
<output axis="0" external_port_id="4" internal_layer_id="8"/>
</port_map>
<back_edges>
<edge from-layer="1" from-port="0" to-layer="0" to-port="0"/>
<edge from-layer="5" from-port="0" to-layer="2" to-port="0"/>
</back_edges>
<body>
<layers>
<layer id="0" name="cond_in" type="Parameter" version="opset1">
<data element_type="boolean" shape=""/>
<output>
<port id="0" precision="BOOL"/>
</output>
</layer>
<layer id="1" name="cond_identity/sink_port_0" type="Result" version="opset1">
<input>
<port id="0"/>
</input>
</layer>
<layer id="2" name="a_in" type="Parameter" version="opset1">
<data element_type="f32" shape="1,2"/>
<output>
<port id="0" precision="FP32">
<dim>1</dim>
<dim>2</dim>
</port>
</output>
</layer>
<layer id="3" name="b/Output_0/Data__const" type="Const" version="opset1">
<data element_type="f32" offset="13" shape="1,2" size="8"/>
<output>
<port id="1" precision="FP32">
<dim>1</dim>
<dim>2</dim>
</port>
</output>
</layer>
<layer id="4" name="loop_body_add" type="Add" version="opset1">
<input>
<port id="0">
<dim>1</dim>
<dim>2</dim>
</port>
<port id="1">
<dim>1</dim>
<dim>2</dim>
</port>
</input>
<output>
<port id="2" precision="FP32">
<dim>1</dim>
<dim>2</dim>
</port>
</output>
</layer>
<layer id="5" name="loop_body_add/sink_port_0" type="Result" version="opset1">
<input>
<port id="0">
<dim>1</dim>
<dim>2</dim>
</port>
</input>
</layer>
<layer id="6" name="11_input_port_1/value/Output_0/Data__const" type="Const" version="opset1">
<data element_type="i64" offset="21" shape="1" size="8"/>
<output>
<port id="1" precision="I64">
<dim>1</dim>
</port>
</output>
</layer>
<layer id="7" name="11" type="Unsqueeze" version="opset1">
<input>
<port id="0">
<dim>1</dim>
<dim>2</dim>
</port>
<port id="1">
<dim>1</dim>
</port>
</input>
<output>
<port id="2" precision="FP32">
<dim>3</dim>
<dim>1</dim>
<dim>2</dim>
</port>
</output>
</layer>
<layer id="8" name="output_accumulator/sink_port_0" type="Result" version="opset1">
<input>
<port id="0">
<dim>3</dim>
<dim>1</dim>
<dim>2</dim>
</port>
</input>
</layer>
</layers>
<edges>
<edge from-layer="0" from-port="0" to-layer="1" to-port="0"/>
<edge from-layer="2" from-port="0" to-layer="4" to-port="0"/>
<edge from-layer="3" from-port="1" to-layer="4" to-port="1"/>
<edge from-layer="4" from-port="2" to-layer="5" to-port="0"/>
<edge from-layer="4" from-port="2" to-layer="7" to-port="0"/>
<edge from-layer="6" from-port="1" to-layer="7" to-port="1"/>
<edge from-layer="7" from-port="2" to-layer="8" to-port="0"/>
</edges>
</body>
</layer>
<layer id="4" name="122_input_port_1/value124_const" type="Const" version="opset1">
<data element_type="f32" offset="9" shape="" size="4"/>
<output>
<port id="1" precision="FP32"/>
</output>
</layer>
<layer id="5" name="a_final" type="Add" version="opset1">
<input>
<port id="0">
<dim>1</dim>
<dim>2</dim>
</port>
<port id="1"/>
</input>
<output>
<port id="2" precision="FP32">
<dim>1</dim>
<dim>2</dim>
</port>
</output>
</layer>
<layer id="6" name="a_final/sink_port_0" type="Result" version="opset1">
<input>
<port id="0">
<dim>1</dim>
<dim>2</dim>
</port>
</input>
</layer>
<layer id="7" name="126_input_port_1/value128_const" type="Const" version="opset1">
<data element_type="f32" offset="9" shape="" size="4"/>
<output>
<port id="1" precision="FP32"/>
</output>
</layer>
<layer id="8" name="a_values" type="Add" version="opset1">
<input>
<port id="0">
<dim>3</dim>
<dim>1</dim>
<dim>2</dim>
</port>
<port id="1"/>
</input>
<output>
<port id="2" precision="FP32">
<dim>3</dim>
<dim>1</dim>
<dim>2</dim>
</port>
</output>
</layer>
<layer id="9" name="a_values/sink_port_0" type="Result" version="opset1">
<input>
<port id="0">
<dim>3</dim>
<dim>1</dim>
<dim>2</dim>
</port>
</input>
</layer>
</layers>
<edges>
<edge from-layer="0" from-port="1" to-layer="3" to-port="0"/>
<edge from-layer="1" from-port="1" to-layer="3" to-port="1"/>
<edge from-layer="2" from-port="0" to-layer="3" to-port="2"/>
<edge from-layer="3" from-port="3" to-layer="5" to-port="0"/>
<edge from-layer="4" from-port="1" to-layer="5" to-port="1"/>
<edge from-layer="5" from-port="2" to-layer="6" to-port="0"/>
<edge from-layer="3" from-port="4" to-layer="8" to-port="0"/>
<edge from-layer="7" from-port="1" to-layer="8" to-port="1"/>
<edge from-layer="8" from-port="2" to-layer="9" to-port="0"/>
</edges>
</net>

View File

@ -2,14 +2,15 @@
// SPDX-License-Identifier: Apache-2.0
//
#include <gtest/gtest.h>
#include <fstream>
#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<ngraph::Function> CreateTestFunction(
const std::string& name, const ngraph::PartialShape& ps) {
std::shared_ptr<ngraph::Function> CreateTestFunction(const std::string& name, const ngraph::PartialShape& ps) {
using namespace ngraph;
const auto param = std::make_shared<op::Parameter>(element::f16, ps);
const auto convert = std::make_shared<op::Convert>(param, element::f32);
const auto result = std::make_shared<op::Result>(convert);
return std::make_shared<Function>(ResultVector{result},
ParameterVector{param}, name);
return std::make_shared<Function>(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());

View File

@ -0,0 +1,209 @@
// Copyright (C) 2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#include <gtest/gtest.h>
#include <fstream>
#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::Function>(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::Function>(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::Function>(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::Function>(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::Function>(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::Function>(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::Function>(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::Function>(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::Function>(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::Function>(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));
}

View File

@ -0,0 +1,118 @@
// Copyright (C) 2018-2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#include <gtest/gtest.h>
#include <fstream>
#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));
}

View File

@ -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<ov::Function> 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<ov::Function> 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

View File

@ -2,23 +2,21 @@
// SPDX-License-Identifier: Apache-2.0
//
#include <common_test_utils/file_utils.hpp>
#include <gtest/gtest.h>
#include <file_utils.h>
#include <ie_api.h>
#include <ie_iextension.h>
#include "common_test_utils/ngraph_test_utils.hpp"
#include "ie_core.hpp"
#include "ngraph/ngraph.hpp"
#include "transformations/serialize.hpp"
#include <openvino/opsets/opset8.hpp>
#include <transformations/rt_info/attributes.hpp>
#include <common_test_utils/file_utils.hpp>
#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,23 +26,6 @@ protected:
CommonTestUtils::removeIRFiles(m_out_xml_path, m_out_bin_path);
}
std::shared_ptr<ngraph::Function> 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;
};
@ -53,8 +34,7 @@ TEST_F(RTInfoSerializationTest, all_attributes_latest) {
auto init_info = [](RTMap& info) {
info[VariantWrapper<ngraph::FusedNames>::get_type_info_static()] =
std::make_shared<VariantWrapper<ngraph::FusedNames>>(ngraph::FusedNames("add"));
info[ov::PrimitivesPriority::get_type_info_static()] =
std::make_shared<ov::PrimitivesPriority>("priority");
info[ov::PrimitivesPriority::get_type_info_static()] = std::make_shared<ov::PrimitivesPriority>("priority");
info[ov::OldApiMap::get_type_info_static()] = std::make_shared<ov::OldApiMap>(
ov::OldApiMapAttr(std::vector<uint64_t>{0, 2, 3, 1}, ngraph::element::Type_t::f32));
};
@ -71,10 +51,10 @@ TEST_F(RTInfoSerializationTest, all_attributes_latest) {
}
pass::Manager m;
m.register_pass<pass::Serialize>(m_out_xml_path, m_out_bin_path);
m.register_pass<ov::pass::Serialize>(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) {
@ -110,8 +90,7 @@ TEST_F(RTInfoSerializationTest, all_attributes_v10) {
auto init_info = [](RTMap& info) {
info[VariantWrapper<ngraph::FusedNames>::get_type_info_static()] =
std::make_shared<VariantWrapper<ngraph::FusedNames>>(ngraph::FusedNames("add"));
info[ov::PrimitivesPriority::get_type_info_static()] =
std::make_shared<ov::PrimitivesPriority>("priority");
info[ov::PrimitivesPriority::get_type_info_static()] = std::make_shared<ov::PrimitivesPriority>("priority");
};
std::shared_ptr<ngraph::Function> function;
@ -126,10 +105,10 @@ TEST_F(RTInfoSerializationTest, all_attributes_v10) {
}
pass::Manager m;
m.register_pass<pass::Serialize>(m_out_xml_path, m_out_bin_path, pass::Serialize::Version::IR_V10);
m.register_pass<ov::pass::Serialize>(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) {
@ -148,8 +127,7 @@ TEST_F(RTInfoSerializationTest, all_attributes_v11) {
auto init_info = [](RTMap& info) {
info[VariantWrapper<ngraph::FusedNames>::get_type_info_static()] =
std::make_shared<VariantWrapper<ngraph::FusedNames>>(ngraph::FusedNames("add"));
info[ov::PrimitivesPriority::get_type_info_static()] =
std::make_shared<ov::PrimitivesPriority>("priority");
info[ov::PrimitivesPriority::get_type_info_static()] = std::make_shared<ov::PrimitivesPriority>("priority");
};
std::shared_ptr<ngraph::Function> function;
@ -164,10 +142,10 @@ TEST_F(RTInfoSerializationTest, all_attributes_v11) {
}
pass::Manager m;
m.register_pass<pass::Serialize>(m_out_xml_path, m_out_bin_path);
m.register_pass<ov::pass::Serialize>(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) {
@ -216,10 +194,10 @@ TEST_F(RTInfoSerializationTest, parameter_result_v11) {
}
pass::Manager m;
m.register_pass<pass::Serialize>(m_out_xml_path, m_out_bin_path, pass::Serialize::Version::IR_V11);
m.register_pass<ov::pass::Serialize>(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());

View File

@ -0,0 +1,88 @@
// Copyright (C) 2018-2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#include "openvino/pass/serialize.hpp"
#include <gtest/gtest.h>
#include <fstream>
#include "openvino/util/file_util.hpp"
#include "read_ir.hpp"
#include "util/graph_comparator.hpp"
#include "util/test_common.hpp"
using SerializationParams = std::tuple<std::string, std::string>;
class SerializationTest : public ov::test::TestsCommon, public testing::WithParamInterface<SerializationParams> {
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

View File

@ -4,16 +4,13 @@
#include <gtest/gtest.h>
#include <file_utils.h>
#include <ie_api.h>
#include <ie_iextension.h>
#include "common_test_utils/ngraph_test_utils.hpp"
#include "ie_core.hpp"
#include "ngraph/ngraph.hpp"
#include "transformations/serialize.hpp"
#include <ngraph/opsets/opset6.hpp>
#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<ngraph::Function> function;
{
auto parameter = std::make_shared<ngraph::opset6::Parameter>(ngraph::element::Type_t::f32, ngraph::Shape{1, 3, 10, 10});
auto parameter =
std::make_shared<ov::opset8::Parameter>(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<ngraph::opset6::Relu>(parameter);
auto relu_prev = std::make_shared<ov::opset8::Relu>(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<ngraph::opset6::Relu>(relu_prev);
auto relu = std::make_shared<ov::opset8::Relu>(relu_prev);
relu->set_friendly_name("relu");
relu->get_output_tensor(0).set_names({"relu,t", "identity"});
const ngraph::ResultVector results{std::make_shared<ngraph::opset6::Result>(relu)};
const ngraph::ResultVector results{std::make_shared<ov::opset8::Result>(relu)};
results[0]->set_friendly_name("out");
ngraph::ParameterVector params{parameter};
function = std::make_shared<ngraph::Function>(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());
const auto res = fc.compare(result, function);
EXPECT_TRUE(res.valid) << res.message;
}

View File

@ -0,0 +1,89 @@
// Copyright (C) 2018-2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#include "test_common.hpp"
#include <algorithm>
#include <cctype>
#include <chrono>
#ifdef _WIN32
# ifndef NOMINMAX
# define NOMINMAX
# endif
# define _WINSOCKAPI_
# include <windows.h>
# 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<std::chrono::nanoseconds>(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

View File

@ -0,0 +1,25 @@
// Copyright (C) 2018-2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#pragma once
#include <gtest/gtest.h>
#include <memory>
#include <string>
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