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
[CPU][REF][GatherElements]: Support negative indices. (#30800)
Piotr Kowalczyk41aea968b926
diff --git a/docs/articles_en/documentation/openvino-ir-format/operation-sets/operation-specs/movement/gather-elements-6.rst b/docs/articles_en/documentation/openvino-ir-format/operation-sets/operation-specs/movement/gather-elements-6.rstindex 00e79d4ef5d..57b68439252 100644--- a/docs/articles_en/documentation/openvino-ir-format/operation-sets/operation-specs/movement/gather-elements-6.rst+++ b/docs/articles_en/documentation/openvino-ir-format/operation-sets/operation-specs/movement/gather-elements-6.rst@@ -99,7 +99,11 @@ Example 3 ``indices`` has lesser (than ``data``) shape:* **1**: Tensor of type *T*. This is a tensor of a ``rank >= 1``. **Required.*** **2**: Tensor of type *T_IND* with the same rank as the input. All index values are expected to be- within bounds ``[0, s-1]``, where ``s`` is size along ``axis`` dimension of the ``data`` tensor. **Required.**+ within bounds ``[-s, s-1]``, where ``s`` is size along ``axis`` dimension of the ``data`` tensor. **Required.**++.. note::++ Behavior before 2025.3 OpenVINO release: Negative indices were not supported.**Outputs**:diff --git a/src/core/reference/include/openvino/reference/gather_elements.hpp b/src/core/reference/include/openvino/reference/gather_elements.hppindex 0ac1f7438d6..db670f2397a 100644--- a/src/core/reference/include/openvino/reference/gather_elements.hpp+++ b/src/core/reference/include/openvino/reference/gather_elements.hpp@@ -8,6 +8,17 @@namespace ov {namespace reference {+namespace helpers {+template <typename T>+T handle_negative_indices(const T* indices, size_t idx, T axis_dim_size) {+ const T index = indices[idx];+ OPENVINO_ASSERT(index < axis_dim_size && index >= -axis_dim_size,+ "indices values of GatherElement exceed data size");+ const T fixedIdx = index < 0 ? axis_dim_size + index : index;+ return fixedIdx;+}+} // namespace helpers+template <typename T, typename U>void gather_elements(const T* data,const U* indices,@@ -23,13 +34,13 @@ void gather_elements(const T* data,throw std::domain_error{"axis for GatherElements exceeds allowed range [0, data_rank)"};}+ const U axis_dim_size = static_cast<U>(data_shape[axis]);+// in 1D case results can be achieved without additional calculationsif (data_shape.size() == 1) {for (size_t i = 0; i < indices_shape[0]; i++) {- if (static_cast<size_t>(indices[i]) >= data_shape[0]) {- throw std::domain_error{"indices values of GatherElement exceed data size"};- }- out[i] = data[indices[i]];+ const U idx = helpers::handle_negative_indices<U>(indices, i, axis_dim_size);+ out[i] = data[idx];}return;}@@ -40,14 +51,10 @@ void gather_elements(const T* data,size_t num_columns = indices_shape[1];size_t data_num_columns = data_shape[1];if (data_shape.size() == 2) {- size_t idx;if (axis == 0) {for (size_t i = 0; i < num_rows; i++)for (size_t j = 0; j < num_columns; j++) {- idx = indices[num_columns * i + j];- if (idx < 0 || idx >= data_shape[0]) {- throw std::domain_error{"indices values of GatherElement exceed data size"};- }+ const U idx = helpers::handle_negative_indices<U>(indices, num_columns * i + j, axis_dim_size);out[num_columns * i + j] = data[data_num_columns * idx + j];}return;@@ -55,11 +62,7 @@ void gather_elements(const T* data,{for (size_t i = 0; i < num_rows; i++)for (size_t j = 0; j < num_columns; j++) {- idx = indices[num_columns * i + j];- if (idx < 0 || idx >= data_shape[1]) {- throw std::domain_error{"indices values of GatherElement exceed data size"};- }-+ const U idx = helpers::handle_negative_indices<U>(indices, num_columns * i + j, axis_dim_size);out[num_columns * i + j] = data[data_num_columns * i + idx];}return;@@ -99,10 +102,8 @@ void gather_elements(const T* data,for (size_t outer_sum = 0, i = 0; outer_sum < max_outer_sum; outer_sum += outer_sum_inc)for (size_t k = 0; k < indices_shape[axis]; k++)for (size_t inner_sum = 0; inner_sum < max_inner_sum; inner_sum++) {- if (indices[i] < 0 || static_cast<size_t>(indices[i]) >= data_shape[axis]) {- throw std::domain_error{"indices values of GatherElement exceed data size"};- }- out[i] = data[outer_sum + max_inner_sum * indices[i] + inner_sum];+ const U idx = helpers::handle_negative_indices<U>(indices, i, axis_dim_size);+ out[i] = data[outer_sum + max_inner_sum * idx + inner_sum];i++;}}diff --git a/src/frontends/onnx/tests/__init__.py b/src/frontends/onnx/tests/__init__.pyindex 5c80394f32a..7d7ae09d177 100644--- a/src/frontends/onnx/tests/__init__.py+++ b/src/frontends/onnx/tests/__init__.py@@ -39,8 +39,6 @@ xfail_issue_33606 = xfail_test(reason="RuntimeError: OV does not support the fol"Det")xfail_issue_33651 = xfail_test(reason="RuntimeError: OV does not support the following ONNX operations: ""TfIdfVectorizer")-xfail_issue_33581 = xfail_test(reason="RuntimeError: OV does not support the following ONNX operations: "- "GatherElements")xfail_issue_90649 = xfail_test(reason="RuntimeError: OV does not support the following ONNX operations:""DFT, LayerNormalization, ""MelWeightMatrix, SequenceMap, STFT")diff --git a/src/frontends/onnx/tests/tests_python/test_backend.py b/src/frontends/onnx/tests/tests_python/test_backend.pyindex ccb9d59ba9f..8ae9b14950b 100644--- a/src/frontends/onnx/tests/tests_python/test_backend.py+++ b/src/frontends/onnx/tests/tests_python/test_backend.py@@ -10,7 +10,6 @@ from tests import (BACKEND_NAME,skip_rng_tests,xfail_issue_33488,- xfail_issue_33581,xfail_issue_33596,xfail_issue_33606,xfail_issue_33651,@@ -226,10 +225,6 @@ tests_expected_to_fail = [xfail_issue_38706,"OnnxBackendNodeModelTest.test_split_zero_size_splits_cpu",),- (- xfail_issue_33581,- "OnnxBackendNodeModelTest.test_gather_elements_negative_indices_cpu",- ),(xfail_issue_38713,"OnnxBackendNodeModelTest.test_momentum_cpu",diff --git a/src/plugins/intel_cpu/src/nodes/gather_elements.cpp b/src/plugins/intel_cpu/src/nodes/gather_elements.cppindex 61768c95445..ed1da29f777 100644--- a/src/plugins/intel_cpu/src/nodes/gather_elements.cpp+++ b/src/plugins/intel_cpu/src/nodes/gather_elements.cpp@@ -77,6 +77,7 @@ void GatherElements::prepareParams() {strideAxDst_ *= dstDims[i];}dstAxDim_ = dstDims[axis_];+ dataAxDim_ = dataDims[axis_];if (axis_ > 0) {strideAx1Diff_ = 1;for (size_t i = dataDims.size() - 1; i >= axis_; i--) {@@ -114,6 +115,14 @@ void GatherElements::initSupportedPrimitiveDescriptors() {void GatherElements::executeDynamicImpl(const dnnl::stream& strm) {execute(strm);}+namespace helpers {+static int HandleNegativeIndices(const int* indices, int idx, int axisDimSize) {+ const int index = indices[idx];+ OPENVINO_ASSERT(index < axisDimSize && index >= -axisDimSize, "indices values of GatherElement exceed data size");+ const int fixedIdx = index < 0 ? axisDimSize + index : index;+ return fixedIdx;+}+} // namespace helperstemplate <typename dataType>void GatherElements::directExecution() {@@ -143,7 +152,8 @@ void GatherElements::directExecution() {dstShift0 += strideAx1Diff_;}}- dstData[o] = srcData[o + dstShift0 + (indices[o] - dstAxIdx) * strideAxDst_];+ const int idx = helpers::HandleNegativeIndices(indices, o, dataAxDim_);+ dstData[o] = srcData[o + dstShift0 + (idx - dstAxIdx) * strideAxDst_];}};diff --git a/src/plugins/intel_cpu/src/nodes/gather_elements.h b/src/plugins/intel_cpu/src/nodes/gather_elements.hindex c2c3081ec36..06211973eb8 100644--- a/src/plugins/intel_cpu/src/nodes/gather_elements.h+++ b/src/plugins/intel_cpu/src/nodes/gather_elements.h@@ -40,6 +40,7 @@ private:size_t dataTypeSize_ = 0;int strideAxDst_ = 0;int dstAxDim_ = 0;+ int dataAxDim_ = 0;int strideAx1Diff_ = 0;template <typename dataType>diff --git a/src/plugins/template/tests/functional/op_reference/gather_elements.cpp b/src/plugins/template/tests/functional/op_reference/gather_elements.cppindex 27f4780f232..9aa17e3d04d 100644--- a/src/plugins/template/tests/functional/op_reference/gather_elements.cpp+++ b/src/plugins/template/tests/functional/op_reference/gather_elements.cpp@@ -96,16 +96,32 @@ std::vector<GatherElementsParams> generateParams() {0,reference_tests::Tensor(IN_ET, {7}, std::vector<T>{2, 3, 1, 3, 1, 1, 3}),"evaluate_1D_gather_elements_3_indices_int32"),+ GatherElementsParams(+ reference_tests::Tensor(IN_ET, {3}, std::vector<T>{1, 2, 3}),+ reference_tests::Tensor(element::i32, {7}, std::vector<int32_t>{-2, -1, -3, -1, -3, -3, -1}),+ 0,+ reference_tests::Tensor(IN_ET, {7}, std::vector<T>{2, 3, 1, 3, 1, 1, 3}),+ "evaluate_1D_gather_elements_3_negative_indices_int32"),GatherElementsParams(reference_tests::Tensor(IN_ET, {2, 2}, std::vector<T>{1, 2, 3, 4}),reference_tests::Tensor(element::i32, {2, 2}, std::vector<int32_t>{0, 1, 0, 0}),0,reference_tests::Tensor(IN_ET, {2, 2}, std::vector<T>{1, 4, 1, 2}),"evaluate_2D_gather_elements_2x2_indices_int32_axis_0"),+ GatherElementsParams(reference_tests::Tensor(IN_ET, {2, 2}, std::vector<T>{1, 2, 3, 4}),+ reference_tests::Tensor(element::i32, {2, 2}, std::vector<int32_t>{-2, -1, -2, -2}),+ 0,+ reference_tests::Tensor(IN_ET, {2, 2}, std::vector<T>{1, 4, 1, 2}),+ "evaluate_2D_gather_elements_2x2_negative_indices_int32_axis_0"),GatherElementsParams(reference_tests::Tensor(IN_ET, {2, 2}, std::vector<T>{1, 2, 3, 4}),reference_tests::Tensor(element::i32, {2, 2}, std::vector<int32_t>{0, 1, 0, 0}),1,reference_tests::Tensor(IN_ET, {2, 2}, std::vector<T>{1, 2, 3, 3}),"evaluate_2D_gather_elements_2x2_indices_int32_axis_1"),+ GatherElementsParams(reference_tests::Tensor(IN_ET, {2, 2}, std::vector<T>{1, 2, 3, 4}),+ reference_tests::Tensor(element::i32, {2, 2}, std::vector<int32_t>{-2, -1, -2, -2}),+ 1,+ reference_tests::Tensor(IN_ET, {2, 2}, std::vector<T>{1, 2, 3, 3}),+ "evaluate_2D_gather_elements_2x2_negative_indices_int32_axis_1"),GatherElementsParams(reference_tests::Tensor(IN_ET, {2, 2}, std::vector<T>{1, 2, 3, 4}),reference_tests::Tensor(element::i32, {2, 2}, std::vector<int32_t>{0, 1, 0, 0}),-1,@@ -122,6 +138,14 @@ std::vector<GatherElementsParams> generateParams() {-1,reference_tests::Tensor(IN_ET, {3, 2, 2}, std::vector<T>{2, 1, 3, 4, 6, 6, 8, 7, 9, 9, 12, 12}),"evaluate_3D_gather_elements_3x2x2_indices_int32"),+ GatherElementsParams(+ reference_tests::Tensor(IN_ET, {3, 2, 2}, std::vector<T>{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}),+ reference_tests::Tensor(element::i32,+ {3, 2, 2},+ std::vector<int32_t>{-1, -2, -2, -1, -1, -1, -1, -2, 0, 0, 1, 1}),+ -1,+ reference_tests::Tensor(IN_ET, {3, 2, 2}, std::vector<T>{2, 1, 3, 4, 6, 6, 8, 7, 9, 9, 12, 12}),+ "evaluate_3D_gather_elements_3x2x2_negative_indices_int32"),GatherElementsParams(reference_tests::Tensor(IN_ET, {3, 2, 2, 2}, std::vector<T>{1, 2, 3, 4, 5, 6, 7, 8,9, 10, 11, 12, 13, 14, 15, 16,diff --git a/src/tests/functional/shared_test_classes/src/single_op/gather_elements.cpp b/src/tests/functional/shared_test_classes/src/single_op/gather_elements.cppindex 2171874ecb5..f3cdc4e31d9 100644--- a/src/tests/functional/shared_test_classes/src/single_op/gather_elements.cpp+++ b/src/tests/functional/shared_test_classes/src/single_op/gather_elements.cpp@@ -53,8 +53,13 @@ void GatherElementsLayerTest::SetUp() {auto axis_dim = targetStaticShapes[0][0][axis < 0 ? axis + targetStaticShapes[0][0].size() : axis];ov::test::utils::InputGenerateData in_data;- in_data.start_from = 0;- in_data.range = axis_dim - 1;+ if (targetDevice == "CPU") {+ in_data.start_from = -static_cast<double>(axis_dim);+ in_data.range = (2 * axis_dim) - 1;+ } else {+ in_data.start_from = 0;+ in_data.range = axis_dim - 1;+ }auto indices_node_tensor = ov::test::utils::create_and_fill_tensor(indices_type, indices_shape, in_data);auto indices_node = std::make_shared<ov::op::v0::Constant>(indices_node_tensor);