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
[Ref][CPU] Add string support to `Transpose` (#34862)
Przemyslaw Wysocki83b20a8538f3
diff --git a/src/core/reference/include/openvino/reference/transpose.hpp b/src/core/reference/include/openvino/reference/transpose.hppindex 5770dc8856c..780ba95186d 100644--- a/src/core/reference/include/openvino/reference/transpose.hpp+++ b/src/core/reference/include/openvino/reference/transpose.hpp@@ -7,6 +7,7 @@#include <cfenv>#include <cmath>#include <numeric>+#include <string>#include <vector>#include "openvino/core/shape.hpp"@@ -30,5 +31,38 @@ void transpose(const char* data,size_t element_size,const std::vector<int64_t>& axes_order,const Shape& out_shape);++/**+ * @brief Reference implementation of Transpose operator for string element type.+ *+ * @param data Pointer to input string array.+ * @param out Pointer to output string array.+ * @param data_shape Input data shape.+ * @param axes_order Transpose order.+ * @param out_shape Output data shape.+ */+void transpose(const std::string* data,+ std::string* out,+ const Shape& data_shape,+ const std::vector<int64_t>& axes_order,+ const Shape& out_shape);++/**+ * @brief Reference implementation of Transpose operator for i4/u4 element types.+ *+ * Supports tensors up to 3D. For 3D tensors only the order [0, 2, 1] is supported.+ *+ * @param data Pointer to input data (packed 4-bit values).+ * @param out Pointer to output data (packed 4-bit values).+ * @param data_shape Input data shape.+ * @param axes_order Transpose order.+ * @param out_shape Output data shape.+ */+void transpose_4bit(const uint8_t* data,+ uint8_t* out,+ const Shape& data_shape,+ const std::vector<int64_t>& axes_order,+ const Shape& out_shape);+} // namespace reference} // namespace ovdiff --git a/src/core/reference/src/op/transpose.cpp b/src/core/reference/src/op/transpose.cppindex 293f937629e..980a405264b 100644--- a/src/core/reference/src/op/transpose.cpp+++ b/src/core/reference/src/op/transpose.cpp@@ -6,11 +6,15 @@#include <cfenv>#include <cmath>+#include <cstdint>#include <numeric>+#include <string>#include <vector>#include "openvino/core/shape.hpp"#include "openvino/reference/reshape.hpp"+#include "openvino/reference/utils/coordinate_index.hpp"+#include "openvino/reference/utils/coordinate_transform.hpp"namespace ov {namespace reference {@@ -25,5 +29,104 @@ void transpose(const char* data,const AxisVector axes_vector(axes_order.begin(), axes_order.end());reshape(data, out, data_shape, axes_vector, out_shape, element_size);}++void transpose(const std::string* data,+ std::string* out,+ const Shape& data_shape,+ const std::vector<int64_t>& axes_order,+ const Shape& out_shape) {+ const size_t ndim = data_shape.size();+ ov::Coordinate src_coord(ndim);+ const ov::CoordinateTransformBasic dst_transform{out_shape};+ for (const auto& dst_coord : dst_transform) {+ for (size_t j = 0; j < ndim; ++j)+ src_coord[axes_order[j]] = dst_coord[j];+ out[ov::coordinate_index(dst_coord, out_shape)] = data[ov::coordinate_index(src_coord, data_shape)];+ }+}++namespace {+enum class int4_extract_t : uint8_t { low_half = 0, high_half = 4 };++struct int4_iterator {+ explicit int4_iterator(uint8_t* ptr) : m_ptr(ptr), m_half(int4_extract_t::low_half) {}+ explicit int4_iterator(uint8_t* ptr, int4_extract_t half) : m_ptr(ptr), m_half(half) {}++ void operator++() {+ if (m_half == int4_extract_t::low_half) {+ m_half = int4_extract_t::high_half;+ } else {+ m_half = int4_extract_t::low_half;+ m_ptr += 1;+ }+ }++ int4_iterator operator+(const size_t shift) const {+ return int4_iterator{m_ptr + shift / 2, shift % 2 ? int4_extract_t::high_half : int4_extract_t::low_half};+ }++ void copy_from(const int4_iterator& from) const {+ uint8_t from_val = *from.m_ptr;+ uint8_t mask_from = from.m_half == int4_extract_t::high_half ? 0xF0 : 0x0F;+ uint8_t mask_to = m_half == int4_extract_t::high_half ? 0x0F : 0xF0;++ if (from.m_half < m_half) {+ from_val <<= 4;+ } else if (from.m_half > m_half) {+ from_val >>= 4;+ } else {+ from_val &= mask_from;+ }++ *m_ptr = (*m_ptr & mask_to) | from_val;+ }++ uint8_t* m_ptr;+ int4_extract_t m_half;+};++void transpose_xy_4bit(int4_iterator& out_ptr, int4_iterator& in_ptr, size_t out_shape_d0, size_t out_shape_d1) {+ for (size_t i = 0; i < out_shape_d0; i++) {+ size_t off = i;+ for (size_t j = 0; j < out_shape_d1; j++) {+ out_ptr.copy_from(in_ptr + off);+ ++out_ptr;+ off += out_shape_d0;+ }+ }+}+} // namespace++void transpose_4bit(const uint8_t* data,+ uint8_t* out,+ const Shape& data_shape,+ const std::vector<int64_t>& axes_order,+ const Shape& out_shape) {+ if (data_shape.size() == 2) {+ auto out_ptr = int4_iterator(out);+ auto in_ptr = int4_iterator(const_cast<uint8_t*>(data));+ transpose_xy_4bit(out_ptr, in_ptr, out_shape[0], out_shape[1]);+ } else if (data_shape.size() == 3) {+ OPENVINO_ASSERT(axes_order[0] == 0 && axes_order[1] == 2 && axes_order[2] == 1,+ "Unsupported transpose order for i4/u4 type");+ const auto out_batch = out_shape[0];+ const auto out_shape_d0 = out_shape[1];+ const auto out_shape_d1 = out_shape[2];+ OPENVINO_ASSERT((out_shape_d0 * out_shape_d1) % 2 == 0,+ "Only supports even number of i4/u4 data in each batch for transposing");+ size_t batch_offset = 0;+ for (size_t b = 0; b < out_batch; b++) {+ uint8_t* out_batch_base = out + batch_offset / 2;+ uint8_t* in_batch_base = const_cast<uint8_t*>(data) + batch_offset / 2;+ auto out_ptr = int4_iterator(out_batch_base);+ auto in_ptr = int4_iterator(in_batch_base);+ transpose_xy_4bit(out_ptr, in_ptr, out_shape_d0, out_shape_d1);+ batch_offset += out_shape_d0 * out_shape_d1;+ }+ } else {+ OPENVINO_THROW("Transpose for i4/u4 dtype is supported only for ndims <= 3");+ }+}+} // namespace reference} // namespace ovdiff --git a/src/core/src/op/transpose.cpp b/src/core/src/op/transpose.cppindex 64a9244c4a0..516ab4c388a 100644--- a/src/core/src/op/transpose.cpp+++ b/src/core/src/op/transpose.cpp@@ -1,9 +1,10 @@// Copyright (C) 2018-2026 Intel Corporation// SPDX-License-Identifier: Apache-2.0//-#include "openvino/op/transpose.hpp"+#include <string>+#include "bound_evaluate.hpp"#include "itt.hpp"#include "openvino/core/validation_util.hpp"@@ -39,8 +40,6 @@ std::shared_ptr<Node> Transpose::clone_with_new_inputs(const OutputVector& new_areturn std::make_shared<Transpose>(new_args[ARG], new_args[ORDER]);}-enum class int4_extract_t : uint8_t { low_half = 0, high_half = 4 };-bool Transpose::evaluate(TensorVector& outputs, const TensorVector& inputs) const {OV_OP_SCOPE(v1_Transpose_evaluate);OPENVINO_ASSERT(outputs.size() == 1);@@ -56,84 +55,18 @@ bool Transpose::evaluate(TensorVector& outputs, const TensorVector& inputs) consauto& out = outputs[ARG_T];out.set_shape(out_shape);- struct int4_iterator {- explicit int4_iterator(uint8_t* ptr) : m_ptr(ptr), m_half(int4_extract_t::low_half) {}- explicit int4_iterator(uint8_t* ptr, int4_extract_t half) : m_ptr(ptr), m_half(half) {}- void operator++() {- if (m_half == int4_extract_t::low_half) {- m_half = int4_extract_t::high_half;- } else {- m_half = int4_extract_t::low_half;- m_ptr += 1;- }- }-- int4_iterator operator+(const size_t shift) {- return int4_iterator{m_ptr + shift / 2,- shift % 2 ? int4_extract_t::high_half : int4_extract_t::low_half};- }-- void copy_from(const int4_iterator& from) const {- // TODO: DOUBLE CHECK THIS- // perhaps that's not entirely accurate- uint8_t from_val = *from.m_ptr;- uint8_t mask_from = from.m_half == int4_extract_t::high_half ? 0xF0 : 0x0F;- uint8_t mask_to = m_half == int4_extract_t::high_half ? 0x0F : 0xF0;-- if (from.m_half < m_half) {- from_val <<= 4;- } else if (from.m_half > m_half) {- from_val >>= 4;- } else {- from_val &= mask_from;- }-- *m_ptr = (*m_ptr & mask_to) | from_val;- }-- uint8_t* m_ptr;- int4_extract_t m_half;- };-if (arg_type == ov::element::i4 || arg_type == ov::element::u4) {- // The int4_iterator not supports const pointer but these data are not modified- auto transpose_xy =- [](int4_iterator& out_ptr, int4_iterator& in_ptr, size_t out_shape_d0, size_t out_shape_d1) {- for (size_t i = 0; i < out_shape_d0; i++) {- size_t off = i;- for (size_t j = 0; j < out_shape_d1; j++) {- out_ptr.copy_from(in_ptr + off);- ++out_ptr;- off += out_shape_d0;- }- }- };- if (arg.get_shape().size() == 2) {- auto out_ptr = int4_iterator(static_cast<uint8_t*>(out.data()));- auto in_ptr = int4_iterator(static_cast<uint8_t*>(const_cast<void*>(arg.data())));- const auto out_shape_d0 = out_shape[0];- const auto out_shape_d1 = out_shape[1];- transpose_xy(out_ptr, in_ptr, out_shape_d0, out_shape_d1);- } else if (arg.get_shape().size() == 3) {- OPENVINO_ASSERT(axes_order[0] == 0 && axes_order[1] == 2 && axes_order[2] == 1,- "Unsupported transpose order for i4/u4 type");- const auto out_batch = out_shape[0];- const auto out_shape_d0 = out_shape[1];- const auto out_shape_d1 = out_shape[2];- OPENVINO_ASSERT((out_shape_d0 * out_shape_d1) % 2 == 0,- "Only supports even number of i4/u4 data in each batch for transposing");- size_t batch_offset = 0;- for (size_t b = 0; b < out_batch; b++) {- uint8_t* out_batch_base = static_cast<uint8_t*>(out.data()) + batch_offset / 2;- uint8_t* in_batch_base = (static_cast<uint8_t*>(const_cast<void*>(arg.data()))) + batch_offset / 2;- auto out_ptr = int4_iterator(out_batch_base);- auto in_ptr = int4_iterator(in_batch_base);- transpose_xy(out_ptr, in_ptr, out_shape_d0, out_shape_d1);- batch_offset += out_shape_d0 * out_shape_d1;- }- } else {- OPENVINO_THROW("Transpose for i4/u4 dtype is supported only for ndims <= 3");- }+ reference::transpose_4bit(static_cast<const uint8_t*>(arg.data()),+ static_cast<uint8_t*>(out.data()),+ arg.get_shape(),+ axes_order,+ out_shape);+ } else if (arg_type == ov::element::string) {+ reference::transpose(static_cast<const std::string*>(arg.data()),+ static_cast<std::string*>(out.data()),+ arg.get_shape(),+ axes_order,+ out_shape);} else {reference::transpose(static_cast<const char*>(arg.data()),static_cast<char*>(out.data()),diff --git a/src/core/tests/type_prop/transpose.cpp b/src/core/tests/type_prop/transpose.cppindex 0877675139e..93e004d90dd 100644--- a/src/core/tests/type_prop/transpose.cpp+++ b/src/core/tests/type_prop/transpose.cpp@@ -427,3 +427,33 @@ TEST_P(TransposeTest, propagate_symbols) {EXPECT_EQ(get_shape_symbols(output->get_output_partial_shape(op::v1::Transpose::ARG_T)), exp_symbols);}++TEST(type_prop, transpose_arg_string_element_type_static) {+ auto arg = make_shared<ov::op::v0::Parameter>(element::string, Shape{3, 4});+ auto input_order = ov::op::v0::Constant::create(element::i64, Shape{2}, vector<int64_t>{1, 0});++ auto r = make_shared<op::v1::Transpose>(arg, input_order);++ EXPECT_EQ(r->get_output_element_type(0), element::string);+ EXPECT_EQ(r->get_output_partial_shape(0), (PartialShape{4, 3}));+}++TEST(type_prop, transpose_arg_string_element_type_dynamic_shape) {+ auto arg = make_shared<ov::op::v0::Parameter>(element::string, PartialShape{-1, -1, -1});+ auto input_order = make_shared<ov::op::v0::Parameter>(element::i64, Shape{3});++ auto r = make_shared<op::v1::Transpose>(arg, input_order);++ EXPECT_EQ(r->get_output_element_type(0), element::string);+ EXPECT_EQ(r->get_output_partial_shape(0), PartialShape::dynamic(3));+}++TEST(type_prop, transpose_arg_string_element_type_empty_order) {+ auto arg = make_shared<ov::op::v0::Parameter>(element::string, Shape{2, 3, 4});+ auto input_order = ov::op::v0::Constant::create(element::i64, Shape{0}, vector<int64_t>{});++ auto r = make_shared<op::v1::Transpose>(arg, input_order);++ EXPECT_EQ(r->get_output_element_type(0), element::string);+ EXPECT_EQ(r->get_output_partial_shape(0), (PartialShape{4, 3, 2}));+}diff --git a/src/plugins/intel_cpu/src/nodes/transpose.cpp b/src/plugins/intel_cpu/src/nodes/transpose.cppindex 119b981ebc3..b743844a491 100644--- a/src/plugins/intel_cpu/src/nodes/transpose.cpp+++ b/src/plugins/intel_cpu/src/nodes/transpose.cpp@@ -52,6 +52,11 @@ bool Transpose::isSupportedOperation(const std::shared_ptr<const ov::Node>& op,errorMessage = "Constant expected as the second input for static shapes.";return false;}++ if (op->get_input_element_type(INPUT_DATA_IDX) == ov::element::string) {+ errorMessage = "String element type is not supported.";+ return false;+ }} catch (...) {return false;}diff --git a/src/plugins/template/tests/functional/op_reference/transpose.cpp b/src/plugins/template/tests/functional/op_reference/transpose.cppindex fdb377fe9e4..46c9e6a1c54 100644--- a/src/plugins/template/tests/functional/op_reference/transpose.cpp+++ b/src/plugins/template/tests/functional/op_reference/transpose.cpp@@ -105,6 +105,19 @@ TEST_P(ReferenceTransposeLayerTest, CompareWithRefs) {}}+TEST(TransposeEvaluateTest, ReturnsFalseForNonIntegralOrderType) {+ const auto data = std::make_shared<op::v0::Parameter>(element::f32, Shape{2, 3});+ const auto order_param = std::make_shared<op::v0::Parameter>(element::i64, Shape{2});+ const auto transpose = std::make_shared<op::v1::Transpose>(data, order_param);++ ov::Tensor order_tensor(element::f32, Shape{2});+ order_tensor.data<float>()[0] = 1.0f;+ order_tensor.data<float>()[1] = 0.0f;+ ov::TensorVector outputs{ov::Tensor(element::f32, Shape{3, 2})};+ const ov::TensorVector inputs{ov::Tensor(element::f32, Shape{2, 3}), order_tensor};+ EXPECT_FALSE(transpose->evaluate(outputs, inputs));+}+template <element::Type_t IN_ET>std::vector<TransposeParams> generateTransposeParams() {using T = typename element_type_traits<IN_ET>::value_type;@@ -180,6 +193,61 @@ std::vector<TransposeParams> generateTransposeParams() {return transposeParams;}+std::vector<TransposeParams> generateTransposeParamsForString() {+ using T = std::string;+ const auto ET = element::string;+ return std::vector<TransposeParams>{+ // 2D transpose with uniform-length strings+ TransposeParams(PartialShape::dynamic(),+ reference_tests::Tensor(ET, {2, 3}, std::vector<T>{"a0", "a1", "a2", "b0", "b1", "b2"}),+ reference_tests::Tensor(element::i64, {2}, std::vector<int64_t>{1, 0}),+ reference_tests::Tensor(ET, {3, 2}, std::vector<T>{"a0", "b0", "a1", "b1", "a2", "b2"}),+ "transpose_string_2d"),+ // 2D transpose with variable-length strings+ TransposeParams(PartialShape::dynamic(),+ reference_tests::Tensor(ET, {2, 3}, std::vector<T>{"a", "abc", "abcd", "x", "xy", "xyz"}),+ reference_tests::Tensor(element::i64, {2}, std::vector<int64_t>{1, 0}),+ reference_tests::Tensor(ET, {3, 2}, std::vector<T>{"a", "x", "abc", "xy", "abcd", "xyz"}),+ "transpose_string_2d_variable_length"),+ // 3D transpose with uniform-length strings+ TransposeParams(+ PartialShape::dynamic(),+ reference_tests::Tensor(ET,+ {2, 2, 3},+ std::vector<T>{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l"}),+ reference_tests::Tensor(element::i64, {3}, std::vector<int64_t>{2, 0, 1}),+ reference_tests::Tensor(ET,+ {3, 2, 2},+ std::vector<T>{"a", "d", "g", "j", "b", "e", "h", "k", "c", "f", "i", "l"}),+ "transpose_string_3d"),+ // 3D transpose with variable-length strings+ TransposeParams(+ PartialShape::dynamic(),+ reference_tests::Tensor(+ ET,+ {2, 2, 3},+ std::vector<T>{"", "x", "hello", "world!", "ab", "abcde", "A", "BB", "CCC", "DDDD", "EEEEE", "FFFFFF"}),+ reference_tests::Tensor(element::i64, {3}, std::vector<int64_t>{2, 0, 1}),+ reference_tests::Tensor(+ ET,+ {3, 2, 2},+ std::vector<T>{"", "world!", "A", "DDDD", "x", "ab", "BB", "EEEEE", "hello", "abcde", "CCC", "FFFFFF"}),+ "transpose_string_3d_variable_length"),+ // static-shape identity (order {0,1})+ TransposeParams({},+ reference_tests::Tensor(ET, {2, 3}, std::vector<T>{"x0", "x1", "x2", "y0", "y1", "y2"}),+ reference_tests::Tensor(element::i64, {2}, std::vector<int64_t>{0, 1}),+ reference_tests::Tensor(ET, {2, 3}, std::vector<T>{"x0", "x1", "x2", "y0", "y1", "y2"}),+ "transpose_string_identity_static"),+ // dynamic-shape identity (order {0,1})+ TransposeParams(PartialShape::dynamic(),+ reference_tests::Tensor(ET, {2, 3}, std::vector<T>{"x0", "x1", "x2", "y0", "y1", "y2"}),+ reference_tests::Tensor(element::i64, {2}, std::vector<int64_t>{0, 1}),+ reference_tests::Tensor(ET, {2, 3}, std::vector<T>{"x0", "x1", "x2", "y0", "y1", "y2"}),+ "transpose_string_identity_dynamic"),+ };+}+template <element::Type_t IN_ET>std::vector<TransposeParams> generateThrowingTransposeParams() {using T = typename element_type_traits<IN_ET>::value_type;@@ -219,6 +287,7 @@ std::vector<TransposeParams> generateTransposeCombinedParams() {generateTransposeParams<element::Type_t::f32>(),generateThrowingTransposeParams<element::Type_t::f32>(),generateThrowingTransposeParams<element::Type_t::i32>(),+ generateTransposeParamsForString(),};std::vector<TransposeParams> combinedParams;