Skip to content

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

[GPU] Support NCHW to NWHC permute in the permute_tile_8x8_4x4 kernel (#37982)

Deepak Soma Reddy5db509d08823
diff --git a/src/plugins/intel_gpu/src/kernel_selector/kernels/permute/permute_kernel_tile_8x8_4x4.cpp b/src/plugins/intel_gpu/src/kernel_selector/kernels/permute/permute_kernel_tile_8x8_4x4.cpp
index 6e530f80ee5..0e80e2a99b0 100644
--- a/src/plugins/intel_gpu/src/kernel_selector/kernels/permute/permute_kernel_tile_8x8_4x4.cpp
+++ b/src/plugins/intel_gpu/src/kernel_selector/kernels/permute/permute_kernel_tile_8x8_4x4.cpp
@@ -15,6 +15,35 @@
namespace kernel_selector {
+// Detect the permute pattern where the feature (cldnn dim 1) exchanges places with a
+// spatial dimension while the remaining axes keep their order:
+// 4D: cldnn order [0,2,1,3] - from ONNX/OV perm [0,3,2,1] (NCHW -> NWHC)
+// 5D: cldnn order [0,4,1,3,2] - from ONNX/OV perm [0,2,4,3,1]
+// 6D: cldnn order [0,5,1,3,2,4] - from ONNX/OV perm [0,2,3,5,4,1]
+// cldnn orders are derived from OV orders by convert_permute_order() (bfyx -> bfxy
+// spatial mirroring), so the ONNX/OV correspondence is not the identity at higher ranks.
+// The same tiled-transpose kernel handles this case with a swapped OUTPUT_TILED_ORDER
+// (args 2 and 3 of OUTPUT_GET_INDEX exchanged): reads are identical (coalesced along x),
+// writes are still coalesced (f_tile indexes the innermost x_out dimension of the output).
+static inline bool IsSwappingFX(const std::vector<uint16_t>& order) {
+ const auto match = [&order](const uint16_t* expected, size_t size) {
+ return std::equal(expected, expected + size, order.begin());
+ };
+ if (order.size() == 4) {
+ static const uint16_t kOrder4[] = {0, 2, 1, 3};
+ return match(kOrder4, 4);
+ }
+ if (order.size() == 5) {
+ static const uint16_t kOrder5[] = {0, 4, 1, 3, 2};
+ return match(kOrder5, 5);
+ }
+ if (order.size() == 6) {
+ static const uint16_t kOrder6[] = {0, 5, 1, 3, 2, 4};
+ return match(kOrder6, 6);
+ }
+ return false;
+}
+
ParamsKey PermuteKernel_tile_8x8_4x4::GetSupportedKey() const {
ParamsKey k;
k.EnableInputDataType(Datatype::F16);
@@ -83,18 +112,37 @@ static inline std::string GetTiledOutputOrder(const permute_params& params) {
std::string order_str;
int32_t dim_diff = static_cast<int32_t>(dim_change.first) - static_cast<int32_t>(dim_change.second);
+ const bool swap_f_x = IsSwappingFX(params.order);
+
if (dim_diff == 0) {
- switch (dim_change.first) {
- case 4 :
- order_str = "b, y, (x * TILE_SIZE + lh), (f * TILE_SIZE)";
- break;
- case 5 :
- order_str = "b, z, y, (x * TILE_SIZE + lh), (f * TILE_SIZE)";
- break;
- case 6 :
- order_str = "b, w, z, y, (x * TILE_SIZE + lh), (f * TILE_SIZE)";
- break;
- default : throw std::runtime_error("Unsupported combination\n");
+ if (swap_f_x) {
+ // IsSwappingFX patterns: cldnn [0,2,1,3] (4D) / [0,4,1,3,2] (5D) / [0,5,1,3,2,4] (6D).
+ // Args 2 and 3 of OUTPUT_GET_INDEX are exchanged vs the is_rotating_except_batch case.
+ switch (dim_change.first) {
+ case 4 :
+ order_str = "b, (x * TILE_SIZE + lh), y, (f * TILE_SIZE)";
+ break;
+ case 5 :
+ order_str = "b, z, (x * TILE_SIZE + lh), y, (f * TILE_SIZE)";
+ break;
+ case 6 :
+ order_str = "b, w, z, (x * TILE_SIZE + lh), y, (f * TILE_SIZE)";
+ break;
+ default : throw std::runtime_error("Unsupported combination\n");
+ }
+ } else {
+ switch (dim_change.first) {
+ case 4 :
+ order_str = "b, y, (x * TILE_SIZE + lh), (f * TILE_SIZE)";
+ break;
+ case 5 :
+ order_str = "b, z, y, (x * TILE_SIZE + lh), (f * TILE_SIZE)";
+ break;
+ case 6 :
+ order_str = "b, w, z, y, (x * TILE_SIZE + lh), (f * TILE_SIZE)";
+ break;
+ default : throw std::runtime_error("Unsupported combination\n");
+ }
}
} else if (dim_diff > 0) {
// dim is shrinked
@@ -281,19 +329,35 @@ bool PermuteKernel_tile_8x8_4x4::Validate(const Params& p) const {
DO_NOT_USE_THIS_KERNEL(p.layerID);
}
+ // Accepted cldnn permutation orders. cldnn orders use bfxy spatial numbering and are
+ // derived from OV orders by convert_permute_order() (bfyx -> bfxy mirroring), so the
+ // ONNX/OV correspondence is not the identity:
+ //
+ // is_rotating_except_batch: cldnn [0,3,1,2] (4D) / [0,4,1,2,3] (5D) / ...
+ // Feature (dim 1) is pushed to the cldnn-y (last spatial) position; spatial dims rotate
+ // left. Corresponds to ONNX perm [0,2,3,1] (4D: NCHW -> NHWC).
+ //
+ // IsSwappingFX: cldnn [0,2,1,3] (4D) / [0,4,1,3,2] (5D) / [0,5,1,3,2,4] (6D)
+ // Feature (dim 1) exchanges places with a spatial dimension; the remaining axes
+ // keep their order. For 4D this is ONNX perm [0,3,2,1] (NCHW -> NWHC).
+ // Uses the same tiled read pattern but with output args 2 and 3 exchanged in
+ // OUTPUT_TILED_ORDER so that writes remain coalesced.
+
std::function<bool(const std::vector<uint16_t>&)> is_rotating_except_batch = [](const std::vector<uint16_t>& order) {
- // Target transform: Rotate feature dim to back to be taken as inner-most axis
- // ex) 0(b), 4(f), 1(z), 2(y), 3(x)
- // ex) 0(b), 3(f), 1(y), 2(x)
- if ((int32_t) order[1] != order.size() - 1) return false;
+ if ((int32_t) order[1] != (int32_t)order.size() - 1) return false;
if ((int32_t) order[0] != 0) return false;
for (int32_t i = 2; i < (int32_t) order.size(); ++i) {
- if ((int32_t)order[i] != (i - 1)) return false;
+ if ((int32_t)order[i] != (i - 1)) return false;
}
return true;
};
- if (!is_rotating_except_batch(params.order)) {
+ const bool is_swapping_fx = IsSwappingFX(params.order);
+ if (!is_rotating_except_batch(params.order) && !is_swapping_fx) {
+ DO_NOT_USE_THIS_KERNEL(p.layerID);
+ }
+
+ if (is_swapping_fx && params.inputs[0].GetDims().size() != params.outputs[0].GetDims().size()) {
DO_NOT_USE_THIS_KERNEL(p.layerID);
}
@@ -312,6 +376,13 @@ bool PermuteKernel_tile_8x8_4x4::Validate(const Params& p) const {
DO_NOT_USE_THIS_KERNEL(p.layerID);
}
+ // Fused op output indexing is only validated for the is_rotating_except_batch
+ // branch; the f<->spatial swap branch stores through a transposed output index,
+ // so fall back to another implementation when a fused op is present.
+ if (has_fused_op(params) && IsSwappingFX(params.order)) {
+ DO_NOT_USE_THIS_KERNEL(p.layerID);
+ }
+
return true;
}
diff --git a/src/plugins/intel_gpu/tests/unit/test_cases/permute_gpu_test.cpp b/src/plugins/intel_gpu/tests/unit/test_cases/permute_gpu_test.cpp
index 17ca4be84e4..e1cd541c7fe 100644
--- a/src/plugins/intel_gpu/tests/unit/test_cases/permute_gpu_test.cpp
+++ b/src/plugins/intel_gpu/tests/unit/test_cases/permute_gpu_test.cpp
@@ -1935,7 +1935,8 @@ void TiledPermuteTest::run_test(const std::vector<ov::Dimension::value_type>& si
std::swap(internal_sizes.at(2), internal_sizes.back());
cldnn::tensor tensor(internal_sizes);
- cldnn::format format = sizes.size() == 4 ? cldnn::format::bfyx : cldnn::format::bfzyx;
+ cldnn::format format = sizes.size() == 4 ? cldnn::format::bfyx
+ : (sizes.size() == 5 ? cldnn::format::bfzyx : cldnn::format::bfwzyx);
std::vector<uint16_t> order = {0};
if (permute_order.empty()) {
@@ -2412,6 +2413,112 @@ TEST_P(permute_f_y_axes_tile, combined) {
run_test<cldnn::data_types::i64>(p.sizes, p.format_fsv, "permute_f_y_axes", {0, 2, 1, 3});
}
+// IsSwappingFX branch of the tiled kernel (cldnn order [0,2,1,3], i.e. ONNX
+// [0,3,2,1] / NCHW -> NWHC). Note: permute_f_y_axes_tile above forces the
+// separate pre-existing permute_f_y_axes kernel, which accepts cldnn [0,3,2,1]
+// (ONNX [0,2,1,3] / NCHW -> NHCW in bfyx terms) instead.
+class permute_tile_swap_fx_4d : public TiledPermuteTest {};
+
+INSTANTIATE_TEST_SUITE_P(smoke_permute_tile_swap_fx_4d,
+ permute_tile_swap_fx_4d,
+ ::testing::ValuesIn(std::vector<TiledPermuteParam>{
+ {{1, 16, 8, 8}, format::bfyx},
+ {{1, 32, 64, 32}, format::bfyx},
+ }),
+ TiledPermuteTest::PrintToStringParamName);
+
+TEST_P(permute_tile_swap_fx_4d, combined) {
+ auto p = GetParam();
+ run_test<cldnn::data_types::f32>(p.sizes, p.format_fsv, "permute_tile_8x8_4x4", {0, 3, 2, 1});
+ run_test<cldnn::data_types::f16>(p.sizes, p.format_fsv, "permute_tile_8x8_4x4", {0, 3, 2, 1});
+ run_test<cldnn::data_types::bf16>(p.sizes, p.format_fsv, "permute_tile_8x8_4x4", {0, 3, 2, 1});
+}
+
+// Unforced kernel selection for the model's permute (NCHW -> NWHC 4D, f16).
+// No implementation is forced: this asserts that the default selector picks the
+// tiled kernel instead of falling back to permute_ref, which is the behavioral
+// regression this change targets.
+TEST(permute_tile_swap_fx_selection, unforced_uses_tiled_kernel) {
+ auto& engine = get_test_engine();
+ const std::vector<ov::Dimension::value_type> sizes{1, 128, 200, 200};
+ cldnn::tensor tensor(sizes);
+ auto input = engine.allocate_memory(cldnn::layout(cldnn::data_types::f16, cldnn::format::bfyx, tensor));
+ tests::set_random_values<ov::float16>(input);
+
+ topology topology_selection = topology(
+ input_layout("input", input->get_layout()),
+ reorder("reorder", input_info("input"), {cldnn::data_types::f16, format::bfyx, tensor}),
+ permute("output", input_info("reorder"), std::vector<uint16_t>{0, 3, 2, 1}));
+ auto net = get_network(engine, topology_selection, get_test_default_config(engine), get_test_stream_ptr(), false);
+ net->set_input_data("input", input);
+ auto res = net->execute();
+ ASSERT_FALSE(res.empty());
+
+ // The default selection must pick the tiled kernel, not the permute_ref fallback.
+ EXPECT_NE(net->get_primitive_info("output").find("permute_tile_8x8_4x4"), std::string::npos);
+
+ // Numerical sanity: ONNX perm {0,3,2,1} (NCHW -> NWHC) maps
+ // out[b][W][H][C] == in[b][C][H][W]. In cldnn bfyx terms the input is
+ // (b, f=C, y=H, x=W) and the output is (b, f=W, y=H, x=C), so
+ // out[b][fo=W][yo=H][xo=C] == in[b][fi=C][yi=H][xi=W].
+ auto output = res.at("output").get_memory();
+ ASSERT_EQ(output->get_layout().format, format::bfyx);
+ const auto in_t = input->get_layout().get_tensor();
+ const auto out_t = output->get_layout().get_tensor();
+ const int32_t IF = in_t.feature[0], IY = in_t.spatial[1], IX = in_t.spatial[0];
+ const int32_t OB = out_t.batch[0], OF = out_t.feature[0], OY = out_t.spatial[1], OX = out_t.spatial[0];
+ cldnn::mem_lock<ov::float16> out_ptr(output, get_test_stream());
+ cldnn::mem_lock<ov::float16> in_ptr(input, get_test_stream());
+ for (int32_t b = 0; b < OB; ++b)
+ for (int32_t fo = 0; fo < OF; ++fo)
+ for (int32_t yo = 0; yo < OY; ++yo)
+ for (int32_t xo = 0; xo < OX; ++xo) {
+ size_t out_off = ((size_t)(b * OF + fo) * OY + yo) * OX + xo;
+ size_t in_off = ((size_t)(b * IF + xo) * IY + yo) * IX + fo;
+ EXPECT_EQ(static_cast<float>(in_ptr[in_off]), static_cast<float>(out_ptr[out_off]))
+ << "mismatch at b=" << b << " fo=" << fo << " yo=" << yo << " xo=" << xo;
+ }
+}
+
+// IsSwappingFX branch of the tiled kernel extended to 5D (cldnn order
+// [0,2,4,3,1]).
+class permute_tile_swap_fx_5d : public TiledPermuteTest {};
+
+INSTANTIATE_TEST_SUITE_P(smoke_permute_tile_swap_fx_5d,
+ permute_tile_swap_fx_5d,
+ ::testing::ValuesIn(std::vector<TiledPermuteParam>{
+ {{1, 16, 8, 8, 8}, format::bfzyx},
+ {{1, 16, 4, 8, 16}, format::bfzyx},
+ {{2, 64, 8, 8, 8}, format::bfzyx},
+ }),
+ TiledPermuteTest::PrintToStringParamName);
+
+TEST_P(permute_tile_swap_fx_5d, combined) {
+ auto p = GetParam();
+ run_test<cldnn::data_types::f32>(p.sizes, p.format_fsv, "permute_tile_8x8_4x4", {0, 2, 4, 3, 1});
+ run_test<cldnn::data_types::f16>(p.sizes, p.format_fsv, "permute_tile_8x8_4x4", {0, 2, 4, 3, 1});
+ run_test<cldnn::data_types::bf16>(p.sizes, p.format_fsv, "permute_tile_8x8_4x4", {0, 2, 4, 3, 1});
+}
+
+// IsSwappingFX branch of the tiled kernel extended to 6D (cldnn order
+// [0,2,3,5,4,1]).
+class permute_tile_swap_fx_6d : public TiledPermuteTest {};
+
+INSTANTIATE_TEST_SUITE_P(smoke_permute_tile_swap_fx_6d,
+ permute_tile_swap_fx_6d,
+ ::testing::ValuesIn(std::vector<TiledPermuteParam>{
+ {{1, 8, 4, 4, 4, 4}, format::bfwzyx},
+ {{1, 16, 4, 4, 8, 8}, format::bfwzyx},
+ }),
+ TiledPermuteTest::PrintToStringParamName);
+
+TEST_P(permute_tile_swap_fx_6d, combined) {
+ auto p = GetParam();
+ run_test<cldnn::data_types::f32>(p.sizes, p.format_fsv, "permute_tile_8x8_4x4", {0, 2, 3, 5, 4, 1});
+ run_test<cldnn::data_types::f16>(p.sizes, p.format_fsv, "permute_tile_8x8_4x4", {0, 2, 3, 5, 4, 1});
+ run_test<cldnn::data_types::bf16>(p.sizes, p.format_fsv, "permute_tile_8x8_4x4", {0, 2, 3, 5, 4, 1});
+}
+
// permute_xy_swap kernel: optimized 4D X<->Y transpose ({0, 1, 3, 2}).
// Constraints (see PermuteKernel_xy_swap::Validate):
// * 4D plain bfyx layout only.
@@ -2612,3 +2719,46 @@ INSTANTIATE_TEST_SUITE_P(, TiledPerformancePermuteTest,
{{1, 256, 128, 256}, format::bfyx},
{{1, 256, 256, 128}, format::b_fs_yx_fsv16},
}));
+
+// The permute_tile_8x8_4x4 kernel now supports the f<->x swap order
+// cldnn [0,2,1,3] (ONNX [0,3,2,1], NCHW -> NWHC).
+TEST(permute_gpu_f32, tile_8x8_4x4_fy_swap) {
+ auto& engine = get_test_engine();
+
+ tensor input_size{1, 16, 16, 8};
+ auto input = engine.allocate_memory({ data_types::f16, format::bfyx, input_size });
+ tests::set_random_values<ov::float16>(input);
+
+ // cldnn order [0,2,1,3] (swap f and x); the input shape is asymmetric so a rotate-only
+ // implementation would produce wrong values.
+ std::vector<uint16_t> order{ 0, 3, 2, 1 };
+ topology topology(input_layout("input", input->get_layout()),
+ permute("output", input_info("input"), order));
+
+ // reference
+ {
+ ExecutionConfig config_ref = get_test_default_config(engine);
+ ov::intel_gpu::ImplementationDesc ref_impl = { format::bfyx, "permute_ref" };
+ config_ref.set_property(ov::intel_gpu::force_implementations(ov::intel_gpu::ImplForcingMap{ { "output", ref_impl } }));
+ network network_ref(engine, topology, config_ref);
+ network_ref.set_input_data("input", input);
+ auto outputs_ref = network_ref.execute();
+ auto output_ref = outputs_ref.at("output").get_memory();
+
+ // tiled kernel (the one that must now accept [0,2,1,3])
+ ExecutionConfig config_tile = get_test_default_config(engine);
+ ov::intel_gpu::ImplementationDesc tile_impl = { format::bfyx, "permute_tile_8x8_4x4" };
+ config_tile.set_property(ov::intel_gpu::force_implementations(ov::intel_gpu::ImplForcingMap{ { "output", tile_impl } }));
+ network network_tile(engine, topology, config_tile);
+ network_tile.set_input_data("input", input);
+ auto outputs_tile = network_tile.execute();
+ auto output_tile = outputs_tile.at("output").get_memory();
+
+ cldnn::mem_lock<ov::float16, mem_lock_type::read> ref_ptr(output_ref, get_test_stream());
+ cldnn::mem_lock<ov::float16, mem_lock_type::read> tile_ptr(output_tile, get_test_stream());
+ for (size_t i = 0; i < ref_ptr.size(); ++i) {
+ ASSERT_NEAR(static_cast<float>(ref_ptr[i]), static_cast<float>(tile_ptr[i]), 1e-3f)
+ << "Mismatch at " << i;
+ }
+ }
+}