OpenVINO™ is an open source toolkit for optimizing and deploying AI inference
C++ aicomputer-visiondeep-learningdeploy-aidiffusion-modelsgenerative-aigood-first-issueinferencellm-inferencenatural-language-processingnlpopenvinooptimize-aiperformance-boostrecommendation-systemspeech-recognitionstable-diffusiontransformersyolo
[GPU] Align reduce input format rank with its shape rank (#37747)
Sungeun Kime121292c6266
diff --git a/src/plugins/intel_gpu/src/graph/graph_optimizer/reorder_inputs.cpp b/src/plugins/intel_gpu/src/graph/graph_optimizer/reorder_inputs.cppindex d5fff5beb17..4ecafbbd4fb 100644--- a/src/plugins/intel_gpu/src/graph/graph_optimizer/reorder_inputs.cpp+++ b/src/plugins/intel_gpu/src/graph/graph_optimizer/reorder_inputs.cpp@@ -14,6 +14,7 @@#include "pooling_inst.h"#include "fully_connected_inst.h"#include "mvn_inst.h"+#include "reduce_inst.h"#ifdef ENABLE_ONEDNN_FOR_GPU#include "gemm_inst.h"@@ -802,6 +803,28 @@ void reorder_inputs::run(program& p, reorder_factory& rf) {}}};++ // Reduce input format is selected from the output rank, so it may not match the input shape rank.+ // oneDNN reduction rejects such a layout, thus realign the format to the input rank here.+ const auto reorder_input_reduce = [&p, &rf](typed_program_node<reduce>& reduce_node) {+ auto dep = reduce_node.get_dependency_with_port(0);+ const auto& input = dep.first;+ auto input_layout = input->get_output_layout();++ if (input_layout.is_dynamic())+ return;++ auto new_layout = input_layout;+ new_layout.format = format::adjust_to_rank(input_layout.format, input_layout.get_partial_shape().size());+ if (new_layout.format == input_layout.format)+ return;++ auto new_input = rf.get_reorder(input->id(), dep.second, input_layout, new_layout);+ if (new_input.first) {+ p.add_intermediate(new_input.first, reduce_node, 0, !new_input.second);+ reduce_node.recalc_output_layouts(false);+ }+ };#ifdef ENABLE_ONEDNN_FOR_GPUconst auto reorder_input_gemm = [&p, &rf](typed_program_node<gemm>& gemm_node) {if (gemm_node.get_preferred_impl_type() != impl_types::onednn || gemm_node.is_dynamic()@@ -837,14 +860,15 @@ void reorder_inputs::run(program& p, reorder_factory& rf) {#endif // ENABLE_ONEDNN_FOR_GPUfor (const auto& prim : p.get_processing_order()) {- program_helpers::do_for_types<detection_output, deconvolution, convolution, fully_connected, pooling, mvn>(+ program_helpers::do_for_types<detection_output, deconvolution, convolution, fully_connected, pooling, mvn, reduce>(*prim,reorder_input_detection_output,reorder_input_and_weights_deconvolution,reorder_convolution,reorder_input_fully_connected,reorder_input_pooling,- reorder_input_mvn);+ reorder_input_mvn,+ reorder_input_reduce);#ifdef ENABLE_ONEDNN_FOR_GPUprogram_helpers::do_for_types<gemm>(diff --git a/src/plugins/intel_gpu/tests/unit/test_cases/reduce_gpu_test.cpp b/src/plugins/intel_gpu/tests/unit/test_cases/reduce_gpu_test.cppindex 5193f7453a2..7d1a0f466ff 100644--- a/src/plugins/intel_gpu/tests/unit/test_cases/reduce_gpu_test.cpp+++ b/src/plugins/intel_gpu/tests/unit/test_cases/reduce_gpu_test.cpp@@ -9,6 +9,7 @@#include <intel_gpu/primitives/reduce.hpp>#include <intel_gpu/primitives/data.hpp>#include "reduce_inst.h"+#include "registry/implementation_manager.hpp"#include <cmath>#include <algorithm>@@ -2260,8 +2261,141 @@ INSTANTIATE_TEST_SUITE_P(onednn_reduce_gpu_byxf_f32_f32,TestParamType_general_reduce_gpu(17, 34, 1, 1, 16, 15, format::byxf, reduce_mode::l1, {1, 0}, "reduce_gpu_byxf", true, data_types::f32, false, data_types::f32),TestParamType_general_reduce_gpu(17, 3, 1, 1, 14, 11, format::byxf, reduce_mode::mean, {1}, "reduce_gpu_byxf", true, data_types::f32, false, data_types::f32)), general_reduce_gpu::PrintToStringParamName);++// Regression test: oneDNN derives its src/dst memory descriptor rank from format::dimension(),+// not from the layout's partial shape rank. If an input layout carries a higher-rank partial shape+// than its assigned format (e.g. a 5D shape squeezed into a 4D bfyx format by an upstream+// reshape/optimization), oneDNN would build a truncated descriptor. reorder_inputs therefore+// realigns the reduce input format to the input rank, so the node must never be compiled with a+// layout whose format rank disagrees with its shape rank.+static void test_reduce_input_format_rank_mismatch(int64_t reduce_axis) {+ auto& engine = get_test_engine();+ if (!engine.get_device_info().supports_immad)+ return;++ const int f_size = 6;+ const int x_size = 8;+ // Partial shape is 5D (b, f, z, y, x) but the format below is 4D (bfyx): intentional mismatch.+ ov::PartialShape input_pshape{1, f_size, 1, 1, x_size};+ layout input_lay(input_pshape, data_types::f16, format::bfyx);+ auto input_mem = engine.allocate_memory(input_lay);++ std::vector<ov::float16> input_data(f_size * x_size);+ for (size_t i = 0; i < input_data.size(); i++)+ input_data[i] = ov::float16(static_cast<float>(i) * 0.1f);+ set_values(input_mem, input_data);++ topology topology;+ topology.add(input_layout("input", input_mem->get_layout()));+ topology.add(reduce("reduce", input_info("input"), reduce_mode::mean, {reduce_axis}, false));++ ExecutionConfig config = get_test_default_config(engine);+ config.set_property(ov::intel_gpu::allow_new_shape_infer(true));+ config.set_property(ov::intel_gpu::optimize_data(true));++ network network(engine, topology, config);+ network.set_input_data("input", input_mem);++ ASSERT_NO_THROW(network.execute());++ auto inst = network.get_primitive("reduce");+ ASSERT_NE(inst->get_impl(), nullptr);+ const auto& in_lay = inst->get_input_layout(0);+ ASSERT_EQ(in_lay.format.dimension(), in_lay.get_partial_shape().size())+ << "reduce input format rank must be realigned to the input shape rank, got " << in_lay.to_short_string();+}++TEST(reduce_gpu, onednn_reduce_input_format_rank_mismatch_blocked_axis) {+ test_reduce_input_format_rank_mismatch(1);+}++// Reducing a spatial axis exercises the tensor-field remapping in reorder_unreduced_axis_no_fusion(),+// which silently zeroed the wrong field when the input tensor rank and format rank disagreed.+TEST(reduce_gpu, onednn_reduce_input_format_rank_mismatch_spatial_axis) {+ test_reduce_input_format_rank_mismatch(4);+}++// Regression test, natural-selection variant: oneDNN's ReduceImplementationManager+// is only registered for shape_types::static_shape (see registry/reduce_impls.cpp), so a+// dynamic-shape reduce node has no oneDNN candidate at compile time. But b_fs_yx_fsv16 (blocked)+// is only registered for OCL's *static* reduce impl, not its dynamic one (see impls/ocl/reduce.cpp+// dyn_formats), so once the shape is realized at runtime there is no valid dynamic candidate+// either. The runtime static-shape fallback then picks oneDNN, without force_implementations,+// exercising the same reorder_unreduced_axis_no_fusion() rank-normalization path.+static void test_reduce_dynamic_node_static_fallback(int64_t reduce_axis) {+ auto& engine = get_test_engine();+ if (!engine.get_device_info().supports_immad)+ return;++ const int f_size = 17;+ const int x_size = 2;+ auto used_layout = layout({1, f_size, 1, x_size}, data_types::f16, format::b_fs_yx_fsv16);+ auto input_mem = engine.allocate_memory(used_layout);++ std::vector<float> input_data(f_size * x_size);+ {+ cldnn::mem_lock<ov::float16> input_ptr(input_mem, get_test_stream());+ for (int fi = 0; fi < f_size; fi++)+ for (int xi = 0; xi < x_size; xi++) {+ float val = static_cast<float>(fi * x_size + xi) * 0.1f;+ input_data[fi * x_size + xi] = val;+ tensor coords(batch(0), feature(fi), spatial(xi, 0, 0, 0));+ input_ptr[used_layout.get_linear_offset(coords)] = ov::float16(val);+ }+ }++ topology topology;+ // The runtime memory keeps its concrete b_fs_yx_fsv16 layout; only the declared input_layout+ // is dynamic, which is what makes the node's compile-time shape_type dynamic.+ topology.add(input_layout("input", layout(ov::PartialShape::dynamic(4), data_types::f16, format::b_fs_yx_fsv16)));+ topology.add(reduce("reduce", input_info("input"), reduce_mode::mean, {reduce_axis}, false));+ topology.add(reorder("reduce_bfyx", input_info("reduce"), format::bfyx, data_types::f16));++ ExecutionConfig config = get_test_default_config(engine);+ config.set_property(ov::intel_gpu::allow_new_shape_infer(true));+ // No force_implementations: verify oneDNN is picked because it is the only valid candidate+ // once the OCL dynamic reduce impl rejects the blocked b_fs_yx_fsv16 format.++ network network(engine, topology, config);+ network.set_input_data("input", input_mem);++ auto outputs = network.execute();++ auto inst = network.get_primitive("reduce");+ auto impl = inst->get_impl();+ ASSERT_NE(impl, nullptr);+ ASSERT_NE(impl->m_manager, nullptr);+ ASSERT_EQ(impl->m_manager->get_impl_type(), impl_types::onednn)+ << "expected oneDNN static fallback to be selected without force_implementations";++ auto out_mem = outputs.at("reduce_bfyx").get_memory();+ cldnn::mem_lock<ov::float16, mem_lock_type::read> out_ptr(out_mem, get_test_stream());++ // axis 1 reduces feature (output indexed by x), axis 3 reduces x (output indexed by feature).+ const int out_size = reduce_axis == 1 ? x_size : f_size;+ const int reduced_size = reduce_axis == 1 ? f_size : x_size;+ for (int oi = 0; oi < out_size; oi++) {+ float expected = 0.f;+ for (int ri = 0; ri < reduced_size; ri++) {+ const int fi = reduce_axis == 1 ? ri : oi;+ const int xi = reduce_axis == 1 ? oi : ri;+ expected += input_data[fi * x_size + xi];+ }+ expected /= reduced_size;+ ASSERT_NEAR(static_cast<float>(out_ptr[oi]), expected, 1e-1f) << "out=" << oi;+ }+}++TEST(reduce_gpu, onednn_reduce_dynamic_node_static_fallback_selection) {+ test_reduce_dynamic_node_static_fallback(1);+}++TEST(reduce_gpu, onednn_reduce_dynamic_node_static_fallback_spatial_axis) {+ test_reduce_dynamic_node_static_fallback(3);+}#endif // ENABLE_ONEDNN_FOR_GPU+#ifdef RUN_ALL_MODEL_CACHING_TESTSTEST_P(general_reduce_gpu_i8_i8, base_cached) { execute(true); }