Skip to content

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

[ONNX] Added translation for int4/uint4 constants/inputs (#24273)

Georgy Krivoruchkoe7d340d79051
diff --git a/src/frontends/onnx/frontend/src/core/tensor.cpp b/src/frontends/onnx/frontend/src/core/tensor.cpp
index 933e46d44a3..4f8f54e2d83 100644
--- a/src/frontends/onnx/frontend/src/core/tensor.cpp
+++ b/src/frontends/onnx/frontend/src/core/tensor.cpp
@@ -82,10 +82,18 @@ std::vector<int8_t> Tensor::get_data() const {
if (m_tensor_proto->has_raw_data()) {
return detail::__get_raw_data<int8_t>(m_tensor_proto->raw_data(), m_tensor_proto->data_type());
}
+#ifdef ONNX_VERSION_116
+ if (m_tensor_proto->data_type() == TensorProto_DataType::TensorProto_DataType_INT8 ||
+ m_tensor_proto->data_type() == TensorProto_DataType::TensorProto_DataType_INT4) {
+ return detail::__get_data<int8_t>(m_tensor_proto->int32_data());
+ }
+ ONNX_INVALID_DATA_TYPE(m_tensor_proto->data_type(), "INT4, INT8, raw data");
+#else
if (m_tensor_proto->data_type() == TensorProto_DataType::TensorProto_DataType_INT8) {
return detail::__get_data<int8_t>(m_tensor_proto->int32_data());
}
ONNX_INVALID_DATA_TYPE(m_tensor_proto->data_type(), "INT8, raw data");
+#endif
}
template <>
@@ -138,10 +146,18 @@ std::vector<uint8_t> Tensor::get_data() const {
if (m_tensor_proto->has_raw_data()) {
return detail::__get_raw_data<uint8_t>(m_tensor_proto->raw_data(), m_tensor_proto->data_type());
}
+#ifdef ONNX_VERSION_116
+ if (m_tensor_proto->data_type() == TensorProto_DataType::TensorProto_DataType_UINT8 ||
+ m_tensor_proto->data_type() == TensorProto_DataType::TensorProto_DataType_UINT4) {
+ return detail::__get_data<uint8_t>(m_tensor_proto->int32_data());
+ }
+ ONNX_INVALID_DATA_TYPE(m_tensor_proto->data_type(), "UINT4, UINT8, raw data");
+#else
if (m_tensor_proto->data_type() == TensorProto_DataType::TensorProto_DataType_UINT8) {
return detail::__get_data<uint8_t>(m_tensor_proto->int32_data());
}
ONNX_INVALID_DATA_TYPE(m_tensor_proto->data_type(), "UINT8, raw data");
+#endif
}
template <>
@@ -183,7 +199,7 @@ std::vector<uint64_t> Tensor::get_data() const {
if (m_tensor_proto->data_type() == TensorProto_DataType::TensorProto_DataType_UINT64) {
return detail::__get_data<uint64_t>(m_tensor_proto->uint64_data());
}
- ONNX_INVALID_DATA_TYPE(m_tensor_proto->data_type(), "UINT63, raw data");
+ ONNX_INVALID_DATA_TYPE(m_tensor_proto->data_type(), "UINT64, raw data");
}
template <>
diff --git a/src/frontends/onnx/frontend/src/core/tensor.hpp b/src/frontends/onnx/frontend/src/core/tensor.hpp
index a001eaefe34..ae6fe28754b 100644
--- a/src/frontends/onnx/frontend/src/core/tensor.hpp
+++ b/src/frontends/onnx/frontend/src/core/tensor.hpp
@@ -13,6 +13,7 @@
#include "exceptions.hpp"
#include "onnx_common/utils.hpp"
#include "openvino/core/shape.hpp"
+#include "openvino/core/type/element_iterator.hpp"
#include "openvino/core/type/element_type.hpp"
#include "openvino/frontend/exception.hpp"
#include "openvino/runtime/aligned_buffer.hpp"
@@ -64,6 +65,10 @@ public:
enum class Type {
undefined = TensorProto_DataType::TensorProto_DataType_UNDEFINED,
float32 = TensorProto_DataType::TensorProto_DataType_FLOAT,
+#ifdef ONNX_VERSION_116
+ uint4 = TensorProto_DataType::TensorProto_DataType_UINT4,
+ int4 = TensorProto_DataType::TensorProto_DataType_INT4,
+#endif
uint8 = TensorProto_DataType::TensorProto_DataType_UINT8,
int8 = TensorProto_DataType::TensorProto_DataType_INT8,
uint16 = TensorProto_DataType::TensorProto_DataType_UINT16,
@@ -141,6 +146,10 @@ public:
return ov::element::f16;
case TensorProto_DataType::TensorProto_DataType_DOUBLE:
return ov::element::f64;
+#ifdef ONNX_VERSION_116
+ case TensorProto_DataType::TensorProto_DataType_INT4:
+ return ov::element::i4;
+#endif
case TensorProto_DataType::TensorProto_DataType_INT8:
return ov::element::i8;
case TensorProto_DataType::TensorProto_DataType_INT16:
@@ -149,6 +158,10 @@ public:
return ov::element::i32;
case TensorProto_DataType::TensorProto_DataType_INT64:
return ov::element::i64;
+#ifdef ONNX_VERSION_116
+ case TensorProto_DataType::TensorProto_DataType_UINT4:
+ return ov::element::u4;
+#endif
case TensorProto_DataType::TensorProto_DataType_UINT8:
return ov::element::u8;
case TensorProto_DataType::TensorProto_DataType_UINT16:
@@ -170,8 +183,8 @@ public:
default:
ONNX_UNSUPPORTED_DATA_TYPE(
m_tensor_proto->data_type(),
- "BOOL, BFLOAT16, FLOAT8E4M3FN, FLOAT8E5M2, FLOAT, FLOAT16, DOUBLE, INT8, INT16, INT32, INT64, "
- "UINT8, UINT16, UINT32, UINT64, STRING");
+ "BOOL, BFLOAT16, FLOAT8E4M3FN, FLOAT8E5M2, FLOAT, FLOAT16, DOUBLE, INT4, INT8, INT16, INT32, INT64, "
+ "UINT4, UINT8, UINT16, UINT32, UINT64, STRING");
}
}
@@ -192,6 +205,10 @@ public:
return make_ov_constant<ov::float16>(ov::element::f16);
case TensorProto_DataType::TensorProto_DataType_DOUBLE:
return make_ov_constant<double>(ov::element::f64);
+#ifdef ONNX_VERSION_116
+ case TensorProto_DataType::TensorProto_DataType_INT4:
+ return make_ov_constant<int8_t>(ov::element::i4);
+#endif
case TensorProto_DataType::TensorProto_DataType_INT8:
return make_ov_constant<int8_t>(ov::element::i8);
case TensorProto_DataType::TensorProto_DataType_INT16:
@@ -200,6 +217,10 @@ public:
return make_ov_constant<int32_t>(ov::element::i32);
case TensorProto_DataType::TensorProto_DataType_INT64:
return make_ov_constant<int64_t>(ov::element::i64);
+#ifdef ONNX_VERSION_116
+ case TensorProto_DataType::TensorProto_DataType_UINT4:
+ return make_ov_constant<uint8_t>(ov::element::u4);
+#endif
case TensorProto_DataType::TensorProto_DataType_UINT8:
return make_ov_constant<uint8_t>(ov::element::u8);
case TensorProto_DataType::TensorProto_DataType_UINT16:
@@ -217,10 +238,17 @@ public:
case TensorProto_DataType::TensorProto_DataType_STRING:
return make_ov_constant<std::string>(ov::element::string);
default:
+#ifdef ONNX_VERSION_116
+ ONNX_UNSUPPORTED_DATA_TYPE(
+ m_tensor_proto->data_type(),
+ "BOOL, BFLOAT16, FLOAT8E4M3FN, FLOAT8E5M2, FLOAT, FLOAT16, DOUBLE, INT4, INT8, INT16, INT32, INT64, "
+ "UINT4, UINT8, UINT16, UINT32, UINT64, STRING");
+#else
ONNX_UNSUPPORTED_DATA_TYPE(
m_tensor_proto->data_type(),
"BOOL, BFLOAT16, FLOAT8E4M3FN, FLOAT8E5M2, FLOAT, FLOAT16, DOUBLE, INT8, INT16, INT32, INT64, "
"UINT8, UINT16, UINT32, UINT64, STRING");
+#endif
}
}
@@ -271,10 +299,13 @@ private:
std::shared_ptr<ov::op::v0::Constant> make_ov_constant(const ov::element::Type& type) const {
std::shared_ptr<ov::op::v0::Constant> constant{nullptr};
auto data = get_data<T>();
- auto data_size = data.size();
- if (data_size == shape_size(m_shape)) {
- constant = std::make_shared<ov::op::v0::Constant>(type, m_shape, data);
- } else if (data_size == 0 && m_shape.size() == 0) {
+ auto element_count = data.size();
+ if (ov::element::is_nibble_type(type)) {
+ element_count *= 2; // Each byte contains 2 data items
+ }
+ if (element_count == shape_size(m_shape)) {
+ constant = std::make_shared<ov::op::v0::Constant>(type, m_shape, data.data());
+ } else if (element_count == 0 && m_shape.size() == 0) {
constant = common::make_failsafe_constant(type);
} else {
FRONT_END_THROW("Tensor shape doesn't match data size");
diff --git a/src/frontends/onnx/frontend/src/utils/common.cpp b/src/frontends/onnx/frontend/src/utils/common.cpp
index 912ea505d62..87601822b4b 100644
--- a/src/frontends/onnx/frontend/src/utils/common.cpp
+++ b/src/frontends/onnx/frontend/src/utils/common.cpp
@@ -42,6 +42,10 @@ const ov::element::Type& get_ov_element_type(int64_t onnx_type) {
return ov::element::f16;
case TensorProto_DataType::TensorProto_DataType_FLOAT:
return ov::element::f32;
+#ifdef ONNX_VERSION_116
+ case TensorProto_DataType::TensorProto_DataType_INT4:
+ return ov::element::i4;
+#endif
case TensorProto_DataType::TensorProto_DataType_INT8:
return ov::element::i8;
case TensorProto_DataType::TensorProto_DataType_INT16:
@@ -50,6 +54,10 @@ const ov::element::Type& get_ov_element_type(int64_t onnx_type) {
return ov::element::i32;
case TensorProto_DataType::TensorProto_DataType_INT64:
return ov::element::i64;
+#ifdef ONNX_VERSION_116
+ case TensorProto_DataType::TensorProto_DataType_UINT4:
+ return ov::element::u4;
+#endif
case TensorProto_DataType::TensorProto_DataType_UINT8:
return ov::element::u8;
case TensorProto_DataType::TensorProto_DataType_UINT16:
@@ -69,9 +77,15 @@ const ov::element::Type& get_ov_element_type(int64_t onnx_type) {
case TensorProto_DataType::TensorProto_DataType_STRING:
return ov::element::string;
}
+#ifdef ONNX_VERSION_116
+ ONNX_UNSUPPORTED_DATA_TYPE(onnx_type,
+ "BOOL, BFLOAT16, FLOAT8E4M3FN, FLOAT8E5M2, FLOAT, FLOAT16, DOUBLE, INT4, INT8, INT16, "
+ "INT32, INT64, UINT4, UINT8, UINT16, UINT32, UINT64, STRING, UNDEFINED");
+#else
ONNX_UNSUPPORTED_DATA_TYPE(onnx_type,
"BOOL, BFLOAT16, FLOAT8E4M3FN, FLOAT8E5M2, FLOAT, FLOAT16, DOUBLE, INT8, INT16, "
"INT32, INT64, UINT8, UINT16, UINT32, UINT64, STRING, UNDEFINED");
+#endif
}
std::shared_ptr<ov::Node> get_monotonic_range_along_node_rank(const ov::Output<ov::Node>& value,
diff --git a/src/frontends/onnx/onnx_common/CMakeLists.txt b/src/frontends/onnx/onnx_common/CMakeLists.txt
index d63bce40830..a743c5ac40a 100644
--- a/src/frontends/onnx/onnx_common/CMakeLists.txt
+++ b/src/frontends/onnx/onnx_common/CMakeLists.txt
@@ -35,3 +35,18 @@ ov_link_system_libraries(${TARGET_NAME} PUBLIC onnx_proto onnx)
ov_add_clang_format_target(${TARGET_NAME}_clang FOR_TARGETS ${TARGET_NAME})
ov_install_static_lib(${TARGET_NAME} ${OV_CPACK_COMP_CORE})
+
+# Temporary solution until vcpkg doesn't have fresh ONNX,
+# trying determine used version of ONNX to enable modern functionality
+find_package(ONNX 1.16.0 QUIET COMPONENTS onnx onnx_proto NO_MODULE)
+if(ONNX_FOUND)
+ target_compile_definitions(${TARGET_NAME} PUBLIC ONNX_VERSION_116)
+else()
+ if(EXISTS "${CMAKE_SOURCE_DIR}/thirdparty/onnx/onnx/VERSION_NUMBER")
+ file(READ "${CMAKE_SOURCE_DIR}/thirdparty/onnx/onnx/VERSION_NUMBER" ONNX_VERSION)
+ string(STRIP "${ONNX_VERSION}" ONNX_VERSION)
+ if((ONNX_VERSION GREATER "1.16.0") OR (ONNX_VERSION EQUAL "1.16.0"))
+ target_compile_definitions(${TARGET_NAME} PUBLIC ONNX_VERSION_116)
+ endif()
+ endif()
+endif()
diff --git a/src/frontends/onnx/onnx_common/src/utils.cpp b/src/frontends/onnx/onnx_common/src/utils.cpp
index 35cbc6e8957..6ec409c5671 100644
--- a/src/frontends/onnx/onnx_common/src/utils.cpp
+++ b/src/frontends/onnx/onnx_common/src/utils.cpp
@@ -30,6 +30,10 @@ size_t get_onnx_data_size(int32_t onnx_type) {
return sizeof(ov::float8_e4m3);
case TensorProto_DataType_FLOAT8E5M2:
return sizeof(ov::float8_e5m2);
+#ifdef ONNX_VERSION_116
+ case TensorProto_DataType_INT4:
+ return sizeof(int8_t);
+#endif
case TensorProto_DataType_INT8:
return sizeof(int8_t);
case TensorProto_DataType_INT16:
@@ -38,6 +42,10 @@ size_t get_onnx_data_size(int32_t onnx_type) {
return sizeof(int32_t);
case TensorProto_DataType_INT64:
return sizeof(int64_t);
+#ifdef ONNX_VERSION_116
+ case TensorProto_DataType_UINT4:
+ return sizeof(uint8_t);
+#endif
case TensorProto_DataType_UINT8:
return sizeof(uint8_t);
case TensorProto_DataType_UINT16:
@@ -58,10 +66,16 @@ const std::map<ov::element::Type_t, TensorProto_DataType> OV_2_ONNX_TYPES = {
{ov::element::Type_t::f16, TensorProto_DataType::TensorProto_DataType_FLOAT16},
{ov::element::Type_t::f32, TensorProto_DataType::TensorProto_DataType_FLOAT},
{ov::element::Type_t::f64, TensorProto_DataType::TensorProto_DataType_DOUBLE},
+#ifdef ONNX_VERSION_116
+ {ov::element::Type_t::i4, TensorProto_DataType::TensorProto_DataType_INT4},
+#endif
{ov::element::Type_t::i8, TensorProto_DataType::TensorProto_DataType_INT8},
{ov::element::Type_t::i16, TensorProto_DataType::TensorProto_DataType_INT16},
{ov::element::Type_t::i32, TensorProto_DataType::TensorProto_DataType_INT32},
{ov::element::Type_t::i64, TensorProto_DataType::TensorProto_DataType_INT64},
+#ifdef ONNX_VERSION_116
+ {ov::element::Type_t::u4, TensorProto_DataType::TensorProto_DataType_UINT4},
+#endif
{ov::element::Type_t::u8, TensorProto_DataType::TensorProto_DataType_UINT8},
{ov::element::Type_t::u16, TensorProto_DataType::TensorProto_DataType_UINT16},
{ov::element::Type_t::u32, TensorProto_DataType::TensorProto_DataType_UINT32},
diff --git a/src/frontends/onnx/tests/CMakeLists.txt b/src/frontends/onnx/tests/CMakeLists.txt
index 27ce545394e..599c7c43b05 100644
--- a/src/frontends/onnx/tests/CMakeLists.txt
+++ b/src/frontends/onnx/tests/CMakeLists.txt
@@ -134,6 +134,21 @@ target_compile_definitions(ov_onnx_frontend_tests
set(ONNX_OPSET_VERSION 17 CACHE INTERNAL "Supported version of ONNX operator set")
target_compile_definitions(ov_onnx_frontend_tests PRIVATE ONNX_OPSET_VERSION=${ONNX_OPSET_VERSION})
+# Temporary solution until vcpkg doesn't have fresh ONNX,
+# trying determine used version of ONNX to enable modern functionality
+find_package(ONNX 1.16.0 QUIET COMPONENTS onnx onnx_proto NO_MODULE)
+if(ONNX_FOUND)
+ target_compile_definitions(ov_onnx_frontend_tests PRIVATE ONNX_VERSION_116)
+else()
+ if(EXISTS "${CMAKE_SOURCE_DIR}/thirdparty/onnx/onnx/VERSION_NUMBER")
+ file(READ "${CMAKE_SOURCE_DIR}/thirdparty/onnx/onnx/VERSION_NUMBER" ONNX_VERSION)
+ string(STRIP "${ONNX_VERSION}" ONNX_VERSION)
+ if((ONNX_VERSION GREATER "1.16.0") OR (ONNX_VERSION EQUAL "1.16.0"))
+ target_compile_definitions(ov_onnx_frontend_tests PRIVATE ONNX_VERSION_116)
+ endif()
+ endif()
+endif()
+
if(ONNX_TESTS_DEPENDENCIES)
add_dependencies(ov_onnx_frontend_tests ${ONNX_TESTS_DEPENDENCIES})
endif()
diff --git a/src/frontends/onnx/tests/models/int4_const.prototxt b/src/frontends/onnx/tests/models/int4_const.prototxt
new file mode 100644
index 00000000000..75babf7f19d
--- /dev/null
+++ b/src/frontends/onnx/tests/models/int4_const.prototxt
@@ -0,0 +1,41 @@
+ir_version: 10
+producer_name: "OpenVINO ONNX Frontend"
+graph {
+ name: "test"
+ node {
+ output: "Y"
+ op_type: "Constant"
+ attribute {
+ name: "value"
+ t {
+ dims: 4
+ data_type: 22
+ int32_data: 15
+ int32_data: 113
+ name: "int4_const"
+ }
+ type: TENSOR
+ }
+ }
+ node {
+ input: "Y"
+ output: "O"
+ op_type: "Shape"
+ }
+ output {
+ name: "O"
+ type {
+ tensor_type {
+ elem_type: 7
+ shape {
+ dim {
+ dim_value: 1
+ }
+ }
+ }
+ }
+ }
+}
+opset_import {
+ version: 21
+}
diff --git a/src/frontends/onnx/tests/models/int4_input.prototxt b/src/frontends/onnx/tests/models/int4_input.prototxt
new file mode 100644
index 00000000000..430830962b6
--- /dev/null
+++ b/src/frontends/onnx/tests/models/int4_input.prototxt
@@ -0,0 +1,57 @@
+ir_version: 9
+producer_name: "OpenVINO ONNX Frontend"
+graph {
+ name: "test"
+ node {
+ input: "I"
+ output: "O"
+ op_type: "Shape"
+ }
+ node {
+ input: "I"
+ output: "V"
+ op_type: "Identity"
+ }
+ input {
+ name: "I"
+ type {
+ tensor_type {
+ elem_type: 22
+ shape {
+ dim {
+ dim_value: 5
+ }
+ }
+ }
+ }
+ }
+ output {
+ name: "O"
+ type {
+ tensor_type {
+ elem_type: 7
+ shape {
+ dim {
+ dim_value: 1
+ }
+ }
+ }
+ }
+ }
+ output {
+ name: "V"
+ type {
+ tensor_type {
+ elem_type: 22
+ shape {
+ dim {
+ dim_value: 5
+ }
+ }
+ }
+ }
+ }
+}
+opset_import {
+ version: 21
+}
diff --git a/src/frontends/onnx/tests/models/uint4_const.prototxt b/src/frontends/onnx/tests/models/uint4_const.prototxt
new file mode 100644
index 00000000000..49072ae4f71
--- /dev/null
+++ b/src/frontends/onnx/tests/models/uint4_const.prototxt
@@ -0,0 +1,41 @@
+ir_version: 10
+producer_name: "OpenVINO ONNX Frontend"
+graph {
+ name: "test"
+ node {
+ output: "Y"
+ op_type: "Constant"
+ attribute {
+ name: "value"
+ t {
+ dims: 4
+ data_type: 21
+ int32_data: 16
+ int32_data: 248
+ name: "uint4_const"
+ }
+ type: TENSOR
+ }
+ }
+ node {
+ input: "Y"
+ output: "O"
+ op_type: "Shape"
+ }
+ output {
+ name: "O"
+ type {
+ tensor_type {
+ elem_type: 7
+ shape {
+ dim {
+ dim_value: 1
+ }
+ }
+ }
+ }
+ }
+}
+opset_import {
+ version: 21
+}
diff --git a/src/frontends/onnx/tests/models/uint4_input.prototxt b/src/frontends/onnx/tests/models/uint4_input.prototxt
new file mode 100644
index 00000000000..d09f25d71d6
--- /dev/null
+++ b/src/frontends/onnx/tests/models/uint4_input.prototxt
@@ -0,0 +1,57 @@
+ir_version: 9
+producer_name: "OpenVINO ONNX Frontend"
+graph {
+ name: "test"
+ node {
+ input: "I"
+ output: "O"
+ op_type: "Shape"
+ }
+ node {
+ input: "I"
+ output: "V"
+ op_type: "Identity"
+ }
+ input {
+ name: "I"
+ type {
+ tensor_type {
+ elem_type: 21
+ shape {
+ dim {
+ dim_value: 3
+ }
+ }
+ }
+ }
+ }
+ output {
+ name: "O"
+ type {
+ tensor_type {
+ elem_type: 7
+ shape {
+ dim {
+ dim_value: 1
+ }
+ }
+ }
+ }
+ }
+ output {
+ name: "V"
+ type {
+ tensor_type {
+ elem_type: 21
+ shape {
+ dim {
+ dim_value: 3
+ }
+ }
+ }
+ }
+ }
+}
+opset_import {
+ version: 21
+}
diff --git a/src/frontends/onnx/tests/onnx_import.in.cpp b/src/frontends/onnx/tests/onnx_import.in.cpp
index b387f6069d0..552f687ed23 100644
--- a/src/frontends/onnx/tests/onnx_import.in.cpp
+++ b/src/frontends/onnx/tests/onnx_import.in.cpp
@@ -159,6 +159,44 @@ OPENVINO_TEST(${BACKEND_NAME}, onnx_bool_init_raw) {
test_case.run();
}
+#ifdef ONNX_VERSION_116
+OPENVINO_TEST(${BACKEND_NAME}, onnx_int4_const) {
+ auto model = convert_model("int4_const.onnx");
+
+ auto test_case = ov::test::TestCase(model, s_device);
+ test_case.add_expected_output(std::vector<int64_t>{4});
+ test_case.run();
+}
+
+OPENVINO_TEST(${BACKEND_NAME}, onnx_int4_input) {
+ const auto model = convert_model("int4_input.onnx");
+ auto test_case = test::TestCase(model);
+ test_case.add_input<uint8_t>({0xEF, 0x01, 0x70});
+ test_case.add_expected_output<int64_t>({5});
+ test_case.add_expected_output<uint8_t>({0xEF, 0x01, 0x70});
+
+ test_case.run();
+}
+
+OPENVINO_TEST(${BACKEND_NAME}, onnx_uint4_const) {
+ auto model = convert_model("uint4_const.onnx");
+
+ auto test_case = ov::test::TestCase(model, s_device);
+ test_case.add_expected_output(std::vector<int64_t>{4});
+ test_case.run();
+}
+
+OPENVINO_TEST(${BACKEND_NAME}, onnx_uint4_input) {
+ const auto model = convert_model("uint4_input.onnx");
+ auto test_case = test::TestCase(model);
+ test_case.add_input<uint8_t>({0x01, 0xF0});
+ test_case.add_expected_output<int64_t>({3});
+ test_case.add_expected_output<uint8_t>({0x01, 0xF0});
+
+ test_case.run();
+}
+#endif
+
OPENVINO_TEST(${BACKEND_NAME}, onnx_model_add_abc_initializers) {
auto model = convert_model("add_abc_initializers.onnx");
diff --git a/src/tests/test_utils/common_test_utils/include/common_test_utils/test_case.hpp b/src/tests/test_utils/common_test_utils/include/common_test_utils/test_case.hpp
index 7347146c507..457086f6805 100644
--- a/src/tests/test_utils/common_test_utils/include/common_test_utils/test_case.hpp
+++ b/src/tests/test_utils/common_test_utils/include/common_test_utils/test_case.hpp
@@ -9,6 +9,7 @@
#include "common_test_utils/all_close.hpp"
#include "common_test_utils/all_close_f.hpp"
#include "common_test_utils/test_tools.hpp"
+#include "openvino/core/type/element_iterator.hpp"
#include "openvino/runtime/core.hpp"
#include "openvino/util/file_util.hpp"
@@ -19,6 +20,23 @@ class TestCase {
public:
TestCase(const std::shared_ptr<ov::Model>& function, const std::string& dev = "TEMPLATE");
+ /// \brief This method is used to eliminate issue caused by calling .data for vector<bool>
+ template <typename T, typename std::enable_if<!std::is_same<T, bool>::value, bool>::type = true>
+ void copy_values_to_tensor(ov::Tensor& tensor, const std::vector<T>& values) {
+ if (!ov::element::is_nibble_type(tensor.get_element_type())) {
+ std::copy(values.begin(), values.end(), tensor.data<T>());
+ } else {
+ size_t size = sizeof(T) * values.size();
+ if (tensor.get_byte_size() < size)
+ size = tensor.get_byte_size();
+ std::memcpy(tensor.data(), values.data(), size);
+ }
+ }
+ template <typename T, typename std::enable_if<std::is_same<T, bool>::value, bool>::type = true>
+ void copy_values_to_tensor(ov::Tensor& tensor, const std::vector<bool>& values) {
+ std::copy(values.begin(), values.end(), tensor.data<bool>());
+ }
+
template <typename T>
void add_input(const ov::Shape& shape, const std::vector<T>& values) {
const auto params = m_function->get_parameters();
@@ -44,8 +62,7 @@ public:
if (is_dynamic) {
ov::Tensor tensor(params.at(m_input_index)->get_element_type(), shape);
-
- std::copy(values.begin(), values.end(), tensor.data<T>());
+ copy_values_to_tensor<T>(tensor, values);
m_request.set_input_tensor(m_input_index, tensor);
} else {
auto tensor = m_request.get_input_tensor(m_input_index);
@@ -56,7 +73,7 @@ public:
tensor.get_size(),
" and values size is ",
values.size());
- std::copy(values.begin(), values.end(), tensor.data<T>());
+ copy_values_to_tensor<T>(tensor, values);
}
++m_input_index;
@@ -122,7 +139,7 @@ public:
m_output_index);
ov::Tensor tensor(results[m_output_index]->get_output_element_type(0), expected_shape);
- std::copy(values.begin(), values.end(), tensor.data<T>());
+ copy_values_to_tensor<T>(tensor, values);
m_expected_outputs.push_back(std::move(tensor));
diff --git a/src/tests/test_utils/common_test_utils/src/all_close.cpp b/src/tests/test_utils/common_test_utils/src/all_close.cpp
index 805e090206c..6421946e60f 100644
--- a/src/tests/test_utils/common_test_utils/src/all_close.cpp
+++ b/src/tests/test_utils/common_test_utils/src/all_close.cpp
@@ -24,14 +24,30 @@ namespace utils {
static_cast<ov::element_type_traits<ov::element::type>::value_type>(atol));
switch (a.get_element_type()) {
- all_close_ov_type(u8) all_close_ov_type(u16) all_close_ov_type(u32) all_close_ov_type(u64) all_close_ov_type(i8)
- all_close_ov_type(i16) all_close_ov_type(i32) all_close_ov_type(i64)
- // all_close_ov_type(bf16)
- // all_close_ov_type(f16)
- all_close_ov_type(f32) all_close_ov_type(f64) all_close_ov_type(boolean) default
- : return ::testing::AssertionFailure()
- << "Cannot compare tensors with unsupported element type: "
- << a.get_element_type();
+ all_close_ov_type(u8);
+ all_close_ov_type(u16);
+ all_close_ov_type(u32);
+ all_close_ov_type(u64);
+ all_close_ov_type(i8);
+ all_close_ov_type(i16);
+ all_close_ov_type(i32);
+ all_close_ov_type(i64);
+ // all_close_ov_type(bf16);
+ // all_close_ov_type(f16);
+ all_close_ov_type(f32);
+ all_close_ov_type(f64);
+ all_close_ov_type(boolean);
+ case ov::element::i4:
+ case ov::element::u4:
+ return all_close(static_cast<const uint8_t*>(a.data()),
+ static_cast<const uint8_t*>(b.data()),
+ a.get_byte_size(),
+ static_cast<uint8_t>(rtol),
+ static_cast<uint8_t>(atol));
+ ;
+ default:
+ return ::testing::AssertionFailure()
+ << "Cannot compare tensors with unsupported element type: " << a.get_element_type();
}
}
} // namespace utils
diff --git a/src/tests/test_utils/common_test_utils/src/test_case.cpp b/src/tests/test_utils/common_test_utils/src/test_case.cpp
index 6674878c636..0df564d4a0e 100644
--- a/src/tests/test_utils/common_test_utils/src/test_case.cpp
+++ b/src/tests/test_utils/common_test_utils/src/test_case.cpp
@@ -118,6 +118,9 @@ std::pair<testing::AssertionResult, size_t> TestCase::compare_results(size_t tol
case element::Type_t::f64:
res = compare_values<double>(exp_result, result_tensor, tolerance_bits);
break;
+ case element::Type_t::i4:
+ res = compare_values<uint8_t>(exp_result, result_tensor, tolerance_bits);
+ break;
case element::Type_t::i8:
res = compare_values<int8_t>(exp_result, result_tensor, tolerance_bits);
break;
@@ -130,6 +133,9 @@ std::pair<testing::AssertionResult, size_t> TestCase::compare_results(size_t tol
case element::Type_t::i64:
res = compare_values<int64_t>(exp_result, result_tensor, tolerance_bits);
break;
+ case element::Type_t::u4:
+ res = compare_values<uint8_t>(exp_result, result_tensor, tolerance_bits);
+ break;
case element::Type_t::u8:
res = compare_values<uint8_t>(exp_result, result_tensor, tolerance_bits);
break;