Skip to content

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

[GPU] Fix wrong index order in Gather (#30054)

David Namadae8b36738e
diff --git a/src/plugins/intel_gpu/src/graph/gather.cpp b/src/plugins/intel_gpu/src/graph/gather.cpp
index 770c47f96f8..9d947967a46 100644
--- a/src/plugins/intel_gpu/src/graph/gather.cpp
+++ b/src/plugins/intel_gpu/src/graph/gather.cpp
@@ -48,6 +48,7 @@ layout gather_inst::calc_output_layout(gather_node const& node, kernel_impl_para
switch (input_layout.format) {
case format::bfyx:
case format::bfzyx:
+ case format::bfwzyx:
case format::b_fs_zyx_fsv16:
case format::b_fs_zyx_fsv32:
output_format = format::get_default_format(dims_converted.size());
diff --git a/src/plugins/intel_gpu/src/graph/impls/ocl/gather.cpp b/src/plugins/intel_gpu/src/graph/impls/ocl/gather.cpp
index 02666fc23e9..167e289ccc9 100644
--- a/src/plugins/intel_gpu/src/graph/impls/ocl/gather.cpp
+++ b/src/plugins/intel_gpu/src/graph/impls/ocl/gather.cpp
@@ -4,6 +4,7 @@
#include "primitive_base.hpp"
+#include "gather.hpp"
#include "gather_inst.h"
#include "gather/gather_kernel_selector.h"
#include "gather/gather_kernel_ref.h"
@@ -177,6 +178,11 @@ public:
}
};
+std::unique_ptr<primitive_impl> GatherImplementationManager::create_impl(const program_node& node, const kernel_impl_params& params) const {
+ assert(node.is_type<gather>());
+ return typed_primitive_impl_ocl<gather>::create<gather_impl>(static_cast<const gather_node&>(node), params);
+}
+
namespace detail {
attach_gather_impl::attach_gather_impl() {
diff --git a/src/plugins/intel_gpu/src/graph/impls/ocl/gather.hpp b/src/plugins/intel_gpu/src/graph/impls/ocl/gather.hpp
new file mode 100644
index 00000000000..0e850dd0f21
--- /dev/null
+++ b/src/plugins/intel_gpu/src/graph/impls/ocl/gather.hpp
@@ -0,0 +1,34 @@
+// Copyright (C) 2024 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "registry/implementation_manager.hpp"
+#include "program_node.h"
+
+#include <memory>
+
+namespace cldnn {
+
+namespace ocl {
+
+struct GatherImplementationManager : public ImplementationManager {
+ OV_GPU_PRIMITIVE_IMPL("ocl::gather")
+ GatherImplementationManager(shape_types shape_type, ValidateFunc vf = nullptr) : ImplementationManager(impl_types::ocl, shape_type, vf) {}
+ std::unique_ptr<primitive_impl> create_impl(const program_node& node, const kernel_impl_params& params) const override;
+ in_out_fmts_t query_formats(const program_node& node) const override {
+ std::vector<format::type> in_fmts(node.get_dependencies().size(), format::any);
+ std::vector<format::type> out_fmts(node.get_outputs_count(), format::any);
+
+ for (size_t i = 0; i < node.get_dependencies().size(); i++) {
+ size_t in_rank = node.get_input_layout(i).get_rank();
+ in_fmts[i] = format::get_default_format(in_rank);
+ }
+ size_t out_rank = node.get_output_layout().get_rank();
+ out_fmts[0] = format::get_default_format(out_rank);
+
+ return {in_fmts, out_fmts};
+ }
+};
+
+} // namespace ocl
+} // namespace cldnn
diff --git a/src/plugins/intel_gpu/src/graph/impls/ocl/register.cpp b/src/plugins/intel_gpu/src/graph/impls/ocl/register.cpp
index 821afd4165d..de74ea6ae5d 100644
--- a/src/plugins/intel_gpu/src/graph/impls/ocl/register.cpp
+++ b/src/plugins/intel_gpu/src/graph/impls/ocl/register.cpp
@@ -31,7 +31,6 @@ void register_implementations() {
REGISTER_OCL(experimental_detectron_topk_rois);
REGISTER_OCL(eltwise);
REGISTER_OCL(fully_connected);
- REGISTER_OCL(gather);
REGISTER_OCL(gather_elements);
REGISTER_OCL(gemm);
REGISTER_OCL(generate_proposals);
diff --git a/src/plugins/intel_gpu/src/graph/impls/ocl/register.hpp b/src/plugins/intel_gpu/src/graph/impls/ocl/register.hpp
index 6b341528001..a089dc7c065 100644
--- a/src/plugins/intel_gpu/src/graph/impls/ocl/register.hpp
+++ b/src/plugins/intel_gpu/src/graph/impls/ocl/register.hpp
@@ -26,7 +26,6 @@
#include "intel_gpu/primitives/experimental_detectron_topk_rois.hpp"
#include "intel_gpu/primitives/eye.hpp"
#include "intel_gpu/primitives/fully_connected.hpp"
-#include "intel_gpu/primitives/gather.hpp"
#include "intel_gpu/primitives/gather_elements.hpp"
#include "intel_gpu/primitives/gather_tree.hpp"
#include "intel_gpu/primitives/gemm.hpp"
diff --git a/src/plugins/intel_gpu/src/graph/registry/gather_impls.cpp b/src/plugins/intel_gpu/src/graph/registry/gather_impls.cpp
index c432b878549..50fb4077365 100644
--- a/src/plugins/intel_gpu/src/graph/registry/gather_impls.cpp
+++ b/src/plugins/intel_gpu/src/graph/registry/gather_impls.cpp
@@ -7,14 +7,18 @@
#include "intel_gpu/primitives/gather.hpp"
#include "primitive_inst.h"
+#if OV_GPU_WITH_OCL
+ #include "impls/ocl/gather.hpp"
+#endif
+
namespace ov::intel_gpu {
using namespace cldnn;
const std::vector<std::shared_ptr<cldnn::ImplementationManager>>& Registry<gather>::get_implementations() {
static const std::vector<std::shared_ptr<ImplementationManager>> impls = {
- OV_GPU_GET_INSTANCE_OCL(gather, shape_types::static_shape, not_in_shape_flow())
- OV_GPU_GET_INSTANCE_OCL(gather, shape_types::dynamic_shape, not_in_shape_flow())
+ OV_GPU_CREATE_INSTANCE_OCL(ocl::GatherImplementationManager, shape_types::static_shape, not_in_shape_flow())
+ OV_GPU_CREATE_INSTANCE_OCL(ocl::GatherImplementationManager, shape_types::dynamic_shape, not_in_shape_flow())
OV_GPU_GET_INSTANCE_CPU(gather, shape_types::static_shape, in_shape_flow())
OV_GPU_GET_INSTANCE_CPU(gather, shape_types::dynamic_shape, in_shape_flow())
};
diff --git a/src/plugins/intel_gpu/src/kernel_selector/kernels/gather/gather_kernel_ref.cpp b/src/plugins/intel_gpu/src/kernel_selector/kernels/gather/gather_kernel_ref.cpp
index 09b8af7b01c..3a4ffeb0a43 100644
--- a/src/plugins/intel_gpu/src/kernel_selector/kernels/gather/gather_kernel_ref.cpp
+++ b/src/plugins/intel_gpu/src/kernel_selector/kernels/gather/gather_kernel_ref.cpp
@@ -155,19 +155,6 @@ static inline std::vector<std::string> GetOrder(size_t size) {
return idx_order;
}
-static inline std::vector<std::string> GetFinalIndexOrder(size_t size) {
- std::vector<std::string> idx_order;
-
- OPENVINO_ASSERT(size > 4, "[GPU] Only support 5 or 6 dimensions");
-
- if (size == 5) {
- idx_order = {"b", "f", "0", "z", "0"};
- } else if (size == 6) {
- idx_order = {"b", "f", "0", "w", "z", "0"};
- }
- return idx_order;
-}
-
static std::string GetDictionaryIndexOrder(const gather_params& params, size_t axis) {
auto idx_order = GetOrder(params.outputs[0].GetDims().size());
auto input_axis_index_macro = "INPUT_AXIS_INDEX";
@@ -196,24 +183,30 @@ static std::string GetDictionaryIndexOrder(const gather_params& params, size_t a
static std::string GetIndicesIdxOrder(const gather_params& params, size_t axis, int64_t batch_dim) {
std::vector<std::string> idx_order;
- if ((axis == (size_t)batch_dim) && (axis > 1) && (params.inputs[1].GetDims().size() > 4)) {
- idx_order = GetFinalIndexOrder(params.outputs[0].GetDims().size());
- } else {
- idx_order = GetOrder(params.outputs[0].GetDims().size());
- auto zero_val = "0";
+ const size_t output_rank = params.outputs[0].GetDims().size();
+ const size_t indices_rank = params.inputs[1].GetDims().size();
+ const size_t indices_dims_num = GetNonEmptyDimsNumber(params.inputs[1]);
- size_t indices_dims_num = GetNonEmptyDimsNumber(params.inputs[1]);
+ idx_order = GetOrder(output_rank);
+ const auto zero_val = "0";
- // Shift indices of Gather indices input related to output dims
- for (size_t i = batch_dim; i < indices_dims_num; i++)
- idx_order[i] = idx_order[axis + i - batch_dim];
-
- for (size_t i = indices_dims_num; i < idx_order.size(); i++)
+ // Shift indices of Gather indices input related to output dims
+ for (size_t i = static_cast<size_t>(batch_dim); i < indices_dims_num; i++) {
+ size_t output_idx = axis + i - static_cast<size_t>(batch_dim);
+ if (output_idx < idx_order.size()) {
+ idx_order[i] = idx_order[output_idx];
+ } else {
idx_order[i] = zero_val;
+ }
+ }
- // Fix size to inputs[1] dims size
- for (size_t i = 0; i < params.outputs[0].GetDims().size() - params.inputs[1].GetDims().size(); i++)
- idx_order.pop_back();
+ for (size_t i = indices_dims_num; i < idx_order.size(); ++i) {
+ idx_order[i] = zero_val;
+ }
+
+ // Fix size to inputs[1] dims size
+ while (idx_order.size() > indices_rank) {
+ idx_order.pop_back();
}
return GetOrderString(idx_order);
diff --git a/src/plugins/intel_gpu/tests/unit/test_cases/gather_gpu_test.cpp b/src/plugins/intel_gpu/tests/unit/test_cases/gather_gpu_test.cpp
index 0eade851b2a..74962e4e4b5 100644
--- a/src/plugins/intel_gpu/tests/unit/test_cases/gather_gpu_test.cpp
+++ b/src/plugins/intel_gpu/tests/unit/test_cases/gather_gpu_test.cpp
@@ -1205,6 +1205,39 @@ TEST(gather_gpu_fp16, d22_axisF) {
}
}
+TEST(gather_gpu_fp16, prevent_reorder_to_indices_node) {
+ auto& engine = get_test_engine();
+
+ auto data_layout = layout{ ov::PartialShape{ 4, 1, 6, 2500, 2 }, data_types::f16, format::bfzyx };
+ auto output_layout = layout{ ov::PartialShape{ 4, 1, 6, 2500}, data_types::f16, format::bfyx };
+ auto indices_layout = layout{ ov::PartialShape{ 4, 1 }, data_types::i32, format::bfyx };
+
+ int64_t input_rank = static_cast<int64_t>(data_layout.get_rank());
+ int64_t batch_dims = static_cast<int64_t>(2);
+ int64_t axis = static_cast<int64_t>(4);
+
+ topology topology(
+ input_layout("data", data_layout),
+ input_layout("indices", indices_layout),
+ gather("gather", input_info("data"), input_info("indices"), axis, input_rank, output_layout.get_shape(), batch_dims),
+ reorder("result", input_info("gather"), output_layout)
+ );
+
+ ExecutionConfig config = get_test_default_config(engine);
+ config.set_property(ov::intel_gpu::optimize_data(true));
+
+ auto program = program::build_program(engine, topology, config);
+
+ ASSERT_NE(program, nullptr);
+
+ for (auto dep : program->get_node("gather").get_dependencies()) {
+ ASSERT_NE(dep.first->is_type<reorder>(), true);
+ }
+
+ auto actual_shape = program->get_node("result").get_output_layout().get_partial_shape();
+ ASSERT_EQ(output_layout.get_partial_shape(), actual_shape);
+}
+
TEST(gather_gpu_fp32, d14_axisB) {
// Dictionary : 2x2x1x1
// Indexes : 1x4x1x1