Skip to content

Commit

Permalink
[core] Extend model serialize API to support std::filesystem::path (#…
Browse files Browse the repository at this point in the history
…28043)

### Details:
 - Extend `Serialize` and `save_model` API to support cpp17 path class

### Tickets:
 - CVS-158889

---------

Signed-off-by: Raasz, Pawel <[email protected]>
  • Loading branch information
praasz authored Dec 13, 2024
1 parent 9128e5d commit 6b2f8ec
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 4 deletions.
30 changes: 26 additions & 4 deletions src/core/include/openvino/core/graph_util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
#include "openvino/op/parameter.hpp"
#include "openvino/pass/serialize.hpp"

#ifdef OPENVINO_CPP_VER_17
# include <filesystem>
#endif

namespace ov {

OPENVINO_API
Expand Down Expand Up @@ -288,27 +292,45 @@ bool replace_node_update_name(const std::shared_ptr<Node>& target, const std::sh
/// \param bin_path Path where .bin file will be saved (optional).
/// The same name as for xml_path will be used by default.
/// \param version Version of the generated IR (optional).
/// \{
OPENVINO_API
void serialize(const std::shared_ptr<const ov::Model>& m,
const std::string& xml_path,
const std::string& bin_path = "",
ov::pass::Serialize::Version version = ov::pass::Serialize::Version::UNSPECIFIED);

#ifdef OPENVINO_CPP_VER_17
template <class Path, std::enable_if_t<std::is_same_v<Path, std::filesystem::path>>* = nullptr>
void serialize(const std::shared_ptr<const ov::Model>& m,
const Path& xml_path,
const Path& bin_path = {""},
ov::pass::Serialize::Version version = ov::pass::Serialize::Version::UNSPECIFIED) {
serialize(m, xml_path.string(), bin_path.string(), version);
}
#endif
/// \}

/// \brief Save given model into IR. Floating point weights are compressed to FP16 by default.
/// This method saves a model to IR applying all necessary transformations that usually applied
/// in model conversion flow provided by mo tool. Paricularly, floatting point weights are compressed to FP16.
/// in model conversion flow provided by mo tool. Particularly, floating point weights are compressed to FP16.
/// \param model Model which will be converted to IR representation.
/// \param output_model Path to the output model file, must have extension .xml
/// \param compress_to_fp16 Whether to compress floatting point weights to FP16 (true by default)
/// \param compress_to_fp16 Whether to compress floating point weights to FP16 (true by default)
OPENVINO_API
void save_model(const std::shared_ptr<const ov::Model>& model,
const std::string& output_model,
bool compress_to_fp16 = true);

#if defined(OPENVINO_ENABLE_UNICODE_PATH_SUPPORT)
OPENVINO_API
void save_model(const std::shared_ptr<const ov::Model>& model,
const std::wstring& output_model,
bool compress_to_fp16 = true);
#endif
} // namespace ov

#ifdef OPENVINO_CPP_VER_17
template <class Path, std::enable_if_t<std::is_same_v<Path, std::filesystem::path>>* = nullptr>
void save_model(const std::shared_ptr<const ov::Model>& model, const Path& output_model, bool compress_to_fp16 = true) {
save_model(model, output_model.string(), compress_to_fp16);
}
#endif
} // namespace ov
11 changes: 11 additions & 0 deletions src/core/include/openvino/pass/serialize.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
#include "openvino/opsets/opset.hpp"
#include "openvino/pass/pass.hpp"

#ifdef OPENVINO_CPP_VER_17
# include <filesystem>
#endif

