Skip to content

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

[GPU] Add indices constant check for crop primitive selection in CreateGatherOpBase (#26361)

Wilson Seok39c7e24e9031
diff --git a/src/plugins/intel_gpu/src/plugin/ops/gather.cpp b/src/plugins/intel_gpu/src/plugin/ops/gather.cpp
index 5979a69627e..e8d9236a7e6 100644
--- a/src/plugins/intel_gpu/src/plugin/ops/gather.cpp
+++ b/src/plugins/intel_gpu/src/plugin/ops/gather.cpp
@@ -100,8 +100,12 @@ void CreateGatherOpBase(ProgramBuilder& p, const std::shared_ptr<T>& op, const i
const auto input_shape = op->get_input_partial_shape(0);
const auto input_rank = input_shape.rank().get_length();
const auto& indices = op->input_value(1);
+ const auto& indices_node = indices.get_node_shared_ptr();
+ auto indices_constant = std::dynamic_pointer_cast<ov::op::v0::Constant>(indices_node);
+ bool is_indices_constant = (indices_constant) ? true : false;
+
if (is_static && axis == 0 && input_rank > 1 && indices.get_partial_shape().rank().get_length() == 0 &&
- std::equal(input_shape.begin()+1, input_shape.end(), out_shape.begin()+1)) {
+ std::equal(input_shape.begin()+1, input_shape.end(), out_shape.begin()+1) && is_indices_constant) {
// Gather -> Crop
// this Gather simply divides an input tensor along Batch axis
auto get_crop_layer_name = [&](std::string name, size_t idx)->std::string {
@@ -109,8 +113,6 @@ void CreateGatherOpBase(ProgramBuilder& p, const std::shared_ptr<T>& op, const i
};
// Get indices info to calculate offset
- const auto& indices_node = indices.get_node_shared_ptr();
- auto indices_constant = std::dynamic_pointer_cast<ov::op::v0::Constant>(indices_node);
float result = 0.f;
OPENVINO_ASSERT(ov::op::util::get_single_value(indices_constant, result),
"Unsupported indices node in ", op->get_friendly_name(), " (", op->get_type_name(), ")");
diff --git a/src/plugins/intel_gpu/tests/functional/single_layer_tests/gather_indices_input.cpp b/src/plugins/intel_gpu/tests/functional/single_layer_tests/gather_indices_input.cpp
new file mode 100644
index 00000000000..7d3f66e6081
--- /dev/null
+++ b/src/plugins/intel_gpu/tests/functional/single_layer_tests/gather_indices_input.cpp
@@ -0,0 +1,108 @@
+// Copyright (C) 2024 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "common_test_utils/ov_tensor_utils.hpp"
+#include "common_test_utils/file_utils.hpp"
+#include "shared_test_classes/base/ov_subgraph.hpp"
+
+#include "openvino/op/parameter.hpp"
+#include "openvino/op/constant.hpp"
+#include "openvino/op/gather.hpp"
+
+namespace {
+
+using ov::test::InputShape;
+
+using GatherIndicesInputParams = std::tuple<
+ ov::Shape, // Input shapes
+ ov::Shape, // Indices shape
+ std::tuple<int, int>, // Gather axis and batch
+ ov::element::Type>; // input precision
+
+class GatherIndicesInputTest : public testing::WithParamInterface<GatherIndicesInputParams>,
+ virtual public ov::test::SubgraphBaseStaticTest {
+public:
+ static std::string getTestCaseName(testing::TestParamInfo<GatherIndicesInputParams> obj) {
+ ov::Shape input_shape;
+ ov::Shape indices_shape;
+ std::tuple<int, int> axis_batch_idx;
+ ov::element::Type input_precision;
+
+ std::tie(input_shape, indices_shape, axis_batch_idx, input_precision) = obj.param;
+
+ std::ostringstream result;
+ result << "IS=(";
+ result << ov::test::utils::vec2str(input_shape) << "_";
+ result << "axis=" << std::get<0>(axis_batch_idx) << "_";
+ result << "batch_idx=" << std::get<1>(axis_batch_idx) << "_";
+ result << "indices_shape=" << ov::test::utils::vec2str(indices_shape) << "_";
+ result << "input_precision=" << input_precision;
+ return result.str();
+ }
+
+protected:
+ ov::Shape input_shape, indices_shape;
+ std::tuple<int, int> axis_batch_idx;
+
+ void SetUp() override {
+ ov::element::Type input_precision;
+ targetDevice = ov::test::utils::DEVICE_GPU;
+ std::tie(input_shape, indices_shape, axis_batch_idx, input_precision) = GetParam();
+
+ int axis = std::get<0>(axis_batch_idx);
+ int batch_idx = std::get<1>(axis_batch_idx);
+
+ ov::test::utils::InputGenerateData in_data;
+ in_data.start_from = -10;
+ in_data.range = 10;
+ auto input_tensor = ov::test::utils::create_and_fill_tensor(input_precision, input_shape, in_data);
+ auto input_node = std::make_shared<ov::op::v0::Constant>(input_tensor);
+ auto indices_node = std::make_shared<ov::op::v0::Parameter>(ov::element::i64, indices_shape);
+ auto axis_node = ov::op::v0::Constant::create(ov::element::i64, indices_shape, {axis});
+
+ auto gather = std::make_shared<ov::op::v8::Gather>(input_node, indices_node, axis_node, batch_idx);
+
+ auto result = std::make_shared<ov::op::v0::Result>(gather);
+ function = std::make_shared<ov::Model>(result, ov::ParameterVector{indices_node}, "gather");
+ }
+
+void generate_inputs(const std::vector<ov::Shape>& target_shapes) override {
+ inputs.clear();
+ const auto& func_inputs = function->inputs();
+ auto& data_input = func_inputs[0];
+ ov::Tensor tensor;
+ ov::test::utils::InputGenerateData in_data;
+ in_data.start_from = 0;
+ in_data.range = input_shape[std::get<0>(axis_batch_idx)];
+ tensor = ov::test::utils::create_and_fill_tensor(ov::element::i64, indices_shape, in_data);
+ inputs.insert({data_input.get_node_shared_ptr(), tensor});
+}
+};
+
+TEST_P(GatherIndicesInputTest, Inference) {
+ run();
+}
+
+const std::vector<ov::element::Type> input_precisions = {ov::element::f16, ov::element::f32};
+
+const std::vector<ov::Shape> input_shapes = {
+ {{271, 300}},
+};
+
+const ov::Shape indices_shape = {ov::Shape{}};
+
+const std::tuple<int, int> axis_batch_idx = {0, 0};
+
+// Gather input is constant [271,300]
+// Gather indices is parameter [] (scalar)
+// Gather axis is constant [] (scalar)
+// This graph should not to be selected crop primitive in CreateGatherOpBase()
+INSTANTIATE_TEST_SUITE_P(Smoke_GatherIndicesInputTest,
+ GatherIndicesInputTest,
+ ::testing::Combine(::testing::ValuesIn(input_shapes),
+ ::testing::Values(indices_shape),
+ ::testing::Values(axis_batch_idx),
+ ::testing::ValuesIn(input_precisions)),
+ GatherIndicesInputTest::getTestCaseName);
+} // namespace