Skip to content

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

[core] Improve Constant get_vector for low precisions (#26177)

Pawel Raaszb1cd3aa867ac
diff --git a/src/core/include/openvino/op/constant.hpp b/src/core/include/openvino/op/constant.hpp
index 8247e41cd3f..df630e96576 100644
--- a/src/core/include/openvino/op/constant.hpp
+++ b/src/core/include/openvino/op/constant.hpp
@@ -277,13 +277,33 @@ public:
/// \return The initialization literals for the tensor constant.
std::vector<std::string> get_value_strings() const;
- template <typename T>
+ /// @brief Get constant buffer as vector of element type T.
+ ///
+ /// For low precision the vector do not perform bit unpacks.
+ /// The returned vector has N elements where:
+ /// - N is (elements count * (precision byte size / T byte size)) for standard precisions.
+ /// - N is (byte size) for low precisions.
+ ///
+ /// @tparam T Output vector type which byte size must be less or equal of byte size of Constant's precision.
+ /// @return Vector of N elements of Type T.
+ template <typename T, typename std::enable_if<!std::is_same<bool, T>::value>::type* = nullptr>
std::vector<T> get_vector() const {
- const T* p = get_data_ptr<T>();
- if (p == nullptr) {
- OPENVINO_THROW("Cannot create vector! Buffer is not allocated.");
+ const auto p = get_data_ptr<T>();
+ OPENVINO_ASSERT(p != nullptr, "Cannot create vector! Buffer is not allocated.");
+ auto v = std::vector<T>(p, p + (get_byte_size() / sizeof(T)));
+ if (!m_alloc_buffer_on_visit_attributes) {
+ // result vector requires update when Constant share data (e.g read weight from IR binary file)
+ set_unused_bits(v.data());
}
- return std::vector<T>(p, p + shape_size(m_shape));
+ return v;
+ }
+
+ template <typename T, typename std::enable_if<std::is_same<bool, T>::value>::type* = nullptr>
+ std::vector<T> get_vector() const {
+ const auto p = get_data_ptr<T>();
+ OPENVINO_ASSERT(p != nullptr, "Cannot create vector! Buffer is not allocated.");
+ auto v = std::vector<T>(p, p + (get_byte_size() / sizeof(T)));
+ return v;
}
/// \brief Return the Constant's value as a vector cast to type T
@@ -403,7 +423,7 @@ public:
void alloc_buffer_on_visit_attributes(bool val);
/// @brief Get view on constant data as tensor.
- /// @return OV::Tensor with constant data.
+ /// @return ov::Tensor with constant data.
const Tensor get_tensor_view() const;
/// @return Constant's strides in bytes.
@@ -414,6 +434,13 @@ private:
size_t get_num_elements_to_cast(const int64_t n) const;
+ /// \brief Sets buffer's not used bits to zero.
+ ///
+ /// In case of low precision there can be some storage area which is not used (not defined state).
+ ///
+ /// \param buffer Pointer to buffer with Constant values.
+ void set_unused_bits(void* buffer) const;
+
#ifdef __clang__
# pragma clang diagnostic push
# ifdef __has_warning
diff --git a/src/core/src/op/constant.cpp b/src/core/src/op/constant.cpp
index fb308655378..90b293fb564 100644
--- a/src/core/src/op/constant.cpp
+++ b/src/core/src/op/constant.cpp
@@ -258,9 +258,41 @@ void Constant::allocate_buffer(bool memset_allocation) {
if (m_element_type == ov::element::string) {
m_data = std::make_shared<StringAlignedBuffer>(num_elements, byte_size, host_alignment(), memset_allocation);
} else {
+ constexpr uint8_t init_value = 0;
m_data = std::make_shared<AlignedBuffer>(byte_size, host_alignment());
+
if (memset_allocation) {
- std::memset(m_data->get_ptr(), 0, m_data->size());
+ std::memset(m_data->get_ptr(), init_value, m_data->size());
+ } else {
+ set_unused_bits(m_data->get_ptr());
+ }
+ }
+}
+
+void Constant::set_unused_bits(void* buffer) const {
+ const auto byte_size = m_data->size();
+
+ if (byte_size > 0) {
+ const auto num_elements = shape_size(m_shape);
+
+ if (element::is_bit_type(m_element_type)) {
+ constexpr size_t storage_unit_byte_size = 1;
+ const auto not_aligned_elements = num_elements % (8 / m_element_type.bitwidth());
+ const uint8_t not_used_bits_mask = 0xff >> (m_element_type.bitwidth() * not_aligned_elements);
+ reinterpret_cast<uint8_t*>(buffer)[byte_size - storage_unit_byte_size] &= ~not_used_bits_mask;
+ } else if (element::is_nibble_type(m_element_type) && (num_elements % 2)) {
+ constexpr size_t storage_unit_byte_size = 1;
+ reinterpret_cast<uint8_t*>(buffer)[byte_size - storage_unit_byte_size] &= 0x0FU;
+ } else if (element::is_split_bit_type(m_element_type)) {
+ constexpr size_t storage_unit_byte_size = 3;
+ const auto num_values = (24U / m_element_type.bitwidth());
+ const auto not_aligned_elements = num_elements % num_values;
+ const uint16_t not_used_upper_mask = ~(0xffff >> (not_aligned_elements * (16U / num_values)));
+
+ auto ptr = reinterpret_cast<uint8_t*>(buffer) + (byte_size - storage_unit_byte_size);
+ ptr[0] &= not_used_upper_mask >> 8U;
+ ptr[1] &= not_used_upper_mask & 0x00ff;
+ ptr[2] &= ~(0xff >> (not_aligned_elements * (8U / num_values)));
}
}
}
@@ -290,7 +322,8 @@ Constant::Constant(const Constant& other)
m_byte_strides{other.m_byte_strides},
m_data{other.m_data},
m_all_elements_bitwise_identical{other.m_all_elements_bitwise_identical.load()},
- m_all_elements_bitwise_identical_checked{other.m_all_elements_bitwise_identical_checked.load()} {
+ m_all_elements_bitwise_identical_checked{other.m_all_elements_bitwise_identical_checked.load()},
+ m_alloc_buffer_on_visit_attributes{other.m_alloc_buffer_on_visit_attributes} {
constructor_validate_and_infer_types();
}
diff --git a/src/core/tests/constant.cpp b/src/core/tests/constant.cpp
index aaf91a4335f..012a2aab6cf 100644
--- a/src/core/tests/constant.cpp
+++ b/src/core/tests/constant.cpp
@@ -100,7 +100,7 @@ TEST(constant, boolean_vector) {
EXPECT_EQ(v[2], 1);
EXPECT_EQ(v[3], 0);
- const char* p = c.get_data_ptr<char>();
+ const auto p = c.get_vector<char>();
EXPECT_EQ(p[0], 1);
EXPECT_EQ(p[1], 0);
EXPECT_EQ(p[2], 1);
@@ -301,7 +301,7 @@ TEST(constant, int4_string) {
const auto p = c.get_data_ptr<uint8_t>();
EXPECT_EQ(0x01, p[0]);
- EXPECT_EQ(0x0F, p[1] & 0x0F);
+ EXPECT_EQ(0x0F, p[1]);
EXPECT_EQ(input, c.get_value_strings());
@@ -322,7 +322,7 @@ TEST(constant, int4_string_broadcast_negative_number) {
const auto p = c.get_data_ptr<uint8_t>();
EXPECT_EQ(0xFF, p[0]);
- EXPECT_EQ(0x0F, p[1] & 0x0F);
+ EXPECT_EQ(0x0F, p[1]);
EXPECT_EQ(std::vector<std::string>(3, "-1"), c.get_value_strings());
}
@@ -338,7 +338,7 @@ TEST(constant, int4_string_broadcast_positive_number) {
const auto p = c.get_data_ptr<uint8_t>();
EXPECT_EQ(0x11, p[0]);
- EXPECT_EQ(0x01, p[1] & 0x0F);
+ EXPECT_EQ(0x01, p[1]);
EXPECT_EQ(std::vector<std::string>(3, "1"), c.get_value_strings());
}
@@ -354,7 +354,7 @@ TEST(constant, int4_vector_negative_number) {
const auto p = c.get_data_ptr<uint8_t>();
EXPECT_EQ(0xEF, p[0]);
- EXPECT_EQ(0x0F, p[1] & 0x0F);
+ EXPECT_EQ(0x0F, p[1]);
}
TEST(constant, int4_vector_positive_number) {
@@ -368,7 +368,7 @@ TEST(constant, int4_vector_positive_number) {
const auto p = c.get_data_ptr<uint8_t>();
EXPECT_EQ(0x21, p[0]);
- EXPECT_EQ(0x05, p[1] & 0x0F);
+ EXPECT_EQ(0x05, p[1]);
}
TEST(constant, int4_vector_broadcast_negative_number) {
@@ -382,7 +382,7 @@ TEST(constant, int4_vector_broadcast_negative_number) {
const auto p = c.get_data_ptr<uint8_t>();
EXPECT_EQ(0xFF, p[0]);
- EXPECT_EQ(0x0F, p[1] & 0x0F);
+ EXPECT_EQ(0x0F, p[1]);
}
TEST(constant, int4_vector_broadcast_positive_number) {
@@ -394,9 +394,9 @@ TEST(constant, int4_vector_broadcast_positive_number) {
EXPECT_EQ(v[1], int8_t(3));
EXPECT_EQ(v[2], int8_t(3));
- const auto p = c.get_data_ptr<uint8_t>();
+ const auto p = c.get_vector<uint8_t>();
EXPECT_EQ(0x33, p[0]);
- EXPECT_EQ(0x03, p[1] & 0x0F);
+ EXPECT_EQ(0x03, p[1]);
}
TEST(constant, int4_input_value_validation) {
@@ -447,8 +447,6 @@ TEST(constant, int8_string) {
EXPECT_EQ(input, c.get_value_strings());
- EXPECT_EQ(input, c.get_value_strings());
-
for (unsigned i = 0; i != input.size(); ++i) {
EXPECT_EQ(input[i], c.convert_value_to_string(i));
}
@@ -1004,8 +1002,8 @@ TEST(constant, uint3_string_broadcast) {
const auto p = c.get_data_ptr<uint8_t>();
EXPECT_EQ(p[0], 0b01010101);
- EXPECT_EQ(p[1] & 0b11000000, 0b01000000);
- EXPECT_EQ(p[2] & 0b11000000, 0b11000000);
+ EXPECT_EQ(p[1], 0b01000000);
+ EXPECT_EQ(p[2], 0b11111000);
}
TEST(constant, uint3_vector_less_than_one_storage_unit) {
@@ -1018,10 +1016,10 @@ TEST(constant, uint3_vector_less_than_one_storage_unit) {
ASSERT_EQ(v.size(), shape_size(shape));
EXPECT_THAT(v, ElementsAre(5, 3, 1));
- const auto p = c.get_data_ptr<uint8_t>();
- EXPECT_EQ(p[0] & 0b11111100, 0b01110100);
- // p[1] ignore
- EXPECT_EQ(p[2] & 0b11100000, 0b10000000);
+ const auto p = c.get_vector<uint8_t>();
+ EXPECT_EQ(p[0], 0b01110100);
+ EXPECT_EQ(p[1], 0);
+ EXPECT_EQ(p[2], 0b10000000);
}
TEST(constant, uint3_vector_greater_than_one_storage_unit) {
@@ -1034,14 +1032,14 @@ TEST(constant, uint3_vector_greater_than_one_storage_unit) {
ASSERT_EQ(v.size(), shape_size(shape));
EXPECT_THAT(v, ElementsAre(2, 3, 1, 0, 4, 5, 6, 7, 5, 2));
- const auto p = c.get_data_ptr<uint8_t>();
+ const auto p = c.get_vector<uint8_t>();
EXPECT_EQ(p[0], 0b10110100);
EXPECT_EQ(p[1], 0b00011011);
EXPECT_EQ(p[2], 0b00001111);
- EXPECT_EQ(p[3] & 0b11110000, 0b01100000);
- // p[4] ignore
- EXPECT_EQ(p[5] & 0b11000000, 0b10000000);
+ EXPECT_EQ(p[3], 0b01100000);
+ EXPECT_EQ(p[4], 0);
+ EXPECT_EQ(p[5], 0b10000000);
}
TEST(constant, uint3_vector_broadcast) {
@@ -1143,7 +1141,7 @@ TEST(constant, uint4_vector) {
EXPECT_EQ(v[2], 1);
EXPECT_EQ(v[3], 0);
- const auto p = c.get_data_ptr<uint8_t>();
+ const auto p = c.get_vector<uint8_t>();
EXPECT_EQ(p[0], 0x01);
EXPECT_EQ(p[1], 0x01);
}
@@ -1159,9 +1157,29 @@ TEST(constant, uint4_vector_broadcast) {
const auto p = c.get_data_ptr<uint8_t>();
const auto first_byte = p[0];
- const auto second_byte = p[1] & 0x0F;
+ const auto second_byte = p[1];
EXPECT_EQ(0x11, first_byte);
EXPECT_EQ(0x01, second_byte);
+
+ const auto vector = c.get_vector<uint8_t>();
+ EXPECT_EQ(vector[0], 0x11);
+ EXPECT_EQ(vector[1], 0x01);
+}
+
+TEST(constant, uint4_get_vector_from_one_element) {
+ auto c = std::make_shared<op::v0::Constant>(element::u4, Shape{1}, 9);
+ auto v = c->get_vector<uint8_t>();
+
+ ASSERT_EQ(v.size(), 1);
+ EXPECT_EQ(v[0], 0x09);
+}
+
+TEST(constant, uint4_get_vector_from_scalar) {
+ auto c = std::make_shared<op::v0::Constant>(element::u4, Shape{}, 8);
+ auto v = c->get_vector<uint8_t>();
+
+ ASSERT_EQ(v.size(), 1);
+ EXPECT_EQ(v[0], 0x08);
}
TEST(constant, uint4_input_value_validation) {
@@ -1240,8 +1258,8 @@ TEST(constant, uint6_vector_less_than_one_storage_unit) {
const auto p = c.get_data_ptr<uint8_t>();
EXPECT_EQ(p[0], 0x57);
- EXPECT_EQ(p[1] & 0xF0, 0x10);
- EXPECT_EQ(p[2] & 0b1111100, 0b00010000);
+ EXPECT_EQ(p[1], 0x10);
+ EXPECT_EQ(p[2], 0b00010000);
}
TEST(constant, uint6_vector_greater_than_one_storage_unit) {
@@ -1254,14 +1272,14 @@ TEST(constant, uint6_vector_greater_than_one_storage_unit) {
ASSERT_EQ(v.size(), shape_size(shape));
EXPECT_THAT(v, ElementsAre(25, 3, 1, 0, 45, 5));
- const auto p = c.get_data_ptr<uint8_t>();
+ const auto p = c.get_vector<uint8_t>();
EXPECT_EQ(p[0], 0x93);
EXPECT_EQ(p[1], 0x10);
EXPECT_EQ(p[2], 0b01000000);
EXPECT_EQ(p[3], 0xd5);
- // p[4] ignore
- EXPECT_EQ(p[5] & 0b11110000, 0b10000000);
+ EXPECT_EQ(p[4], 0);
+ EXPECT_EQ(p[5], 0b10000000);
}
TEST(constant, uint6_vector_broadcast) {
@@ -1667,7 +1685,7 @@ TEST(constant, nf4_write_custom_type) {
auto p = c.get_data_ptr<uint8_t>();
EXPECT_EQ(p[0], 0x20);
- EXPECT_EQ(p[1] & 0x0f, 0x0f);
+ EXPECT_EQ(p[1], 0x0f);
EXPECT_THROW(c.get_strides(), Exception);
}
@@ -2016,7 +2034,7 @@ TEST(constant, f4e2m1_vector) {
const auto p = c.get_data_ptr<uint8_t>();
EXPECT_EQ(p[0], 0x6b);
EXPECT_EQ(p[1], 0x3c);
- EXPECT_EQ(p[2] & 0x0F, 0x0d);
+ EXPECT_EQ(p[2], 0x0d);
}
TEST(constant, f4e2m1_from_float_vector) {
@@ -2024,10 +2042,10 @@ TEST(constant, f4e2m1_from_float_vector) {
auto v = c.cast_vector<float>();
EXPECT_THAT(v, ElementsAre(-1.5f, 4.0f, -2.0f, 1.5f, -3.0f));
- const auto p = c.get_data_ptr<uint8_t>();
+ const auto p = c.get_vector<uint8_t>();
EXPECT_EQ(p[0], 0x6b);
EXPECT_EQ(p[1], 0x3c);
- EXPECT_EQ(p[2] & 0x0F, 0x0d);
+ EXPECT_EQ(p[2], 0x0d);
}
TEST(constant, f4e2m1_vector_broadcast) {
@@ -2039,7 +2057,7 @@ TEST(constant, f4e2m1_vector_broadcast) {
const auto p = c.get_data_ptr<uint8_t>();
EXPECT_EQ(0x33, p[0]);
- EXPECT_EQ(0x03, p[1] & 0x0F);
+ EXPECT_EQ(0x03, p[1]);
}
TEST(constant, f4e2m1_write_then_cast_custom_type) {
@@ -2109,7 +2127,7 @@ TEST(constant, f8e8m0_from_float_vector) {
auto v = c.cast_vector<float>();
EXPECT_THAT(v, ElementsAre(std::numeric_limits<float>::min() / 2, 4.0f, 2.0f, 2.0f, 2.0f));
- const auto p = c.get_data_ptr<uint8_t>();
+ const auto p = c.get_vector<uint8_t>();
EXPECT_EQ(p[0], 0x00);
EXPECT_EQ(p[1], 0x81);
EXPECT_EQ(p[2], 0x80);
diff --git a/src/frontends/ir/tests/frontend_test_basic.cpp b/src/frontends/ir/tests/frontend_test_basic.cpp
index 5780168ce80..ca1a244ffc7 100644
--- a/src/frontends/ir/tests/frontend_test_basic.cpp
+++ b/src/frontends/ir/tests/frontend_test_basic.cpp
@@ -356,6 +356,92 @@ TEST_P(IRFrontendMMapTests, model_with_weights_reading_from_disk) {
EXPECT_TRUE(res.valid) << res.message;
}
+TEST_P(IRFrontendMMapTests, model_with_lp_weights_reading_from_disk) {
+ std::string xmlModel = R"V0G0N(
+<?xml version="1.0" ?>
+<net name="Model" version="11">
+ <layers>
+ <layer id="0" name="A" type="Parameter" version="opset1">
+ <data shape="" element_type="u4" />
+ <output>
+ <port id="0" precision="U4" names="" />
+ </output>
+ </layer>
+ <layer id="1" name="my_const" type="Const" version="opset1">
+ <data element_type="u4" shape="" offset="0" size="1" />
+ <output>
+ <port id="0" precision="U4" />
+ </output>
+ </layer>
+ <layer id="2" name="Add_4" type="Add" version="opset1">
+ <data auto_broadcast="numpy" />
+ <input>
+ <port id="0" precision="U4" />
+ <port id="1" precision="U4" />
+ </input>
+ <output>
+ <port id="2" precision="U4" />
+ </output>
+ </layer>
+ <layer id="3" name="Result_5" type="Result" version="opset1">
+ <input>
+ <port id="0" precision="U4" />
+ </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>
+ <rt_info />
+</net>
+)V0G0N";
+
+ std::vector<unsigned char> buffer(1, 0);
+ auto buffer_ptr = reinterpret_cast<uint8_t*>(buffer.data());
+ buffer_ptr[0] = 0x18;
+ auto t = ov::Tensor(ov::element::u4, ov::Shape{}, buffer.data());
+
+ createTemporalModelFile(xmlModel, buffer);
+
+ std::shared_ptr<ov::Model> model;
+
+ ov::Core new_core;
+ new_core.set_property(ov::enable_mmap(GetParam()));
+ OV_ASSERT_NO_THROW(model = new_core.read_model(xmlFileName, binFileName));
+ ASSERT_TRUE(!!model);
+
+ std::shared_ptr<ov::Model> modelRef;
+ {
+ auto parameter = std::make_shared<ov::opset1::Parameter>(ov::element::u4, ov::Shape{});
+ parameter->set_friendly_name("A");
+ auto constant = std::make_shared<ov::opset1::Constant>(t);
+ constant->set_friendly_name("my_const");
+ auto transpose = std::make_shared<ov::opset1::Add>(parameter, constant);
+ transpose->set_friendly_name("Add_4");
+ auto result = std::make_shared<ov::opset1::Result>(transpose);
+ result->set_friendly_name("Result_5");
+ modelRef = std::make_shared<ov::Model>(ov::NodeVector{result}, ov::ParameterVector{parameter});
+ }
+
+ const auto fc = FunctionsComparator::with_default()
+ .enable(FunctionsComparator::ATTRIBUTES)
+ .enable(FunctionsComparator::PRECISIONS)
+ .enable(FunctionsComparator::RUNTIME_KEYS)
+ .enable(FunctionsComparator::NAMES)
+ .enable(FunctionsComparator::CONST_VALUES);
+ const auto res = fc.compare(model, modelRef);
+ EXPECT_TRUE(res.valid) << res.message;
+
+ for (auto&& op : model->get_ops()) {
+ if (auto c = ov::as_type<ov::op::v0::Constant>(op.get())) {
+ const auto v = c->get_vector<uint8_t>();
+ EXPECT_EQ(v[0], 0x08);
+ }
+ }
+}
+
INSTANTIATE_TEST_SUITE_P(EnableMMapPropery, IRFrontendMMapTests, ::testing::Bool());
TEST_F(IRFrontendTests, model_without_weights_reading_from_disk) {