Skip to content

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

[Transformations] Update MatMulConstTransposesExtraction pattern to match batched MatMul (#32378)

Katarzyna Mitrus2ca57df86a2c
diff --git a/src/common/transformations/src/transformations/common_optimizations/matmul_const_transposes_extraction.cpp b/src/common/transformations/src/transformations/common_optimizations/matmul_const_transposes_extraction.cpp
index 62522eeeb0b..62222338453 100644
--- a/src/common/transformations/src/transformations/common_optimizations/matmul_const_transposes_extraction.cpp
+++ b/src/common/transformations/src/transformations/common_optimizations/matmul_const_transposes_extraction.cpp
@@ -18,8 +18,7 @@ ov::pass::MatMulConstTransposesExtraction::MatMulConstTransposesExtraction() {
pattern::wrap_type<ov::op::v0::Constant, ov::op::v0::FakeQuantize>([](Output<Node> node) -> bool {
const auto& pshape = node.get_partial_shape();
const auto& rank = pshape.rank();
- return rank.is_static() && rank.get_length() >= 2 &&
- std::count(pshape.begin(), pshape.end(), 1) >= rank.get_length() - 2;
+ return rank.is_static() && rank.get_length() >= 2;
});
auto matmul_pattern = pattern::wrap_type<ov::op::v0::MatMul>({data_pattern, weights_pattern});
matcher_pass_callback callback = [=](pattern::Matcher& m) {
diff --git a/src/common/transformations/tests/common_optimizations/matmul_const_transposes_extraction.cpp b/src/common/transformations/tests/common_optimizations/matmul_const_transposes_extraction.cpp
index f38a5272edb..17d8d0341fc 100644
--- a/src/common/transformations/tests/common_optimizations/matmul_const_transposes_extraction.cpp
+++ b/src/common/transformations/tests/common_optimizations/matmul_const_transposes_extraction.cpp
@@ -85,11 +85,42 @@ TEST_F(TransformationTestsF, NegativeMatMulConstTransposesExtractionTransposeBSe
comparator.enable(FunctionsComparator::CmpValues::CONST_VALUES);
}
-TEST_F(TransformationTestsF, NegativeMatMulConstTransposesExtractionNonUnitDims) {
- auto data = std::make_shared<opset8::Parameter>(element::f32, Shape{1, 3, 4});
- auto weights = opset8::Constant::create(element::f32, Shape{2, 3, 2}, {1, 2, 3, 4, 5, 6, 2, 3, 4, 5, 6, 7});
- auto matmul = std::make_shared<opset8::MatMul>(data, weights, true);
- model = std::make_shared<Model>(matmul, ParameterVector{data});
- manager.register_pass<ov::pass::MatMulConstTransposesExtraction>();
+TEST_F(TransformationTestsF, MatMulConstTransposesExtractionNonUnitDims_transpose_a_true) {
+ {
+ auto data = std::make_shared<opset8::Parameter>(element::f32, Shape{1, 3, 4});
+ auto weights = opset8::Constant::create(element::f32, Shape{2, 3, 2}, {1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6});
+ auto matmul = std::make_shared<opset8::MatMul>(data, weights, true, false);
+ model = std::make_shared<Model>(matmul, ParameterVector{data});
+
+ manager.register_pass<ov::pass::MatMulConstTransposesExtraction>();
+ manager.register_pass<ov::pass::ConstantFolding>();
+ }
+ {
+ auto data = std::make_shared<opset8::Parameter>(element::f32, Shape{1, 3, 4});
+ auto weights = opset8::Constant::create(element::f32, Shape{2, 2, 3}, {1, 3, 5, 2, 4, 6, 1, 3, 5, 2, 4, 6});
+ auto matmul = std::make_shared<opset8::MatMul>(data, weights, true, true);
+ model_ref = std::make_shared<Model>(matmul, ParameterVector{data});
+ }
comparator.enable(FunctionsComparator::CmpValues::CONST_VALUES);
+ comparator.enable(FunctionsComparator::CmpValues::ACCURACY);
+}
+
+TEST_F(TransformationTestsF, MatMulConstTransposesExtractionNonUnitDims_transpose_b_false) {
+ {
+ auto data = std::make_shared<opset8::Parameter>(element::f32, Shape{1, 4, 3});
+ auto weights = opset8::Constant::create(element::f32, Shape{2, 3, 2}, {1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6});
+ auto matmul = std::make_shared<opset8::MatMul>(data, weights, false, false);
+ model = std::make_shared<Model>(matmul, ParameterVector{data});
+
+ manager.register_pass<ov::pass::MatMulConstTransposesExtraction>();
+ manager.register_pass<ov::pass::ConstantFolding>();
+ }
+ {
+ auto data = std::make_shared<opset8::Parameter>(element::f32, Shape{1, 4, 3});
+ auto weights = opset8::Constant::create(element::f32, Shape{2, 2, 3}, {1, 3, 5, 2, 4, 6, 1, 3, 5, 2, 4, 6});
+ auto matmul = std::make_shared<opset8::MatMul>(data, weights, false, true);
+ model_ref = std::make_shared<Model>(matmul, ParameterVector{data});
+ }
+ comparator.enable(FunctionsComparator::CmpValues::CONST_VALUES);
+ comparator.enable(FunctionsComparator::CmpValues::ACCURACY);
}
diff --git a/src/plugins/intel_cpu/tests/functional/shared_tests_instances/subgraph_tests/matmul_const_transposes_extraction.cpp b/src/plugins/intel_cpu/tests/functional/shared_tests_instances/subgraph_tests/matmul_const_transposes_extraction.cpp
index 1851fd30789..e0aadc2139e 100644
--- a/src/plugins/intel_cpu/tests/functional/shared_tests_instances/subgraph_tests/matmul_const_transposes_extraction.cpp
+++ b/src/plugins/intel_cpu/tests/functional/shared_tests_instances/subgraph_tests/matmul_const_transposes_extraction.cpp
@@ -19,6 +19,11 @@ std::vector<MatMulConstTransposesExtractionTestShapeParams> shape_params = {
{{2, 3, 5, 10}, {1, 10, 1}, false},
{{2, 3, 5, 10}, {1, 1, 10, 7}, false},
{{2, 3, 5, 10}, {1, 1, 10, 1}, false},
+ {{5, 10}, {2, 10, 7}, false},
+ {{5, 10}, {2, 3, 10, 7}, false},
+ {{2, 3, 5, 10}, {3, 10, 7}, false},
+ {{2, 3, 5, 10}, {1, 3, 10, 7}, false},
+ {{2, 3, 5, 10}, {2, 3, 10, 7}, false},
};
INSTANTIATE_TEST_SUITE_P(smoke_MatMulConstTransposesExtractionTest,
@@ -33,17 +38,12 @@ std::vector<MatMulConstTransposesExtractionTestShapeParams> negative_shape_param
{{5}, {3, 5}, true},
{{5, 5}, {5, 5}, true},
{{5, 10}, {7, 10}, true},
- {{5, 10}, {2, 10, 7}, false},
- {{5, 10}, {2, 3, 10, 7}, false},
{{1, 1, 5, 10}, {10}, false},
{{1, 1, 5, 10}, {7, 10}, true},
{{1, 1, 5, 10}, {1, 1, 7, 10}, true},
{{2, 3, 5, 10}, {7, 10}, true},
{{2, 3, 5, 10}, {3, 7, 10}, true},
{{2, 3, 5, 10}, {2, 3, 7, 10}, true},
- {{2, 3, 5, 10}, {3, 10, 7}, false},
- {{2, 3, 5, 10}, {1, 3, 10, 7}, false},
- {{2, 3, 5, 10}, {2, 3, 10, 7}, false},
};
INSTANTIATE_TEST_SUITE_P(smoke_NegativeMatMulConstTransposesExtractionTest,
@@ -61,6 +61,11 @@ std::vector<MatMulConstTransposesExtractionTestShapeParams> shape_params2 = {
{{2, 3, 5, 10}, {10, 7}, false},
{{2, 3, 5, 10}, {1, 10, 7}, false},
{{2, 3, 5, 10}, {1, 1, 10, 7}, false},
+ {{5, 10}, {2, 10, 7}, false},
+ {{5, 10}, {2, 3, 10, 7}, false},
+ {{2, 3, 5, 10}, {3, 10, 7}, false},
+ {{2, 3, 5, 10}, {1, 3, 10, 7}, false},
+ {{2, 3, 5, 10}, {2, 3, 10, 7}, false},
};
INSTANTIATE_TEST_SUITE_P(smoke_QuantizedMatMulConstTransposesExtractionTest,