Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

[core] Add util function to calculate padding to aligned size (#32381)

Pawel Raasz9aec408dcd03
diff --git a/src/core/dev_api/openvino/core/memory_util.hpp b/src/core/dev_api/openvino/core/memory_util.hpp
index cdf6a98bb45..8ef88d8c36f 100644
--- a/src/core/dev_api/openvino/core/memory_util.hpp
+++ b/src/core/dev_api/openvino/core/memory_util.hpp
@@ -38,4 +38,20 @@ OPENVINO_API std::optional<size_t> get_memory_size_safe(const element::Type& typ
* @return Memory size in bytes or std::nullopt if overflow occurs.
*/
OPENVINO_API std::optional<size_t> get_memory_size_safe(const element::Type& type, const ov::Shape& shape);
+
+/**
+ * @brief Calculates padding size in bytes to align given position to specified alignment.
+ *
+ * @param alignment The desired alignment value (bytes).
+ * @param pos Given position (address) from which padding size should be calculated (bytes).
+ * @return Padding size in bytes.
+ */
+constexpr size_t align_padding_size(size_t alignment, size_t pos) {
+ if (alignment != 0) {
+ const auto pad = (pos % alignment);
+ return pad == 0 ? pad : alignment - pad;
+ } else {
+ return 0;
+ }
+}
} // namespace ov::util
diff --git a/src/core/src/runtime/aligned_buffer.cpp b/src/core/src/runtime/aligned_buffer.cpp
index c42a0ce177f..ab457c3fb57 100644
--- a/src/core/src/runtime/aligned_buffer.cpp
+++ b/src/core/src/runtime/aligned_buffer.cpp
@@ -7,6 +7,8 @@
#include <algorithm>
#include <memory>
+#include "openvino/core/memory_util.hpp"
+
namespace ov {
AlignedBuffer::AlignedBuffer() : m_allocated_buffer(nullptr), m_aligned_buffer(nullptr), m_byte_size(0) {}
@@ -15,11 +17,7 @@ AlignedBuffer::AlignedBuffer(size_t byte_size, size_t alignment) : m_byte_size(b
size_t allocation_size = m_byte_size + alignment;
m_allocated_buffer = new char[allocation_size];
m_aligned_buffer = m_allocated_buffer;
- size_t mod = (alignment != 0) ? reinterpret_cast<size_t>(m_aligned_buffer) % alignment : 0;
-
- if (mod != 0) {
- m_aligned_buffer += (alignment - mod);
- }
+ m_aligned_buffer += util::align_padding_size(alignment, reinterpret_cast<size_t>(m_allocated_buffer));
}
AlignedBuffer::AlignedBuffer(AlignedBuffer&& other)
diff --git a/src/core/tests/memory_util.cpp b/src/core/tests/memory_util.cpp
index 5150323d9f7..b54912683dc 100644
--- a/src/core/tests/memory_util.cpp
+++ b/src/core/tests/memory_util.cpp
@@ -91,4 +91,31 @@ TEST(GetMemorySizeOverflowTest, zero_number_of_elements) {
EXPECT_EQ(ov::util::get_memory_size_safe(element::i16, 0), std::optional<size_t>(0));
EXPECT_EQ(ov::util::get_memory_size_safe(element::string, 0), std::optional<size_t>(0));
}
+
+using AlignTestParam = std::tuple<uintptr_t, size_t, size_t>;
+
+class AlignTest : public ::testing::TestWithParam<AlignTestParam> {};
+
+INSTANTIATE_TEST_SUITE_P(AlignTestSuite,
+ AlignTest,
+ testing::Values(AlignTestParam{0, 0, 0},
+ AlignTestParam{20, 0, 0},
+ AlignTestParam{0, 64, 0},
+ AlignTestParam{20, 64, 44},
+ AlignTestParam{64, 64, 0},
+ AlignTestParam{65, 64, 63},
+ AlignTestParam{128, 64, 0},
+ AlignTestParam{130, 64, 62},
+ AlignTestParam{0, 100, 0},
+ AlignTestParam{63, 100, 37},
+ AlignTestParam{130, 100, 70}),
+ testing::PrintToStringParamName());
+
+TEST_P(AlignTest, align_padding_size) {
+ const auto& [pos, aligment, expected] = GetParam();
+
+ const auto pad = ov::util::align_padding_size(aligment, pos);
+
+ EXPECT_EQ(pad, expected);
+}
} // namespace ov::test
diff --git a/src/inference/src/dev/compilation_context.cpp b/src/inference/src/dev/compilation_context.cpp
index 2ab46e42638..fbb3579e337 100644
--- a/src/inference/src/dev/compilation_context.cpp
+++ b/src/inference/src/dev/compilation_context.cpp
@@ -10,6 +10,7 @@
#endif
#include "itt.hpp"
+#include "openvino/core/memory_util.hpp"
#include "openvino/core/parallel.hpp"
#include "openvino/pass/manager.hpp"
#include "openvino/runtime/compilation_context.hpp"
@@ -184,9 +185,9 @@ CompiledBlobHeader::CompiledBlobHeader(const std::string& ieVersion,
std::istream& operator>>(std::istream& stream, CompiledBlobHeader& header) {
std::string xmlStr;
- auto start = stream.tellg();
+ const auto start = stream.tellg();
std::getline(stream, xmlStr);
- auto end = stream.tellg();
+ const auto bytes_read = static_cast<size_t>(stream.tellg() - start);
pugi::xml_document document;
pugi::xml_parse_result res = document.load_string(xmlStr.c_str());
@@ -198,10 +199,7 @@ std::istream& operator>>(std::istream& stream, CompiledBlobHeader& header) {
header.m_runtimeInfo = ov::util::pugixml::get_str_attr(compiledBlobNode, "runtime_info");
header.m_headerSizeAlignment = ov::util::pugixml::get_uint_attr(compiledBlobNode, "header_size_alignment");
- if (const uint32_t headerSizeAlignment = header.m_headerSizeAlignment; headerSizeAlignment) {
- size_t bytes_read = static_cast<size_t>(end - start);
- size_t pad =
- (headerSizeAlignment - (bytes_read % headerSizeAlignment)) % headerSizeAlignment; // 0 if already aligned
+ if (const auto pad = util::align_padding_size(header.m_headerSizeAlignment, bytes_read); pad > 0) {
stream.seekg(static_cast<std::streamoff>(pad), std::ios::cur);
OPENVINO_ASSERT(stream.good(), "Failed to seek over padding in compiled blob header");
}
@@ -218,19 +216,15 @@ std::ostream& operator<<(std::ostream& stream, const CompiledBlobHeader& header)
compiledBlobNode.append_attribute("header_size_alignment")
.set_value(std::to_string(header.m_headerSizeAlignment).c_str());
- auto start = stream.tellp();
+ const auto start = stream.tellp();
document.save(stream, nullptr, pugi::format_raw);
document.reset();
stream << std::endl;
- auto end = stream.tellp();
// add padding
- if (const uint32_t headerSizeAlignment = header.m_headerSizeAlignment; headerSizeAlignment) {
- size_t bytes_written = static_cast<size_t>(end - start);
- size_t pad = (headerSizeAlignment - (bytes_written % headerSizeAlignment)) %
- headerSizeAlignment; // 0 if already aligned
- std::fill_n(std::ostream_iterator<char>(stream), pad, 0);
- }
+ const auto bytes_written = static_cast<size_t>(stream.tellp() - start);
+ const auto pad = util::align_padding_size(header.get_header_size_alignment(), bytes_written);
+ std::fill_n(std::ostream_iterator<char>(stream), pad, 0);
return stream;
}
@@ -256,9 +250,8 @@ inline std::string getline_from_buffer(const char* buffer, size_t size, size_t&
} // namespace
void CompiledBlobHeader::read_from_buffer(const char* buffer, size_t buffer_size, size_t& pos) {
- size_t start = pos;
+ const auto start = pos;
std::string xmlStr = ov::getline_from_buffer(buffer, buffer_size, pos);
- size_t end = pos;
pugi::xml_document document;
pugi::xml_parse_result res = document.load_string(xmlStr.c_str());
@@ -270,11 +263,6 @@ void CompiledBlobHeader::read_from_buffer(const char* buffer, size_t buffer_size
m_runtimeInfo = ov::util::pugixml::get_str_attr(compiledBlobNode, "runtime_info");
m_headerSizeAlignment = ov::util::pugixml::get_uint_attr(compiledBlobNode, "header_size_alignment");
- if (const uint32_t headerSizeAlignment = m_headerSizeAlignment; headerSizeAlignment) {
- size_t bytes_read = static_cast<size_t>(end - start);
- size_t pad =
- (headerSizeAlignment - (bytes_read % headerSizeAlignment)) % headerSizeAlignment; // 0 if already aligned
- pos += pad;
- }
+ pos += util::align_padding_size(m_headerSizeAlignment, pos - start);
}
} // namespace ov