Skip to content

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

[CPU] Support degenerate matrices in MatMul (#26830)

Maksim Kutakovb2cd7aa2c548
diff --git a/src/plugins/intel_cpu/src/nodes/matmul.cpp b/src/plugins/intel_cpu/src/nodes/matmul.cpp
index 50cb3353612..92d8f356728 100644
--- a/src/plugins/intel_cpu/src/nodes/matmul.cpp
+++ b/src/plugins/intel_cpu/src/nodes/matmul.cpp
@@ -543,6 +543,20 @@ void MatMul::prepareParams() {
if (!src0MemPtr || !src0MemPtr->isDefined() || !src1MemPtr || !src1MemPtr->isDefined())
OPENVINO_THROW(errorPrefix, " has undefined input memory");
+ // check for a degenerate case. In this context the degenerate case is a matrix multiplication where the
+ // collapsing dimension is zero, e.g., AB=C, where A has the shape [10, 0] and B has the shape [0, 20],
+ // consequently C has shape [10, 20]. In this scenario C is a null matrix (a matrix filled with zeroes)
+ // according to the empty sum convention.
+ if (src0MemPtr->getDesc().getShape().hasZeroDims() && src0MemPtr->getDesc().getShape().hasZeroDims() &&
+ !dstMemPtr->getDesc().getShape().hasZeroDims()) {
+ // todo: obviously we need a special executor that would process fused ops providing a correct result
+ OPENVINO_ASSERT(!withBiases && fusedWith.empty(),
+ "Matmul doesn't support a degenerate case when other ops are fused");
+ //reset executor
+ execPtr.reset();
+ return;
+ }
+
const NodeDesc* selected_pd = getSelectedPrimitiveDescriptor();
if (selected_pd == nullptr)
OPENVINO_THROW(errorPrefix, " did not set preferable primitive descriptor");
@@ -646,6 +660,9 @@ void MatMul::prepareParams() {
void MatMul::execute(dnnl::stream strm) {
if (execPtr) {
execPtr->exec(primArgs, strm);
+ } else if (hasEmptyInputTensors()) {
+ // this is a degenerate case, fill output with zeroes
+ getDstMemoryAtPort(0)->nullify();
} else {
OPENVINO_THROW(errorPrefix, " doesn't have an initialized executor");
}
@@ -691,6 +708,10 @@ const std::vector<impl_desc_type>& MatMul::getDefaultImplPriority() {
return priorities;
}
+bool MatMul::isExecutable() const {
+ return !hasEmptyOutputTensors();
+}
+
} // namespace node
} // namespace intel_cpu
} // namespace ov
diff --git a/src/plugins/intel_cpu/src/nodes/matmul.h b/src/plugins/intel_cpu/src/nodes/matmul.h
index 7b8f064e172..2e487148d0e 100644
--- a/src/plugins/intel_cpu/src/nodes/matmul.h
+++ b/src/plugins/intel_cpu/src/nodes/matmul.h
@@ -43,6 +43,8 @@ public:
const std::vector<impl_desc_type>& getDefaultImplPriority() override;
bool canBeExecutedInInt8() const override;
+ bool isExecutable() const override;
+
protected:
AttrPtr initPrimitiveAttr() override;
AttrPtr initPrimitiveAttr(const VectorDims& dims);
diff --git a/src/plugins/intel_cpu/src/nodes/memory.cpp b/src/plugins/intel_cpu/src/nodes/memory.cpp
index 74a3b670dad..88693ebfa49 100644
--- a/src/plugins/intel_cpu/src/nodes/memory.cpp
+++ b/src/plugins/intel_cpu/src/nodes/memory.cpp
@@ -419,7 +419,7 @@ MemoryInputBase::~MemoryInputBase() {
}
MemoryOutputBase& MemoryInputBase::getOutputNode() {
- OPENVINO_ASSERT(outputNode, "MemoryOutput ", getName(), " doesn't have sibling input");
+ OPENVINO_ASSERT(outputNode, "MemoryInput ", getName(), " doesn't have sibling output");
return *outputNode;
}
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 934a0f4bc95..9b5d7287875 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
@@ -35,8 +35,8 @@ const std::vector<ShapeRelatedParams> IS = {
const std::vector<ShapeRelatedParams> IS_Dynamic = {
{
{ //dynamic case description each pair per each input has {{dynamic shape}, {{static shape case1}, {static shape case2}, ...}
- {{-1, -1}, {{55, 12}, {33, 7}}}, // input 0
- {{-1, -1}, {{12, 55}, {7, 33}}} // input 1
+ {{-1, -1}, {{55, 12}, {33, 7}, {33, 0}, {0, 33}}}, // input 0
+ {{-1, -1}, {{12, 55}, {7, 33}, {0, 33}, {33, 0}}} // input 1
},
{false, false}
},
diff --git a/src/plugins/intel_cpu/tests/functional/shared_tests_instances/subgraph_tests/lora_pattern.cpp b/src/plugins/intel_cpu/tests/functional/shared_tests_instances/subgraph_tests/lora_pattern.cpp
new file mode 100644
index 00000000000..d85ced5f07a
--- /dev/null
+++ b/src/plugins/intel_cpu/tests/functional/shared_tests_instances/subgraph_tests/lora_pattern.cpp
@@ -0,0 +1,21 @@
+// Copyright (C) 2018-2024 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "subgraph_tests/lora_pattern.hpp"
+
+using namespace ov::test;
+
+namespace {
+
+INSTANTIATE_TEST_SUITE_P(smoke,
+ LoraPatternConvolution,
+ ::testing::Values(ov::test::utils::DEVICE_CPU),
+ LoraPatternBase::getTestCaseName);
+
+INSTANTIATE_TEST_SUITE_P(smoke,
+ LoraPatternMatmul,
+ ::testing::Values(ov::test::utils::DEVICE_CPU),
+ LoraPatternBase::getTestCaseName);
+
+} // namespace
diff --git a/src/tests/functional/plugin/shared/include/subgraph_tests/lora_pattern.hpp b/src/tests/functional/plugin/shared/include/subgraph_tests/lora_pattern.hpp
new file mode 100644
index 00000000000..8f9687b7b93
--- /dev/null
+++ b/src/tests/functional/plugin/shared/include/subgraph_tests/lora_pattern.hpp
@@ -0,0 +1,23 @@
+// Copyright (C) 2024 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#pragma once
+
+#include "shared_test_classes/subgraph/lora_pattern.hpp"
+
+namespace ov {
+namespace test {
+
+TEST_P(LoraPatternMatmul, empty_tensors) {
+ targetStaticShapes = {{{{1, 20, K}}, {{N, K}}}};
+ run_test_empty_tensors();
+}
+
+TEST_P(LoraPatternConvolution, empty_tensors) {
+ targetStaticShapes = {{{1, num_channels, 64, 64}}};
+ run_test_empty_tensors();
+}
+
+} // namespace test
+} // namespace ov
\ No newline at end of file
diff --git a/src/tests/functional/shared_test_classes/include/shared_test_classes/subgraph/lora_pattern.hpp b/src/tests/functional/shared_test_classes/include/shared_test_classes/subgraph/lora_pattern.hpp
new file mode 100644
index 00000000000..16764d37dcf
--- /dev/null
+++ b/src/tests/functional/shared_test_classes/include/shared_test_classes/subgraph/lora_pattern.hpp
@@ -0,0 +1,44 @@
+// Copyright (C) 2018-2024 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#pragma once
+
+#include "shared_test_classes/base/ov_subgraph.hpp"
+
+namespace ov {
+namespace test {
+
+class LoraPatternBase : public SubgraphBaseTest {
+public:
+ static std::string getTestCaseName(const testing::TestParamInfo<const char*>& obj);
+
+protected:
+ void run_test_empty_tensors();
+
+protected:
+ static constexpr auto t4_name = "lora/MatMul.B";
+ static constexpr auto t5_name = "lora/MatMul.alpha";
+ static constexpr auto t6_name = "lora/MatMul.A";
+ static constexpr auto netType = ov::element::f32;
+};
+
+class LoraPatternMatmul : public LoraPatternBase, public testing::WithParamInterface<const char*> {
+public:
+ void SetUp() override;
+
+protected:
+ static constexpr size_t K = 563ul; // Weights matrix K dimension
+ static constexpr size_t N = 2048ul; // Weights matrix N dimension
+};
+
+class LoraPatternConvolution : public LoraPatternBase, public testing::WithParamInterface<const char*> {
+public:
+ void SetUp() override;
+
+protected:
+ static constexpr size_t num_channels = 320ul;
+};
+
+} // namespace test
+} // namespace ov
\ No newline at end of file
diff --git a/src/tests/functional/shared_test_classes/src/subgraph/lora_pattern.cpp b/src/tests/functional/shared_test_classes/src/subgraph/lora_pattern.cpp
new file mode 100644
index 00000000000..6f74fd09b02
--- /dev/null
+++ b/src/tests/functional/shared_test_classes/src/subgraph/lora_pattern.cpp
@@ -0,0 +1,143 @@
+// Copyright (C) 2018-2024 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "shared_test_classes/subgraph/lora_pattern.hpp"
+
+#include "common_test_utils/node_builders/eltwise.hpp"
+#include "common_test_utils/node_builders/convolution.hpp"
+#include "common_test_utils/ov_tensor_utils.hpp"
+#include "shared_test_classes/base/ov_subgraph.hpp"
+
+namespace ov {
+namespace test {
+
+
+std::string LoraPatternBase::getTestCaseName(const testing::TestParamInfo<const char*>& obj) {
+ auto device_name = obj.param;
+ return std::string{"targetDevice="} + device_name; //NOLINT
+}
+
+constexpr ov::element::Type LoraPatternBase::netType; //redundant variable definition for C++ prior to C++17
+
+void LoraPatternBase::run_test_empty_tensors() {
+ compile_model();
+ inferRequest = compiledModel.create_infer_request();
+ ASSERT_TRUE(inferRequest);
+ generate_inputs(targetStaticShapes.front());
+ for (const auto& input : inputs) {
+ inferRequest.set_tensor(input.first, input.second);
+ }
+
+ inferRequest.infer();
+ auto outputs = function->outputs();
+
+ auto tx_result = inferRequest.get_tensor(outputs[0]);
+ auto tz_result = inferRequest.get_tensor(outputs[1]);
+ ov::test::utils::compare(tx_result, tz_result, 1e-4, 1e-4);
+}
+
+void LoraPatternMatmul::SetUp() {
+ targetDevice = this->GetParam();
+
+ ov::PartialShape shape_x = {-1, -1, K};
+ ov::PartialShape shape_w = {N, K};
+
+ auto param_y = std::make_shared<ov::op::v0::Parameter>(netType, shape_x);
+ auto param_w = std::make_shared<ov::op::v0::Parameter>(netType, shape_w);
+
+ // "Main" matrix multiplication from the original transformer model
+ auto tx = std::make_shared<ov::op::v0::MatMul>(param_y, param_w, false, true);
+
+ // LoRA parameters from states
+ auto variable_t4 = std::make_shared<ov::op::util::Variable>(
+ ov::op::util::VariableInfo{ov::PartialShape({N, -1}), netType, t4_name});
+ auto t4 = std::make_shared<ov::op::v6::ReadValue>(variable_t4);
+ auto t4_assign = std::make_shared<ov::op::v6::Assign>(t4, variable_t4);
+
+ auto variable_t5 = std::make_shared<ov::op::util::Variable>(
+ ov::op::util::VariableInfo{ov::PartialShape({1, -1}), netType, t5_name});
+ auto t5 = std::make_shared<ov::op::v6::ReadValue>(variable_t5);
+ auto t5_assign = std::make_shared<ov::op::v6::Assign>(t5, variable_t5);
+
+ auto variable_t6 = std::make_shared<ov::op::util::Variable>(
+ ov::op::util::VariableInfo{ov::PartialShape({-1, K}), netType, t6_name});
+ auto t6 = std::make_shared<ov::op::v6::ReadValue>(variable_t6);
+ auto t6_assign = std::make_shared<ov::op::v6::Assign>(t6, variable_t6);
+
+ // Apply LoRA parameters to the current activations
+ auto t5810 = std::make_shared<ov::op::v0::MatMul>(param_y, t6, false, true);
+ auto t5811 = std::make_shared<ov::op::v1::Multiply>(t5810, t5);
+ auto t5812 = std::make_shared<ov::op::v0::MatMul>(t5811, t4, false, true);
+
+ // Mix LoRA part into normally computed activations after the "main" MatMul
+ auto tz = std::make_shared<ov::op::v1::Add>(tx, t5812);
+
+ auto result_x = std::make_shared<ov::op::v0::Result>(tx);
+ auto result_z = std::make_shared<ov::op::v0::Result>(tz);
+
+ function = std::make_shared<ov::Model>(ov::ResultVector({result_x, result_z}),
+ ov::SinkVector({t4_assign, t5_assign, t6_assign}),
+ ov::ParameterVector({param_y, param_w}));
+}
+
+void LoraPatternConvolution::SetUp() {
+ targetDevice = this->GetParam();
+
+ ov::PartialShape shape_x = {-1, num_channels, -1, -1};
+
+ auto param_y = std::make_shared<ov::op::v0::Parameter>(netType, shape_x);
+
+ // Original Convolution that is modified by LoRA adapter later
+ auto tx = ov::test::utils::make_convolution(param_y,
+ netType,
+ {1, 1},
+ {1, 1},
+ {0, 0},
+ {0, 0},
+ {1, 1},
+ ov::op::PadType::EXPLICIT,
+ num_channels);
+
+ // LoRA parameters from states
+ auto variable_t4 = std::make_shared<ov::op::util::Variable>(
+ ov::op::util::VariableInfo{ov::PartialShape({num_channels, -1}), netType, t4_name});
+ auto t4 = std::make_shared<ov::op::v6::ReadValue>(variable_t4);
+ auto t4_assign = std::make_shared<ov::op::v6::Assign>(t4, variable_t4);
+
+ auto variable_t5 = std::make_shared<ov::op::util::Variable>(
+ ov::op::util::VariableInfo{ov::PartialShape({1, -1}), netType, t5_name});
+ auto t5 = std::make_shared<ov::op::v6::ReadValue>(variable_t5);
+ auto t5_assign = std::make_shared<ov::op::v6::Assign>(t5, variable_t5);
+
+ auto variable_t6 = std::make_shared<ov::op::util::Variable>(
+ ov::op::util::VariableInfo{ov::PartialShape({-1, num_channels}), netType, t6_name});
+ auto t6 = std::make_shared<ov::op::v6::ReadValue>(variable_t6);
+ auto t6_assign = std::make_shared<ov::op::v6::Assign>(t6, variable_t6);
+
+ // LoRA pattern with additional Transposes to move channel dimensions into positions where MatMul can be applied
+ auto t4940 =
+ std::make_shared<ov::op::v0::Constant>(ov::element::i32, ov::Shape{4}, std::vector<size_t>{2, 3, 0, 1});
+
+ auto t4941 = std::make_shared<ov::op::v1::Transpose>(param_y, t4940);
+ auto t4942 = std::make_shared<ov::op::v0::MatMul>(t4941, t6, false, true);
+ auto t4943 = std::make_shared<ov::op::v1::Multiply>(t4942, t5);
+ auto t4944 = std::make_shared<ov::op::v0::MatMul>(t4943, t4, false, true);
+
+ auto t4945 =
+ std::make_shared<ov::op::v0::Constant>(ov::element::i32, ov::Shape{4}, std::vector<size_t>{2, 3, 0, 1});
+ auto t4946 = std::make_shared<ov::op::v1::Transpose>(t4944, t4945);
+
+ // Mix LoRA part into normally computed activations after the "main" MatMul
+ auto tz = std::make_shared<ov::op::v1::Add>(tx, t4946);
+
+ auto result_x = std::make_shared<ov::op::v0::Result>(tx);
+ auto result_z = std::make_shared<ov::op::v0::Result>(tz);
+
+ function = std::make_shared<ov::Model>(ov::ResultVector({result_x, result_z}),
+ ov::SinkVector({t4_assign, t5_assign, t6_assign}),
+ ov::ParameterVector({param_y}));
+}
+
+} // namespace test
+} // namespace ov
\ No newline at end of file