Skip to content

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

[GPU] Prevent converting MatMul to FC when rank_a less than rank_b (#35702)

David Namfb31377873bb
diff --git a/src/plugins/intel_gpu/src/plugin/transformations/convert_matmul_to_fc.cpp b/src/plugins/intel_gpu/src/plugin/transformations/convert_matmul_to_fc.cpp
index bad64ce1489..87ff5642291 100644
--- a/src/plugins/intel_gpu/src/plugin/transformations/convert_matmul_to_fc.cpp
+++ b/src/plugins/intel_gpu/src/plugin/transformations/convert_matmul_to_fc.cpp
@@ -89,6 +89,12 @@ ConvertMatMulToFullyConnected::ConvertMatMulToFullyConnected(bool supports_immad
auto rank_a = shape_a.rank().get_length();
auto rank_b = shape_b.rank().get_length();
+ // The fully_connected primitive does not support this situation (rank_a < rank_b).
+ // So, we need to choose GEMM instead of fully_connected.
+ if (rank_a < rank_b) {
+ return false;
+ }
+
/*
* get_aligned_shapes function align two input shapes to have the same size and
* the same batch dimensions (last two dimensions are not comparable).
diff --git a/src/plugins/intel_gpu/tests/unit/transformations/convert_matmul_to_fc_test.cpp b/src/plugins/intel_gpu/tests/unit/transformations/convert_matmul_to_fc_test.cpp
index 30f71d07ff3..08dd74373b0 100644
--- a/src/plugins/intel_gpu/tests/unit/transformations/convert_matmul_to_fc_test.cpp
+++ b/src/plugins/intel_gpu/tests/unit/transformations/convert_matmul_to_fc_test.cpp
@@ -728,3 +728,22 @@ TEST_F(TransformationTestsF, ConvertMatMulToFullyConnectedExceptionTest) {
model_ref = std::make_shared<ov::Model>(ov::OutputVector{matmul1, matmul2}, ov::ParameterVector{input1});
}
}
+
+TEST_F(TransformationTestsF, ConvertMatMulToFullyConnectedTest_rank_a_less_than_rank_b) {
+ {
+ auto activation = std::make_shared<ov::opset1::Parameter>(ov::element::f32, ov::Shape{1, 128});
+ auto weights = ov::opset1::Constant::create(ov::element::f32, ov::Shape{1, 1, 256, 128}, {1.0f});
+ auto matmul = std::make_shared<ov::opset1::MatMul>(activation, weights, false, true);
+
+ model = std::make_shared<ov::Model>(ov::OutputVector{matmul}, ov::ParameterVector{activation});
+ manager.register_pass<ConvertMatMulToFullyConnected>(true); // supports_immad=true
+ }
+ {
+ // Expected: MatMul remains unchanged (rank_a=2 < rank_b=4 rejected by rank check)
+ auto activation = std::make_shared<ov::opset1::Parameter>(ov::element::f32, ov::Shape{1, 128});
+ auto weights = ov::opset1::Constant::create(ov::element::f32, ov::Shape{1, 1, 256, 128}, {1.0f});
+ auto matmul = std::make_shared<ov::opset1::MatMul>(activation, weights, false, true);
+
+ model_ref = std::make_shared<ov::Model>(ov::OutputVector{matmul}, ov::ParameterVector{activation});
+ }
+}