Skip to content

Commit d19dc49

Browse files
authored
[core] Custom IR (de) serializer to store/read model without weights (#31969)
### Details: - Add IR xml utils for serialization and deserialization. - Add test sample with customized serializer and de-serializer to not store the weights in IR xml. The weights are passed as bin file (origin weights) and weights map for Constants added during model transformation ### Tickets: - CVS-172369 --------- Signed-off-by: Raasz, Pawel <[email protected]>
1 parent a984198 commit d19dc49

File tree

9 files changed

+451
-50
lines changed

9 files changed

+451
-50
lines changed

src/core/dev_api/openvino/xml_util/constant_writer.hpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,11 @@
1313

1414
namespace ov::util {
1515

16-
class OPENVINO_API OstreamHashWrapperBin final : public std::streambuf {
16+
class OstreamHashWrapperBin final : public std::streambuf {
1717
uint64_t m_res = 0lu;
1818

1919
public:
20-
uint64_t get_result() const {
21-
return m_res;
22-
}
20+
uint64_t get_result() const;
2321
std::streamsize xsputn(const char* s, std::streamsize n) override;
2422
};
2523

src/core/src/xml_util/constant_writer.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111

1212
namespace ov::util {
1313

14+
uint64_t OstreamHashWrapperBin::get_result() const {
15+
return m_res;
16+
}
17+
1418
std::streamsize OstreamHashWrapperBin::xsputn(const char* s, std::streamsize n) {
1519
m_res = u64_hash_combine(m_res, *reinterpret_cast<const uint64_t*>(s));
1620
return n;

src/core/tests/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,16 @@ ov_add_test_target(
4444
Threads::Threads
4545
openvino::conditional_compilation
4646
openvino::runtime::dev
47+
openvino::xml_util
4748
ADD_CLANG_FORMAT
4849
LABELS
4950
OV UNIT CORE
5051
)
5152

5253
target_include_directories(${TARGET_NAME} PRIVATE $<TARGET_PROPERTY:openvino_core_obj,SOURCE_DIR>/src
5354
${CMAKE_CURRENT_SOURCE_DIR})
55+
# LTO
56+
set_target_properties(${TARGET_NAME} PROPERTIES INTERPROCEDURAL_OPTIMIZATION_RELEASE ${ENABLE_LTO})
5457

5558
target_compile_definitions(${TARGET_NAME}
5659
PRIVATE

src/core/tests/xml_util/custom_ir.cpp

Lines changed: 422 additions & 0 deletions
Large diffs are not rendered by default.

src/core/xml_util/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ target_include_directories(${TARGET_NAME} PUBLIC
2727

2828
ov_add_clang_format_target(${TARGET_NAME}_clang FOR_TARGETS ${TARGET_NAME})
2929
target_link_libraries(${TARGET_NAME} PRIVATE openvino::runtime)
30+
# LTO
31+
set_target_properties(${TARGET_NAME} PROPERTIES INTERPROCEDURAL_OPTIMIZATION_RELEASE ${ENABLE_LTO})
3032

3133
if(NOT BUILD_SHARED_LIBS)
3234
target_compile_definitions(${TARGET_NAME} PUBLIC OPENVINO_STATIC_LIBRARY)

src/core/xml_util/include/openvino/xml_util/xml_deserialize_util.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ void str_to_container(const std::string& value, T& res) {
3535
}
3636
}
3737

38+
template <>
39+
void str_to_container<std::vector<std::string>>(const std::string& value, std::vector<std::string>& res);
40+
3841
class XmlDeserializer : public ov::AttributeVisitor {
3942
public:
4043
explicit XmlDeserializer(const pugi::xml_node& node,

src/core/xml_util/src/xml_deserialize_util.cpp

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,18 @@
2929
#include "transformations/rt_info/attributes.hpp"
3030

3131
namespace ov::util {
32+
33+
template <>
34+
void str_to_container<std::vector<std::string>>(const std::string& value, std::vector<std::string>& res) {
35+
std::stringstream ss(value);
36+
std::string field;
37+
while (getline(ss, field, ',')) {
38+
field = ov::util::trim(field);
39+
if (!field.empty()) {
40+
res.emplace_back(field);
41+
}
42+
}
43+
}
3244
namespace {
3345

3446
bool getStrAttribute(const pugi::xml_node& node, const std::string& name, std::string& value) {
@@ -91,17 +103,6 @@ void str_to_set_of_strings(const std::string& value, std::set<std::string>& res)
91103
}
92104
}
93105

94-
void str_to_container(const std::string& value, std::vector<std::string>& res) {
95-
std::stringstream ss(value);
96-
std::string field;
97-
while (getline(ss, field, ',')) {
98-
field = ov::util::trim(field);
99-
if (!field.empty()) {
100-
res.emplace_back(field);
101-
}
102-
}
103-
}
104-
105106
/**
106107
* @brief Function deserializing tensor names.
107108
*

src/frontends/ir/src/utils.hpp

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,4 @@
1212

1313
namespace ov {
1414
void operator>>(const std::stringstream& in, ov::element::Type& type);
15-
16-
template <class T>
17-
void str_to_container(const std::string& value, T& res) {
18-
std::stringstream ss(value);
19-
std::string field;
20-
while (getline(ss, field, ',')) {
21-
if (field.empty())
22-
OPENVINO_THROW("Cannot get vector of parameters! \"", value, "\" is incorrect");
23-
std::stringstream fs(field);
24-
typename T::value_type val;
25-
fs >> val;
26-
res.insert(res.end(), val);
27-
}
28-
}
29-
30-
template <class T>
31-
T stringToType(const std::string& valStr) {
32-
T ret{0};
33-
std::istringstream ss(valStr);
34-
if (!ss.eof()) {
35-
ss >> ret;
36-
}
37-
return ret;
38-
}
3915
} // namespace ov

src/plugins/intel_cpu/src/nodes/proposal.cpp

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
#include <memory>
1010
#include <oneapi/dnnl/dnnl_common.hpp>
1111
#include <string>
12-
#include <utility>
1312
#include <vector>
1413

1514
#include "cpu_types.h"
@@ -48,16 +47,9 @@ static std::vector<float> generate_anchors(proposal_conf& conf) {
4847
// enumerate all transformed boxes
4948
for (size_t ratio = 0; ratio < num_ratios; ++ratio) {
5049
// transformed width & height for given ratio factors
51-
const auto [ratio_w, ratio_h] = [&]() {
52-
if (round_ratios) {
53-
float rw = std::roundf(std::sqrt(base_area / ratios[ratio]));
54-
float rh = std::roundf(rw * ratios[ratio]);
55-
return std::make_pair(rw, rh);
56-
}
57-
float rw = std::sqrt(base_area / ratios[ratio]);
58-
float rh = rw * ratios[ratio];
59-
return std::make_pair(rw, rh);
60-
}();
50+
const float ratio_w =
51+
round_ratios ? std::roundf(std::sqrt(base_area / ratios[ratio])) : std::sqrt(base_area / ratios[ratio]);
52+
const float ratio_h = round_ratios ? std::roundf(ratio_w * ratios[ratio]) : ratio_w * ratios[ratio];
6153

6254
float* const p_anchors_wm = anchors_ptr + 0 * num_ratios * num_scales + ratio * num_scales;
6355
float* const p_anchors_hm = anchors_ptr + 1 * num_ratios * num_scales + ratio * num_scales;

0 commit comments

Comments
 (0)