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
Refactor reshape function to avoid u4/i4 type has memory issue (#34478)
liang2fabc60bf465
diff --git a/src/core/reference/include/openvino/reference/reshape.hpp b/src/core/reference/include/openvino/reference/reshape.hppindex 16648730620..d83e73e91a5 100644--- a/src/core/reference/include/openvino/reference/reshape.hpp+++ b/src/core/reference/include/openvino/reference/reshape.hpp@@ -12,17 +12,28 @@ namespace reference {/*** @brief Basic reshape operation, without axes reorder.+ * This only for byte like types(elem size is N*8)** @param in Pointer to input data.* @param out Pointer to output data.* @param in_shape Input data shape.- * @param out_shape Output data shape.- * @param elem_size Single data element size im bytes.+ * @param elem_size Single data element size in bytes.*/inline void reshape(const char* in, char* out, const Shape& in_shape, size_t elem_size) {std::memcpy(out, in, shape_size(in_shape) * elem_size);}+/**+ * @brief Basic reshape operation copy with real size, without axes reorder.+ *+ * @param in Pointer to input data.+ * @param out Pointer to output data.+ * @param copy_size Number of bytes to copy.+ */+inline void reshape(const char* in, char* out, size_t copy_size) {+ std::memcpy(out, in, copy_size);+}+/*** @brief Basic reshape operation for string element type.* Input data are simply copied to the output.diff --git a/src/core/src/op/reshape.cpp b/src/core/src/op/reshape.cppindex b52b750bacc..483f1d54665 100644--- a/src/core/src/op/reshape.cpp+++ b/src/core/src/op/reshape.cpp@@ -67,8 +67,7 @@ bool Reshape::evaluate_reshape(TensorVector& outputs, const TensorVector& inputs} else {ov::reference::reshape(static_cast<const char*>(inputs[0].data()),static_cast<char*>(outputs[0].data()),- inputs[0].get_shape(),- inputs[0].get_element_type().size());+ inputs[0].get_byte_size());}return true;}diff --git a/src/plugins/template/tests/functional/op_reference/base_reference_test.hpp b/src/plugins/template/tests/functional/op_reference/base_reference_test.hppindex d9e963f161d..35bf023d0c6 100644--- a/src/plugins/template/tests/functional/op_reference/base_reference_test.hpp+++ b/src/plugins/template/tests/functional/op_reference/base_reference_test.hpp@@ -48,7 +48,7 @@ template <class T>ov::Tensor CreateTensor(const ov::element::Type& element_type, const std::vector<T>& values, size_t size = 0) {size_t real_size = size ? size : values.size() * sizeof(T) / element_type.size();ov::Tensor tensor{element_type, {real_size}};- std::memcpy(tensor.data(), values.data(), std::min(real_size * element_type.size(), sizeof(T) * values.size()));+ std::memcpy(tensor.data(), values.data(), std::min(tensor.get_byte_size(), sizeof(T) * values.size()));return tensor;}diff --git a/src/plugins/template/tests/functional/op_reference/reshape.cpp b/src/plugins/template/tests/functional/op_reference/reshape.cppindex 43cd1f31682..b6f11ee322e 100644--- a/src/plugins/template/tests/functional/op_reference/reshape.cpp+++ b/src/plugins/template/tests/functional/op_reference/reshape.cpp@@ -299,6 +299,29 @@ std::vector<ReshapeParams> generateParamsForReshape8Bit() {return params;}+template <element::Type_t ET>+std::vector<ReshapeParams> generateParamsForReshape4Bit() {+ using T = typename element_type_traits<ET>::value_type;++ std::vector<ReshapeParams> params{+ ReshapeParams(Shape{2, 2, 3},+ Shape{12},+ ET,+ ET,+ std::vector<T>{0x12, 0x34, 0x56, 0x78, 0x1A, 0x2C},+ std::vector<T>{0x12, 0x34, 0x56, 0x78, 0x1A, 0x2C},+ false),+ ReshapeParams(Shape{1, 1, 1}, Shape{}, ET, ET, std::vector<T>{6}, std::vector<T>{6}, false),+ ReshapeParams(Shape{}, Shape{1, 1, 1, 1, 1, 1}, ET, ET, std::vector<T>{7}, std::vector<T>{7}, false),+ ReshapeParams(Shape{3}, Shape{3, 1}, ET, ET, std::vector<T>{0x12, 0x03}, std::vector<T>{0x12, 0x03}, false),+ ReshapeParams(Shape{3}, Shape{1, 3}, ET, ET, std::vector<T>{0x12, 0x03}, std::vector<T>{0x12, 0x03}, false),+ ReshapeParams(Shape{3}, Shape{1, 3, 1}, ET, ET, std::vector<T>{0x12, 0x03}, std::vector<T>{0x12, 0x03}, false),+ ReshapeParams(Shape{1}, Shape{}, ET, ET, std::vector<T>{1}, std::vector<T>{1}, false),+ ReshapeParams(Shape{}, Shape{}, ET, ET, std::vector<T>{1}, std::vector<T>{1}, false)};++ return params;+}+template <element::Type_t ET>std::vector<ReshapeShuffleParams> generateParamsForReshapeShuffle() {using T = typename element_type_traits<ET>::value_type;@@ -325,6 +348,8 @@ std::vector<ReshapeParams> generateCombinedParamsForReshape() {generateParamsForReshape<element::Type_t::u16>(),generateParamsForReshape8Bit<element::Type_t::i8>(),generateParamsForReshape8Bit<element::Type_t::u8>(),+ generateParamsForReshape4Bit<element::Type_t::i4>(),+ generateParamsForReshape4Bit<element::Type_t::u4>(),generateParamsForReshapeString()};std::vector<ReshapeParams> combinedParams;