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] Stop Horizontal FC Fusion if concat folding is not possible (#38261)
Vladislav Golubev020cbe9ba612
diff --git a/src/plugins/intel_gpu/src/plugin/transformations/fc_horizontal_fusion.cpp b/src/plugins/intel_gpu/src/plugin/transformations/fc_horizontal_fusion.cppindex 4fb95ad592a..4e2d0009bd6 100644--- a/src/plugins/intel_gpu/src/plugin/transformations/fc_horizontal_fusion.cpp+++ b/src/plugins/intel_gpu/src/plugin/transformations/fc_horizontal_fusion.cpp@@ -4,12 +4,15 @@#include "fc_horizontal_fusion.hpp"+#include <string_view>+#include "intel_gpu/op/fully_connected.hpp"#include "intel_gpu/op/fully_connected_compressed.hpp"#include "intel_gpu/op/placeholder.hpp"#include "intel_gpu/runtime/debug_configuration.hpp"#include "openvino/core/graph_util.hpp"#include "openvino/core/rt_info.hpp"+#include "openvino/core/validation_util.hpp"#include "openvino/op/add.hpp"#include "openvino/op/concat.hpp"#include "openvino/op/multiply.hpp"@@ -105,10 +108,10 @@ FullyConnectedHorizontalFusion::FullyConnectedHorizontalFusion(bool fuse_mlp_swiauto input_node = m_fc->get_input_node_shared_ptr(0);std::vector<std::shared_ptr<op::FullyConnectedCompressed>> fc_nodes;ov::NodeVector fc_nodes_vec;- ov::NodeVector weight_nodes;- ov::NodeVector scale_nodes;- ov::NodeVector bias_nodes;- ov::NodeVector zp_nodes;+ ov::OutputVector weight_nodes;+ ov::OutputVector scale_nodes;+ ov::OutputVector bias_nodes;+ ov::OutputVector zp_nodes;int32_t bias_rank = -1;for (auto user : input_node->get_users()) {auto fc_user = ov::as_type_ptr<op::FullyConnectedCompressed>(user);@@ -116,7 +119,7 @@ FullyConnectedHorizontalFusion::FullyConnectedHorizontalFusion(bool fuse_mlp_swiOPENVINO_ASSERT(fc_user->inputs().size() >= 4, "Compressed FC should have at least 4 inputs");fc_nodes.push_back(fc_user);fc_nodes_vec.push_back(fc_user);- weight_nodes.push_back(fc_user->get_input_node_shared_ptr(1));+ weight_nodes.push_back(fc_user->input_value(1));if (!ov::as_type_ptr<op::Placeholder>(fc_user->get_input_node_shared_ptr(2))) {if (bias_rank == -1) {bias_rank = static_cast<int32_t>(fc_user->get_input_partial_shape(2).size());@@ -124,11 +127,11 @@ FullyConnectedHorizontalFusion::FullyConnectedHorizontalFusion(bool fuse_mlp_swiif (bias_rank != static_cast<int32_t>(fc_user->get_input_partial_shape(2).size())) {return false;}- bias_nodes.push_back(fc_user->get_input_node_shared_ptr(2));+ bias_nodes.push_back(fc_user->input_value(2));}- scale_nodes.push_back(fc_user->get_input_node_shared_ptr(3));+ scale_nodes.push_back(fc_user->input_value(3));if (fc_user->inputs().size() > 4) {- zp_nodes.push_back(fc_user->get_input_node_shared_ptr(4));+ zp_nodes.push_back(fc_user->input_value(4));}}}@@ -158,22 +161,75 @@ FullyConnectedHorizontalFusion::FullyConnectedHorizontalFusion(bool fuse_mlp_swi}orig_n_sizes.push_back(fc->get_input_shape(weight_idx)[n_axis]);}- ov::OutputVector weight_nodes_as_output_vector;- for (size_t i = 0; i < weight_nodes.size(); ++i) {- weight_nodes_as_output_vector.push_back(weight_nodes[i]->output(0));+ // Concatenates and folds the given constant-like nodes into a Constant right away, instead of relying on ConstantFolding pass:+ // unfolded Concat on weights (e.g. in case of unsupported precision) may then fail the pipeline if the GPU also doesn't support this.+ // So if the concatenation at ov::Model is not possible, the transformation just returns false+ auto concat_and_fold = [](const ov::OutputVector& outputs, int64_t axis, std::string_view name_suffix) -> std::shared_ptr<ov::Node> {+ auto concat = std::make_shared<ov::op::v0::Concat>(outputs, axis);+ auto folded = ov::util::get_constant_from_source(concat);+ if (!folded) {+ GPU_DEBUG_TRACE_DETAIL << "FullyConnectedHorizontalFusion: failed to constant-fold the concat of " << name_suffix << std::endl;+ return nullptr;+ }+ const auto nodes = ov::as_node_vector(outputs);+ folded->set_friendly_name(nodes[0]->get_friendly_name() + std::string(name_suffix));+ ov::copy_runtime_info(nodes, folded);+ return folded;+ };++ // Fold weights and scales before the graph is modified below, so that bailing out leaves the model intact+ auto fused_weight = concat_and_fold(weight_nodes, transpose_b ? 0 : 1, "_fused_weight");+ if (!fused_weight) {+ return false;}- auto fused_weight = std::make_shared<ov::op::v0::Concat>(weight_nodes_as_output_vector, transpose_b ? 0 : 1);- fused_weight->set_friendly_name(weight_nodes[0]->get_friendly_name() + "_fused_weight");- ov::copy_runtime_info(weight_nodes, fused_weight);- ov::OutputVector scales_as_output_vector;- for (size_t i = 0; i < scale_nodes.size(); ++i) {- scales_as_output_vector.push_back(scale_nodes[i]->output(0));+ auto fused_scale = concat_and_fold(scale_nodes, 0, "_fused_scale");+ if (!fused_scale) {+ return false;}- auto fused_scale = std::make_shared<ov::op::v0::Concat>(scales_as_output_vector, 0);- fused_scale->set_friendly_name(scale_nodes[0]->get_friendly_name() + "_fused_scale");- ov::copy_runtime_info(scale_nodes, fused_scale);+ std::shared_ptr<ov::Node> fused_zps;+ if (!zp_nodes.empty()) {+ bool single_zp_value = (ov::shape_size(zp_nodes[0].get_shape()) == 1);+ int32_t scalar_zp_val = 0;+ if (single_zp_value) {+ if (auto zp_const = ov::as_type_ptr<ov::op::v0::Constant>(zp_nodes[0].get_node_shared_ptr())) {+ scalar_zp_val = zp_const->cast_vector<int32_t>()[0];+ } else if (auto zp_convert = ov::as_type_ptr<ov::op::v0::Convert>(zp_nodes[0].get_node_shared_ptr())) {+ auto zp_const = ov::as_type_ptr<ov::op::v0::Constant>(zp_convert->get_input_node_shared_ptr(0));+ scalar_zp_val = zp_const->cast_vector<int32_t>()[0];+ }+ fused_zps = zp_nodes[0].get_node_shared_ptr();+ }+ if (single_zp_value) {+ for (size_t i = 1; i < zp_nodes.size(); ++i) {+ bool current_is_scalar = (ov::shape_size(zp_nodes[i].get_shape()) == 1);+ if (!current_is_scalar) {+ return false;+ }+ // validate all zp values are same+ int32_t cur_zp_val = 0;+ if (auto zp_const = ov::as_type_ptr<ov::op::v0::Constant>(zp_nodes[i].get_node_shared_ptr())) {+ cur_zp_val = zp_const->cast_vector<int32_t>()[0];+ } else if (auto zp_convert = ov::as_type_ptr<ov::op::v0::Convert>(zp_nodes[i].get_node_shared_ptr())) {+ auto zp_const = ov::as_type_ptr<ov::op::v0::Constant>(zp_convert->get_input_node_shared_ptr(0));+ cur_zp_val = zp_const->cast_vector<int32_t>()[0];+ } else {+ OPENVINO_THROW("Unsupported zp input node for FC horizontal fusion");+ }+ if (cur_zp_val != scalar_zp_val) {+ return false;+ }+ }+ } else {+ fused_zps = concat_and_fold(zp_nodes, 0, "_fused_zps");+ if (!fused_zps) {+ return false;+ }+ }+ }++ // Biases are processed last, as only here the graph gets modified: all the checks that may decline the fusion are done by now// check if the FCs do not have bias inputs, but all of the fc has a bias add user, set them as bias inputs// Currently horizontal fusing is applied only when fusing is applied for N dim// Also, fuse biases for the last dimension too, if@@ -181,6 +237,7 @@ FullyConnectedHorizontalFusion::FullyConnectedHorizontalFusion(bool fuse_mlp_swi// - Rank of the bias shapes are same// - all other dims except last dim is 1 (e.g., [1, 1, N])size_t n_bias_users = 0;+ bool bias_from_add_users = false;if (bias_nodes.empty()) {for (auto fc : fc_nodes) {if (fc->get_users().size() == 1 && fc->get_users()[0]->get_type_info() == ov::opset1::Add::get_type_info_static() &&@@ -205,92 +262,48 @@ FullyConnectedHorizontalFusion::FullyConnectedHorizontalFusion(bool fuse_mlp_swifor (size_t i = 0; i < fc_nodes.size(); ++i) {auto orig_fc = fc_nodes[i];auto bias_node = orig_fc->get_users()[0];- auto bias_const_ptr = orig_fc->get_users()[0]->get_input_node_shared_ptr(1);- bias_nodes.push_back(bias_const_ptr);- }- for (size_t i = 0; i < fc_nodes.size(); ++i) {- auto orig_fc = fc_nodes[i];- auto bias_node = orig_fc->get_users()[0];- GPU_DEBUG_TRACE_DETAIL << "Set Add op user " << bias_node->get_friendly_name() << " as the FC " << orig_fc->get_friendly_name()- << "'s bias input" << std::endl;- auto bias_const = orig_fc->get_users()[0]->input_value(1);- auto orig_users_of_bias_user = bias_node->get_users();- ov::OutputVector fc_inputs = orig_fc->input_values();- fc_inputs[2] = bias_const;- auto new_fc = orig_fc->clone_with_new_inputs(fc_inputs);- new_fc->set_friendly_name(orig_fc->get_friendly_name() + "_with_bias");- ov::copy_runtime_info(orig_fc, new_fc);- for (auto u : orig_users_of_bias_user) {- for (size_t idx = 0; idx < u->inputs().size(); ++idx) {- if (u->get_input_node_shared_ptr(idx) == bias_node) {- u->input(idx).replace_source_output(new_fc->output(0));- }- }- }- fc_nodes[i] = ov::as_type_ptr<op::FullyConnectedCompressed>(new_fc);- bias_node->clear_control_dependencies();- orig_fc->clear_control_dependencies();+ bias_nodes.push_back(bias_node->input_value(1));}+ bias_from_add_users = true;}}std::shared_ptr<ov::Node> fused_bias;if (bias_nodes.size() == fc_nodes.size()) {- ov::OutputVector bias_nodes_as_output_vector;- for (size_t i = 0; i < bias_nodes.size(); ++i) {- bias_nodes_as_output_vector.push_back(bias_nodes[i]->output(0));+ fused_bias = concat_and_fold(bias_nodes, bias_rank - 1, "_fused_bias");+ if (!fused_bias) {+ return false;}- fused_bias = std::make_shared<ov::op::v0::Concat>(bias_nodes_as_output_vector, bias_rank - 1);- fused_bias->set_friendly_name(bias_nodes[0]->get_friendly_name() + "_fused_bias");- ov::copy_runtime_info(bias_nodes, fused_bias);} else {fused_bias = std::make_shared<op::Placeholder>();}- std::shared_ptr<ov::Node> fused_zps;- if (!zp_nodes.empty()) {- // scalar zp- auto zp_shape = zp_nodes[0]->get_output_shape(0);- bool is_scalar = (ov::shape_size(zp_nodes[0]->get_output_shape(0)) == 1);- int32_t scalar_zp_val = 0;- if (is_scalar) {- if (auto zp_const = ov::as_type_ptr<ov::op::v0::Constant>(zp_nodes[0])) {- scalar_zp_val = zp_const->cast_vector<int32_t>()[0];- } else if (auto zp_convert = ov::as_type_ptr<ov::op::v0::Convert>(zp_nodes[0])) {- auto zp_const = ov::as_type_ptr<ov::op::v0::Constant>(zp_convert->get_input_node_shared_ptr(0));- scalar_zp_val = zp_const->cast_vector<int32_t>()[0];- }- fused_zps = zp_nodes[0];- }- if (is_scalar) {- for (size_t i = 1; i < zp_nodes.size(); ++i) {- bool current_is_scalar = (ov::shape_size(zp_nodes[i]->get_output_shape(0)) == 1);- if (!current_is_scalar) {- return false;- }- // validate all zp values are same- int32_t cur_zp_val = 0;- if (auto zp_const = ov::as_type_ptr<ov::op::v0::Constant>(zp_nodes[i])) {- cur_zp_val = zp_const->cast_vector<int32_t>()[0];- } else if (auto zp_convert = ov::as_type_ptr<ov::op::v0::Convert>(zp_nodes[i])) {- auto zp_const = ov::as_type_ptr<ov::op::v0::Constant>(zp_convert->get_input_node_shared_ptr(0));- cur_zp_val = zp_const->cast_vector<int32_t>()[0];- } else {- OPENVINO_THROW("Unsupported zp input node for FC horizontal fusion");- }- if (cur_zp_val != scalar_zp_val) {- return false;+ if (bias_from_add_users) {+ for (size_t i = 0; i < fc_nodes.size(); ++i) {+ auto orig_fc = fc_nodes[i];+ auto bias_node = orig_fc->get_users()[0];+ GPU_DEBUG_TRACE_DETAIL << "Set Add op user " << bias_node->get_friendly_name() << " as the FC " << orig_fc->get_friendly_name()+ << "'s bias input" << std::endl;+ auto bias_const = orig_fc->get_users()[0]->input_value(1);+ auto orig_users_of_bias_user = bias_node->get_users();+ ov::OutputVector fc_inputs = orig_fc->input_values();+ fc_inputs[2] = bias_const;+ auto new_fc = orig_fc->clone_with_new_inputs(fc_inputs);+ new_fc->set_friendly_name(orig_fc->get_friendly_name() + "_with_bias");+ ov::copy_runtime_info(orig_fc, new_fc);+ for (auto u : orig_users_of_bias_user) {+ for (size_t idx = 0; idx < u->inputs().size(); ++idx) {+ if (u->get_input_node_shared_ptr(idx) == bias_node) {+ u->input(idx).replace_source_output(new_fc->output(0));+ }}}- } else {- ov::OutputVector zp_nodes_as_output_vector;- for (size_t i = 0; i < zp_nodes.size(); ++i) {- zp_nodes_as_output_vector.push_back(zp_nodes[i]->output(0));- }- fused_zps = std::make_shared<ov::op::v0::Concat>(zp_nodes_as_output_vector, 0);- fused_zps->set_friendly_name(zp_nodes[0]->get_friendly_name() + "_fused_zps");+ fc_nodes[i] = ov::as_type_ptr<op::FullyConnectedCompressed>(new_fc);+ bias_node->clear_control_dependencies();+ orig_fc->clear_control_dependencies();}}+// Create new fc with merged weights, bias, scale, zpstd::shared_ptr<ov::Node> new_fc;if (fused_zps) {diff --git a/src/plugins/intel_gpu/tests/unit/transformations/horizontal_fc_fusion_test.cpp b/src/plugins/intel_gpu/tests/unit/transformations/horizontal_fc_fusion_test.cppindex 40df8bbe7cf..0b7abe4be61 100644--- a/src/plugins/intel_gpu/tests/unit/transformations/horizontal_fc_fusion_test.cpp+++ b/src/plugins/intel_gpu/tests/unit/transformations/horizontal_fc_fusion_test.cpp@@ -66,20 +66,9 @@ TEST_F(TransformationTestsF, FullyConnectedHorizontalFusion_no_bias_no_zp) {}{auto input = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::PartialShape{-1, 7, 4096});- auto weight1 = std::make_shared<ov::op::v0::Constant>(ov::element::u4, ov::Shape{1024, 4096});- weight1->set_friendly_name("weight2_1");- auto weight2 = std::make_shared<ov::op::v0::Constant>(ov::element::u4, ov::Shape{512, 4096});- weight2->set_friendly_name("weight2_2");- auto weight3 = std::make_shared<ov::op::v0::Constant>(ov::element::u4, ov::Shape{128, 4096});- weight3->set_friendly_name("weight2_3");- auto weights = ov::OutputVector{weight1, weight2, weight3};- auto weight_fused = std::make_shared<ov::op::v0::Concat>(weights, 0);+ auto weight_fused = std::make_shared<ov::op::v0::Constant>(ov::element::u4, ov::Shape{1664, 4096});auto bias1 = std::make_shared<ov::intel_gpu::op::Placeholder>();- auto scale1 = std::make_shared<ov::op::v0::Constant>(ov::element::f16, ov::Shape{1024, 32});- auto scale2 = std::make_shared<ov::op::v0::Constant>(ov::element::f16, ov::Shape{512, 32});- auto scale3 = std::make_shared<ov::op::v0::Constant>(ov::element::f16, ov::Shape{128, 32});- auto scales = ov::OutputVector{scale1, scale2, scale3};- auto scale_fused = std::make_shared<ov::op::v0::Concat>(scales, 0);+ auto scale_fused = std::make_shared<ov::op::v0::Constant>(ov::element::f16, ov::Shape{1664, 32});auto fc_fused = std::make_shared<ov::intel_gpu::op::FullyConnectedCompressed>(input, weight_fused, bias1, scale_fused);auto axis_const = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{1}, {fc_fused->get_output_partial_shape(0).size() - 1});std::vector<int64_t> orig_n_sizes = {1024, 512, 128};@@ -254,24 +243,9 @@ TEST_F(TransformationTestsF, FullyConnectedHorizontalFusion_bias_no_zp) {}{auto input = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::PartialShape{-1, 7, 4096});- auto weight1 = std::make_shared<ov::op::v0::Constant>(ov::element::u4, ov::Shape{1024, 4096});- weight1->set_friendly_name("weight2_1");- auto weight2 = std::make_shared<ov::op::v0::Constant>(ov::element::u4, ov::Shape{512, 4096});- weight2->set_friendly_name("weight2_2");- auto weight3 = std::make_shared<ov::op::v0::Constant>(ov::element::u4, ov::Shape{128, 4096});- weight3->set_friendly_name("weight2_3");- auto weights = ov::OutputVector{weight1, weight2, weight3};- auto weight_fused = std::make_shared<ov::op::v0::Concat>(weights, 0);- auto bias1 = std::make_shared<ov::op::v0::Constant>(ov::element::f16, ov::Shape{1, 1024});- auto bias2 = std::make_shared<ov::op::v0::Constant>(ov::element::f16, ov::Shape{1, 512});- auto bias3 = std::make_shared<ov::op::v0::Constant>(ov::element::f16, ov::Shape{1, 128});- auto biases = ov::OutputVector{bias1, bias2, bias3};- auto bias_fused = std::make_shared<ov::op::v0::Concat>(biases, 1);- auto scale1 = std::make_shared<ov::op::v0::Constant>(ov::element::f16, ov::Shape{1024, 32});- auto scale2 = std::make_shared<ov::op::v0::Constant>(ov::element::f16, ov::Shape{512, 32});- auto scale3 = std::make_shared<ov::op::v0::Constant>(ov::element::f16, ov::Shape{128, 32});- auto scales = ov::OutputVector{scale1, scale2, scale3};- auto scale_fused = std::make_shared<ov::op::v0::Concat>(scales, 0);+ auto weight_fused = std::make_shared<ov::op::v0::Constant>(ov::element::u4, ov::Shape{1664, 4096});+ auto bias_fused = std::make_shared<ov::op::v0::Constant>(ov::element::f16, ov::Shape{1, 1664});+ auto scale_fused = std::make_shared<ov::op::v0::Constant>(ov::element::f16, ov::Shape{1664, 32});auto fc_fused = std::make_shared<ov::intel_gpu::op::FullyConnectedCompressed>(input, weight_fused, bias_fused, scale_fused);auto axis_const = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{1}, {fc_fused->get_output_partial_shape(0).size() - 1});std::vector<int64_t> orig_n_sizes = {1024, 512, 128};@@ -333,24 +307,9 @@ TEST_F(TransformationTestsF, FullyConnectedHorizontalFusion_eltwise_bias_zp) {}{auto input = std::make_shared<ov::op::v0::Parameter>(ov::element::f16, ov::PartialShape{-1, 7, 4096});- auto weight1 = std::make_shared<ov::op::v0::Constant>(ov::element::u4, ov::Shape{1024, 4096});- weight1->set_friendly_name("weight2_1");- auto weight2 = std::make_shared<ov::op::v0::Constant>(ov::element::u4, ov::Shape{512, 4096});- weight2->set_friendly_name("weight2_2");- auto weight3 = std::make_shared<ov::op::v0::Constant>(ov::element::u4, ov::Shape{128, 4096});- weight3->set_friendly_name("weight2_3");- auto weights = ov::OutputVector{weight1, weight2, weight3};- auto weight_fused = std::make_shared<ov::op::v0::Concat>(weights, 0);- auto bias1 = std::make_shared<ov::op::v0::Constant>(ov::element::f16, ov::Shape{1, 1024});- auto bias2 = std::make_shared<ov::op::v0::Constant>(ov::element::f16, ov::Shape{1, 512});- auto bias3 = std::make_shared<ov::op::v0::Constant>(ov::element::f16, ov::Shape{1, 128});- auto biases = ov::OutputVector{bias1, bias2, bias3};- auto bias_fused = std::make_shared<ov::op::v0::Concat>(biases, 1);- auto scale1 = std::make_shared<ov::op::v0::Constant>(ov::element::f16, ov::Shape{1024, 32});- auto scale2 = std::make_shared<ov::op::v0::Constant>(ov::element::f16, ov::Shape{512, 32});- auto scale3 = std::make_shared<ov::op::v0::Constant>(ov::element::f16, ov::Shape{128, 32});- auto scales = ov::OutputVector{scale1, scale2, scale3};- auto scale_fused = std::make_shared<ov::op::v0::Concat>(scales, 0);+ auto weight_fused = std::make_shared<ov::op::v0::Constant>(ov::element::u4, ov::Shape{1664, 4096});+ auto bias_fused = std::make_shared<ov::op::v0::Constant>(ov::element::f16, ov::Shape{1, 1664});+ auto scale_fused = std::make_shared<ov::op::v0::Constant>(ov::element::f16, ov::Shape{1664, 32});auto fc_fused = std::make_shared<ov::intel_gpu::op::FullyConnectedCompressed>(input, weight_fused, bias_fused, scale_fused);auto axis_const = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{1}, {fc_fused->get_output_partial_shape(0).size() - 1});std::vector<int64_t> orig_n_sizes = {1024, 512, 128};@@ -422,24 +381,9 @@ TEST_F(TransformationTestsF, FullyConnectedHorizontalFusion_eltwise_bias_zp_scal}{auto input = std::make_shared<ov::op::v0::Parameter>(ov::element::f16, ov::PartialShape{-1, 7, 4096});- auto weight1 = std::make_shared<ov::op::v0::Constant>(ov::element::u4, ov::Shape{1024, 4096});- weight1->set_friendly_name("weight2_1");- auto weight2 = std::make_shared<ov::op::v0::Constant>(ov::element::u4, ov::Shape{512, 4096});- weight2->set_friendly_name("weight2_2");- auto weight3 = std::make_shared<ov::op::v0::Constant>(ov::element::u4, ov::Shape{128, 4096});- weight3->set_friendly_name("weight2_3");- auto weights = ov::OutputVector{weight1, weight2, weight3};- auto weight_fused = std::make_shared<ov::op::v0::Concat>(weights, 0);- auto bias1 = std::make_shared<ov::op::v0::Constant>(ov::element::f16, ov::Shape{1, 1024});- auto bias2 = std::make_shared<ov::op::v0::Constant>(ov::element::f16, ov::Shape{1, 512});- auto bias3 = std::make_shared<ov::op::v0::Constant>(ov::element::f16, ov::Shape{1, 128});- auto biases = ov::OutputVector{bias1, bias2, bias3};- auto bias_fused = std::make_shared<ov::op::v0::Concat>(biases, 1);- auto scale1 = std::make_shared<ov::op::v0::Constant>(ov::element::f16, ov::Shape{1024, 32});- auto scale2 = std::make_shared<ov::op::v0::Constant>(ov::element::f16, ov::Shape{512, 32});- auto scale3 = std::make_shared<ov::op::v0::Constant>(ov::element::f16, ov::Shape{128, 32});- auto scales = ov::OutputVector{scale1, scale2, scale3};- auto scale_fused = std::make_shared<ov::op::v0::Concat>(scales, 0);+ auto weight_fused = std::make_shared<ov::op::v0::Constant>(ov::element::u4, ov::Shape{1664, 4096});+ auto bias_fused = std::make_shared<ov::op::v0::Constant>(ov::element::f16, ov::Shape{1, 1664});+ auto scale_fused = std::make_shared<ov::op::v0::Constant>(ov::element::f16, ov::Shape{1664, 32});auto fc_fused = std::make_shared<ov::intel_gpu::op::FullyConnectedCompressed>(input, weight_fused, bias_fused, scale_fused);const std::vector<float> scale_factor = {8.f};auto mul_input = std::make_shared<ov::op::v0::Constant>(ov::element::f16, ov::Shape{}, scale_factor);@@ -496,21 +440,9 @@ TEST_F(TransformationTestsF, FullyConnectedHorizontalFusion_transpose_b_false) {{auto input = std::make_shared<ov::op::v0::Parameter>(ov::element::f16, ov::PartialShape{-1, 7, 4096});// [K, N] weights- auto weight1 = std::make_shared<ov::op::v0::Constant>(ov::element::u4, ov::Shape{4096, 1024});- weight1->set_friendly_name("weight2_1");- auto weight2 = std::make_shared<ov::op::v0::Constant>(ov::element::u4, ov::Shape{4096, 512});- weight2->set_friendly_name("weight2_2");- auto weight3 = std::make_shared<ov::op::v0::Constant>(ov::element::u4, ov::Shape{4096, 128});- weight3->set_friendly_name("weight2_3");- auto weights = ov::OutputVector{weight1, weight2, weight3};- // concat along N axis (1) for [K, N] layout- auto weight_fused = std::make_shared<ov::op::v0::Concat>(weights, 1);+ auto weight_fused = std::make_shared<ov::op::v0::Constant>(ov::element::u4, ov::Shape{4096, 1664});auto bias1 = std::make_shared<ov::intel_gpu::op::Placeholder>();- auto scale1 = std::make_shared<ov::op::v0::Constant>(ov::element::f16, ov::Shape{1024, 32});- auto scale2 = std::make_shared<ov::op::v0::Constant>(ov::element::f16, ov::Shape{512, 32});- auto scale3 = std::make_shared<ov::op::v0::Constant>(ov::element::f16, ov::Shape{128, 32});- auto scales = ov::OutputVector{scale1, scale2, scale3};- auto scale_fused = std::make_shared<ov::op::v0::Concat>(scales, 0);+ auto scale_fused = std::make_shared<ov::op::v0::Constant>(ov::element::f16, ov::Shape{1664, 32});auto fc_fused = std::make_shared<ov::intel_gpu::op::FullyConnectedCompressed>(input, weight_fused, bias1, scale_fused, ov::element::dynamic, false);auto axis_const = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{1}, {fc_fused->get_output_partial_shape(0).size() - 1});std::vector<int64_t> orig_n_sizes = {1024, 512, 128};@@ -528,6 +460,115 @@ TEST_F(TransformationTestsF, FullyConnectedHorizontalFusion_transpose_b_false) {}}+TEST_F(TransformationTestsF, FullyConnectedHorizontalFusion_u3_weights_byte_packed) {+ // u3 weights are byte-packed, so the fused weight must preserve the original packed data+ const std::vector<int64_t> pattern = {7, -1};+ auto make_weight = [](const ov::Shape& shape, const std::vector<uint8_t>& bytes) {+ return std::make_shared<ov::op::v0::Constant>(ov::element::u3, shape, bytes.data());+ };+ auto make_scale = [](size_t output_channels) {+ return std::make_shared<ov::op::v0::Constant>(ov::element::f16, ov::Shape{output_channels, 1});+ };+ {+ auto input = std::make_shared<ov::op::v0::Parameter>(ov::element::f16, ov::PartialShape{-1, 7, 8});+ auto weight1 = make_weight(ov::Shape{2, 8}, {0x11, 0x22, 0x33, 0x44, 0x55, 0x66});+ auto weight2 = make_weight(ov::Shape{3, 8}, {0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF});+ auto weight3 = make_weight(ov::Shape{1, 8}, {0x01, 0x02, 0x03});+ auto fc1 = std::make_shared<ov::intel_gpu::op::FullyConnectedCompressed>(input,+ weight1,+ std::make_shared<ov::intel_gpu::op::Placeholder>(),+ make_scale(2));+ auto fc2 = std::make_shared<ov::intel_gpu::op::FullyConnectedCompressed>(input,+ weight2,+ std::make_shared<ov::intel_gpu::op::Placeholder>(),+ make_scale(3));+ auto fc3 = std::make_shared<ov::intel_gpu::op::FullyConnectedCompressed>(input,+ weight3,+ std::make_shared<ov::intel_gpu::op::Placeholder>(),+ make_scale(1));+ auto reshape_pattern = std::make_shared<ov::op::v0::Constant>(ov::element::i64, ov::Shape{2}, pattern);+ auto reshape1 = std::make_shared<ov::op::v1::Reshape>(fc1, reshape_pattern, true);+ auto reshape2 = std::make_shared<ov::op::v1::Reshape>(fc2, reshape_pattern, true);+ auto reshape3 = std::make_shared<ov::op::v1::Reshape>(fc3, reshape_pattern, true);+ auto result1 = std::make_shared<ov::op::v0::Result>(reshape1);+ auto result2 = std::make_shared<ov::op::v0::Result>(reshape2);+ auto result3 = std::make_shared<ov::op::v0::Result>(reshape3);+ model = std::make_shared<ov::Model>(ov::ResultVector{result1, result2, result3}, ov::ParameterVector{input});+ manager.register_pass<FullyConnectedHorizontalFusion>();+ }+ {+ auto input = std::make_shared<ov::op::v0::Parameter>(ov::element::f16, ov::PartialShape{-1, 7, 8});+ auto fused_weight = make_weight(ov::Shape{6, 8},+ {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99,+ 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x01, 0x02, 0x03});+ auto fc_fused = std::make_shared<ov::intel_gpu::op::FullyConnectedCompressed>(input,+ fused_weight,+ std::make_shared<ov::intel_gpu::op::Placeholder>(),+ make_scale(6));+ auto axis_const = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{1}, {2});+ auto split_const = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{3}, {2, 3, 1});+ auto split = std::make_shared<ov::op::v1::VariadicSplit>(fc_fused, axis_const, split_const);+ auto reshape_pattern = std::make_shared<ov::op::v0::Constant>(ov::element::i64, ov::Shape{2}, pattern);+ auto reshape1 = std::make_shared<ov::op::v1::Reshape>(split->output(0), reshape_pattern, true);+ auto reshape2 = std::make_shared<ov::op::v1::Reshape>(split->output(1), reshape_pattern, true);+ auto reshape3 = std::make_shared<ov::op::v1::Reshape>(split->output(2), reshape_pattern, true);+ auto result1 = std::make_shared<ov::op::v0::Result>(reshape1);+ auto result2 = std::make_shared<ov::op::v0::Result>(reshape2);+ auto result3 = std::make_shared<ov::op::v0::Result>(reshape3);+ model_ref = std::make_shared<ov::Model>(ov::ResultVector{result1, result2, result3}, ov::ParameterVector{input});+ }+ comparator.enable(FunctionsComparator::ATTRIBUTES);+ comparator.enable(FunctionsComparator::CONST_VALUES);+}++TEST_F(TransformationTestsF, FullyConnectedHorizontalFusion_u3_misaligned_no_fusion) {+ // With K=4 each u3 row occupies 12 bits, so Concat along N (or along the zp's first axis) is not byte aligned and+ // Concat::evaluate fails. The fused constant can't be folded, hence the transformation must bail out and leave+ // the FCs untouched. Two independent groups of 3 FCs cover different failure points:+ // 1. u3 weights: the weights folding fails.+ // 2. u4 weights with u3 zero points and bias Adds: the zero points folding fails after the biases are collected.+ // The bias Adds must not be bypassed, as the pass has to leave the model intact when it returns false.+ auto make_model = []() {+ ov::ResultVector results;+ ov::ParameterVector params;+ {+ auto input = std::make_shared<ov::op::v0::Parameter>(ov::element::f16, ov::PartialShape{-1, 7, 4});+ params.push_back(input);+ for (size_t n : {2, 3, 1}) {+ auto weight = std::make_shared<ov::op::v0::Constant>(ov::element::u3, ov::Shape{n, 4});+ auto scale = std::make_shared<ov::op::v0::Constant>(ov::element::f16, ov::Shape{n, 1});+ auto fc = std::make_shared<ov::intel_gpu::op::FullyConnectedCompressed>(input,+ weight,+ std::make_shared<ov::intel_gpu::op::Placeholder>(),+ scale);+ results.push_back(std::make_shared<ov::op::v0::Result>(fc));+ }+ }+ {+ auto input = std::make_shared<ov::op::v0::Parameter>(ov::element::f16, ov::PartialShape{-1, 7, 4096});+ params.push_back(input);+ for (size_t n : {1024, 512, 128}) {+ auto weight = std::make_shared<ov::op::v0::Constant>(ov::element::u4, ov::Shape{n, 4096});+ auto scale = std::make_shared<ov::op::v0::Constant>(ov::element::f16, ov::Shape{n, 32});+ auto zp = std::make_shared<ov::op::v0::Constant>(ov::element::u3, ov::Shape{1, 1, 4});+ auto fc = std::make_shared<ov::intel_gpu::op::FullyConnectedCompressed>(input,+ weight,+ std::make_shared<ov::intel_gpu::op::Placeholder>(),+ scale,+ zp);+ auto add_input = std::make_shared<ov::op::v0::Constant>(ov::element::f16, ov::Shape{1, n});+ auto add = std::make_shared<ov::op::v1::Add>(fc, add_input);+ results.push_back(std::make_shared<ov::op::v0::Result>(add));+ }+ }+ return std::make_shared<ov::Model>(results, params);+ };+ model = make_model();+ model_ref = make_model();+ manager.register_pass<FullyConnectedHorizontalFusion>();+ comparator.enable(FunctionsComparator::ATTRIBUTES);+}+} // namespace intel_gpu} // namespace test} // namespace ov\ No newline at end of file