Skip to content

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

[GPU] Fix rank mismatch of oneDNN 1D group conv binary post-ops (#39679)

Wilson Seok4ad119b2f961
diff --git a/src/plugins/intel_gpu/src/graph/program_node.cpp b/src/plugins/intel_gpu/src/graph/program_node.cpp
index f184ab2b3e0..d5ba5e6ee07 100644
--- a/src/plugins/intel_gpu/src/graph/program_node.cpp
+++ b/src/plugins/intel_gpu/src/graph/program_node.cpp
@@ -1730,6 +1730,19 @@ void program_node::create_onednn_primitive_attributes(const std::vector<fused_pr
}
};
+ // Binary post-op descriptors must have the same rank as the dst descriptor (see get_conv_memory_descs()).
+ // Lazy: this function also runs from calc_output_layouts(), before the output layout of the node is valid.
+ auto keep_dst_rank = [&]() {
+ bool keep_rank = is_type<reduce>() || cldnn::format::is_blocked(get_output_layout().format);
+ if (is_type<convolution>()) {
+ keep_rank |= cldnn::format::is_blocked(get_input_layout(0).format) || cldnn::format::is_grouped(get_input_layout(1).format) ||
+ this->as<convolution>().get_primitive()->grouped_weights_shape;
+ } else if (is_type<deconvolution>()) {
+ keep_rank |= cldnn::format::is_blocked(get_input_layout(0).format) || cldnn::format::is_grouped(get_input_layout(1).format);
+ }
+ return keep_rank;
+ };
+
int32_t num_inplace_post_ops = 0;
for (size_t idx = 0; idx < cldnn_post_ops.size(); idx++) {
const auto& desc = cldnn_post_ops[idx];
@@ -1811,14 +1824,9 @@ void program_node::create_onednn_primitive_attributes(const std::vector<fused_pr
dnnl::memory::format_tag fmt = onednn::convert_gemm_data_format(dims, in.format);
post_ops.append_binary(alg, dnnl::memory::desc(dims, dt, fmt));
update_onednn_post_op_list(op_type, dep_idx, fmt, false, dims, dt);
- } else if (is_type<reduce>()) {
- auto mem_desc = onednn::layout_to_memory_desc_blocked(in, dnnl::memory::format_tag::undef);
- post_ops.append_binary(alg, mem_desc);
- update_onednn_post_op_list(op_type, dep_idx, onednn::convert_data_format(in.format), false, mem_desc.get_dims(), mem_desc.get_data_type());
} else {
- auto mem_desc = cldnn::format::is_blocked(get_output_layout().format)
- ? onednn::layout_to_memory_desc_blocked(in, dnnl::memory::format_tag::undef)
- : onednn::layout_to_memory_desc(in, dnnl::memory::format_tag::undef);
+ auto mem_desc = keep_dst_rank() ? onednn::layout_to_memory_desc_blocked(in, dnnl::memory::format_tag::undef)
+ : onednn::layout_to_memory_desc(in, dnnl::memory::format_tag::undef);
post_ops.append_binary(alg, mem_desc);
update_onednn_post_op_list(op_type, dep_idx, onednn::convert_data_format(in.format), false, mem_desc.get_dims(), mem_desc.get_data_type());
}
@@ -1861,7 +1869,7 @@ void program_node::create_onednn_primitive_attributes(const std::vector<fused_pr
const auto& q_param = desc.get_typed_fuse_params<QuantizeFuseParams>();
if (q_param->_per_tensor_output_range && q_param->_out_lo < q_param->_out_hi) {
bool is_fc_or_gemm = this->is_type<gemm>() || this->is_type<fully_connected>();
- bool is_output_blocked = cldnn::format::is_blocked(this->get_output_layout().format);
+ bool keep_rank = keep_dst_rank();
// 1. pre-scale & pre-shift
{
if (q_param->_per_tensor_input_scale && q_param->_per_tensor_input_shift) {
@@ -1875,11 +1883,11 @@ void program_node::create_onednn_primitive_attributes(const std::vector<fused_pr
auto in_scale = get_input_layout(dep_idx++);
resize_layout_for_fc(this, in_scale);
- dnnl::memory::desc in_scale_desc = onednn::layout_to_memory_desc(in_scale, is_fc_or_gemm, is_output_blocked);
+ dnnl::memory::desc in_scale_desc = onednn::layout_to_memory_desc(in_scale, is_fc_or_gemm, keep_rank);
post_ops.append_binary(dnnl::algorithm::binary_mul, in_scale_desc);
update_onednn_post_op_list(onednn_post_op_type::binary_mul,
dep_idx - 1,
- onednn::get_default_data_format(in_scale),
+ keep_rank ? onednn::convert_data_format(in_scale.format) : onednn::get_default_data_format(in_scale),
false,
in_scale_desc.get_dims(),
in_scale_desc.get_data_type());
@@ -1893,11 +1901,11 @@ void program_node::create_onednn_primitive_attributes(const std::vector<fused_pr
auto in_shift = get_input_layout(dep_idx++);
resize_layout_for_fc(this, in_shift);
- dnnl::memory::desc in_shift_desc = onednn::layout_to_memory_desc(in_shift, is_fc_or_gemm, is_output_blocked);
+ dnnl::memory::desc in_shift_desc = onednn::layout_to_memory_desc(in_shift, is_fc_or_gemm, keep_rank);
post_ops.append_binary(dnnl::algorithm::binary_add, in_shift_desc);
update_onednn_post_op_list(onednn_post_op_type::binary_add,
dep_idx - 1,
- onednn::get_default_data_format(in_shift),
+ keep_rank ? onednn::convert_data_format(in_shift.format) : onednn::get_default_data_format(in_shift),
false,
in_shift_desc.get_dims(),
in_shift_desc.get_data_type());
@@ -1930,14 +1938,15 @@ void program_node::create_onednn_primitive_attributes(const std::vector<fused_pr
auto out_scale = get_input_layout(dep_idx++);
resize_layout_for_fc(this, out_scale);
- dnnl::memory::desc out_scale_desc = onednn::layout_to_memory_desc(out_scale, is_fc_or_gemm, is_output_blocked);
+ dnnl::memory::desc out_scale_desc = onednn::layout_to_memory_desc(out_scale, is_fc_or_gemm, keep_rank);
post_ops.append_binary(dnnl::algorithm::binary_mul, out_scale_desc);
- update_onednn_post_op_list(onednn_post_op_type::binary_mul,
- dep_idx - 1,
- onednn::get_default_data_format(out_scale),
- false,
- out_scale_desc.get_dims(),
- out_scale_desc.get_data_type());
+ update_onednn_post_op_list(
+ onednn_post_op_type::binary_mul,
+ dep_idx - 1,
+ keep_rank ? onednn::convert_data_format(out_scale.format) : onednn::get_default_data_format(out_scale),
+ false,
+ out_scale_desc.get_dims(),
+ out_scale_desc.get_data_type());
}
}
diff --git a/src/plugins/intel_gpu/tests/unit/fusions/convolution_fusion_test.cpp b/src/plugins/intel_gpu/tests/unit/fusions/convolution_fusion_test.cpp
index ab4e7a83817..7933e4515fc 100644
--- a/src/plugins/intel_gpu/tests/unit/fusions/convolution_fusion_test.cpp
+++ b/src/plugins/intel_gpu/tests/unit/fusions/convolution_fusion_test.cpp
@@ -4594,4 +4594,110 @@ INSTANTIATE_TEST_SUITE_P(fusings_gpu, conv_elt_fp16_onednn, ::testing::ValuesIn(
convolution_test_params{ CASE_CONV_FP16_1, 2, 2, 4 },
}));
+// oneDNN keeps the 4D rank of a 1D grouped convolution in plain format, so binary post-ops must keep it too.
+class conv_fp32_1d_group_eltwise_onednn : public WeightsPrimitiveFusingTestOneDNN {
+public:
+ layout get_1d_input_layout(convolution_test_params& p, bool is_dynamic) {
+ auto in_layout = get_input_layout(p);
+ if (is_dynamic)
+ in_layout.set_partial_shape({ ov::Dimension::dynamic(), p.in_shape[1], p.in_shape[2] });
+ return in_layout;
+ }
+
+ void execute_1d(convolution_test_params& p, bool is_caching_test = false) {
+ ov::intel_gpu::ImplementationDesc conv_impl = { format::bfyx, "", impl_types::onednn };
+ cfg_fused.set_property(ov::intel_gpu::allow_new_shape_infer(true));
+ cfg_fused.set_property(ov::intel_gpu::force_implementations(ov::intel_gpu::ImplForcingMap{ { "conv_prim", conv_impl } }));
+ cfg_not_fused.set_property(ov::intel_gpu::allow_new_shape_infer(true));
+
+ auto network_not_fused = get_network(engine, topology_non_fused, cfg_not_fused, get_test_stream_ptr(cfg_not_fused), is_caching_test);
+ auto network_fused = get_network(engine, topology_fused, cfg_fused, get_test_stream_ptr(cfg_fused), is_caching_test);
+ auto input_prim = get_mem(get_input_layout(p));
+ network_fused->set_input_data("input", input_prim);
+ network_not_fused->set_input_data("input", input_prim);
+
+ p.expected_fused_primitives = p.expected_fused_primitives_onednn;
+ compare(*network_not_fused, *network_fused, p);
+ }
+
+ void execute_eltwise(convolution_test_params& p, bool is_dynamic) {
+ if (!engine.get_device_info().supports_immad)
+ GTEST_SKIP();
+
+ create_topologies(
+ input_layout("input", get_1d_input_layout(p, is_dynamic)),
+ data("weights", get_mem(get_weights_layout(p))),
+ data("scale_data", get_mem(get_output_layout(p))),
+ data("shift_data", get_mem(get_output_layout(p))),
+ convolution("conv_prim", input_info("input"), "weights", "", p.groups, p.stride, p.dilation, p.pad, p.pad, true),
+ eltwise("scale", { input_info("conv_prim"), input_info("scale_data") }, eltwise_mode::prod),
+ eltwise("shift", { input_info("scale"), input_info("shift_data") }, eltwise_mode::sum),
+ reorder("reorder_bfyx", input_info("shift"), p.default_format, data_types::f32)
+ );
+
+ tolerance = default_tolerance(p.default_type);
+ execute_1d(p);
+ }
+};
+
+TEST_P(conv_fp32_1d_group_eltwise_onednn, static_shape) {
+ auto p = GetParam();
+ execute_eltwise(p, false);
+}
+
+TEST_P(conv_fp32_1d_group_eltwise_onednn, dynamic_shape) {
+ auto p = GetParam();
+ execute_eltwise(p, true);
+}
+
+// in_shape; out_shape; weights_shape; stride; pad; dilation; groups; data_type; input_format; weights_type; weights_format; default_type; default_format;
+#define CASE_CONV_FP32_1D_GROUP_1 { 1, 16, 16 }, { 1, 16, 16 }, { 16, 1, 1, 7 }, { 1 }, { 3 }, { 1 }, 16, data_types::f32, format::bfyx, data_types::f32, format::bfyx, data_types::f32, format::bfyx
+#define CASE_CONV_FP32_1D_GROUP_2 { 1, 32, 16 }, { 1, 32, 16 }, { 32, 1, 1, 31 }, { 1 }, { 15 }, { 1 }, 32, data_types::f32, format::bfyx, data_types::f32, format::bfyx, data_types::f32, format::bfyx
+
+INSTANTIATE_TEST_SUITE_P(fusings_gpu, conv_fp32_1d_group_eltwise_onednn, ::testing::ValuesIn(std::vector<convolution_test_params>{
+ convolution_test_params{ CASE_CONV_FP32_1D_GROUP_1, 2, 2, 4 },
+ convolution_test_params{ CASE_CONV_FP32_1D_GROUP_2, 2, 2, 4 },
+}));
+
+class conv_fp32_1d_group_quantize_onednn : public conv_fp32_1d_group_eltwise_onednn {
+public:
+ void execute_quantize(convolution_test_params& p, bool is_caching_test) {
+ if (!engine.get_device_info().supports_immad)
+ GTEST_SKIP();
+
+ // Rank-3 bounds keep the quantize scales rank-3, as in a 1D model.
+ layout single_element_layout{ ov::PartialShape{ 1, 1, 1 }, p.default_type, p.default_format };
+ create_topologies(
+ input_layout("input", get_input_layout(p)),
+ data("weights", get_mem(get_weights_layout(p))),
+ data("in_lo", get_mem(get_per_channel_layout(p), -10, 0)),
+ data("in_hi", get_mem(get_per_channel_layout(p), 1, 10)),
+ data("out_lo", get_mem(single_element_layout, 0)),
+ data("out_hi", get_mem(single_element_layout, 255)),
+ convolution("conv_prim", input_info("input"), "weights", "", p.groups, p.stride, p.dilation, p.pad, p.pad, true),
+ quantize("quantize", input_info("conv_prim"), input_info("in_lo"), input_info("in_hi"),
+ input_info("out_lo"), input_info("out_hi"), 256, data_types::u8),
+ reorder("reorder_bfyx", input_info("quantize"), p.default_format, data_types::f32)
+ );
+
+ tolerance = 1.f;
+ execute_1d(p, is_caching_test);
+ }
+};
+
+TEST_P(conv_fp32_1d_group_quantize_onednn, per_channel) {
+ auto p = GetParam();
+ execute_quantize(p, false);
+}
+
+TEST_P(conv_fp32_1d_group_quantize_onednn, per_channel_cached) {
+ auto p = GetParam();
+ execute_quantize(p, true);
+}
+
+INSTANTIATE_TEST_SUITE_P(fusings_gpu, conv_fp32_1d_group_quantize_onednn, ::testing::ValuesIn(std::vector<convolution_test_params>{
+ convolution_test_params{ CASE_CONV_FP32_1D_GROUP_1, 2, 2, 3 },
+ convolution_test_params{ CASE_CONV_FP32_1D_GROUP_2, 2, 2, 3 },
+}));
+
#endif // ENABLE_ONEDNN_FOR_GPU