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] Fix dynamic FP16 oneDNN conv preferred format and grouped channel count (#40189)
Andrew Kwangwoong Park619890384b15
diff --git a/src/plugins/intel_gpu/src/graph/layout_optimizer.cpp b/src/plugins/intel_gpu/src/graph/layout_optimizer.cppindex 047437a9781..f85d745320c 100644--- a/src/plugins/intel_gpu/src/graph/layout_optimizer.cpp+++ b/src/plugins/intel_gpu/src/graph/layout_optimizer.cpp@@ -178,10 +178,16 @@ int64_t cldnn::get_convolution_channel_count(const convolution_node& conv_node,auto weights_layout = conv_node.weights().get_output_layout();if (weights_layout.is_static()) {const auto& shape = weights_layout.get_partial_shape();- if (is_input) {- channel_count = shape[conv_node.get_groups() > 1 ? 2 : 1].get_length();- } else {- channel_count = shape[conv_node.get_groups() > 1 ? 1 : 0].get_length();+ const bool grouped_weights = conv_node.get_primitive()->grouped_weights_shape || format::is_grouped(weights_layout.format);+ const int64_t groups = static_cast<int64_t>(conv_node.get_groups());+ if (grouped_weights) {+ // Grouped weights shape is [G, O_per_group, I_per_group, ...]+ if (shape.size() >= 3) {+ channel_count = groups * shape[is_input ? 2 : 1].get_length();+ }+ } else if (shape.size() >= 2) {+ // Non-grouped weights shape is [G * O_per_group, I_per_group, ...]+ channel_count = is_input ? groups * shape[1].get_length() : shape[0].get_length();}}}@@ -1287,30 +1293,14 @@ void layout_optimizer::set_onednn_dyn_conv_preferred_format(convolution_node& nonode.set_preferred_input_fmt(0, get_fsv16_format(rank));node.set_preferred_output_fmt(0, get_fsv16_format(rank));- // Override input for small channels (≤ 16)- // fsv16 format uses 16-element blocks. channels ≤ 16 waste block padding- // e.g. 8ch uses only 8/16 elements per block (50% waste), planar format is more efficient- if (input_channels > 0 && input_channels <= 16) {+ // Override with default planar format for small channels (<= 8).+ // Matches oneDNN jit:ir small-channel threshold for FP16 (get_default_mad_block(f16) / 2 = 8),+ // where fsv16 (aBcd16b) is rejected and falls back to ocl:ref:any.+ if (input_channels > 0 && input_channels <= 8) {node.set_preferred_input_fmt(0, format::get_default_format(rank));}- // Override output for small channels (≤ 16)- // same as input - avoid fsv16 block padding overhead for small channel counts- if (output_channels > 0 && output_channels <= 16) {- node.set_preferred_output_fmt(0, format::get_default_format(rank));- }-- // Override output for channel expansion operations (small input → large output)- // when expanding from small input channels (≤16) to large output channels (≥32),- // planar output format enables OneDNN to select optimized JIT kernel instead of reference kernel- // Thresholds explained:- // - input ≤ 16: matches fsv16 block size, input side uses planar format (set above)- // - output ≥ 32: 2 or more fsv16 blocks (32/16=2), where blocked write overhead exceeds- // sequential write benefits. planar format provides better cache locality- // and memory access patterns for large channel generation- // e.g. 3ch → 1024ch would create 64 fsv16 blocks with scattered writes,- // but planar format allows efficient sequential writes- if (input_channels > 0 && input_channels <= 16 && output_channels >= 32) {+ if (output_channels > 0 && output_channels <= 8) {node.set_preferred_output_fmt(0, format::get_default_format(rank));}} else if (is_fp32_input) {diff --git a/src/plugins/intel_gpu/tests/unit/passes/select_preferred_formats_test.cpp b/src/plugins/intel_gpu/tests/unit/passes/select_preferred_formats_test.cppindex e347234ba36..351262e3d46 100644--- a/src/plugins/intel_gpu/tests/unit/passes/select_preferred_formats_test.cpp+++ b/src/plugins/intel_gpu/tests/unit/passes/select_preferred_formats_test.cpp@@ -2,28 +2,28 @@// SPDX-License-Identifier: Apache-2.0//-#include "intel_gpu/runtime/layout.hpp"-#include "test_utils.h"--#include "intel_gpu/runtime/engine.hpp"+#include <memory>-#include "intel_gpu/primitives/fully_connected.hpp"-#include "intel_gpu/primitives/reorder.hpp"+#include "convolution_inst.h"+#include "data_inst.h"+#include "intel_gpu/graph/network.hpp"+#include "intel_gpu/graph/program.hpp"#include "intel_gpu/primitives/convolution.hpp"#include "intel_gpu/primitives/eltwise.hpp"+#include "intel_gpu/primitives/fully_connected.hpp"#include "intel_gpu/primitives/permute.hpp"+#include "intel_gpu/primitives/reduce.hpp"+#include "intel_gpu/primitives/reorder.hpp"#include "intel_gpu/primitives/reshape.hpp"-#include "intel_gpu/graph/program.hpp"-#include "data_inst.h"-#include "convolution_inst.h"-#include "permute_inst.h"-#include "intel_gpu/graph/network.hpp"+#include "intel_gpu/runtime/engine.hpp"+#include "intel_gpu/runtime/layout.hpp"+#include "layout_optimizer.h"#include "pass_manager.h"-#include "to_string_utils.h"-+#include "permute_inst.h"+#include "primitive_inst.h"#include "program_wrapper.h"--#include <memory>+#include "test_utils.h"+#include "to_string_utils.h"using namespace cldnn;using namespace ::tests;@@ -387,4 +387,181 @@ TEST(test_select_preferred_formats, dynamic_compressed_fc_keeps_fbyx_scale_and_zASSERT_EQ(fc_node.get_dependency(3).id(), "zp_reorder");ASSERT_EQ(fc_node.get_preferred_input_fmt(2), format::fbyx);ASSERT_EQ(fc_node.get_preferred_input_fmt(3), format::fbyx);+}++#ifdef ENABLE_ONEDNN_FOR_GPU+TEST(test_select_preferred_formats, onednn_dyn_conv_fp16_preferred_format) {+ auto& engine = get_test_engine();+ if (!engine.get_device_info().supports_immad) {+ GTEST_SKIP() << "oneDNN convolutions are only used on devices with immad support";+ }++ struct ConvFmtCase {+ int64_t ic;+ int64_t oc;+ format::type expected_in_fmt;+ format::type expected_out_fmt;+ };++ const std::vector<ConvFmtCase> cases = {+ {3, 32, format::bfyx, format::b_fs_yx_fsv16},+ {8, 32, format::bfyx, format::b_fs_yx_fsv16},+ {8, 64, format::bfyx, format::b_fs_yx_fsv16},+ {9, 32, format::b_fs_yx_fsv16, format::b_fs_yx_fsv16},+ {32, 16, format::b_fs_yx_fsv16, format::b_fs_yx_fsv16},+ {16, 16, format::b_fs_yx_fsv16, format::b_fs_yx_fsv16},+ {16, 96, format::b_fs_yx_fsv16, format::b_fs_yx_fsv16},+ {32, 8, format::b_fs_yx_fsv16, format::bfyx},+ {32, 9, format::b_fs_yx_fsv16, format::b_fs_yx_fsv16},+ {32, 4, format::b_fs_yx_fsv16, format::bfyx},+ {4, 16, format::bfyx, format::b_fs_yx_fsv16},+ };++ for (const auto& tc : cases) {+ auto weights = engine.allocate_memory({ov::PartialShape{tc.oc, tc.ic, 3, 3}, data_types::f16, format::bfyx});+ set_values(weights, std::vector<ov::float16>(weights->count(), ov::float16(0.01F)));+ topology topo;+ topo.add(input_layout("input", layout{ov::PartialShape{1, tc.ic, -1, -1}, data_types::f16, format::bfyx}));+ topo.add(data("weights", weights));+ topo.add(convolution("conv", input_info("input"), "weights", "", 1, {1, 1}, {1, 1}, {1, 1}, {1, 1}, false));+ topo.add(reorder("out", input_info("conv"), format::bfyx, data_types::f32));++ auto config = get_test_default_config(engine);+ config.set_property(ov::intel_gpu::optimize_data(true));+ config.set_property(ov::intel_gpu::allow_new_shape_infer(true));++ network net(engine, topo, config);+ ASSERT_EQ(net.get_program()->get_layout_optimizer().get_optimization_attributes().byxf_onednn_convolution, 0);++ const auto& conv_node = net.get_primitive("conv")->get_node();+ ASSERT_TRUE(conv_node.is_dynamic());+ ASSERT_EQ(conv_node.get_preferred_impl_type(), impl_types::onednn);+ EXPECT_EQ(conv_node.get_preferred_input_fmt(0), tc.expected_in_fmt) << "ic=" << tc.ic << ", oc=" << tc.oc;+ EXPECT_EQ(conv_node.get_preferred_output_fmt(0), tc.expected_out_fmt) << "ic=" << tc.ic << ", oc=" << tc.oc;+ EXPECT_EQ(conv_node.get_input_layout(0).format, tc.expected_in_fmt) << "ic=" << tc.ic << ", oc=" << tc.oc;+ EXPECT_EQ(conv_node.get_output_layout(0).format, tc.expected_out_fmt) << "ic=" << tc.ic << ", oc=" << tc.oc;++ auto input_mem = engine.allocate_memory({ov::PartialShape{1, tc.ic, 16, 16}, data_types::f16, format::bfyx});+ set_values(input_mem, std::vector<ov::float16>(input_mem->count(), ov::float16(1.0F)));+ net.set_input_data("input", input_mem);+ auto outputs = net.execute();+ ASSERT_EQ(outputs.count("out"), 1U);++ const auto* conv_impl = net.get_primitive("conv")->get_impl();+ ASSERT_NE(conv_impl, nullptr);+ EXPECT_TRUE(conv_impl->is_onednn()) << "ic=" << tc.ic << ", oc=" << tc.oc;+ EXPECT_EQ(conv_impl->get_kernel_name().find("ref"), std::string::npos)+ << "ic=" << tc.ic << ", oc=" << tc.oc << ", kernel=" << conv_impl->get_kernel_name();+ }+}++TEST(test_select_preferred_formats, onednn_dyn_conv_16ch_reduce_mean_uses_onednn) {+ auto& engine = get_test_engine();+ if (!engine.get_device_info().supports_immad) {+ GTEST_SKIP() << "oneDNN convolutions and reductions are only used on devices with immad support";+ }++ auto weights1 = engine.allocate_memory({ov::PartialShape{16, 16, 3, 3}, data_types::f16, format::bfyx});+ auto weights2 = engine.allocate_memory({ov::PartialShape{4, 16, 1, 1}, data_types::f16, format::bfyx});+ set_values(weights1, std::vector<ov::float16>(weights1->count(), ov::float16(0.01F)));+ set_values(weights2, std::vector<ov::float16>(weights2->count(), ov::float16(0.01F)));++ // Squeeze-and-excitation block pattern: 16ch conv -> ReduceMean(H, W) -> 1x1 conv (16 -> 4).+ // With 16ch kept in b_fs_yx_fsv16, ReduceMean keeps blocked layout and uses oneDNN reduction at runtime.+ topology topo;+ topo.add(input_layout("input", layout{ov::PartialShape{1, 16, -1, -1}, data_types::f16, format::bfyx}));+ topo.add(data("weights1", weights1));+ topo.add(data("weights2", weights2));+ topo.add(convolution("conv1", input_info("input"), "weights1", "", 1, {1, 1}, {1, 1}, {1, 1}, {1, 1}, false));+ topo.add(reduce("reduce_mean", input_info("conv1"), reduce_mode::mean, {2, 3}, true));+ topo.add(convolution("conv2", input_info("reduce_mean"), "weights2", "", 1, {1, 1}, {1, 1}, {0, 0}, {0, 0}, false));+ topo.add(reorder("out", input_info("conv2"), format::bfyx, data_types::f32));++ auto config = get_test_default_config(engine);+ config.set_property(ov::intel_gpu::optimize_data(true));+ config.set_property(ov::intel_gpu::allow_new_shape_infer(true));++ network net(engine, topo, config);+ const auto& conv1_node = net.get_primitive("conv1")->get_node();+ const auto& reduce_node = net.get_primitive("reduce_mean")->get_node();+ ASSERT_EQ(conv1_node.get_output_layout(0).format, format::b_fs_yx_fsv16);+ ASSERT_EQ(reduce_node.get_input_layout(0).format, format::b_fs_yx_fsv16);+ ASSERT_EQ(reduce_node.get_output_layout(0).format, format::b_fs_yx_fsv16);++ auto input_mem = engine.allocate_memory({ov::PartialShape{1, 16, 32, 32}, data_types::f16, format::bfyx});+ set_values(input_mem, std::vector<ov::float16>(input_mem->count(), ov::float16(1.0F)));+ net.set_input_data("input", input_mem);+ auto outputs = net.execute();+ ASSERT_EQ(outputs.count("out"), 1U);++ const auto* reduce_impl = net.get_primitive("reduce_mean")->get_impl();+ ASSERT_NE(reduce_impl, nullptr);+ EXPECT_TRUE(reduce_impl->is_onednn()) << "Expected oneDNN reduction, got: " << reduce_impl->get_kernel_name();+}+#endif // ENABLE_ONEDNN_FOR_GPU++TEST(test_select_preferred_formats, get_convolution_channel_count_grouped_dynamic_channels) {+ auto& engine = get_test_engine();++ struct ChannelCase {+ uint32_t groups;+ bool grouped_weights;+ bool allow_new_shape_infer;+ ov::PartialShape input_shape;+ ov::PartialShape weights_shape;+ format::type weights_fmt;+ int64_t expected_ic;+ int64_t expected_oc;+ };++ const std::vector<ChannelCase> cases = {+ {560, true, true, ov::PartialShape{1, -1, -1, -1}, ov::PartialShape{560, 1, 1, 3, 3}, format::bfzyx, 560, 560},+ {4, true, true, ov::PartialShape{1, -1, -1, -1}, ov::PartialShape{4, 8, 6, 3, 3}, format::bfzyx, 24, 32},+ {1, true, true, ov::PartialShape{1, -1, -1, -1}, ov::PartialShape{1, 32, 16, 3, 3}, format::bfzyx, 16, 32},+ {4, false, false, ov::PartialShape{1, 24, 8, 8}, ov::PartialShape{32, 6, 3, 3}, format::bfyx, 24, 32},+ {1, false, true, ov::PartialShape{1, -1, -1, -1}, ov::PartialShape{32, 3, 3, 3}, format::bfyx, 3, 32},+ };++ const layout dyn_ch_layout{ov::PartialShape::dynamic(4), data_types::f16, format::bfyx};+ for (const auto& tc : cases) {+ auto weights = engine.allocate_memory({tc.weights_shape, data_types::f16, tc.weights_fmt});+ topology topo;+ topo.add(input_layout("input", layout{tc.input_shape, data_types::f16, format::bfyx}));+ topo.add(data("weights", weights));+ topo.add(convolution("conv", input_info("input"), "weights", "", tc.groups, {1, 1}, {1, 1}, {1, 1}, {1, 1}, tc.grouped_weights));++ auto config = get_test_default_config(engine);+ config.set_property(ov::intel_gpu::allow_new_shape_infer(tc.allow_new_shape_infer));++ auto prog = program::build_program(engine, topo, config, false, true);+ const auto& conv_node = prog->get_node("conv").as<convolution>();+ EXPECT_EQ(get_convolution_channel_count(conv_node, dyn_ch_layout, true), tc.expected_ic)+ << "groups=" << tc.groups << ", grouped_weights=" << tc.grouped_weights;+ EXPECT_EQ(get_convolution_channel_count(conv_node, dyn_ch_layout, false), tc.expected_oc)+ << "groups=" << tc.groups << ", grouped_weights=" << tc.grouped_weights;+ }++#ifdef ENABLE_ONEDNN_FOR_GPU+ if (engine.get_device_info().supports_immad) {+ auto weights = engine.allocate_memory({ov::PartialShape{64, 1, 1, 3, 3}, data_types::f16, format::bfzyx});+ topology topo;+ topo.add(input_layout("input", layout{ov::PartialShape{1, -1, -1, -1}, data_types::f16, format::bfyx}));+ topo.add(data("weights", weights));+ topo.add(convolution("conv", input_info("input"), "weights", "", 64, {1, 1}, {1, 1}, {1, 1}, {1, 1}, true));+ topo.add(reorder("out", input_info("conv"), format::bfyx, data_types::f32));++ auto config = get_test_default_config(engine);+ config.set_property(ov::intel_gpu::optimize_data(true));+ config.set_property(ov::intel_gpu::allow_new_shape_infer(true));++ network net(engine, topo, config);+ ASSERT_EQ(net.get_program()->get_layout_optimizer().get_optimization_attributes().byxf_onednn_convolution, 0);+ const auto& conv_node = net.get_primitive("conv")->get_node();+ ASSERT_EQ(conv_node.get_preferred_impl_type(), impl_types::onednn);+ EXPECT_EQ(conv_node.get_preferred_input_fmt(0), format::b_fs_yx_fsv16);+ EXPECT_EQ(conv_node.get_preferred_output_fmt(0), format::b_fs_yx_fsv16);+ EXPECT_EQ(conv_node.get_input_layout(0).format, format::b_fs_yx_fsv16);+ EXPECT_EQ(conv_node.get_output_layout(0).format, format::b_fs_yx_fsv16);+ }+#endif // ENABLE_ONEDNN_FOR_GPU}\ No newline at end of file