Skip to content

Commit 445fc48

Browse files
authored
Set allocated for empty constant byte to zero (openvinotoolkit#29378)
### Details: - That is simplified fix for openvinotoolkit#29329, just to fix the particular issue with empty constant in PA transformation - Need to be reverted when openvinotoolkit#29329 is merged ### Tickets: - CVS-162291
1 parent 14cbd4d commit 445fc48

File tree

2 files changed

+73
-1
lines changed

2 files changed

+73
-1
lines changed

src/core/src/op/constant.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,8 @@ void Constant::allocate_buffer(bool memset_allocation) {
261261
constexpr uint8_t init_value = 0;
262262
m_data = std::make_shared<AlignedBuffer>(byte_size, host_alignment());
263263

264-
if (memset_allocation) {
264+
// AlignedBuffer allocates 1 byte for empty constants, and we set it to zero
265+
if (memset_allocation || byte_size == 0) {
265266
std::memset(m_data->get_ptr(), init_value, m_data->size());
266267
} else {
267268
set_unused_bits(m_data->get_ptr());

src/core/tests/pass/serialization/const_compression.cpp

+71
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77
#include <fstream>
88

99
#include "common_test_utils/common_utils.hpp"
10+
#include "common_test_utils/graph_comparator.hpp"
1011
#include "common_test_utils/test_common.hpp"
1112
#include "openvino/opsets/opset8.hpp"
1213
#include "openvino/pass/serialize.hpp"
14+
#include "openvino/runtime/core.hpp"
1315
#include "transformations/common_optimizations/compress_float_constants.hpp"
1416

1517
class SerializationConstantCompressionTest : public ov::test::TestsCommon {
@@ -312,3 +314,72 @@ TEST_F(SerializationConstantCompressionTest, IdenticalConstantsDifferentTypesI32
312314

313315
ASSERT_EQ(file_size(bin_1), unique_const_count * ov::shape_size(shape) * sizeof(int32_t));
314316
}
317+
318+
TEST_F(SerializationConstantCompressionTest, EmptyConstants) {
319+
constexpr int unique_const_count = 1;
320+
auto A = ov::opset8::Constant::create(ov::element::i32, ov::Shape{0}, std::vector<int32_t>{});
321+
auto B = ov::opset8::Constant::create(ov::element::i32, ov::Shape{0}, std::vector<int32_t>{});
322+
323+
auto model_initial = std::make_shared<ov::Model>(ov::NodeVector{A, B}, ov::ParameterVector{});
324+
325+
ov::pass::Serialize(m_out_xml_path_1, m_out_bin_path_1).run_on_model(model_initial);
326+
327+
std::ifstream xml_1(m_out_xml_path_1, std::ios::binary);
328+
std::ifstream bin_1(m_out_bin_path_1, std::ios::binary);
329+
330+
ASSERT_EQ(file_size(bin_1), unique_const_count * sizeof(int8_t));
331+
332+
ov::Core core;
333+
auto model_imported = core.read_model(m_out_xml_path_1, m_out_bin_path_1);
334+
335+
bool success;
336+
std::string message;
337+
std::tie(success, message) = compare_functions(model_initial, model_imported, true, true, false, true, true);
338+
ASSERT_TRUE(success) << message;
339+
}
340+
341+
TEST_F(SerializationConstantCompressionTest, EmptyAndNotEmptyConstantSameValues) {
342+
constexpr int unique_const_count = 1;
343+
auto A = ov::opset8::Constant::create(ov::element::i32, ov::Shape{0}, std::vector<int32_t>{});
344+
auto B = ov::opset8::Constant::create(ov::element::i8, ov::Shape{1}, std::vector<int8_t>{0});
345+
346+
auto model_initial = std::make_shared<ov::Model>(ov::NodeVector{A, B}, ov::ParameterVector{});
347+
348+
ov::pass::Serialize(m_out_xml_path_1, m_out_bin_path_1).run_on_model(model_initial);
349+
350+
std::ifstream xml_1(m_out_xml_path_1, std::ios::binary);
351+
std::ifstream bin_1(m_out_bin_path_1, std::ios::binary);
352+
353+
ASSERT_EQ(file_size(bin_1), unique_const_count * sizeof(int8_t));
354+
355+
ov::Core core;
356+
auto model_imported = core.read_model(m_out_xml_path_1, m_out_bin_path_1);
357+
358+
bool success;
359+
std::string message;
360+
std::tie(success, message) = compare_functions(model_initial, model_imported, true, true, false, true, true);
361+
ASSERT_TRUE(success) << message;
362+
}
363+
364+
TEST_F(SerializationConstantCompressionTest, EmptyAndNotEmptyConstantsDifferentValues) {
365+
constexpr int unique_const_count = 2;
366+
auto A = ov::opset8::Constant::create(ov::element::i32, ov::Shape{0}, std::vector<int32_t>{});
367+
auto B = ov::opset8::Constant::create(ov::element::i8, ov::Shape{1}, std::vector<int8_t>{1});
368+
369+
auto model_initial = std::make_shared<ov::Model>(ov::NodeVector{A, B}, ov::ParameterVector{});
370+
371+
ov::pass::Serialize(m_out_xml_path_1, m_out_bin_path_1).run_on_model(model_initial);
372+
373+
std::ifstream xml_1(m_out_xml_path_1, std::ios::binary);
374+
std::ifstream bin_1(m_out_bin_path_1, std::ios::binary);
375+
376+
ASSERT_EQ(file_size(bin_1), unique_const_count * sizeof(int8_t));
377+
378+
ov::Core core;
379+
auto model_imported = core.read_model(m_out_xml_path_1, m_out_bin_path_1);
380+
381+
bool success;
382+
std::string message;
383+
std::tie(success, message) = compare_functions(model_initial, model_imported, true, true, false, true, true);
384+
ASSERT_TRUE(success) << message;
385+
}

0 commit comments

Comments
 (0)