Skip to content

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

[TEMPLATE] Fix GatherND evaluate on TEMPLATE plugin (#35988)

Pawel Raasz80736f9aea5f
diff --git a/src/plugins/template/backend/ops/gather_nd.cpp b/src/plugins/template/backend/ops/gather_nd.cpp
index 30cf0558473..075ee02e940 100644
--- a/src/plugins/template/backend/ops/gather_nd.cpp
+++ b/src/plugins/template/backend/ops/gather_nd.cpp
@@ -5,7 +5,9 @@
#include "openvino/reference/gather_nd.hpp"
#include "evaluate_node.hpp"
+#include "gather_nd_shape_inference.hpp"
#include "openvino/core/type/element_type_traits.hpp"
+#include "openvino/core/validation_util.hpp"
#include "openvino/op/gather_nd.hpp"
template <ov::element::Type_t ET>
@@ -13,7 +15,9 @@ bool evaluate(const std::shared_ptr<ov::op::v5::GatherND>& op,
ov::TensorVector& outputs,
const ov::TensorVector& inputs) {
using T = typename ov::element_type_traits<ET>::value_type;
- if (op->get_input_element_type(1) == ov::element::i64) {
+ const auto output_shapes = ov::op::v5::shape_infer(op.get(), ov::util::get_tensors_partial_shapes(inputs));
+ outputs[0].set_shape(output_shapes[0].get_shape());
+ if (const auto& ind_type = op->get_input_element_type(1); ind_type == ov::element::i64) {
ov::reference::gather_nd<T, int64_t>(inputs[0].data<T>(),
inputs[1].data<int64_t>(),
outputs[0].data<T>(),
@@ -21,7 +25,7 @@ bool evaluate(const std::shared_ptr<ov::op::v5::GatherND>& op,
inputs[1].get_shape(),
outputs[0].get_shape(),
static_cast<int>(op->get_batch_dims()));
- } else if (op->get_input_element_type(1) == ov::element::i32) {
+ } else if (ind_type == ov::element::i32) {
ov::reference::gather_nd<T, int32_t>(inputs[0].data<T>(),
inputs[1].data<int32_t>(),
outputs[0].data<T>(),
@@ -30,7 +34,7 @@ bool evaluate(const std::shared_ptr<ov::op::v5::GatherND>& op,
outputs[0].get_shape(),
static_cast<int>(op->get_batch_dims()));
} else {
- OPENVINO_THROW("Unexpected indices type for GatherND operation");
+ OPENVINO_THROW("Unexpected indices type for GatherND operation: ", ind_type);
}
return true;
}
@@ -40,7 +44,9 @@ bool evaluate(const std::shared_ptr<ov::op::v8::GatherND>& op,
ov::TensorVector& outputs,
const ov::TensorVector& inputs) {
using T = typename ov::element_type_traits<ET>::value_type;
- if (op->get_input_element_type(1) == ov::element::i64) {
+ const auto output_shapes = ov::op::v8::shape_infer(op.get(), ov::util::get_tensors_partial_shapes(inputs));
+ outputs[0].set_shape(output_shapes[0].get_shape());
+ if (const auto& ind_type = op->get_input_element_type(1); ind_type == ov::element::i64) {
ov::reference::gather_nd<T, int64_t>(inputs[0].data<T>(),
inputs[1].data<int64_t>(),
outputs[0].data<T>(),
@@ -48,7 +54,7 @@ bool evaluate(const std::shared_ptr<ov::op::v8::GatherND>& op,
inputs[1].get_shape(),
outputs[0].get_shape(),
static_cast<int>(op->get_batch_dims()));
- } else if (op->get_input_element_type(1) == ov::element::i32) {
+ } else if (ind_type == ov::element::i32) {
ov::reference::gather_nd<T, int32_t>(inputs[0].data<T>(),
inputs[1].data<int32_t>(),
outputs[0].data<T>(),
@@ -57,7 +63,7 @@ bool evaluate(const std::shared_ptr<ov::op::v8::GatherND>& op,
outputs[0].get_shape(),
static_cast<int>(op->get_batch_dims()));
} else {
- OPENVINO_THROW("Unexpected indices type for GatherND operation");
+ OPENVINO_THROW("Unexpected indices type for GatherND operation: ", ind_type);
}
return true;
}
diff --git a/src/plugins/template/tests/functional/shared_tests_instances/single_layer_tests/gather_nd.cpp b/src/plugins/template/tests/functional/shared_tests_instances/single_layer_tests/gather_nd.cpp
new file mode 100644
index 00000000000..d7d45463a15
--- /dev/null
+++ b/src/plugins/template/tests/functional/shared_tests_instances/single_layer_tests/gather_nd.cpp
@@ -0,0 +1,55 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "openvino/op/gather_nd.hpp"
+
+#include <vector>
+
+#include "openvino/op/non_zero.hpp"
+#include "openvino/op/not_equal.hpp"
+#include "openvino/op/transpose.hpp"
+#include "shared_test_classes/base/ov_subgraph.hpp"
+
+namespace ov::test {
+
+class GatherNDLayerTemplateTest : virtual public SubgraphBaseTest {
+protected:
+ void SetUp() override {
+ targetDevice = utils::DEVICE_TEMPLATE;
+ }
+
+ template <class TGatherND>
+ static std::shared_ptr<Model> make_nonzero_gather_model(PartialShape data_shape, int batch_dims) {
+ const auto data = std::make_shared<op::v0::Parameter>(element::i32, data_shape);
+ // zero constant to compare against
+ const auto zero = op::v0::Constant::create(element::i32, Shape{1, 1}, {0});
+ const auto not_equal = std::make_shared<op::v1::NotEqual>(data, zero);
+ // NonZero: output shape [2, -1] i64 (data-dependent)
+ const auto non_zero = std::make_shared<op::v3::NonZero>(not_equal, element::i64);
+ // Transpose order [1, 0]: output shape [-1, 2] i64
+ const auto order = op::v0::Constant::create(element::i64, Shape{2}, {1, 0});
+ const auto transpose = std::make_shared<op::v1::Transpose>(non_zero, order);
+ const auto gather = std::make_shared<TGatherND>(data, transpose, batch_dims);
+ return std::make_shared<ov::Model>(gather->outputs(), ov::ParameterVector{data}, "gatherND");
+ }
+};
+
+TEST_F(GatherNDLayerTemplateTest, smoke_dynamic_v5_nonzero) {
+ const auto input_shapes =
+ std::vector<InputShape>{InputShape{ov::PartialShape({1, -1}), std::vector<ov::Shape>{ov::Shape{1, 101}}}};
+ init_input_shapes(input_shapes);
+ function = make_nonzero_gather_model<op::v5::GatherND>(inputDynamicShapes.at(0), 0);
+
+ run();
+}
+
+TEST_F(GatherNDLayerTemplateTest, smoke_dynamic_v8_nonzero) {
+ const auto input_shapes =
+ std::vector<InputShape>{InputShape{ov::PartialShape({1, -1}), std::vector<ov::Shape>{ov::Shape{1, 99}}}};
+ init_input_shapes(input_shapes);
+ function = make_nonzero_gather_model<op::v8::GatherND>(inputDynamicShapes.at(0), 0);
+
+ run();
+}
+} // namespace ov::test