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] support [num_group, N, group_size] weights layout for FullyConnectedCompressed (#38206)
Yaru Du3c203973be08
diff --git a/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/permute_bf_swap.cl b/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/permute_bf_swap.clnew file mode 100644index 00000000000..73bd36039f2--- /dev/null+++ b/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/permute_bf_swap.cl@@ -0,0 +1,21 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#include "include/batch_headers/fetch_data.cl"++// Packed UINT2 fast path for bfyx order {1, 0, 2, 3}: [B, F, Y, X] -> [F, B, Y, X].+// Requires static, contiguous input/output with zero offsets and no fused operations.+// Y * X must be divisible by 64 (16 packed bytes). Each work-item copies 16 bytes+// from plane (b, f) to (f, b), preserving the spatial order without unpacking UINT2 values.+KERNEL(permute_bf_swap)(const __global INPUT0_TYPE* input, __global OUTPUT_TYPE* output) {+ const uint block = (uint)get_global_id(0);+ const uint f = (uint)get_global_id(1);+ const uint b = (uint)get_global_id(2);++ const uint input_block = (b * INPUT0_FEATURE_NUM + f) * PLANE_BLOCKS + block;+ const uint output_block = (f * INPUT0_BATCH_NUM + b) * PLANE_BLOCKS + block;++ const uchar16 values = vload16(input_block, (const __global uchar*)input);+ vstore16(values, output_block, (__global uchar*)output);+}diff --git a/src/plugins/intel_gpu/src/kernel_selector/kernels/permute/permute_kernel_bf_swap.cpp b/src/plugins/intel_gpu/src/kernel_selector/kernels/permute/permute_kernel_bf_swap.cppnew file mode 100644index 00000000000..5999fbb4737--- /dev/null+++ b/src/plugins/intel_gpu/src/kernel_selector/kernels/permute/permute_kernel_bf_swap.cpp@@ -0,0 +1,76 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#include "permute_kernel_bf_swap.h"++#include "common_tools.h"+#include "kernel_selector_utils.h"++namespace kernel_selector {++namespace {+constexpr size_t kVectorBytes = 16;+}++ParamsKey PermuteKernel_bf_swap::GetSupportedKey() const {+ ParamsKey key;+ key.EnableInputDataType(Datatype::UINT2);+ key.EnableOutputDataType(Datatype::UINT2);+ key.EnableInputLayout(DataLayout::bfyx);+ key.EnableOutputLayout(DataLayout::bfyx);+ key.EnableTensorOffset();+ key.EnableTensorPitches();+ key.EnableBatching();+ return key;+}++JitConstants PermuteKernel_bf_swap::GetJitConstants(const permute_params& params,+ const CommonDispatchData& dispatch_data) const {+ auto jit = Parent::GetJitConstants(params, dispatch_data);+ jit.AddConstant(MakeJitConstant("VECTOR_BYTES", kVectorBytes));+ jit.AddConstant(MakeJitConstant("PLANE_BLOCKS", params.inputs[0].Y().v * params.inputs[0].X().v / 4 /+ kVectorBytes));+ return jit;+}++CommonDispatchData PermuteKernel_bf_swap::SetDefault(const permute_params& params) const {+ CommonDispatchData dispatch_data;+ const auto& input = params.inputs[0];+ const size_t plane_blocks = input.Y().v * input.X().v / 4 / kVectorBytes;+ dispatch_data.gws = {plane_blocks, input.Feature().v, input.Batch().v};+ dispatch_data.lws = GetOptimalLocalWorkGroupSizes(dispatch_data.gws,+ params.engineInfo,+ input.GetLayout(),+ params.outputs[0].GetLayout());+ return dispatch_data;+}++bool PermuteKernel_bf_swap::Validate(const Params& p) const {+ if (!Parent::Validate(p)) {+ DO_NOT_USE_THIS_KERNEL(p.layerID);+ }++ const auto& params = static_cast<const permute_params&>(p);+ const auto& input = params.inputs[0];+ const auto& output = params.outputs[0];+ const size_t plane_elements = input.Y().v * input.X().v;++ if (params.order != std::vector<uint16_t>{1, 0, 2, 3} ||+ input.GetDType() != Datatype::UINT2 || output.GetDType() != Datatype::UINT2 ||+ input.GetLayout() != DataLayout::bfyx || output.GetLayout() != DataLayout::bfyx ||+ params.has_dynamic_tensors() || !params.fused_ops.empty() ||+ input.PitchesDifferFromLogicalDims() || output.PitchesDifferFromLogicalDims() ||+ input.GetFirstElementOffset() != 0 || output.GetFirstElementOffset() != 0 ||+ plane_elements % (4 * kVectorBytes) != 0) {+ DO_NOT_USE_THIS_KERNEL(p.layerID);+ }++ return true;+}++KernelsPriority PermuteKernel_bf_swap::GetKernelsPriority(const Params& /*params*/) const {+ return FORCE_PRIORITY_1;+}++} // namespace kernel_selectordiff --git a/src/plugins/intel_gpu/src/kernel_selector/kernels/permute/permute_kernel_bf_swap.h b/src/plugins/intel_gpu/src/kernel_selector/kernels/permute/permute_kernel_bf_swap.hnew file mode 100644index 00000000000..ea68d873520--- /dev/null+++ b/src/plugins/intel_gpu/src/kernel_selector/kernels/permute/permute_kernel_bf_swap.h@@ -0,0 +1,27 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#pragma once++#include "permute_kernel_base.h"++namespace kernel_selector {++class PermuteKernel_bf_swap : public PermuteKernelBase {+public:+ using Parent = PermuteKernelBase;+ using Parent::Parent;+ PermuteKernel_bf_swap() : PermuteKernelBase("permute_bf_swap") {}+ ~PermuteKernel_bf_swap() override = default;++ bool Validate(const Params& p) const override;+ KernelsPriority GetKernelsPriority(const Params& params) const override;+ ParamsKey GetSupportedKey() const override;++protected:+ JitConstants GetJitConstants(const permute_params& params, const CommonDispatchData& dispatch_data) const override;+ CommonDispatchData SetDefault(const permute_params& params) const override;+};++} // namespace kernel_selectordiff --git a/src/plugins/intel_gpu/src/kernel_selector/kernels/permute/permute_kernel_selector.cpp b/src/plugins/intel_gpu/src/kernel_selector/kernels/permute/permute_kernel_selector.cppindex 6e81864879e..4f278d92d5e 100644--- a/src/plugins/intel_gpu/src/kernel_selector/kernels/permute/permute_kernel_selector.cpp+++ b/src/plugins/intel_gpu/src/kernel_selector/kernels/permute/permute_kernel_selector.cpp@@ -7,6 +7,7 @@#include "permute_kernel_tile_8x8_4x4.h"#include "permute_kernel_tile_8x8_4x4_fsv.h"#include "permute_kernel_bfzyx_to_bfyxz.h"+#include "permute_kernel_bf_swap.h"#include "permute_kernel_f_y_axes.h"#include "permute_kernel_xy_swap.h"@@ -17,6 +18,7 @@ permute_kernel_selector::permute_kernel_selector() {Attach<PermuteKernel_tile_8x8_4x4>();Attach<PermuteKernel_tile_8x8_4x4_fsv>();Attach<PermuteKernel_bfzyx_to_bfyxz>();+ Attach<PermuteKernel_bf_swap>();Attach<PermuteKernel_f_y_axes>();Attach<PermuteKernel_xy_swap>();}diff --git a/src/plugins/intel_gpu/src/kernel_selector/sources.cmake b/src/plugins/intel_gpu/src/kernel_selector/sources.cmakeindex ef225a836ae..9740e95f563 100644--- a/src/plugins/intel_gpu/src/kernel_selector/sources.cmake+++ b/src/plugins/intel_gpu/src/kernel_selector/sources.cmake@@ -475,6 +475,8 @@ set(GPU_KERNEL_SELECTOR_SOURCES${CMAKE_CURRENT_LIST_DIR}/kernels/one_hot/one_hot_kernel_selector.h${CMAKE_CURRENT_LIST_DIR}/kernels/permute/permute_kernel_base.cpp${CMAKE_CURRENT_LIST_DIR}/kernels/permute/permute_kernel_base.h+ ${CMAKE_CURRENT_LIST_DIR}/kernels/permute/permute_kernel_bf_swap.cpp+ ${CMAKE_CURRENT_LIST_DIR}/kernels/permute/permute_kernel_bf_swap.h${CMAKE_CURRENT_LIST_DIR}/kernels/permute/permute_kernel_bfzyx_to_bfyxz.cpp${CMAKE_CURRENT_LIST_DIR}/kernels/permute/permute_kernel_bfzyx_to_bfyxz.h${CMAKE_CURRENT_LIST_DIR}/kernels/permute/permute_kernel_f_y_axes.cpp@@ -933,6 +935,7 @@ set(GPU_KERNEL_SELECTOR_CL_KERNELS${CMAKE_CURRENT_LIST_DIR}/cl_kernels/normalize_gpu_across_spatial_ref.cl${CMAKE_CURRENT_LIST_DIR}/cl_kernels/normalize_gpu_within_spatial_ref.cl${CMAKE_CURRENT_LIST_DIR}/cl_kernels/one_hot_ref.cl+ ${CMAKE_CURRENT_LIST_DIR}/cl_kernels/permute_bf_swap.cl${CMAKE_CURRENT_LIST_DIR}/cl_kernels/permute_bfzyx_to_bfyxz.cl${CMAKE_CURRENT_LIST_DIR}/cl_kernels/permute_f_y_axes.cl${CMAKE_CURRENT_LIST_DIR}/cl_kernels/permute_ref.cldiff --git a/src/plugins/intel_gpu/src/plugin/transformations/compressed_weights_pattern.hpp b/src/plugins/intel_gpu/src/plugin/transformations/compressed_weights_pattern.hppindex 9dbd603805c..0ed37eff6bb 100644--- a/src/plugins/intel_gpu/src/plugin/transformations/compressed_weights_pattern.hpp+++ b/src/plugins/intel_gpu/src/plugin/transformations/compressed_weights_pattern.hpp@@ -51,6 +51,12 @@ using ov::pass::operator|;/* Transpose after decompression, with an optional preceding Reshape. */\auto transpose_const_m = wrap_type<ov::op::v0::Constant>();\auto transpose_input_m = reshape_m | mul_m;\- auto transpose_m = wrap_type<ov::op::v1::Transpose>({transpose_input_m, transpose_const_m});\+ auto transpose_after_reshape_m = wrap_type<ov::op::v1::Transpose>({transpose_input_m, transpose_const_m});\+\+ /* Transpose before the final Reshape. */\+ auto transpose_before_reshape_input_m = wrap_type<ov::op::v1::Transpose>({mul_m, transpose_const_m});\+ auto transpose_before_reshape_const_m = wrap_type<ov::op::v0::Constant>();\+ auto transpose_before_reshape_m = wrap_type<ov::op::v1::Reshape>({transpose_before_reshape_input_m, transpose_before_reshape_const_m}, reshape_squeeze);\+ auto transpose_m = transpose_after_reshape_m | transpose_before_reshape_m;\\auto compressed_weights_input_m = mul_m | convert_reshape_m | mul2_m | transpose_m;diff --git a/src/plugins/intel_gpu/src/plugin/transformations/convert_fc_to_compressed.cpp b/src/plugins/intel_gpu/src/plugin/transformations/convert_fc_to_compressed.cppindex 67972d772cd..eefb1d31e51 100644--- a/src/plugins/intel_gpu/src/plugin/transformations/convert_fc_to_compressed.cpp+++ b/src/plugins/intel_gpu/src/plugin/transformations/convert_fc_to_compressed.cpp@@ -4,6 +4,7 @@#include "convert_fc_to_compressed.hpp"+#include <algorithm>#include <memory>#include "compressed_weights_pattern.hpp"@@ -45,7 +46,8 @@ ConvertFullyConnectedToFullyConnectedCompressed::ConvertFullyConnectedToFullyConif (!fc || transformation_callback(fc)) {return false;}- bool has_transpose = pattern_map.count(transpose_m) != 0u;+ const bool has_transpose_before_reshape = pattern_map.count(transpose_before_reshape_m) != 0u;+ const bool has_transpose = pattern_map.count(transpose_m) != 0u;auto scale_shape = pattern_map.at(mul_const_m).get_shape();bool sub_with_convert = pattern_map.count(sub_convert_const_m) > 0;@@ -62,6 +64,24 @@ ConvertFullyConnectedToFullyConnectedCompressed::ConvertFullyConnectedToFullyConconst std::shared_ptr<ov::Node> weights_key = pattern_map.count(weights_m) ? weights_m : std::shared_ptr<ov::Node>(weights_param_m);const auto weight_ptr = pattern_map.at(weights_key).get_node_shared_ptr();const bool weights_are_constant = ov::is_type<ov::op::v0::Constant>(weight_ptr);+ if (has_transpose_before_reshape) {+ if (weight_ptr->get_element_type() != ov::element::u2) {+ return false;+ }+ if (!weights_are_constant) {+ const auto& group_shape =+ pattern_map.at(transpose_before_reshape_input_m).get_node()->get_input_partial_shape(0);+ if (!group_shape.is_static()) {+ return false;+ }+ const auto shape = group_shape.to_shape();+ const ov::Shape group_dims(shape.begin() + 2, shape.end());+ // The runtime UINT2 batch/feature swap processes groups of 64 elements.+ if (ov::shape_size(group_dims) % 64 != 0) {+ return false;+ }+ }+ }if (weight_ptr->get_element_type() == ov::element::u8 || weight_ptr->get_element_type() == ov::element::i8) {weight_u8 = true;}@@ -119,16 +139,21 @@ ConvertFullyConnectedToFullyConnectedCompressed::ConvertFullyConnectedToFullyCon};const ov::Output<Node>& fc_input_a = fc->input(0).get_source_output();- const auto& scale = reshape_const(pattern_map.at(mul_const_m).get_node_shared_ptr());+ const auto& scale = has_transpose_before_reshape+ ? pattern_map.at(mul_const_m).get_node_shared_ptr()+ : reshape_const(pattern_map.at(mul_const_m).get_node_shared_ptr());std::shared_ptr<ov::Node> optional_zero_point = nullptr;const bool with_zero_point = pattern_map.count(subtract_m) > 0;if (with_zero_point) {- optional_zero_point = convert_zp_const(reshape_const(pattern_map.at(sub_const_m).get_node_shared_ptr()));+ const auto zero_point = has_transpose_before_reshape+ ? pattern_map.at(sub_const_m).get_node_shared_ptr()+ : reshape_const(pattern_map.at(sub_const_m).get_node_shared_ptr());+ optional_zero_point = convert_zp_const(zero_point);}std::shared_ptr<ov::Node> fc_input_b = weight_ptr;- if (weights_are_constant) {+ if (weights_are_constant && !has_transpose_before_reshape) {fc_input_b = reshape_const(weight_ptr);} else if (pattern_map.count(weights_reshape_m)) {fc_input_b = pattern_map.at(weights_reshape_m).get_node_shared_ptr();@@ -166,7 +191,9 @@ ConvertFullyConnectedToFullyConnectedCompressed::ConvertFullyConnectedToFullyCon}if (has_transpose) {- const auto& transpose = pattern_map.at(transpose_m).get_node_shared_ptr();+ const auto& transpose = pattern_map+ .at(has_transpose_before_reshape ? transpose_before_reshape_input_m : transpose_after_reshape_m)+ .get_node_shared_ptr();std::shared_ptr<ov::Node> transpose_const = pattern_map.at(transpose_const_m).get_node_shared_ptr();if (ov::shape_size(transpose_const->get_shape()) != fc_input_b->get_output_partial_shape(0).size()) {std::vector<int32_t> new_order(fc_input_b->get_output_partial_shape(0).size());@@ -183,6 +210,36 @@ ConvertFullyConnectedToFullyConnectedCompressed::ConvertFullyConnectedToFullyContranspose_b_zp = !fc->get_transpose_b();}+ if (has_transpose_before_reshape) {+ const auto& reshape = pattern_map.at(transpose_before_reshape_m).get_node_shared_ptr();+ const auto reshape_const_node = pattern_map.at(transpose_before_reshape_const_m).get_node_shared_ptr();+ fc_input_b = reshape->clone_with_new_inputs({fc_input_b->output(0), reshape_const_node});+ result_nodes.push_back(fc_input_b);++ auto reshape_squeeze = [&](std::shared_ptr<ov::Node> node) {+ const auto& shape = node->get_output_shape(0);+ const auto output_rank = fc_input_b->get_output_shape(0).size();+ if (shape.size() <= output_rank) {+ return node;+ }++ ov::Shape output_shape(shape.begin(), shape.begin() + output_rank - 1);+ output_shape.push_back(ov::shape_size(ov::Shape(shape.begin() + output_rank - 1, shape.end())));+ auto shape_const = ov::op::v0::Constant::create(ov::element::i32,+ ov::Shape{output_shape.size()},+ output_shape);+ auto reshaped = std::make_shared<ov::op::v1::Reshape>(node, shape_const, false);+ result_nodes.push_back(shape_const);+ result_nodes.push_back(reshaped);+ return std::static_pointer_cast<ov::Node>(reshaped);+ };++ fc_input_scale = reshape_squeeze(fc_input_scale);+ if (with_zero_point) {+ fc_input_zp = reshape_squeeze(fc_input_zp);+ }+ }+if (pattern_map.count(mul2_m)) {auto mul2_op_const = ov::as_type_ptr<ov::op::v0::Constant>(pattern_map.at(mul2_const_m).get_node_shared_ptr());fc_input_scale = ov::op::util::make_try_fold<ov::op::v1::Multiply>(fc_input_scale, mul2_op_const);diff --git a/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp b/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cppindex f2645b05e26..f12e9ce51a4 100644--- a/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp+++ b/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp@@ -50,6 +50,7 @@#include "openvino/op/concat.hpp"#include "openvino/op/constant.hpp"#include "openvino/op/convolution.hpp"+#include "openvino/op/convert.hpp"#include "openvino/op/gated_delta_net.hpp"#include "openvino/op/gather.hpp"#include "openvino/op/grouped_matmul.hpp"@@ -71,6 +72,7 @@#include "openvino/op/rnn_cell.hpp"#include "openvino/op/rnn_sequence.hpp"#include "openvino/op/scaled_dot_product_attention.hpp"+#include "openvino/op/subtract.hpp"#include "openvino/op/squeeze.hpp"#include "openvino/op/paged_attention.hpp"#include "openvino/op/paged_gated_delta_net.hpp"@@ -333,13 +335,25 @@ static bool is_decompression_multiply(const std::shared_ptr<const ov::Node> nodereturn true;};+ auto has_u2_weight_input = [](const std::shared_ptr<const ov::Node>& dequantization_multiply) {+ auto weights = dequantization_multiply->input_value(0).get_node_shared_ptr();+ if (auto subtract = ov::as_type_ptr<ov::op::v1::Subtract>(weights)) {+ weights = subtract->input_value(0).get_node_shared_ptr();+ }+ const auto weights_convert = ov::as_type_ptr<ov::op::v0::Convert>(weights);+ return weights_convert && weights_convert->get_input_element_type(0) == ov::element::u2;+ };+if (all_has_types(consumers, {ov::opset1::Reshape::get_type_info_static()}) || all_has_types(consumers, {ov::op::v1::Transpose::get_type_info_static()})) {for (const auto& consumer : consumers) {auto get_child_consumers = [&]() {auto child_consumers = consumer.get_node()->get_output_target_inputs(0);- // Reshape + Transpose chain- if (all_has_types(child_consumers, { ov::op::v1::Transpose::get_type_info_static() })) {+ // Reshape + Transpose or Transpose + Reshape chain+ const bool is_u2_transpose_then_reshape_chain = ov::is_type<ov::op::v1::Transpose>(consumer.get_node()) && has_u2_weight_input(node) &&+ all_has_types(child_consumers, {ov::op::v1::Reshape::get_type_info_static()});+ const bool general_reshape_transpose_chain = all_has_types(child_consumers, {ov::op::v1::Transpose::get_type_info_static()});+ if (general_reshape_transpose_chain || is_u2_transpose_then_reshape_chain) {std::set<ov::Input<ov::Node>> next_child_consumers;for (const auto& child_consumer : child_consumers) {const auto grand_child_consumers = child_consumer.get_node()->get_output_target_inputs(0);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.cppindex 7d006058411..d2b19e5362e 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@@ -7,6 +7,7 @@#include "random_generator.hpp"#include <intel_gpu/primitives/input_layout.hpp>+#include <intel_gpu/primitives/implementation_desc.hpp>#include <intel_gpu/primitives/permute.hpp>#include <intel_gpu/primitives/reorder.hpp>#include <intel_gpu/primitives/data.hpp>@@ -2773,3 +2774,93 @@ TEST(permute_gpu_f32, tile_8x8_4x4_fy_swap) {}}}++TEST(permute_gpu_u2, bf_swap_selected_for_aligned_bfyx) {+ auto& engine = get_test_engine();+ auto input = engine.allocate_memory({data_types::u2, format::bfyx, tensor{2, 3, 16, 4}});++ topology topology(input_layout("input", input->get_layout()),+ permute("output", input_info("input"), {1, 0, 2, 3}));+ network network(engine, topology, get_test_default_config(engine));++ const auto& output_node = network.get_program()->get_node("output");+ ASSERT_NE(output_node.get_selected_impl(), nullptr);+ EXPECT_EQ(output_node.get_selected_impl()->get_kernel_name(), "permute_bf_swap");+}++TEST(permute_gpu_u2, bf_swap_rejected_for_unaligned_plane) {+ auto& engine = get_test_engine();+ auto input = engine.allocate_memory({data_types::u2, format::bfyx, tensor{2, 3, 8, 4}});++ topology topology(input_layout("input", input->get_layout()),+ permute("output", input_info("input"), {1, 0, 2, 3}));++ EXPECT_ANY_THROW(check_exception_massage(engine, topology, "Could not find a suitable kernel"));+}++TEST(permute_gpu_u2, bf_swap_rejected_for_wrong_data_type) {+ auto& engine = get_test_engine();+ auto input = engine.allocate_memory({data_types::f32, format::bfyx, tensor{2, 3, 16, 4}});++ topology topology(input_layout("input", input->get_layout()),+ permute("output", input_info("input"), {1, 0, 2, 3}));+ network network(engine, topology, get_test_default_config(engine));++ const auto& output_node = network.get_program()->get_node("output");+ ASSERT_NE(output_node.get_selected_impl(), nullptr);+ EXPECT_NE(output_node.get_selected_impl()->get_kernel_name(), "permute_bf_swap");+}++TEST(permute_gpu_u2, bf_swap_packed_output_matches_permutation) {+ auto& engine = get_test_engine();+ constexpr size_t batches = 2;+ constexpr size_t features = 3;+ constexpr size_t height = 16;+ constexpr size_t width = 4;+ constexpr size_t input_elements = batches * features * height * width;++ auto input = engine.allocate_memory({data_types::u2, format::bfyx, tensor{batches, features, height, width}});+ std::vector<uint8_t> input_data((input_elements + 3) / 4, 0);+ std::vector<uint8_t> expected_data(input_data.size(), 0);++ auto pack = [](std::vector<uint8_t>& data, size_t index, uint8_t value) {+ data[index / 4] |= static_cast<uint8_t>((value & 0x03) << ((index % 4) * 2));+ };+ auto input_index = [&](size_t batch, size_t feature, size_t y, size_t x) {+ return x + width * (y + height * (feature + features * batch));+ };+ auto output_index = [&](size_t batch, size_t feature, size_t y, size_t x) {+ return x + width * (y + height * (feature + batches * batch));+ };++ for (size_t batch = 0; batch < batches; ++batch) {+ for (size_t feature = 0; feature < features; ++feature) {+ for (size_t y = 0; y < height; ++y) {+ for (size_t x = 0; x < width; ++x) {+ const auto value = static_cast<uint8_t>((x * 3 + y * (feature + 1) + batch) % 4);+ pack(input_data, input_index(batch, feature, y, x), value);+ pack(expected_data, output_index(feature, batch, y, x), value);+ }+ }+ }+ }+ ASSERT_NE(input_data, expected_data);+ set_values(input, input_data);++ topology topology(input_layout("input", input->get_layout()),+ permute("output", input_info("input"), {1, 0, 2, 3}));++ network network(engine, topology, get_test_default_config(engine));+ network.set_input_data("input", input);++ const auto& output_node = network.get_program()->get_node("output");+ ASSERT_NE(output_node.get_selected_impl(), nullptr);+ ASSERT_EQ(output_node.get_selected_impl()->get_kernel_name(), "permute_bf_swap");++ auto output = network.execute().at("output").get_memory();+ cldnn::mem_lock<uint8_t, mem_lock_type::read> output_ptr(output, get_test_stream());+ ASSERT_EQ(expected_data.size(), output_ptr.size());+ for (size_t i = 0; i < expected_data.size(); ++i) {+ EXPECT_EQ(expected_data[i], output_ptr[i]) << "mismatch at packed byte " << i;+ }+}diff --git a/src/plugins/intel_gpu/tests/unit/transformations/convert_fc_to_compressed_test.cpp b/src/plugins/intel_gpu/tests/unit/transformations/convert_fc_to_compressed_test.cppindex 86e59821f15..d6c7c61c1dd 100644--- a/src/plugins/intel_gpu/tests/unit/transformations/convert_fc_to_compressed_test.cpp+++ b/src/plugins/intel_gpu/tests/unit/transformations/convert_fc_to_compressed_test.cpp@@ -22,6 +22,8 @@#include "plugin/transformations/convert_fc_to_compressed.hpp"#include <memory>+#include <sstream>+#include <vector>using namespace testing;using namespace ov::intel_gpu;@@ -892,6 +894,233 @@ TEST_F(TransformationTestsF, ConvertFCToCompressed24_mxfp4e2m1) {}}+enum class TransposeBeforeReshapePattern {+ ScaleOnly,+ ScaleAndZeroPoint,+ Rank4ScaleAndZeroPoint,+};++struct TransposeBeforeReshapeParams {+ ov::element::Type weights_type;+ bool parameter_weights;+ size_t group_size;+ TransposeBeforeReshapePattern pattern;+};++class ConvertFCToCompressedTransposeBeforeReshapeTest+ : public TransformationTestsF,+ public testing::WithParamInterface<TransposeBeforeReshapeParams> {+public:+ static std::string getTestCaseName(const testing::TestParamInfo<TransposeBeforeReshapeParams>& info) {+ const auto& [weights_type, parameter_weights, group_size, pattern] = info.param;+ std::ostringstream name;+ name << weights_type << (parameter_weights ? "_Parameter" : "_Constant") << "_GroupSize" << group_size;+ switch (pattern) {+ case TransposeBeforeReshapePattern::ScaleOnly:+ name << "_ScaleOnly";+ break;+ case TransposeBeforeReshapePattern::ScaleAndZeroPoint:+ name << "_ScaleAndZeroPoint";+ break;+ case TransposeBeforeReshapePattern::Rank4ScaleAndZeroPoint:+ name << "_Rank4ScaleAndZeroPoint";+ break;+ }+ return name.str();+ }++protected:+ void SetUp() override {+ TransformationTestsF::SetUp();+ const auto& params = GetParam();+ model = getModel(params);+ if (isConverted(params)) {+ model_ref = getCompressedModel(params);+ }+ comparator.enable(FunctionsComparator::CONST_VALUES);+ comparator.enable(FunctionsComparator::ATTRIBUTES);+ manager.register_pass<ConvertFullyConnectedToFullyConnectedCompressed>();+ }++ static bool isRank4(const TransposeBeforeReshapeParams& params) {+ return params.pattern == TransposeBeforeReshapePattern::Rank4ScaleAndZeroPoint;+ }++ static bool hasZeroPoint(const TransposeBeforeReshapeParams& params) {+ return params.pattern != TransposeBeforeReshapePattern::ScaleOnly;+ }++ static bool isConverted(const TransposeBeforeReshapeParams& params) {+ return params.weights_type == ov::element::u2 &&+ (!params.parameter_weights || params.group_size % 64 == 0);+ }++ static ov::Shape getWeightsShape(const TransposeBeforeReshapeParams& params) {+ if (isRank4(params)) {+ return {2, 3, 2, params.group_size};+ }+ return {2, 3, params.group_size};+ }++ static ov::Shape getScaleShape(const TransposeBeforeReshapeParams& params) {+ if (isRank4(params)) {+ return {2, 3, 1, 1};+ }+ return {2, 3, 1};+ }++ static ov::Shape getCompressedWeightShape(const TransposeBeforeReshapeParams& params) {+ if (isRank4(params)) {+ return {3, 2, 2 * params.group_size};+ }+ return {3, 2 * params.group_size};+ }++ static ov::Shape getCompressedMetadataShape(const TransposeBeforeReshapeParams& params) {+ if (isRank4(params)) {+ return {2, 3, 1};+ }+ return {2, 3};+ }++ static std::shared_ptr<ov::op::v0::Parameter> makeInput(const TransposeBeforeReshapeParams& params) {+ return std::make_shared<ov::op::v0::Parameter>(ov::element::f16,+ ov::PartialShape{-1,+ 2 * static_cast<ov::Dimension::value_type>(+ params.group_size)});+ }++ static std::shared_ptr<ov::Node> makeWeights(const TransposeBeforeReshapeParams& params,+ ov::ParameterVector& parameters) {+ const auto shape = getWeightsShape(params);+ if (params.parameter_weights) {+ auto weights = std::make_shared<ov::op::v0::Parameter>(params.weights_type, shape);+ parameters.push_back(weights);+ return weights;+ }+ return ov::op::v0::Constant::create(params.weights_type, shape, {1});+ }++ static std::shared_ptr<ov::Model> getModel(const TransposeBeforeReshapeParams& params) {+ auto input = makeInput(params);+ ov::ParameterVector parameters{input};+ auto weights = makeWeights(params, parameters);+ auto weights_convert = std::make_shared<ov::op::v0::Convert>(weights, ov::element::f16);+ ov::Output<ov::Node> dequantized_weights = weights_convert;+ const auto scale_shape = getScaleShape(params);+ if (hasZeroPoint(params)) {+ auto zero_point = ov::op::v0::Constant::create(ov::element::u2, scale_shape, {1});+ auto zero_point_convert = std::make_shared<ov::op::v0::Convert>(zero_point, ov::element::f16);+ dequantized_weights = std::make_shared<ov::op::v1::Subtract>(weights_convert, zero_point_convert);+ }+ auto scale = ov::op::v0::Constant::create(ov::element::f16, scale_shape, {1});+ auto multiply = std::make_shared<ov::op::v1::Multiply>(dequantized_weights, scale);+ const auto transpose_order = isRank4(params) ? ov::Shape{1, 0, 2, 3} : ov::Shape{1, 0, 2};+ auto transpose_order_const = ov::op::v0::Constant::create(ov::element::i32,+ ov::Shape{transpose_order.size()},+ transpose_order);+ auto transpose = std::make_shared<ov::op::v1::Transpose>(multiply, transpose_order_const);+ const auto compressed_weight_shape = getCompressedWeightShape(params);+ auto reshape_shape = ov::op::v0::Constant::create(ov::element::i32,+ ov::Shape{compressed_weight_shape.size()},+ compressed_weight_shape);+ auto reshape = std::make_shared<ov::op::v1::Reshape>(transpose, reshape_shape, false);+ auto no_bias = std::make_shared<ov::intel_gpu::op::Placeholder>();+ auto fc = std::make_shared<ov::intel_gpu::op::FullyConnected>(input, reshape, no_bias);+ return std::make_shared<ov::Model>(ov::OutputVector{fc}, parameters);+ }++ static std::shared_ptr<ov::Model> getCompressedModel(const TransposeBeforeReshapeParams& params) {+ auto input = makeInput(params);+ ov::ParameterVector parameters{input};+ auto weights = makeWeights(params, parameters);+ const auto transpose_order = isRank4(params) ? ov::Shape{1, 0, 2, 3} : ov::Shape{1, 0, 2};+ auto transpose_order_const = ov::op::v0::Constant::create(ov::element::i32,+ ov::Shape{transpose_order.size()},+ transpose_order);+ auto transpose_weights = std::make_shared<ov::op::v1::Transpose>(weights, transpose_order_const);+ const auto compressed_weight_shape = getCompressedWeightShape(params);+ auto reshape_shape = ov::op::v0::Constant::create(ov::element::i32,+ ov::Shape{compressed_weight_shape.size()},+ compressed_weight_shape);+ auto reshape_weights = std::make_shared<ov::op::v1::Reshape>(transpose_weights, reshape_shape, false);++ const auto scale_shape = getScaleShape(params);+ auto scale = ov::op::v0::Constant::create(ov::element::f16, scale_shape, {1});+ const auto compressed_metadata_shape = getCompressedMetadataShape(params);+ auto scale_shape_const = ov::op::v0::Constant::create(ov::element::i32,+ ov::Shape{compressed_metadata_shape.size()},+ compressed_metadata_shape);+ auto reshape_scale = std::make_shared<ov::op::v1::Reshape>(scale, scale_shape_const, false);++ auto no_bias = std::make_shared<ov::intel_gpu::op::Placeholder>();+ std::shared_ptr<ov::intel_gpu::op::FullyConnectedCompressed> fc_compressed;+ if (hasZeroPoint(params)) {+ auto zero_point = ov::op::v0::Constant::create(ov::element::u2, scale_shape, {1});+ auto zero_point_convert = std::make_shared<ov::op::v0::Convert>(zero_point, ov::element::u8);+ auto zero_point_shape_const = ov::op::v0::Constant::create(ov::element::i32,+ ov::Shape{compressed_metadata_shape.size()},+ compressed_metadata_shape);+ auto reshape_zero_point = std::make_shared<ov::op::v1::Reshape>(zero_point_convert,+ zero_point_shape_const,+ false);+ fc_compressed = std::make_shared<ov::intel_gpu::op::FullyConnectedCompressed>(input,+ reshape_weights,+ no_bias,+ reshape_scale,+ reshape_zero_point,+ ov::element::dynamic,+ true,+ false,+ false);+ } else {+ fc_compressed = std::make_shared<ov::intel_gpu::op::FullyConnectedCompressed>(input,+ reshape_weights,+ no_bias,+ reshape_scale,+ ov::element::dynamic,+ true,+ false,+ false);+ }+ return std::make_shared<ov::Model>(ov::OutputVector{fc_compressed}, parameters);+ }+};++TEST_P(ConvertFCToCompressedTransposeBeforeReshapeTest, ConvertsOnlySupportedWeightPatterns) {}++static std::vector<TransposeBeforeReshapeParams> getTransposeBeforeReshapeTestParams() {+ const std::vector<ov::element::Type> weights_types{ov::element::u2,+ ov::element::u4,+ ov::element::i4,+ ov::element::u8,+ ov::element::i8,+ ov::element::f8e4m3,+ ov::element::f8e5m2,+ ov::element::f4e2m1};+ const std::vector<size_t> group_sizes{4, 63, 64, 128};+ std::vector<TransposeBeforeReshapeParams> params;+ for (const auto& weights_type : weights_types) {+ for (const auto parameter_weights : {false, true}) {+ for (const auto group_size : group_sizes) {+ params.push_back({weights_type,+ parameter_weights,+ group_size,+ TransposeBeforeReshapePattern::ScaleOnly});+ }+ }+ }+ params.push_back({ov::element::u2, false, 4, TransposeBeforeReshapePattern::ScaleAndZeroPoint});+ params.push_back({ov::element::u2, false, 2, TransposeBeforeReshapePattern::Rank4ScaleAndZeroPoint});+ return params;+}++INSTANTIATE_TEST_SUITE_P(+ WeightsTypeAndGroupAlignment,+ ConvertFCToCompressedTransposeBeforeReshapeTest,+ testing::ValuesIn(getTransposeBeforeReshapeTestParams()),+ ConvertFCToCompressedTransposeBeforeReshapeTest::getTestCaseName);+} // namespace intel_gpu} // namespace test} // namespace ov