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] Fix axis layout in FC horizontal fusion for transpose_b=false (#37337)
Deepak Bhagat051636abbe67
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 08040899e50..bf69824bf9d 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@@ -127,15 +127,17 @@ FullyConnectedHorizontalFusion::FullyConnectedHorizontalFusion(bool fuse_mlp_swizp_nodes.push_back(fc_user->get_input_node_shared_ptr(4));}}- // fc weight is already transposed to [N, K]+ // Weight layout depends on transpose_b: [N, K] when transpose_b=true (default),+ // [K, N] when transpose_b=false.const size_t weight_idx = 1;if (fc_nodes[0]->get_input_shape(weight_idx).size() != 2)return false;- const size_t n_axis = 0;- const size_t k_axis = 1;+ const bool transpose_b = fc_nodes[0]->get_transpose_b();+ // n_axis is the output (N) dimension, k_axis the contraction (K) dimension.+ const size_t n_axis = transpose_b ? 0 : 1;+ const size_t k_axis = transpose_b ? 1 : 0;auto weight_dtype = fc_nodes[0]->get_input_element_type(weight_idx);auto k_size = fc_nodes[0]->get_input_shape(weight_idx)[k_axis];- const bool transpose_b = fc_nodes[0]->get_transpose_b();std::vector<int64_t> orig_n_sizes;// merge weights, scale, zpfor (auto fc : fc_nodes) {@@ -151,7 +153,7 @@ FullyConnectedHorizontalFusion::FullyConnectedHorizontalFusion(bool fuse_mlp_swifor (size_t i = 0; i < weight_nodes.size(); ++i) {weight_nodes_as_output_vector.push_back(weight_nodes[i]->output(0));}- auto fused_weight = std::make_shared<ov::op::v0::Concat>(weight_nodes_as_output_vector, 0);+ 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);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 80a2664f103..40df8bbe7cf 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@@ -459,6 +459,75 @@ TEST_F(TransformationTestsF, FullyConnectedHorizontalFusion_eltwise_bias_zp_scalcomparator.enable(FunctionsComparator::ATTRIBUTES);}}+TEST_F(TransformationTestsF, FullyConnectedHorizontalFusion_transpose_b_false) {+ // transpose_b=false => weights are in [K, N] layout (opposite of the default [N, K]).+ // Horizontal fusion must concat along the N axis (1) and use the N dims as split sizes,+ // otherwise the fused FC shape-inference throws (contraction dim mismatch).+ std::vector<int64_t> pattern = {7, -1};+ {+ auto input = std::make_shared<ov::op::v0::Parameter>(ov::element::f16, ov::PartialShape{-1, 7, 4096});+ // [K, N] weights: K=4096, N=1024 / 512 / 128+ auto weight1 = std::make_shared<ov::op::v0::Constant>(ov::element::u4, ov::Shape{4096, 1024});+ weight1->set_friendly_name("weight1_1");+ auto weight2 = std::make_shared<ov::op::v0::Constant>(ov::element::u4, ov::Shape{4096, 512});+ weight2->set_friendly_name("weight1_2");+ auto weight3 = std::make_shared<ov::op::v0::Constant>(ov::element::u4, ov::Shape{4096, 128});+ weight3->set_friendly_name("weight1_3");+ auto bias1 = std::make_shared<ov::intel_gpu::op::Placeholder>();+ auto bias2 = std::make_shared<ov::intel_gpu::op::Placeholder>();+ auto bias3 = 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 fc1 = std::make_shared<ov::intel_gpu::op::FullyConnectedCompressed>(input, weight1, bias1, scale1, ov::element::dynamic, false);+ fc1->set_friendly_name("fc1");+ auto fc2 = std::make_shared<ov::intel_gpu::op::FullyConnectedCompressed>(input, weight2, bias2, scale2, ov::element::dynamic, false);+ auto fc3 = std::make_shared<ov::intel_gpu::op::FullyConnectedCompressed>(input, weight3, bias3, scale3, ov::element::dynamic, false);+ 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, 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 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 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};+ auto split_const = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{3}, orig_n_sizes);+ 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);+ }+}+} // namespace intel_gpu} // namespace test} // namespace ov\ No newline at end of file