namespace ov {
namespace pass {

Expand All @@ -35,6 +39,13 @@ class OPENVINO_API Serialize : public ov::pass::ModelPass {

Serialize(const std::string& xmlPath, const std::string& binPath, Version version = Version::UNSPECIFIED);

#ifdef OPENVINO_CPP_VER_17
Serialize(const std::filesystem::path& xmlPath,
const std::filesystem::path& binPath,
Version version = Version::UNSPECIFIED)
: Serialize(xmlPath.string(), binPath.string(), version) {}
#endif

private:
std::ostream* m_xmlFile;
std::ostream* m_binFile;
Expand Down
41 changes: 41 additions & 0 deletions src/core/tests/pass/serialization/deterministicity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,47 @@ TEST_P(SerializationDeterministicityInputOutputTest, FromIrModel) {
EXPECT_TRUE(files_equal(xml_2, xml_1));
}

#ifdef OPENVINO_CPP_VER_17
TEST_P(SerializationDeterministicityInputOutputTest, FromOvModelBybPath) {
auto irVersion = GetParam();

std::shared_ptr<ov::Model> modelRef;
{
auto parameter0 = std::make_shared<ov::opset1::Parameter>(ov::element::f32, ov::Shape{1, 3, 22, 22});
parameter0->set_friendly_name("input0");
auto result0 = std::make_shared<ov::opset1::Result>(parameter0);
result0->set_friendly_name("output0");
auto parameter1 = std::make_shared<ov::opset1::Parameter>(ov::element::f32, ov::Shape{1, 3, 22, 22});
parameter1->set_friendly_name("input1");
auto result1 = std::make_shared<ov::opset1::Result>(parameter1);
result1->set_friendly_name("output1");
modelRef =
std::make_shared<ov::Model>(ov::NodeVector{result0, result1}, ov::ParameterVector{parameter0, parameter1});
}

auto& expected1 = modelRef;
const auto out_xml_path = std::filesystem::path(m_out_xml_path_1);
const auto out_bin_path = std::filesystem::path(m_out_bin_path_1);
ov::pass::Serialize(out_xml_path, out_bin_path, irVersion).run_on_model(modelRef);
auto expected2 = ov::test::readModel(m_out_xml_path_1, m_out_bin_path_1);

ov::pass::Serialize(m_out_xml_path_2, m_out_bin_path_2, irVersion).run_on_model(expected2);

EXPECT_EQ(input0Name, expected1->input(0).get_node()->get_friendly_name());
EXPECT_EQ(input1Name, expected1->input(1).get_node()->get_friendly_name());
EXPECT_EQ(output0Name, expected1->output(0).get_node()->get_friendly_name());
EXPECT_EQ(output1Name, expected1->output(1).get_node()->get_friendly_name());
EXPECT_EQ(input0Name, expected2->input(0).get_node()->get_friendly_name());
EXPECT_EQ(input1Name, expected2->input(1).get_node()->get_friendly_name());
EXPECT_EQ(output0Name, expected2->output(0).get_node()->get_friendly_name());
EXPECT_EQ(output1Name, expected2->output(1).get_node()->get_friendly_name());

std::ifstream xml_1(m_out_xml_path_1, std::ios::in | std::ios::binary);
std::ifstream xml_2(m_out_xml_path_2, std::ios::in | std::ios::binary);
EXPECT_TRUE(files_equal(xml_1, xml_2));
}
#endif

INSTANTIATE_TEST_SUITE_P(DeterministicityInputOutput,
SerializationDeterministicityInputOutputTest,
::testing::Values(ov::pass::Serialize::Version::IR_V10, ov::pass::Serialize::Version::IR_V11));
17 changes: 17 additions & 0 deletions src/core/tests/pass/serialization/serialize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,23 @@ TEST_P(SerializationTest, SaveModel) {
});
}

#ifdef OPENVINO_CPP_VER_17
TEST_P(SerializationTest, CompareFunctionsByPath) {
const auto out_xml_path = std::filesystem::path(m_out_xml_path);
const auto out_bin_path = std::filesystem::path(m_out_bin_path);
CompareSerialized([&out_xml_path, &out_bin_path](const auto& m) {
ov::pass::Serialize(out_xml_path, out_bin_path).run_on_model(m);
});
}

TEST_P(SerializationTest, SaveModelByPath) {
const auto out_xml_path = std::filesystem::path(m_out_xml_path);
CompareSerialized([&out_xml_path](const auto& m) {
ov::save_model(m, out_xml_path, false);
});
}
#endif

INSTANTIATE_TEST_SUITE_P(
IRSerialization,
SerializationTest,
Expand Down

0 comments on commit 6b2f8ec

Please sign in to comment.