Skip to content

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

[CPU] Fix MatMul with zero length dimensions (#38407)

VijayabaskarR-06b0b2b645d7b4
diff --git a/src/plugins/intel_cpu/src/nodes/matmul.cpp b/src/plugins/intel_cpu/src/nodes/matmul.cpp
index 0c6d6287cdf..8b3d1158e28 100644
--- a/src/plugins/intel_cpu/src/nodes/matmul.cpp
+++ b/src/plugins/intel_cpu/src/nodes/matmul.cpp
@@ -266,7 +266,12 @@ void MatMul::createPrimitive() {
m_memory[ARG_DST] = getDstMemoryAtPort(0);
- m_executor = m_factory->make(m_memory, false);
+ // If any input tensor is known to be empty at compile time, the executor is never used:
+ // the output is either empty (the node is not executable) or a null matrix (see prepareParams() and execute()).
+ // So there is no need to create it, moreover, the primitive can't be created for an empty problem.
+ if (!getInputShapeAtPort(0).hasZeroDims() && !getInputShapeAtPort(1).hasZeroDims()) {
+ m_executor = m_factory->make(m_memory, false);
+ }
Node::createPrimitive();
}
diff --git a/src/plugins/intel_cpu/tests/functional/custom/single_layer_tests/instances/common/matmul.cpp b/src/plugins/intel_cpu/tests/functional/custom/single_layer_tests/instances/common/matmul.cpp
index b3c5d2bf705..1494263cdcb 100644
--- a/src/plugins/intel_cpu/tests/functional/custom/single_layer_tests/instances/common/matmul.cpp
+++ b/src/plugins/intel_cpu/tests/functional/custom/single_layer_tests/instances/common/matmul.cpp
@@ -32,6 +32,24 @@ const std::vector<ShapeRelatedParams> IS = {
{static_shapes_to_test_representation({{55, 12}, {12, 55}}), {true, true}},
};
+const std::vector<ShapeRelatedParams> IS_ZeroDim = {
+ // empty output
+ {static_shapes_to_test_representation({{0, 4}, {4, 3}}), {false, false}},
+ {static_shapes_to_test_representation({{0, 4}, {4, 3}}), {true, true}},
+ {static_shapes_to_test_representation({{4, 3}, {3, 0}}), {false, false}},
+ {static_shapes_to_test_representation({{0, 4, 8}, {0, 8, 4}}), {false, false}},
+ {static_shapes_to_test_representation({{2, 0, 3}, {3, 3}}), {false, false}},
+ // degenerate case: zero collapsing dimension, the output is a null matrix
+ {static_shapes_to_test_representation({{4, 0}, {0, 3}}), {false, false}},
+ {
+ {
+ {{-1, 0}, {{4, 0}, {7, 0}}}, // input 0
+ {{0, -1}, {{0, 3}, {0, 5}}} // input 1
+ },
+ {false, false}
+ },
+};
+
const std::vector<ShapeRelatedParams> IS_Dynamic = {
{
{ //dynamic case description each pair per each input has {{dynamic shape}, {{static shape case1}, {static shape case2}, ...}
@@ -299,6 +317,22 @@ const auto testParams = ::testing::Combine(matMulParams,
INSTANTIATE_TEST_SUITE_P(smoke_MM_Static, MatMulLayerCPUTest, testParams, MatMulLayerCPUTest::getTestCaseName);
+const auto matMulParamsZeroDim = ::testing::Combine(::testing::ValuesIn(IS_ZeroDim),
+ ::testing::Values(ElementType::f32, ElementType::i32),
+ ::testing::Values(ElementType::dynamic),
+ ::testing::Values(ElementType::dynamic),
+ ::testing::Values(utils::InputLayerType::PARAMETER),
+ ::testing::Values(ov::test::utils::DEVICE_CPU),
+ ::testing::Values(emptyAdditionalConfig()));
+
+const auto testParamsZeroDim =
+ ::testing::Combine(matMulParamsZeroDim,
+ ::testing::Values(MatMulNodeType::MatMul),
+ ::testing::Values(emptyFusingSpec),
+ ::testing::Values(CPUSpecificParams{{}, {}, {}, CPUTestsBase::any_type}));
+
+INSTANTIATE_TEST_SUITE_P(smoke_MM_ZeroDim, MatMulLayerCPUTest, testParamsZeroDim, MatMulLayerCPUTest::getTestCaseName);
+
const auto matMulParamsDynamic = ::testing::Combine(::testing::ValuesIn(IS_Dynamic),
::testing::ValuesIn(netPRCs()),
::testing::Values(ElementType::dynamic),