Skip to content

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

zero copy permute opt for conv (#33827)

Ashutosh Singh6b898c65b372
diff --git a/src/plugins/intel_gpu/src/graph/graph_optimizer/select_preferred_formats.cpp b/src/plugins/intel_gpu/src/graph/graph_optimizer/select_preferred_formats.cpp
index d3904f3f4d8..f6a731aa1f2 100644
--- a/src/plugins/intel_gpu/src/graph/graph_optimizer/select_preferred_formats.cpp
+++ b/src/plugins/intel_gpu/src/graph/graph_optimizer/select_preferred_formats.cpp
@@ -62,6 +62,58 @@ static void optimize_conv_permute(program_node& node) {
}
}
+static void optimize_permute_conv(program_node& node) {
+ // Goal: Eliminate the Reorder by aligning connection to byxf
+ if (node.get_dependencies().empty())
+ return;
+
+ auto& dep = node.get_dependency(0);
+
+ // Dependency must be a Permute node (not network output)
+ if (!dep.is_type<permute>() || dep.is_output())
+ return;
+
+ if ((node.get_users().size() != 1) || (node.get_output_layout().get_rank() != 4))
+ return;
+
+ auto& pnode = dep.as<permute>();
+
+ if (pnode.get_output_layout().data_type != node.get_output_layout().data_type)
+ return;
+
+ // NHWC <-> NCHW (ensures reverse rotation pattern)
+ if (!pnode.is_reverse_rotating_except_batch())
+ return;
+
+ auto pnode_upstream_fmt = pnode.get_dependency(0).get_preferred_output_fmt();
+ auto node_fmt = node.get_preferred_output_fmt();
+
+ bool is_compatible_format = ((pnode_upstream_fmt == format::bfyx || pnode_upstream_fmt == format::any)
+ && (node_fmt == format::byxf));
+
+ if (!is_compatible_format)
+ return;
+
+ // Set the layouts so that the memory buffer is re-interpreted rather than physically shuffled.
+ node.set_preferred_input_fmt(0, format::byxf);
+
+ // Set Permute Input to match upstream format
+ pnode.set_preferred_input_fmt(0, format::bfyx);
+
+ // This aligns with the Pre-Transpose memory, allowing it to be a zero copy optimization.
+ // alternative approach is to force the Permute node to set output fmt to match convolution input fmt
+ // which will eliminate reorder before convolution: pnode.set_preferred_output_fmt(0, node.get_preferred_input_fmt())
+ // however, for non planar blocked format (b_fs_yx_fsv16) The kernel has to calculate complex offsets to pack 16 channels together into a block.
+ // and may degrade performance which may possibly be not visible for small input like 56x56
+ // as small working set the working set fits largely in the GPU L2 cache. And penalty for complex addressing calculation
+ // may get masked by high L2 bandwidth.
+ pnode.set_preferred_output_fmt(0, format::byxf);
+
+ if (!pnode.has_fused_primitives()) {
+ pnode.can_be_optimized(true);
+ }
+}
+
} // namespace
void select_preferred_formats::run(program& p) {
@@ -119,6 +171,7 @@ void select_preferred_formats::run(program& p) {
}
if (factory->get_impl_type() == impl_types::onednn && (n->is_type<convolution>() || n->is_type<deconvolution>())) {
optimize_conv_permute(*n);
+ optimize_permute_conv(*n);
}
} catch (std::exception& exception) {
GPU_DEBUG_LOG << "WARNING(select_preferred_formats): " << exception.what() << std::endl;
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.cpp
index 6caeff7ad3a..9cd3de9d27b 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
@@ -8,9 +8,12 @@
#include "intel_gpu/runtime/engine.hpp"
#include "intel_gpu/primitives/convolution.hpp"
+#include "intel_gpu/primitives/eltwise.hpp"
+#include "intel_gpu/primitives/permute.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 "pass_manager.h"
#include "to_string_utils.h"
@@ -118,3 +121,171 @@ TEST(test_select_preferred_formats, fsv2_fallback_to_byxf) {
}
}
}
+
+TEST(test_select_preferred_formats, permute_conv_incompatible_format) {
+ // Negative tests for is_compatible_format in optimize_permute_conv:
+ // 1) node_fmt = b_fs_yx_fsv16 (not byxf) -> optimization does NOT trigger
+ // 2) upstream preferred_output_fmt = incompatible (b_fs_yx_fsv16) -> optimization does NOT trigger
+ auto& engine = get_test_engine();
+ if (!engine.get_device_info().supports_immad)
+ return;
+
+ auto build_permute_conv_program = [&engine](const std::vector<uint16_t>& permute_order, tensor input_size, tensor weight_size, format forced_fmt) {
+ auto input = engine.allocate_memory({data_types::f16, format::bfyx, input_size});
+ auto weights = engine.allocate_memory({data_types::f16, format::bfyx, weight_size});
+
+ topology topo;
+ topo.add(data("weights", weights));
+ topo.add(input_layout("input", input->get_layout()));
+ topo.add(permute("perm", input_info("input"), permute_order));
+ topo.add(convolution("conv1", input_info("perm"), "weights", "", 1, {1, 1}, {1, 1}, {0, 0}, {0, 0}, false));
+ topo.add(reorder("output_reorder", input_info("conv1"), format::bfyx, data_types::f16));
+
+ ExecutionConfig config = get_test_default_config(engine);
+ config.set_property(ov::intel_gpu::allow_new_shape_infer(true));
+ ov::intel_gpu::ImplementationDesc impl = {forced_fmt, std::string(""), impl_types::onednn};
+ config.set_property(ov::intel_gpu::force_implementations(ov::intel_gpu::ImplForcingMap{{"conv1", impl}}));
+
+ auto prog = program::build_program(engine, topo, config, false, true);
+ prog->get_layout_optimizer().add_all_onednn_impls_optimization_attribute();
+ prog->get_node("conv1").get_output_layouts(false);
+ return prog;
+ };
+
+ // Sub-case 1: conv preferred_output_fmt not compatible -> optimization should NOT trigger
+ {
+ auto prog = build_permute_conv_program({0, 3, 1, 2}, {1, 32, 16, 8}, {32, 16, 3, 3}, format::b_fs_yx_fsv16);
+ program_wrapper::apply_opt_pass<select_preferred_formats>(*prog);
+
+ auto& conv_node = prog->get_node("conv1");
+ auto& perm_node = prog->get_node("perm");
+ ASSERT_NE(perm_node.get_preferred_output_fmt(0), format::byxf);
+ ASSERT_NE(conv_node.get_preferred_output_fmt(0), format::byxf);
+ ASSERT_FALSE(perm_node.can_be_optimized());
+ }
+
+ // Sub-case 2: upstream preferred_output_fmt = incompatible format -> optimization should NOT trigger
+ {
+ auto prog = build_permute_conv_program({0, 3, 1, 2}, {1, 32, 16, 8}, {32, 16, 3, 3}, format::byxf);
+ prog->get_node("input").set_preferred_output_fmt(0, format::b_fs_yx_fsv16);
+ program_wrapper::apply_opt_pass<select_preferred_formats>(*prog);
+
+ auto& perm_node = prog->get_node("perm");
+ ASSERT_NE(perm_node.get_preferred_output_fmt(0), format::byxf);
+ ASSERT_FALSE(perm_node.can_be_optimized());
+ }
+}
+
+TEST(test_select_preferred_formats, permute_conv_accuracy) {
+ // Verifies that optimize_permute_conv produces numerically correct output
+ // by comparing optimized path against reference, both using oneDNN conv.
+ //
+ // opt_topo: input -> permute -> conv(oneDNN, byxf) -> output_reorder
+ // conv has 1 user -> optimize_permute_conv fires
+ //
+ // ref_topo: same as opt_topo + extra leaf reorder ("ref_tap") from conv
+ // conv has 2 users -> node.get_users().size() != 1
+ // blocks optimize_permute_conv, rest of the pipeline is identical
+ //
+ // Two sub-cases:
+ // 1) Unfused permute: can_be_optimized = true
+ // 2) Fused eltwise(prod) into permute: can_be_optimized = false,
+ // but formats still set. Uses position-dependent scale values so any
+ // layout misalignment produces detectably wrong numerical results.
+ // IMMAD-only: optimize_permute_conv only runs when oneDNN impl is selected.
+ auto& engine = get_test_engine();
+ if (!engine.get_device_info().supports_immad)
+ return;
+
+ auto input = engine.allocate_memory({data_types::f16, format::bfyx, {1, 2, 2, 16}});
+ auto weights = engine.allocate_memory({data_types::f16, format::bfyx, {2, 2, 1, 1}});
+
+ const int num_elements = 64; // 1*2*2*16
+ std::vector<ov::float16> input_vals(num_elements);
+ for (int i = 0; i < num_elements; ++i)
+ input_vals[i] = ov::float16(static_cast<float>(i + 1));
+ set_values<ov::float16>(input, input_vals);
+
+ std::vector<ov::float16> weight_vals(2 * 2, ov::float16(0.f));
+ for (int i = 0; i < 2; ++i)
+ weight_vals[i * 2 + i] = ov::float16(1.f);
+ set_values<ov::float16>(weights, weight_vals);
+
+ // Run ref and opt networks (both oneDNN conv), verify numerical match.
+ // Returns the opt network's program for state checks.
+ auto run_and_verify = [&](const topology& opt_topo, const topology& ref_topo) -> std::shared_ptr<program> {
+ ov::intel_gpu::ImplementationDesc conv_impl = {format::byxf, std::string(""), impl_types::onednn};
+
+ ExecutionConfig ref_config = get_test_default_config(engine);
+ ref_config.set_property(ov::intel_gpu::allow_new_shape_infer(true));
+ ref_config.set_property(ov::intel_gpu::force_implementations(ov::intel_gpu::ImplForcingMap{{"conv1", conv_impl}}));
+ network ref_net(engine, ref_topo, ref_config);
+ ref_net.set_input_data("input", input);
+ auto ref_outputs = ref_net.execute();
+ auto ref_mem = ref_outputs.at("output_reorder").get_memory();
+ cldnn::mem_lock<float, mem_lock_type::read> ref_ptr(ref_mem, get_test_stream());
+
+ ExecutionConfig opt_config = get_test_default_config(engine);
+ opt_config.set_property(ov::intel_gpu::allow_new_shape_infer(true));
+ opt_config.set_property(ov::intel_gpu::force_implementations(ov::intel_gpu::ImplForcingMap{{"conv1", conv_impl}}));
+ network opt_net(engine, opt_topo, opt_config);
+ opt_net.set_input_data("input", input);
+ auto opt_outputs = opt_net.execute();
+ auto opt_mem = opt_outputs.at("output_reorder").get_memory();
+ cldnn::mem_lock<float, mem_lock_type::read> opt_ptr(opt_mem, get_test_stream());
+
+ EXPECT_EQ(ref_mem->get_layout().count(), opt_mem->get_layout().count());
+ for (size_t i = 0; i < ref_mem->get_layout().count(); ++i) {
+ EXPECT_NEAR(ref_ptr[i], opt_ptr[i], 1e-3f) << "Mismatch at index " << i;
+ }
+ return opt_net.get_program();
+ };
+
+ // Sub-case 1: unfused permute -> can_be_optimized = true
+ {
+ topology opt_topo;
+ opt_topo.add(data("weights", weights));
+ opt_topo.add(input_layout("input", input->get_layout()));
+ opt_topo.add(permute("perm", input_info("input"), {0, 3, 1, 2}));
+ opt_topo.add(convolution("conv1", input_info("perm"), "weights", "", 1, {1, 1}, {1, 1}, {0, 0}, {0, 0}, false));
+ opt_topo.add(reorder("output_reorder", input_info("conv1"), format::bfyx, data_types::f32));
+
+ // ref_topo = opt_topo + extra consumer on conv1 to block optimization
+ topology ref_topo(opt_topo);
+ ref_topo.add(reorder("ref_tap", input_info("conv1"), format::bfyx, data_types::f16));
+
+ auto prog = run_and_verify(opt_topo, ref_topo);
+ auto& perm_node = prog->get_node("perm");
+ ASSERT_EQ(perm_node.get_preferred_output_fmt(0), format::byxf);
+ ASSERT_FALSE(perm_node.has_fused_primitives());
+ ASSERT_TRUE(perm_node.can_be_optimized());
+ }
+
+ // Sub-case 2: fused eltwise(prod) into permute -> can_be_optimized = false
+ {
+ auto scale_data = engine.allocate_memory({data_types::f16, format::bfyx, {1, 2, 16, 2}});
+ std::vector<ov::float16> scale_vals(num_elements);
+ for (int i = 0; i < num_elements; ++i)
+ scale_vals[i] = ov::float16(static_cast<float>(i) * 0.1f + 0.5f);
+ set_values<ov::float16>(scale_data, scale_vals);
+
+ topology opt_topo;
+ opt_topo.add(data("weights", weights));
+ opt_topo.add(data("scale_data", scale_data));
+ opt_topo.add(input_layout("input", input->get_layout()));
+ opt_topo.add(permute("perm", input_info("input"), {0, 3, 1, 2}));
+ opt_topo.add(eltwise("scale", {input_info("perm"), input_info("scale_data")}, eltwise_mode::prod));
+ opt_topo.add(convolution("conv1", input_info("scale"), "weights", "", 1, {1, 1}, {1, 1}, {0, 0}, {0, 0}, false));
+ opt_topo.add(reorder("output_reorder", input_info("conv1"), format::bfyx, data_types::f32));
+
+ // ref_topo = opt_topo + extra consumer on conv1 to block optimization
+ topology ref_topo(opt_topo);
+ ref_topo.add(reorder("ref_tap", input_info("conv1"), format::bfyx, data_types::f16));
+
+ auto prog = run_and_verify(opt_topo, ref_topo);
+ auto& perm_node = prog->get_node("perm");
+ ASSERT_EQ(perm_node.get_preferred_output_fmt(0), format::byxf);
+ ASSERT_TRUE(perm_node.has_fused_primitives());
+ ASSERT_FALSE(perm_node.can_be_optimized());
+ }
+}
\ No newline at end of file