Skip to content

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

[GPU][GatherND]: Support negative indices. (#30861)

Piotr Kowalczykd8abef652150
diff --git a/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/gather_nd_ref.cl b/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/gather_nd_ref.cl
index 701eb6fd290..df04cd6bd85 100644
--- a/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/gather_nd_ref.cl
+++ b/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/gather_nd_ref.cl
@@ -52,6 +52,14 @@ KERNEL(gather_nd_ref)(
const uint idx_f = dim2 % F_NUM;
const uint idx_b = dim2 / F_NUM;
+ #if INPUT0_DIMS == 4
+ const int data_dim[INPUT0_DIMS] = {INPUT0_BATCH_NUM, INPUT0_FEATURE_NUM, INPUT0_SIZE_Y, INPUT0_SIZE_X};
+ #elif INPUT0_DIMS == 5
+ const int data_dim[INPUT0_DIMS] = {INPUT0_BATCH_NUM, INPUT0_FEATURE_NUM, INPUT0_SIZE_Z, INPUT0_SIZE_Y, INPUT0_SIZE_X};
+ #else
+ const int data_dim[INPUT0_DIMS] = {INPUT0_BATCH_NUM, INPUT0_FEATURE_NUM, INPUT0_SIZE_W, INPUT0_SIZE_Z, INPUT0_SIZE_Y, INPUT0_SIZE_X};
+ #endif
+
#if INPUT1_DIMS == 4
const uint idx_x = dim0;
const uint idx_y = dim1;
@@ -85,16 +93,9 @@ KERNEL(gather_nd_ref)(
#if IS_DYNAMIC
uint wi_slice = 1;
- #if INPUT0_DIMS == 4
- uint input_dims[4] = {INPUT0_BATCH_NUM, INPUT0_FEATURE_NUM, INPUT0_SIZE_Y, INPUT0_SIZE_X};
- #elif INPUT0_DIMS == 5
- uint input_dims[5] = {INPUT0_BATCH_NUM, INPUT0_FEATURE_NUM, INPUT0_SIZE_Z, INPUT0_SIZE_Y, INPUT0_SIZE_X};
- #else
- uint input_dims[6] = {INPUT0_BATCH_NUM, INPUT0_FEATURE_NUM, INPUT0_SIZE_W, INPUT0_SIZE_Z, INPUT0_SIZE_Y, INPUT0_SIZE_X};
- #endif
const uint indices_last_dim = idx_dim[INDICES_RANK - 1];
for (uint i = BATCH_DIMS + indices_last_dim; i < INPUT0_DIMS; i++)
- wi_slice *= input_dims[i];
+ wi_slice *= data_dim[i];
#else
const uint wi_slice = WI_SLICE_SIZE;
const uint indices_last_dim = INDICES_LAST_DIM;
@@ -113,7 +114,9 @@ KERNEL(gather_nd_ref)(
}
for (uint i = 0; i < indices_last_dim; i++) {
- indices_val[i + BATCH_DIMS] = indices[idx+i];
+ const int indices_val_read = indices[idx + i];
+ const int final_indices_val = indices_val_read < 0 ? indices_val_read + data_dim[i + BATCH_DIMS] : indices_val_read;
+ indices_val[i + BATCH_DIMS] = final_indices_val;
}
#if INPUT0_DIMS == 4
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 2432069fdf6..726a7012a52 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
@@ -57,14 +57,8 @@ 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);
- 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;
- }
+ const double startFrom = -static_cast<double>(max_dim);
+ const auto range = 2 * max_dim;
ov::test::utils::fill_data_random<int>(indices_values.data(), indices_count * slice_rank, range, startFrom);