Skip to content

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

[CPU][REF][GatherND]: Support negative indices. (#30764)

Piotr Kowalczyk67db278a4f30
diff --git a/docs/articles_en/documentation/openvino-ir-format/operation-sets/operation-specs/movement/gather-nd-8.rst b/docs/articles_en/documentation/openvino-ir-format/operation-sets/operation-specs/movement/gather-nd-8.rst
index 4fe61dd0d9d..a87797d3f7b 100644
--- a/docs/articles_en/documentation/openvino-ir-format/operation-sets/operation-specs/movement/gather-nd-8.rst
+++ b/docs/articles_en/documentation/openvino-ir-format/operation-sets/operation-specs/movement/gather-nd-8.rst
@@ -47,9 +47,11 @@ if ``indices.shape[-1] == data.rank - batch_dims``, else
* **1**: ``data`` tensor of type *T*. A tensor of a rank not less than 1. **Required.**
* **2**: ``indices`` tensor of type *T_IND*. A tensor of a rank not less than 1.
- It requires all indices from this tensor to be in the range ``[0, s-1]`` where ``s`` is the corresponding dimension to
+ It requires all indices from this tensor to be in the range ``[-s, s-1]`` where ``s`` is the corresponding dimension to
which this index is applied. **Required.**
+.. note::
+ Behavior before 2025.3 OpenVINO release: Negative indices were not supported.
**Outputs**:
diff --git a/src/plugins/intel_cpu/src/nodes/gather_nd.cpp b/src/plugins/intel_cpu/src/nodes/gather_nd.cpp
index f45f086e2a0..22faa1fe5a0 100644
--- a/src/plugins/intel_cpu/src/nodes/gather_nd.cpp
+++ b/src/plugins/intel_cpu/src/nodes/gather_nd.cpp
@@ -149,7 +149,9 @@ GatherND::GatherNDExecutor::GatherNDExecutor(const GatherNDAttributes& attrs)
static_cast<size_t>(1),
std::multiplies<>())),
idxBatchStride(cycles * sliceRank),
- dstBatchStride(cycles * dataLength) {
+ dstBatchStride(cycles * dataLength),
+ batchDims(attrs.batchDims),
+ srcDims(attrs.srcDims) {
srcShifts.resize(attrs.sliceRank, 0);
for (size_t i = 0; i < attrs.sliceRank; i++) {
srcShifts[i] = attrs.srcStrides[i + attrs.batchDims] * (dataLength > 1 ? dataSize : 1);
@@ -218,7 +220,8 @@ void GatherND::GatherNDExecutor::gatherBlocks(const MemoryPtr& srcMemPtr,
for (size_t j = cStart; j < cycles; j++) {
size_t dataIdx = 0LU;
for (size_t i = 0; i < sliceRank; i++) {
- dataIdx += srcShifts[i] * shiftedIndices[i];
+ const int32_t index = HandleNegativeIndices(shiftedIndices, i);
+ dataIdx += srcShifts[i] * index;
}
cpu_memcpy(shiftedDstData, &(shiftedSrcData[dataIdx]), dataLength);
shiftedDstData += dataLength;
@@ -260,7 +263,8 @@ void GatherND::GatherNDExecutor::gatherElementwise(const MemoryPtr& srcMemPtr,
for (size_t j = cStart; j < cycles; j++) {
size_t dataIdx = 0LU;
for (size_t i = 0LU; i < sliceRank; i++) {
- dataIdx += srcShifts[i] * shiftedIndices[i];
+ const int32_t index = HandleNegativeIndices(shiftedIndices, i);
+ dataIdx += srcShifts[i] * index;
}
shiftedDstData[0] = shiftedSrcData[dataIdx];
shiftedDstData++;
@@ -275,6 +279,14 @@ void GatherND::GatherNDExecutor::gatherElementwise(const MemoryPtr& srcMemPtr,
});
}
+int32_t GatherND::GatherNDExecutor::HandleNegativeIndices(const int32_t* indices, size_t idx) const {
+ int32_t index = indices[idx];
+ if (index < 0) {
+ index += srcDims[idx + batchDims];
+ }
+ return index;
+}
+
void GatherND::executeDynamicImpl(const dnnl::stream& strm) {
execute(strm);
}
diff --git a/src/plugins/intel_cpu/src/nodes/gather_nd.h b/src/plugins/intel_cpu/src/nodes/gather_nd.h
index db32d873aeb..aa4a8d93fa8 100644
--- a/src/plugins/intel_cpu/src/nodes/gather_nd.h
+++ b/src/plugins/intel_cpu/src/nodes/gather_nd.h
@@ -54,6 +54,7 @@ private:
template <typename dataType>
void gatherElementwise(const MemoryPtr& srcMemPtr, const MemoryPtr& idxMemPtr, const MemoryPtr& dstMemPtr);
void gatherBlocks(const MemoryPtr& srcMemPtr, const MemoryPtr& idxMemPtr, const MemoryPtr& dstMemPtr);
+ int32_t HandleNegativeIndices(const int32_t* indices, size_t idx) const;
size_t batchSize = 1lu;
size_t dataSize = 1lu;
@@ -67,6 +68,9 @@ private:
size_t dstBatchStride = 1lu;
VectorDims srcShifts;
+ size_t batchDims = 0lu;
+ VectorDims srcDims;
+
struct GatherNDContext {
GatherNDExecutor* executor;
const MemoryPtr srcMemPtr;
diff --git a/src/plugins/template/tests/functional/op_reference/gather_nd.cpp b/src/plugins/template/tests/functional/op_reference/gather_nd.cpp
index 119b77e3291..8e92b9ded6d 100644
--- a/src/plugins/template/tests/functional/op_reference/gather_nd.cpp
+++ b/src/plugins/template/tests/functional/op_reference/gather_nd.cpp
@@ -89,6 +89,11 @@ std::vector<GatherNDParams> generateParams() {
0,
reference_tests::Tensor(IN_ET, {}, std::vector<T>{15}),
"gather_nd_single_indices"),
+ GatherNDParams(reference_tests::Tensor(IN_ET, {3, 3}, std::vector<T>{10, 11, 12, 13, 14, 15, 16, 17, 18}),
+ reference_tests::Tensor(element::i32, {2}, std::vector<int32_t>{-2, -1}),
+ 0,
+ reference_tests::Tensor(IN_ET, {}, std::vector<T>{15}),
+ "gather_nd_single_negative_indices"),
GatherNDParams(reference_tests::Tensor(IN_ET, {2, 2}, std::vector<T>{10, 11, 12, 13}),
reference_tests::Tensor(element::i32, {2, 2}, std::vector<int32_t>{0, 0, 1, 1}),
0,
@@ -300,6 +305,11 @@ std::vector<GatherNDParams> generateParams_v8() {
0,
reference_tests::Tensor(IN_ET, {2, 1, 2}, std::vector<T>{12, 13, 10, 11}),
"gather_nd_8_batch_1d_from_2d"),
+ GatherNDParams(reference_tests::Tensor(IN_ET, {2, 2}, std::vector<T>{10, 11, 12, 13}),
+ reference_tests::Tensor(element::i32, {2, 1, 1}, std::vector<int32_t>{-1, -2}),
+ 0,
+ reference_tests::Tensor(IN_ET, {2, 1, 2}, std::vector<T>{12, 13, 10, 11}),
+ "gather_nd_8_batch_1d_from_2d_negative"),
GatherNDParams(
reference_tests::Tensor(IN_ET, {2, 2, 2}, std::vector<T>{10, 11, 12, 13, 20, 21, 22, 23}),
reference_tests::Tensor(element::i32, {2, 2, 3}, std::vector<int32_t>{0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0}),
diff --git a/src/tests/functional/shared_test_classes/src/single_op/gather_nd.cpp b/src/tests/functional/shared_test_classes/src/single_op/gather_nd.cpp
index f25d8880aa8..156210b53cc 100644
--- a/src/tests/functional/shared_test_classes/src/single_op/gather_nd.cpp
+++ b/src/tests/functional/shared_test_classes/src/single_op/gather_nd.cpp
@@ -68,7 +68,7 @@ void GatherND8LayerTest::SetUp() {
auto param = std::make_shared<ov::op::v0::Parameter>(model_type, inputDynamicShapes.front());
- auto gather = ov::test::utils::make_gather_nd8(param, indices_shape, indices_type, batch_dims);
+ auto gather = ov::test::utils::make_gather_nd8(param, indices_shape, indices_type, batch_dims, targetDevice);
auto result = std::make_shared<ov::op::v0::Result>(gather);
function = std::make_shared<ov::Model>(result, ov::ParameterVector{param}, "gatherND");
diff --git a/src/tests/test_utils/common_test_utils/include/common_test_utils/node_builders/gather_nd.hpp b/src/tests/test_utils/common_test_utils/include/common_test_utils/node_builders/gather_nd.hpp
index f8d43deae08..974991c8d2a 100644
--- a/src/tests/test_utils/common_test_utils/include/common_test_utils/node_builders/gather_nd.hpp
+++ b/src/tests/test_utils/common_test_utils/include/common_test_utils/node_builders/gather_nd.hpp
@@ -18,7 +18,8 @@ std::shared_ptr<ov::Node> make_gather_nd(const ov::Output<Node>& data_node,
std::shared_ptr<ov::Node> make_gather_nd8(const ov::Output<Node>& data_node,
const ov::Shape& indices_shape,
const ov::element::Type& indices_type,
- const std::size_t batch_dims);
+ const std::size_t batch_dims,
+ const std::string& targetDevice);
} // namespace utils
} // namespace test
} // namespace ov
\ No newline at end of file
diff --git a/src/tests/test_utils/common_test_utils/src/node_builders/gather_nd.cpp b/src/tests/test_utils/common_test_utils/src/node_builders/gather_nd.cpp
index 9ea022a94bd..2432069fdf6 100644
--- a/src/tests/test_utils/common_test_utils/src/node_builders/gather_nd.cpp
+++ b/src/tests/test_utils/common_test_utils/src/node_builders/gather_nd.cpp
@@ -46,7 +46,8 @@ std::shared_ptr<ov::Node> make_gather_nd(const ov::Output<Node>& data_node,
std::shared_ptr<ov::Node> make_gather_nd8(const ov::Output<Node>& data_node,
const ov::Shape& indices_shape,
const ov::element::Type& indices_type,
- const std::size_t batch_dims) {
+ const std::size_t batch_dims,
+ const std::string& targetDevice) {
const auto indices = [&] {
const auto& data_shape = data_node.get_shape();
const auto indices_count =
@@ -56,11 +57,22 @@ std::shared_ptr<ov::Node> make_gather_nd8(const ov::Output<Node>& data_node,
const auto max_dim = *std::max_element(begin(data_shape), end(data_shape));
std::vector<int> indices_values(indices_count * slice_rank);
- ov::test::utils::fill_data_random(indices_values.data(), indices_count * slice_rank, max_dim, 0);
+ double startFrom = 0.0;
+ auto range = max_dim;
+
+ if (targetDevice == "CPU") {
+ // Only CPU plugin supports negative indices.
+ startFrom = -static_cast<double>(max_dim);
+ range = 2 * max_dim;
+ }
+
+ ov::test::utils::fill_data_random<int>(indices_values.data(), indices_count * slice_rank, range, startFrom);
+
auto indices_data = indices_values.data();
for (int i = 0; i < indices_count; i++) {
for (int dim = 0; dim < slice_rank; dim++) {
- indices_data[0] = indices_data[0] % data_shape[dim + batch_dims];
+ const int modulus = static_cast<int>(data_shape[dim + batch_dims]);
+ indices_data[0] = indices_data[0] % modulus;
indices_data++;
}
}