OpenVINO™ is an open source toolkit for optimizing and deploying AI inference
C++ aicomputer-visiondeep-learningdeploy-aidiffusion-modelsgenerative-aigood-first-issueinferencellm-inferencenatural-language-processingnlpopenvinooptimize-aiperformance-boostrecommendation-systemspeech-recognitionstable-diffusiontransformersyolo
[core] Change u2 values packing in byte (#31181)
Pawel Raaszd97acfdce00e
diff --git a/src/core/dev_api/openvino/core/type/element_iterator.hpp b/src/core/dev_api/openvino/core/type/element_iterator.hppindex 7f042104847..aed11325447 100644--- a/src/core/dev_api/openvino/core/type/element_iterator.hpp+++ b/src/core/dev_api/openvino/core/type/element_iterator.hpp@@ -67,6 +67,16 @@ constexpr bool is_byte_type(Type_t et) {return !is_bit_type(et) && !is_split_bit_type(et) && !is_nibble_type(et) && et != string;}+/**+ * @brief Checks if type is packet in byte from LSB -> MSB order.+ *+ * @param et Element type to check.+ * @return True if type is packet LSB first, false otherwise.+ */+constexpr bool is_lsb_packed(Type_t et) {+ return et != u1;+}+/*** @brief Gets bit width of ov::element::Type_t.*@@ -139,16 +149,17 @@ class BitProxy {};* @tparam ET OpenVINO element type.*/template <class T, Type_t ET>-class BitProxy<T, ET, typename std::enable_if<is_bit_type(ET) || is_nibble_type(ET)>::type> {+class BitProxy<T, ET, std::enable_if_t<is_bit_type(ET) || is_nibble_type(ET)>> {private:template <Type_t, class>friend class Iterator; //!< Iterator class is friend to access private members to manipulate pointer.- using Bits = typename std::conditional<std::is_const<T>::value, const uint8_t, uint8_t>::type;+ using Bits = std::conditional_t<std::is_const_v<T>, const uint8_t, uint8_t>;- static constexpr size_t m_bits = bit_width<ET>(); //!< Number of bit for single value.- static constexpr size_t m_num_values = 8 / m_bits; //!< Number values in byte.- static constexpr size_t m_shift_init = is_nibble_type(ET) ? 0 : 8 - m_bits; //!< Initial value for bit shift.+ static constexpr size_t m_bits = bit_width<ET>(); //!< Number of bit for single value.+ static constexpr size_t m_num_values = 8 / m_bits; //!< Number values in byte.+ static constexpr size_t m_shift_init = is_lsb_packed(ET) ? 0 : 8 - m_bits; //!< Initial value for bit shift.+ static constexpr size_t m_shift_last = is_lsb_packed(ET) ? 8 - m_bits : 0; //!< Last value for bit shift.Bits* m_ptr; //!< Pointer to T as Bits used to get value from bits.size_t m_bit_shift; //!< Current bit shift to get value.@@ -167,7 +178,7 @@ private:}public:- using value_type = typename std::decay<T>::type; //!< Fundamental type of bound to BitProxy.+ using value_type = std::decay_t<T>; //!< Fundamental type of bound to BitProxy./*** @brief Compare proxy value with other provided value.@@ -196,7 +207,7 @@ public:** @return Value of BitProxy.*/- template <Type_t ETT = ET, typename std::enable_if<ETT != i4 && ETT != f4e2m1>::type* = nullptr>+ template <Type_t ETT = ET, std::enable_if_t<ETT != i4 && ETT != f4e2m1>* = nullptr>operator value_type() const {return static_cast<value_type>(get_bit_value());}@@ -206,7 +217,7 @@ public:** @return Value of BitProxy.*/- template <Type_t ETT = ET, typename std::enable_if<ETT == f4e2m1>::type* = nullptr>+ template <Type_t ETT = ET, std::enable_if_t<ETT == f4e2m1>* = nullptr>operator value_type() const {return value_type::from_bits(get_bit_value());}@@ -219,12 +230,12 @@ public:** @return Converted NF4 value to float.*/- template <Type_t ETT = ET, typename std::enable_if<ETT == nf4>::type* = nullptr>+ template <Type_t ETT = ET, std::enable_if_t<ETT == nf4>* = nullptr>operator float() const {return ConvertNF4::dequantize(get_bit_value());}- template <Type_t ETT = ET, typename std::enable_if<ETT == f4e2m1>::type* = nullptr>+ template <Type_t ETT = ET, std::enable_if_t<ETT == f4e2m1>* = nullptr>operator float() const {return static_cast<float>(float4_e2m1::from_bits(get_bit_value()));}@@ -234,7 +245,7 @@ public:** @return Value of BitProxy.*/- template <Type_t ETT = ET, typename std::enable_if<ETT == i4>::type* = nullptr>+ template <Type_t ETT = ET, std::enable_if_t<ETT == i4>* = nullptr>operator value_type() const {constexpr auto value_mask = util::make_n_bit_mask(m_bits);constexpr auto value_msb_mask = (1U << (m_bits - 1U));@@ -252,7 +263,7 @@ public:* @brief Sets current ProxyBit to value.* @param v Value to be set.*/- template <Type_t ETT = ET, typename std::enable_if<ETT != f4e2m1>::type* = nullptr>+ template <Type_t ETT = ET, std::enable_if_t<ETT != f4e2m1>* = nullptr>BitProxy<T, ET>& operator=(const value_type v) {constexpr auto value_mask = util::make_n_bit_mask(m_bits);set_bit_value(static_cast<uint8_t>(v) & value_mask);@@ -263,7 +274,7 @@ public:* @brief Sets current ProxyBit to value (f4e2m1 specialization).* @param v Value to be set.*/- template <Type_t ETT = ET, typename std::enable_if<ETT == f4e2m1>::type* = nullptr>+ template <Type_t ETT = ET, std::enable_if_t<ETT == f4e2m1>* = nullptr>BitProxy<T, ET>& operator=(const value_type v) {set_bit_value(v.to_bits());return *this;@@ -273,7 +284,7 @@ public:* @brief Sets current NF4 value from float using quantization.* @param v Value to be set.*/- template <Type_t ETT = ET, typename std::enable_if<ETT == nf4>::type* = nullptr>+ template <Type_t ETT = ET, std::enable_if_t<ETT == nf4>* = nullptr>BitProxy<T, ET>& operator=(const float v) {set_bit_value(ConvertNF4::quantize(v));return *this;@@ -289,7 +300,7 @@ public:* @tparam ET OpenVINO element type.*/template <class T, Type_t ET>-class BitProxy<T, ET, typename std::enable_if<is_split_bit_type(ET)>::type> {+class BitProxy<T, ET, std::enable_if_t<is_split_bit_type(ET)>> {private:template <Type_t, class>friend class Iterator; //!< Iterator class is friend to access private members to manipulate pointer.@@ -314,7 +325,7 @@ private:constexpr BitProxy(T* ptr) noexcept : m_ptr{ptr}, m_bit_shift{m_shift_init} {}public:- using value_type = typename std::decay<T>::type; //!< Fundamental type of sub-byte.+ using value_type = std::decay_t<T>; //!< Fundamental type of sub-byte./*** @brief Compare proxy value is equal than rhs.@@ -411,41 +422,38 @@ public:using iterator_category = std::bidirectional_iterator_tag;using difference_type = std::ptrdiff_t;using value_type = T;- using reference = typename std::conditional<std::is_const<T>::value, const proxy_type&, proxy_type&>::type;- using pointer = typename std::conditional<std::is_const<T>::value, const proxy_type*, proxy_type*>::type;+ using reference = std::conditional_t<std::is_const<T>::value, const proxy_type&, proxy_type&>;+ using pointer = std::conditional_t<std::is_const<T>::value, const proxy_type*, proxy_type*>;- static_assert(std::is_same<typename std::decay<T>::type, ov::fundamental_type_for<ET>>::value,+ static_assert(std::is_same_v<std::decay_t<T>, ov::fundamental_type_for<ET>>,"Iterator value_type must be same as fundamental type of ET");constexpr Iterator(T* ptr) noexcept : m_et_ptr{ptr} {}template <class U>constexpr Iterator(U* ptr) noexcept : m_et_ptr{reinterpret_cast<T*>(ptr)} {- static_assert(std::is_same<typename std::decay<U>::type, int8_t>::value,+ static_assert(std::is_same_v<typename std::decay_t<U>, int8_t>,"User type must be int8_t as base type for LP ET");}// Iteration operators- template <Type_t ETT = ET>- typename std::enable_if<is_bit_type(ETT), Iterator<ET, T>>::type& operator++() {- m_et_ptr.m_bit_shift -= m_et_ptr.m_bits;- m_et_ptr.m_bit_shift = m_et_ptr.m_bit_shift % (m_et_ptr.m_num_values * m_et_ptr.m_bits);- m_et_ptr.m_ptr += static_cast<std::ptrdiff_t>(m_et_ptr.m_bit_shift == m_et_ptr.m_shift_init);- return *this;- }-- template <Type_t ETT = ET>- typename std::enable_if<is_nibble_type(ETT), Iterator<ET, T>>::type& operator++() {- m_et_ptr.m_bit_shift ^= m_et_ptr.m_bits;- m_et_ptr.m_ptr += static_cast<std::ptrdiff_t>(m_et_ptr.m_bit_shift == m_et_ptr.m_shift_init);- return *this;- }-- template <Type_t ETT = ET>- typename std::enable_if<is_split_bit_type(ETT), Iterator<ET, T>>::type& operator++() {- --m_et_ptr.m_bit_shift;- m_et_ptr.m_bit_shift = m_et_ptr.m_bit_shift % m_et_ptr.m_num_values;- m_et_ptr.m_ptr += (m_et_ptr.m_bit_shift == m_et_ptr.m_shift_init) ? 3 : 0;+ Iterator<ET, T>& operator++() {+ if constexpr (is_nibble_type(ET)) {+ m_et_ptr.m_bit_shift ^= m_et_ptr.m_bits;+ m_et_ptr.m_ptr += static_cast<std::ptrdiff_t>(m_et_ptr.m_bit_shift == m_et_ptr.m_shift_init);+ } else if constexpr (is_split_bit_type(ET)) {+ --m_et_ptr.m_bit_shift;+ m_et_ptr.m_bit_shift = m_et_ptr.m_bit_shift % m_et_ptr.m_num_values;+ m_et_ptr.m_ptr += (m_et_ptr.m_bit_shift == m_et_ptr.m_shift_init) ? 3 : 0;+ } else {+ if constexpr (is_lsb_packed(ET)) {+ m_et_ptr.m_bit_shift += m_et_ptr.m_bits;+ } else {+ m_et_ptr.m_bit_shift -= m_et_ptr.m_bits;+ }+ m_et_ptr.m_bit_shift = m_et_ptr.m_bit_shift % (m_et_ptr.m_num_values * m_et_ptr.m_bits);+ m_et_ptr.m_ptr += static_cast<std::ptrdiff_t>(m_et_ptr.m_bit_shift == m_et_ptr.m_shift_init);+ }return *this;}@@ -455,25 +463,25 @@ public:return old;}- template <Type_t ETT = ET>- typename std::enable_if<is_bit_type(ETT), Iterator<ET, T>>::type& operator+=(const difference_type& n) {- const auto advance = n + (m_et_ptr.m_shift_init - m_et_ptr.m_bit_shift) / m_et_ptr.m_bits;- m_et_ptr.m_bit_shift = m_et_ptr.m_shift_init - (advance % m_et_ptr.m_num_values) * m_et_ptr.m_bits;- m_et_ptr.m_ptr += advance / m_et_ptr.m_num_values;- return *this;- }-- template <Type_t ETT = ET>- typename std::enable_if<is_nibble_type(ETT), Iterator<ET, T>>::type& operator+=(const difference_type& n) {- m_et_ptr.m_ptr += n / m_et_ptr.m_num_values;- return (n % m_et_ptr.m_num_values) ? ++*this : *this;- }-- template <Type_t ETT = ET>- typename std::enable_if<is_split_bit_type(ETT), Iterator<ET, T>>::type& operator+=(const difference_type& n) {- const auto advance = n + m_et_ptr.m_shift_init - m_et_ptr.m_bit_shift;- m_et_ptr.m_bit_shift = m_et_ptr.m_shift_init - (advance % m_et_ptr.m_num_values);- m_et_ptr.m_ptr += 3 * (advance / m_et_ptr.m_num_values);+ Iterator<ET, T>& operator+=(const difference_type& n) {+ if constexpr (is_nibble_type(ET)) {+ m_et_ptr.m_ptr += n / m_et_ptr.m_num_values;+ if (n % m_et_ptr.m_num_values) {+ ++*this;+ }+ } else if constexpr (is_split_bit_type(ET)) {+ const auto advance = n + m_et_ptr.m_shift_init - m_et_ptr.m_bit_shift;+ m_et_ptr.m_bit_shift = m_et_ptr.m_shift_init - (advance % m_et_ptr.m_num_values);+ m_et_ptr.m_ptr += 3 * (advance / m_et_ptr.m_num_values);+ } else if constexpr (is_lsb_packed(ET)) {+ const auto advance = n + m_et_ptr.m_bit_shift / m_et_ptr.m_bits;+ m_et_ptr.m_bit_shift = (advance % m_et_ptr.m_num_values) * m_et_ptr.m_bits;+ m_et_ptr.m_ptr += advance / m_et_ptr.m_num_values;+ } else {+ const auto advance = n + (m_et_ptr.m_shift_init - m_et_ptr.m_bit_shift) / m_et_ptr.m_bits;+ m_et_ptr.m_bit_shift = m_et_ptr.m_shift_init - (advance % m_et_ptr.m_num_values) * m_et_ptr.m_bits;+ m_et_ptr.m_ptr += advance / m_et_ptr.m_num_values;+ }return *this;}@@ -483,26 +491,23 @@ public:return tmp;}- template <Type_t ETT = ET>- typename std::enable_if<is_bit_type(ETT), Iterator<ET, T>>::type& operator--() {- m_et_ptr.m_bit_shift += m_et_ptr.m_bits;- m_et_ptr.m_bit_shift = m_et_ptr.m_bit_shift % (m_et_ptr.m_num_values * m_et_ptr.m_bits);- m_et_ptr.m_ptr -= static_cast<std::ptrdiff_t>(m_et_ptr.m_bit_shift == 0);- return *this;- }-- template <Type_t ETT = ET>- typename std::enable_if<is_nibble_type(ETT), Iterator<ET, T>>::type& operator--() {- m_et_ptr.m_bit_shift ^= m_et_ptr.m_bits;- m_et_ptr.m_ptr -= static_cast<std::ptrdiff_t>(m_et_ptr.m_bit_shift == 4);- return *this;- }-- template <Type_t ETT = ET>- typename std::enable_if<is_split_bit_type(ETT), Iterator<ET, T>>::type& operator--() {- ++m_et_ptr.m_bit_shift;- m_et_ptr.m_bit_shift = m_et_ptr.m_bit_shift % m_et_ptr.m_num_values;- m_et_ptr.m_ptr -= m_et_ptr.m_bit_shift == 0 ? 3 : 0;+ Iterator<ET, T>& operator--() {+ if constexpr (is_nibble_type(ET)) {+ m_et_ptr.m_bit_shift ^= m_et_ptr.m_bits;+ m_et_ptr.m_ptr -= static_cast<std::ptrdiff_t>(m_et_ptr.m_bit_shift == 4);+ } else if constexpr (is_split_bit_type(ET)) {+ ++m_et_ptr.m_bit_shift;+ m_et_ptr.m_bit_shift = m_et_ptr.m_bit_shift % m_et_ptr.m_num_values;+ m_et_ptr.m_ptr -= m_et_ptr.m_bit_shift == 0 ? 3 : 0;+ } else {+ if constexpr (is_lsb_packed(ET)) {+ m_et_ptr.m_bit_shift -= m_et_ptr.m_bits;+ } else {+ m_et_ptr.m_bit_shift += m_et_ptr.m_bits;+ }+ m_et_ptr.m_bit_shift = m_et_ptr.m_bit_shift % (m_et_ptr.m_num_values * m_et_ptr.m_bits);+ m_et_ptr.m_ptr -= static_cast<std::ptrdiff_t>(m_et_ptr.m_bit_shift == m_et_ptr.m_shift_last);+ }return *this;}@@ -512,25 +517,25 @@ public:return old;}- template <Type_t ETT = ET>- typename std::enable_if<is_bit_type(ETT), Iterator<ET, T>>::type& operator-=(const difference_type& n) {- const auto advance = m_et_ptr.m_bit_shift / m_et_ptr.m_bits + n;- m_et_ptr.m_bit_shift = (advance % m_et_ptr.m_num_values) * m_et_ptr.m_bits;- m_et_ptr.m_ptr -= advance / m_et_ptr.m_num_values;- return *this;- }-- template <Type_t ETT = ET>- typename std::enable_if<is_nibble_type(ETT), Iterator<ET, T>>::type& operator-=(const difference_type& n) {- m_et_ptr.m_ptr -= n / m_et_ptr.m_num_values;- return (n % m_et_ptr.m_num_values) ? --*this : *this;- }-- template <Type_t ETT = ET>- typename std::enable_if<is_split_bit_type(ETT), Iterator<ET, T>>::type& operator-=(const difference_type& n) {- const auto advance = m_et_ptr.m_bit_shift + n;- m_et_ptr.m_bit_shift = advance % m_et_ptr.m_num_values;- m_et_ptr.m_ptr -= 3 * (advance / m_et_ptr.m_num_values);+ Iterator<ET, T>& operator-=(const difference_type& n) {+ if constexpr (is_nibble_type(ET)) {+ m_et_ptr.m_ptr -= n / m_et_ptr.m_num_values;+ if (n % m_et_ptr.m_num_values) {+ --*this;+ }+ } else if constexpr (is_split_bit_type(ET)) {+ const auto advance = m_et_ptr.m_bit_shift + n;+ m_et_ptr.m_bit_shift = advance % m_et_ptr.m_num_values;+ m_et_ptr.m_ptr -= 3 * (advance / m_et_ptr.m_num_values);+ } else if constexpr (is_lsb_packed(ET)) {+ const auto advance = n + (m_et_ptr.m_shift_last - m_et_ptr.m_bit_shift) / m_et_ptr.m_bits;+ m_et_ptr.m_bit_shift = m_et_ptr.m_shift_last - (advance % m_et_ptr.m_num_values) * m_et_ptr.m_bits;+ m_et_ptr.m_ptr -= advance / m_et_ptr.m_num_values;+ } else {+ const auto advance = m_et_ptr.m_bit_shift / m_et_ptr.m_bits + n;+ m_et_ptr.m_bit_shift = (advance % m_et_ptr.m_num_values) * m_et_ptr.m_bits;+ m_et_ptr.m_ptr -= advance / m_et_ptr.m_num_values;+ }return *this;}diff --git a/src/core/src/op/constant.cpp b/src/core/src/op/constant.cppindex 5577481a8a7..c358c481737 100644--- a/src/core/src/op/constant.cpp+++ b/src/core/src/op/constant.cpp@@ -281,7 +281,9 @@ void Constant::set_unused_bits(void* buffer) const {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);+ const uint8_t not_used_bits_mask = element::is_lsb_packed(m_element_type)+ ? 0xff << (m_element_type.bitwidth() * not_aligned_elements)+ : 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;diff --git a/src/core/tests/constant.cpp b/src/core/tests/constant.cppindex c8c314f3ad6..ff0f6b1c2ed 100644--- a/src/core/tests/constant.cpp+++ b/src/core/tests/constant.cpp@@ -866,17 +866,17 @@ TEST(constant, uint1_write_then_cast_custom_type) {TEST(constant, uint2_string) {const auto shape = Shape{4};- op::v0::Constant c(element::u2, shape, vector<string>{"3", "0", "1", "2"});+ op::v0::Constant c(element::u2, shape, vector<string>{"2", "1", "0", "3"});auto v = c.cast_vector<uint8_t>();ASSERT_EQ(v.size(), shape_size(shape));- EXPECT_THAT(v, ElementsAre(3, 0, 1, 2));+ EXPECT_THAT(v, ElementsAre(2, 1, 0, 3));const auto p = c.get_data_ptr<uint8_t>();EXPECT_EQ(p[0], 0b11000110);- EXPECT_EQ(c.convert_value_to_string(1), "0");- EXPECT_THAT(c.get_value_strings(), ElementsAre("3", "0", "1", "2"));+ EXPECT_EQ(c.convert_value_to_string(1), "1");+ EXPECT_THAT(c.get_value_strings(), ElementsAre("2", "1", "0", "3"));EXPECT_THROW(c.get_strides(), Exception);}@@ -891,7 +891,7 @@ TEST(constant, uint2_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[1], 0b00000001);}TEST(constant, uint2_vector_less_than_single_byte) {@@ -905,7 +905,7 @@ TEST(constant, uint2_vector_less_than_single_byte) {EXPECT_THAT(v, ElementsAre(2, 3, 1));const auto p = c.get_data_ptr<uint8_t>();- EXPECT_EQ(p[0] & 0b11111100, 0b10110100);+ EXPECT_EQ(p[0], 0b00011110);}TEST(constant, uint2_vector_bigger_than_single_byte) {@@ -919,20 +919,27 @@ TEST(constant, uint2_vector_bigger_than_single_byte) {EXPECT_THAT(v, ElementsAre(2, 3, 1, 0, 1, 2, 0));const auto p = c.get_data_ptr<uint8_t>();- EXPECT_EQ(p[0], 0b10110100);- EXPECT_EQ(p[1] & 0b11111100, 0b01100000);+ EXPECT_EQ(p[0], 0b00011110);+ EXPECT_EQ(p[1], 0b00001001);}TEST(constant, uint2_vector_broadcast) {- const auto shape = Shape{3};- op::v0::Constant c(element::u2, shape, vector<int8_t>{2});+ const auto shape = Shape{5};+ // Use dynamic allocation to check not used bits have deterministic value+ auto constant = std::make_shared<ov::op::v0::Constant>(element::u2, shape, vector<int8_t>{2});+ const auto& c = *constant;auto v = c.cast_vector<uint8_t>();ASSERT_EQ(v.size(), shape_size(shape));EXPECT_THAT(v, Each(2));const auto p = c.get_data_ptr<uint8_t>();- EXPECT_EQ(p[0] & 0b11111100, 0b10101000);+ EXPECT_EQ(p[0], 0b10101010);+ EXPECT_EQ(p[1], 0b00000010);++ const auto gv = c.get_vector<uint8_t>();+ EXPECT_EQ(gv[0], 0b10101010);+ EXPECT_EQ(gv[1], 0b00000010);}TEST(constant, uint2_write_then_cast_custom_type) {diff --git a/src/core/tests/element_iterator_test.cpp b/src/core/tests/element_iterator_test.cppindex 37d5fd05f86..0978d231ddc 100644--- a/src/core/tests/element_iterator_test.cpp+++ b/src/core/tests/element_iterator_test.cpp@@ -106,15 +106,15 @@ TEST(ElementIteratorTest, write_u2_data) {std::copy(input.begin(), input.end(), iter);- EXPECT_THAT(output, ElementsAre(0x87, 0x0f, 0x66, 0xe4));+ EXPECT_THAT(output, ElementsAre(0b11010010, 0b11110000, 0b10011001, 0b00011011));}TEST(ElementIteratorTest, read_const_u2_data) {constexpr auto elements_count = 16;- constexpr auto input = std::array<int8_t, get_buffer_size(2, elements_count)>{static_cast<int8_t>(0x87),- 0x0f,- 0x66,- static_cast<int8_t>(0xe4)};+ constexpr auto input = std::array<int8_t, get_buffer_size(2, elements_count)>{static_cast<int8_t>(0b11010010),+ static_cast<int8_t>(0b11110000),+ static_cast<int8_t>(0b10011001),+ static_cast<int8_t>(0b00011011)};auto iter = element::iterator<element::u2>(input.data());EXPECT_THAT(std::vector<int8_t>(iter, iter + elements_count),@@ -123,10 +123,10 @@ TEST(ElementIteratorTest, read_const_u2_data) {TEST(ElementIteratorTest, read_non_const_u2_data) {constexpr auto elements_count = 16;- auto input = std::array<int8_t, get_buffer_size(2, elements_count)>{static_cast<int8_t>(0x87),- 0x0f,- 0x66,- static_cast<int8_t>(0xe4)};+ auto input = std::array<int8_t, get_buffer_size(2, elements_count)>{static_cast<int8_t>(0b11010010),+ static_cast<int8_t>(0b11110000),+ static_cast<int8_t>(0b10011001),+ static_cast<int8_t>(0b00011011)};auto iter = element::iterator<element::u2>(input.data());EXPECT_THAT(std::vector<int8_t>(iter, iter + elements_count),@@ -134,31 +134,39 @@ TEST(ElementIteratorTest, read_non_const_u2_data) {}TEST(ElementIteratorTest, read_u2_data_increment_decrement_iterator) {- auto input = std::array<int8_t, 2>{0x33, static_cast<int8_t>(0x93)};+ auto input = std::array<int8_t, 2>{static_cast<int8_t>(0b00100111), static_cast<int8_t>(0b10011011)};auto iter = element::iterator<element::u2>(input.data() + 1);- EXPECT_EQ(*iter--, 2); // 2nd byte 1st half-nibble- EXPECT_EQ(*iter++, 3); // 1st byte 4th half-nibble- EXPECT_EQ(*++iter, 1); // 2nd byte 2nd half-nibble- EXPECT_EQ(*iter--, 1); // 2nd byte 2nd half-nibble- EXPECT_EQ(*--iter, 3); // 1st byte 4th half-nibble+ EXPECT_EQ(*iter--, 3); // 2nd byte 4th half-nibble+ EXPECT_EQ(*iter++, 0); // 1st byte 1st half-nibble+ EXPECT_EQ(*++iter, 2); // 2nd byte 3rd half-nibble+ EXPECT_EQ(*iter--, 2); // 2nd byte 3rdt half-nibble+ EXPECT_EQ(*--iter, 0); // 1st byte 1st half-nibble+ EXPECT_EQ(*--iter, 2); // 1st byte 2nd half-nibble}TEST(ElementIteratorTest, read_u2_data_iterator_with_offset) {- auto input = std::array<int8_t, 3>{0x43, static_cast<int8_t>(0x93), 0x41};+ auto input = std::array<int8_t, 3>{0b01000011, static_cast<int8_t>(0b100010011), 0b01000001};auto iter = element::iterator<element::u2>(input.data() + 1);- EXPECT_EQ(*iter, 2); // 2nd byte 1st half-nibble- EXPECT_EQ(*(iter - 3), 0); // 1st byte 2nd half-nibble- EXPECT_EQ(*(iter - 4), 1); // 1st byte 1st half-nibble- EXPECT_EQ(*(iter + 1), 1); // 2nd byte 2nd half-nibble- EXPECT_EQ(*(iter + 7), 1); // 3rd byte 4th half-nibble- EXPECT_EQ(*std::prev(iter, 1), 3); // 1st byte 4th half-nibble- EXPECT_EQ(*std::next(iter, 2), 0); // 2nd byte 3rd half-nibble+ EXPECT_EQ(*iter, 3); // 2nd byte 4th half-nibble+ EXPECT_EQ(*(iter - 0), 3); // 2nd byte 4th half-nibble+ EXPECT_EQ(*(iter - 1), 1); // 1st byte 1st half-nibble+ EXPECT_EQ(*(iter - 2), 0); // 1st byte 2nd half-nibble+ EXPECT_EQ(*(iter - 3), 0); // 1st byte 3rd half-nibble+ EXPECT_EQ(*(iter - 4), 3); // 1st byte 4th half-nibble+ EXPECT_EQ(*(iter + 0), 3); // 2nd byte 4th half-nibble+ EXPECT_EQ(*(iter + 1), 0); // 2nd byte 3rd half-nibble+ EXPECT_EQ(*(iter + 7), 1); // 3rd byte 1st half-nibble+ EXPECT_EQ(*std::prev(iter, 1), 1); // 1st byte 4th half-nibble+ EXPECT_EQ(*std::next(iter, 2), 1); // 2nd byte 2nd half-nibble++ --iter;+ EXPECT_EQ(*(iter - 1), 0); // 1st byte 2nd half-nibble}TEST(ElementIteratorTest, u2_value_to_output_stream) {- constexpr auto value = static_cast<int8_t>(0x80);+ constexpr auto value = static_cast<int8_t>(0x02);auto iter = element::iterator<element::u2>(&value);std::stringstream s;@@ -168,9 +176,10 @@ TEST(ElementIteratorTest, u2_value_to_output_stream) {}TEST(ElementIteratorTest, read_u2_from_tensor) {- auto input = std::array<int8_t, 4>{0x32, static_cast<int8_t>(0xa3), 0x41, 0x11};+ const auto input =+ std::array<int8_t, 4>{static_cast<int8_t>(0b10001100), static_cast<int8_t>(0b11001010), 0b01000001, 0b01000100};auto t = ov::Tensor(element::u2, Shape{4, 4}, input.data());- auto iter = element::iterator<element::u2>(static_cast<int8_t*>(t.data(element::u2)));+ auto iter = element::iterator<element::u2>(static_cast<const int8_t*>(std::as_const(t).data(element::u2)));EXPECT_THAT(std::vector<int8_t>(iter, iter + t.get_size()),ElementsAre(0, 3, 0, 2, 2, 2, 0, 3, 1, 0, 0, 1, 0, 1, 0, 1));@@ -211,9 +220,11 @@ TEST(ElementIteratorTest, read_u3_data_iterator_with_offset) {auto iter = element::iterator<element::u3>(input.data() + 3);EXPECT_EQ(*iter, 2);+ EXPECT_EQ(*(iter - 0), 2);EXPECT_EQ(*(iter - 3), 6);EXPECT_EQ(*(iter - 4), 1);EXPECT_EQ(*(iter - 5), 6);+ EXPECT_EQ(*(iter + 0), 2);EXPECT_EQ(*(iter + 1), 3);EXPECT_EQ(*(iter + 5), 5);EXPECT_EQ(*(iter + 7), 7);diff --git a/src/plugins/template/tests/functional/op_reference/convert_like.cpp b/src/plugins/template/tests/functional/op_reference/convert_like.cppindex 435fd24a09a..4bd069ba418 100644--- a/src/plugins/template/tests/functional/op_reference/convert_like.cpp+++ b/src/plugins/template/tests/functional/op_reference/convert_like.cpp@@ -32,7 +32,7 @@ INSTANTIATE_TEST_SUITE_P(ov::element::u2,ov::element::boolean,std::vector<uint8_t>{0b10010011},- std::vector<char>{1, 1, 0, 1}),+ std::vector<char>{1, 0, 1, 1}),ConvertParams(ConversionTypes::CONVERT_LIKE,ov::PartialShape{8},ov::element::u3,@@ -87,7 +87,7 @@ INSTANTIATE_TEST_SUITE_P(ov::element::u2,ov::element::bf16,std::vector<uint8_t>{0b10010011},- std::vector<bfloat16>{2.0f, 1.0f, 0.0f, 3.0f}),+ std::vector<bfloat16>{3.0f, 0.0f, 1.0f, 2.0f}),ConvertParams(ConversionTypes::CONVERT_LIKE,ov::PartialShape{8},ov::element::u3,@@ -135,7 +135,7 @@ INSTANTIATE_TEST_SUITE_P(ov::element::u2,ov::element::f16,std::vector<uint8_t>{0b10010011},- std::vector<float16>{2.0f, 1.0f, 0.0f, 3.0f}),+ std::vector<float16>{3.0f, 0.0f, 1.0f, 2.0f}),ConvertParams(ConversionTypes::CONVERT_LIKE,ov::PartialShape{8},ov::element::u3,@@ -199,7 +199,7 @@ INSTANTIATE_TEST_SUITE_P(ov::element::u2,ov::element::f32,std::vector<uint8_t>{0b10010011},- std::vector<float>{2.0f, 1.0f, 0.0f, 3.0f}),+ std::vector<float>{3.0f, 0.0f, 1.0f, 2.0f}),ConvertParams(ConversionTypes::CONVERT_LIKE,ov::PartialShape{8},ov::element::u3,@@ -380,7 +380,7 @@ INSTANTIATE_TEST_SUITE_P(ov::element::u2,ov::element::i4,std::vector<uint8_t>{0b10010011},- std::vector<uint8_t>{0x12, 0x30}),+ std::vector<uint8_t>{0x03, 0x21}),ConvertParams(ConversionTypes::CONVERT_LIKE,ov::PartialShape{8},ov::element::u3,@@ -483,7 +483,7 @@ INSTANTIATE_TEST_SUITE_P(ov::element::u2,ov::element::i8,std::vector<uint8_t>{0b10010011},- std::vector<int8_t>{2, 1, 0, 3}),+ std::vector<int8_t>{3, 0, 1, 2}),ConvertParams(ConversionTypes::CONVERT_LIKE,ov::PartialShape{8},ov::element::u3,@@ -586,7 +586,7 @@ INSTANTIATE_TEST_SUITE_P(ov::element::u2,ov::element::i16,std::vector<uint8_t>{0b10010011},- std::vector<int16_t>{2, 1, 0, 3}),+ std::vector<int16_t>{3, 0, 1, 2}),ConvertParams(ConversionTypes::CONVERT_LIKE,ov::PartialShape{8},ov::element::u3,@@ -689,7 +689,7 @@ INSTANTIATE_TEST_SUITE_P(ov::element::u2,ov::element::i32,std::vector<uint8_t>{0b10010011},- std::vector<int32_t>{2, 1, 0, 3}),+ std::vector<int32_t>{3, 0, 1, 2}),ConvertParams(ConversionTypes::CONVERT_LIKE,ov::PartialShape{8},ov::element::u3,@@ -792,7 +792,7 @@ INSTANTIATE_TEST_SUITE_P(ov::element::u2,ov::element::i64,std::vector<uint8_t>{0b10010011},- std::vector<int64_t>{2, 1, 0, 3}),+ std::vector<int64_t>{3, 0, 1, 2}),ConvertParams(ConversionTypes::CONVERT_LIKE,ov::PartialShape{8},ov::element::u3,@@ -896,7 +896,7 @@ INSTANTIATE_TEST_SUITE_P(ov::element::u2,ov::element::u1,std::vector<uint8_t>{0b10010011, 0x00},- std::vector<uint8_t>{0b01010000}),+ std::vector<uint8_t>{0b10100000}),ConvertParams(ConversionTypes::CONVERT_LIKE,ov::PartialShape{8},ov::element::u3,@@ -1000,7 +1000,7 @@ INSTANTIATE_TEST_SUITE_P(ov::element::u2,ov::element::u4,std::vector<uint8_t>{0b10010011},- std::vector<uint8_t>{0x12, 0x30}),+ std::vector<uint8_t>{0x03, 0x21}),ConvertParams(ConversionTypes::CONVERT_LIKE,ov::PartialShape{8},ov::element::u3,@@ -1104,7 +1104,7 @@ INSTANTIATE_TEST_SUITE_P(ov::element::u2,ov::element::u8,std::vector<uint8_t>{0b10010011},- std::vector<uint8_t>{2, 1, 0, 3}),+ std::vector<uint8_t>{3, 0, 1, 2}),ConvertParams(ConversionTypes::CONVERT_LIKE,ov::PartialShape{8},ov::element::u3,@@ -1208,7 +1208,7 @@ INSTANTIATE_TEST_SUITE_P(ov::element::u2,ov::element::u16,std::vector<uint8_t>{0b10010011},- std::vector<uint16_t>{2, 1, 0, 3}),+ std::vector<uint16_t>{3, 0, 1, 2}),ConvertParams(ConversionTypes::CONVERT_LIKE,ov::PartialShape{8},ov::element::u3,@@ -1312,7 +1312,7 @@ INSTANTIATE_TEST_SUITE_P(ov::element::u2,ov::element::u32,std::vector<uint8_t>{0b10010011},- std::vector<uint32_t>{2, 1, 0, 3}),+ std::vector<uint32_t>{3, 0, 1, 2}),ConvertParams(ConversionTypes::CONVERT_LIKE,ov::PartialShape{8},ov::element::u3,@@ -1415,7 +1415,7 @@ INSTANTIATE_TEST_SUITE_P(ov::element::u2,ov::element::u64,std::vector<uint8_t>{0b10010011},- std::vector<uint64_t>{2, 1, 0, 3}),+ std::vector<uint64_t>{3, 0, 1, 2}),ConvertParams(ConversionTypes::CONVERT_LIKE,ov::PartialShape{8},ov::element::u3,@@ -1525,103 +1525,103 @@ INSTANTIATE_TEST_SUITE_P(ov::element::boolean,ov::element::u2,std::vector<char>{1, 0, 1, 1, 0, 1, 0, 0},- std::vector<uint8_t>{0b01000101, 0b00010000}),+ std::vector<uint8_t>{0b01010001, 0b00000100}),ConvertParams(ConversionTypes::CONVERT_LIKE,ov::PartialShape{2, 4},ov::element::u1,ov::element::u2,std::vector<uint8_t>{0b11001011},- std::vector<uint8_t>{0x50, 0x45}),+ std::vector<uint8_t>{0x05, 0x51}),ConvertParams(ConversionTypes::CONVERT_LIKE,ov::PartialShape{1, 8},ov::element::u3,ov::element::u2,std::vector<uint8_t>{0b00100111, 0b00011010, 0b00001000},- std::vector<uint8_t>{0b00100111, 0b00011010}),+ std::vector<uint8_t>{0b11011000, 0b10100100}),ConvertParams(ConversionTypes::CONVERT_LIKE,ov::PartialShape{2, 4},ov::element::u4,ov::element::u2,std::vector<uint8_t>{0x21, 0x01, 0x31, 0xff},- std::vector<uint8_t>{0b01100100, 0b01111111}),+ std::vector<uint8_t>{0b00011001, 0b11111101}),ConvertParams(ConversionTypes::CONVERT_LIKE,ov::PartialShape{1, 8},ov::element::u6,ov::element::u2,std::vector<uint8_t>{0x02, 0x13, 0x00, 0x81, 0x04, 0b01001011},- std::vector<uint8_t>{0b00100111, 0b00010000}),+ std::vector<uint8_t>{0b11011000, 0b00000100}),ConvertParams(ConversionTypes::CONVERT_LIKE,ov::PartialShape{2, 4},ov::element::u8,ov::element::u2,std::vector<uint8_t>{0, 2, 1, 3, 4, 1, 2, 2},- std::vector<uint8_t>{0b00100111, 0b00011010}),+ std::vector<uint8_t>{0b11011000, 0b10100100}),ConvertParams(ConversionTypes::CONVERT_LIKE,ov::PartialShape{2, 4},ov::element::u16,ov::element::u2,std::vector<uint16_t>{0, 2, 1, 3, 4, 1, 2, 2},- std::vector<uint8_t>{0b00100111, 0b00011010}),+ std::vector<uint8_t>{0b11011000, 0b10100100}),ConvertParams(ConversionTypes::CONVERT_LIKE,ov::PartialShape{2, 4},ov::element::u32,ov::element::u2,std::vector<uint32_t>{0, 2, 1, 3, 4, 1, 2, 2},- std::vector<uint8_t>{0b00100111, 0b00011010}),+ std::vector<uint8_t>{0b11011000, 0b10100100}),ConvertParams(ConversionTypes::CONVERT_LIKE,ov::PartialShape{2, 4},ov::element::u64,ov::element::u2,std::vector<uint64_t>{0, 2, 1, 3, 4, 1, 2, 2},- std::vector<uint8_t>{0b00100111, 0b00011010}),+ std::vector<uint8_t>{0b11011000, 0b10100100}),ConvertParams(ConversionTypes::CONVERT_LIKE,ov::PartialShape{2, 4},ov::element::i4,ov::element::u2,std::vector<uint8_t>{0x21, 0x01, 0x31, 0xff},- std::vector<uint8_t>{0b01100100, 0b01111111}),+ std::vector<uint8_t>{0b00011001, 0b11111101}),ConvertParams(ConversionTypes::CONVERT_LIKE,ov::PartialShape{2, 4},ov::element::i8,ov::element::u2,std::vector<int8_t>{0, 2, 1, 3, 4, 1, 2, 2},- std::vector<uint8_t>{0b00100111, 0b00011010}),+ std::vector<uint8_t>{0b11011000, 0b10100100}),ConvertParams(ConversionTypes::CONVERT_LIKE,ov::PartialShape{2, 4},ov::element::i16,ov::element::u2,std::vector<int16_t>{0, 2, 1, 3, 4, 1, 2, 2},- std::vector<uint8_t>{0b00100111, 0b00011010}),+ std::vector<uint8_t>{0b11011000, 0b10100100}),ConvertParams(ConversionTypes::CONVERT_LIKE,ov::PartialShape{2, 4},ov::element::i32,ov::element::u2,std::vector<int32_t>{0, 2, 1, 3, 4, 1, 2, 2},- std::vector<uint8_t>{0b00100111, 0b00011010}),+ std::vector<uint8_t>{0b11011000, 0b10100100}),ConvertParams(ConversionTypes::CONVERT_LIKE,ov::PartialShape{2, 4},ov::element::i64,ov::element::u2,std::vector<int64_t>{0, 2, 1, 3, 4, 1, 2, 2},- std::vector<uint8_t>{0b00100111, 0b00011010}),+ std::vector<uint8_t>{0b11011000, 0b10100100}),ConvertParams(ConversionTypes::CONVERT_LIKE,ov::PartialShape{1, 8},ov::element::f8e4m3,ov::element::u2,std::vector<float8_e4m3>{0.1f, 2.0f, 1.1f, 3.0f, 4.0f, 1.0f, 2.2f, 2.8f},- std::vector<uint8_t>{0b00100111, 0b00011010}),+ std::vector<uint8_t>{0b11011000, 0b10100100}),ConvertParams(ConversionTypes::CONVERT_LIKE,ov::PartialShape{2, 4},ov::element::f16,ov::element::u2,std::vector<float16>{0.1f, 2.0f, 1.1f, 3.0f, 4.0f, 1.0f, 2.2f, 2.8f},- std::vector<uint8_t>{0b00100111, 0b00011010}),+ std::vector<uint8_t>{0b11011000, 0b10100100}),ConvertParams(ConversionTypes::CONVERT_LIKE,ov::PartialShape{2, 4},ov::element::f32,ov::element::u2,std::vector<float>{0.1f, 2.0f, 1.1f, 3.0f, 4.0f, 1.0f, 2.2f, 2.8f},- std::vector<uint8_t>{0b00100111, 0b00011010}),+ std::vector<uint8_t>{0b11011000, 0b10100100}),// destination u3ConvertParams(ConversionTypes::CONVERT_LIKE,ov::PartialShape{2, 4},@@ -1639,7 +1639,7 @@ INSTANTIATE_TEST_SUITE_P(ov::PartialShape{1, 8},ov::element::u2,ov::element::u3,- std::vector<uint8_t>{0b00100111, 0b00011010},+ std::vector<uint8_t>{0b11011000, 0b10100100},std::vector<uint8_t>{0b00100111, 0b00011010, 0x00}),ConvertParams(ConversionTypes::CONVERT_LIKE,ov::PartialShape{2, 4},@@ -1754,7 +1754,7 @@ INSTANTIATE_TEST_SUITE_P(ov::PartialShape{1, 8},ov::element::u2,ov::element::u6,- std::vector<uint8_t>{0b00100111, 0b00010000},+ std::vector<uint8_t>{0b11011000, 0b00000100},std::vector<uint8_t>{0x02, 0x13, 0x00, 0x01, 0x00, 0x00}),ConvertParams(ConversionTypes::CONVERT_LIKE,ov::PartialShape{1, 8},