Skip to content

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

[Snippets][CPU] Fix MHA constant B matrix repacking handing (#34201)

Arseniy Obolenskiycab9b7282f09
diff --git a/src/common/snippets/include/snippets/op/subgraph.hpp b/src/common/snippets/include/snippets/op/subgraph.hpp
index d7d776f5cf6..9b66ba037b8 100644
--- a/src/common/snippets/include/snippets/op/subgraph.hpp
+++ b/src/common/snippets/include/snippets/op/subgraph.hpp
@@ -162,7 +162,7 @@ public:
// Non-scalar Constants are tokenized as Parameters inside Subgraph body but some operations with constant inputs
// should have explicit Constants even if they're non-scalar (Reshape, Transpose, Broadcast)
// This check returns True if Constant op which is input of this op should be inside Subgraph body
- static auto constant_input_should_be_inside_body(const std::shared_ptr<ov::Node>& node) -> bool;
+ static auto constant_input_should_be_inside_body(const Input<ov::Node>& node_input) -> bool;
static bool check_broadcast(const std::shared_ptr<const ov::Node>& node);
// Return estimated unique buffer count (upper bound). It's needed for tokenization
static auto get_estimated_buffer_count(const ov::NodeVector& ops) -> size_t;
diff --git a/src/common/snippets/src/op/subgraph.cpp b/src/common/snippets/src/op/subgraph.cpp
index e549ae98577..a509e182125 100644
--- a/src/common/snippets/src/op/subgraph.cpp
+++ b/src/common/snippets/src/op/subgraph.cpp
@@ -266,19 +266,21 @@ auto Subgraph::wrap_node_as_subgraph(const std::shared_ptr<ov::Node>& node) -> s
ov::OutputVector subgraph_inputs;
- for (const auto& input : node->input_values()) {
- if (ov::is_type<ov::opset1::Constant>(input.get_node_shared_ptr()) &&
+ for (size_t i = 0; i < node->get_input_size(); ++i) {
+ const auto& input = node->input(i);
+ const auto& source_output = input.get_source_output();
+ if (ov::is_type<ov::opset1::Constant>(source_output.get_node_shared_ptr()) &&
(ov::shape_size(input.get_shape()) == 1 || ov::is_type<ov::op::v0::FakeQuantize>(node) ||
- constant_input_should_be_inside_body(node))) {
- body_inputs.push_back(input);
+ constant_input_should_be_inside_body(input))) {
+ body_inputs.push_back(source_output);
} else {
auto parameter =
std::make_shared<ov::opset1::Parameter>(input.get_element_type(), input.get_partial_shape());
body_parameters.push_back(parameter);
- body_parameters.back()->set_friendly_name(input.get_node()->get_friendly_name());
+ body_parameters.back()->set_friendly_name(source_output.get_node()->get_friendly_name());
body_inputs.push_back(parameter->output(0));
- subgraph_inputs.push_back(input);
+ subgraph_inputs.push_back(source_output);
}
}
@@ -325,9 +327,10 @@ void Subgraph::fill_empty_output_names(const Output<Node>& target_output_node,
}
}
-auto Subgraph::constant_input_should_be_inside_body(const std::shared_ptr<ov::Node>& node) -> bool {
+auto Subgraph::constant_input_should_be_inside_body(const Input<ov::Node>& node_input) -> bool {
return ov::is_type_any_of<ov::op::v1::Transpose, ov::op::v1::Broadcast, ov::op::v3::Broadcast, ov::op::v1::Reshape>(
- node);
+ node_input.get_node()) &&
+ node_input.get_index() == 1;
}
bool Subgraph::check_broadcast(const std::shared_ptr<const ov::Node>& node) {
diff --git a/src/common/snippets/src/pass/explicit_transpose_matmul_inputs.cpp b/src/common/snippets/src/pass/explicit_transpose_matmul_inputs.cpp
index 62cdd4dad2c..8a4c998b4d2 100644
--- a/src/common/snippets/src/pass/explicit_transpose_matmul_inputs.cpp
+++ b/src/common/snippets/src/pass/explicit_transpose_matmul_inputs.cpp
@@ -15,6 +15,7 @@
#include "openvino/core/except.hpp"
#include "openvino/core/node.hpp"
#include "openvino/core/node_input.hpp"
+#include "openvino/core/node_vector.hpp"
#include "openvino/core/rt_info.hpp"
#include "openvino/core/shape.hpp"
#include "openvino/core/type.hpp"
@@ -28,6 +29,7 @@
#include "openvino/pass/pattern/op/label.hpp"
#include "openvino/util/pp.hpp"
#include "snippets/itt.hpp"
+#include "transformations/utils/utils.hpp"
bool ov::snippets::pass::ExplicitTransposeMatMulInputs::are_weights_scalar(const std::shared_ptr<ov::Node>& node) {
const auto inputs = node->inputs();
@@ -37,12 +39,22 @@ bool ov::snippets::pass::ExplicitTransposeMatMulInputs::are_weights_scalar(const
}
void ov::snippets::pass::ExplicitTransposeMatMulInputs::extract(const ov::Input<ov::Node>& input) {
+ auto get_transpose_order = [&input]() {
+ OPENVINO_ASSERT(input.get_partial_shape().rank().is_static(),
+ "ExplicitTransposeMatMulInputs supports only static ranks of shapes");
+ const auto rank = input.get_partial_shape().size();
+ std::vector<size_t> transpose_order(rank, 0);
+ std::iota(transpose_order.begin(), transpose_order.end(), 0);
+ std::swap(transpose_order[rank - 1], transpose_order[rank - 2]);
+ return transpose_order;
+ };
+
auto parent = input.get_source_output().get_node_shared_ptr();
auto transpose = ov::as_type_ptr<ov::op::v1::Transpose>(parent);
while (!transpose && !ov::is_type<ov::op::v0::Parameter>(parent)) {
// We can set supported order and transposed_<a|b>=false only if ops have scalar shapes to avoid shape
// mismatching
- if (!are_weights_scalar(parent)) {
+ if (parent->get_input_size() == 0 || !are_weights_scalar(parent)) {
break;
}
@@ -69,6 +81,20 @@ void ov::snippets::pass::ExplicitTransposeMatMulInputs::extract(const ov::Input<
return;
}
+ // If there is no existing Transpose and input branch starts from Constant,
+ // fold explicit transposition into the constant value and keep transposed_b=false.
+ if (const auto& constant = ov::as_type_ptr<ov::opset1::Constant>(parent)) {
+ const auto transpose_order = get_transpose_order();
+ const auto constant_order =
+ std::make_shared<opset1::Constant>(ov::element::i32, ov::Shape{transpose_order.size()}, transpose_order);
+ const auto folded_const =
+ ov::as_type_ptr<opset1::Constant>(ov::op::util::make_try_fold<opset1::Transpose>(constant, constant_order));
+ OPENVINO_ASSERT(folded_const != nullptr,
+ "ExplicitTransposeMatMulInputs failed to fold Transpose over Constant input");
+ input.replace_source_output(folded_const);
+ return;
+ }
+
// Create new Transpose before Parameter
OPENVINO_ASSERT(
ov::is_type<opset1::Parameter>(parent),
@@ -78,15 +104,9 @@ void ov::snippets::pass::ExplicitTransposeMatMulInputs::extract(const ov::Input<
"ExplicitTransposeMatMulInputs expects Parameter with one consumer in cases when there isn't "
"existing Transpose on input");
// Extract Transpose from MatMul
- OPENVINO_ASSERT(input.get_partial_shape().rank().is_static(),
- "ExplicitTransposeMatMulInputs supports only static ranks of shapes");
-
- const auto rank = input.get_partial_shape().size();
- std::vector<size_t> transpose_order(rank, 0);
- std::iota(transpose_order.begin(), transpose_order.end(), 0);
- std::swap(transpose_order[rank - 1], transpose_order[rank - 2]);
-
- const auto constant_order = std::make_shared<opset1::Constant>(ov::element::i32, ov::Shape{rank}, transpose_order);
+ const auto transpose_order = get_transpose_order();
+ const auto constant_order =
+ std::make_shared<opset1::Constant>(ov::element::i32, ov::Shape{transpose_order.size()}, transpose_order);
const auto new_transpose = std::make_shared<opset1::Transpose>(parent, constant_order); // parent is Parameter
const auto consumer_input = *(consumers.begin());
consumer_input.replace_source_output(new_transpose);
diff --git a/src/common/snippets/src/pass/extract_constants.cpp b/src/common/snippets/src/pass/extract_constants.cpp
index 43ef157951c..bb4cbd8903d 100644
--- a/src/common/snippets/src/pass/extract_constants.cpp
+++ b/src/common/snippets/src/pass/extract_constants.cpp
@@ -6,6 +6,7 @@
#include <memory>
+#include "openvino/core/except.hpp"
#include "openvino/core/graph_util.hpp"
#include "openvino/core/node_vector.hpp"
#include "openvino/core/shape.hpp"
@@ -28,8 +29,10 @@ bool ov::snippets::pass::ExtractConstants::run_on_subgraph(const std::shared_ptr
continue;
}
- const auto child = constant->get_output_target_inputs(0).begin()->get_node()->shared_from_this();
- if (ov::snippets::op::Subgraph::constant_input_should_be_inside_body(child)) {
+ const auto child_inputs = constant->get_output_target_inputs(0);
+ OPENVINO_ASSERT(!child_inputs.empty(), "ExtractConstants expects Constant to have at least one consumer input");
+ const auto& child_input = *child_inputs.begin();
+ if (ov::snippets::op::Subgraph::constant_input_should_be_inside_body(child_input)) {
continue;
}
diff --git a/src/common/snippets/src/utils/tokenization_utils.cpp b/src/common/snippets/src/utils/tokenization_utils.cpp
index 5c645ff8a52..420534a627b 100644
--- a/src/common/snippets/src/utils/tokenization_utils.cpp
+++ b/src/common/snippets/src/utils/tokenization_utils.cpp
@@ -81,6 +81,7 @@ auto outputs_are_not_broadcastable(const std::shared_ptr<const Node>& node) -> b
}
return !success;
}
+
} // namespace
std::function<bool(const std::shared_ptr<const ov::Node>&)> make_transpose_support_callback(bool include_brgemm_case) {
@@ -225,7 +226,8 @@ bool tokenize_node(const std::shared_ptr<ov::Node>& node, const ov::snippets::pa
cyclicDependencyIsIntoduced(node, currentTopoBounds);
assert(!cyclicDependencyIsIntoduced(node, currentTopoBounds) &&
"Cyclic dependency is introduced by the node itself");
- for (const auto& input_value : input_values) {
+ for (size_t input_index = 0; input_index < input_values.size(); ++input_index) {
+ const auto& input_value = input_values[input_index];
auto input_node = input_value.get_node_shared_ptr();
if (ov::is_type<op::Subgraph>(input_node) && !cyclicDependencyIsIntoduced(input_node, currentTopoBounds)) {
auto subgraph = std::static_pointer_cast<op::Subgraph>(input_node);
@@ -324,7 +326,7 @@ bool tokenize_node(const std::shared_ptr<ov::Node>& node, const ov::snippets::pa
// and will only be used to decompose Transpose into a proper Load, Store and Loop combination.
if (ov::is_type<ov::opset1::Constant>(input_node) &&
(ov::shape_size(input_value.get_shape()) == 1 || ov::is_type<ov::op::v0::FakeQuantize>(node) ||
- op::Subgraph::constant_input_should_be_inside_body(node))) {
+ op::Subgraph::constant_input_should_be_inside_body(node->input(input_index)))) {
internal_inputs.push_back(input_node->output(0));
} else {
external_inputs.push_back(input_value);
@@ -492,7 +494,7 @@ std::shared_ptr<ov::snippets::op::Subgraph> tokenize_ordered_nodes(const ov::Nod
const auto parent = input.get_source_output().get_node_shared_ptr();
const auto constant = ov::as_type_ptr<ov::op::v0::Constant>(parent);
if (constant && (ov::shape_size(input.get_shape()) == 1 || ov::is_type<ov::op::v0::FakeQuantize>(node) ||
- op::Subgraph::constant_input_should_be_inside_body(node))) {
+ op::Subgraph::constant_input_should_be_inside_body(input))) {
// If not all Constant consumers are inside Subgraph body,
// we should make a copy of this Constant for Subgraph body.
if (constant->get_output_target_inputs(0).size() > 1) {
@@ -589,7 +591,7 @@ size_t get_potential_body_params(const std::shared_ptr<ov::Node>& op) {
const auto constant = ov::as_type_ptr<ov::op::v0::Constant>(parent);
const auto is_scalar = constant && (ov::shape_size(input.get_shape()) == 1);
const auto should_be_inside_body =
- constant && ov::snippets::op::Subgraph::constant_input_should_be_inside_body(op);
+ constant && ov::snippets::op::Subgraph::constant_input_should_be_inside_body(op->input(i));
if (!is_scalar && !should_be_inside_body) {
count++;
}
diff --git a/src/common/snippets/tests/src/pass/mha_tokenization.cpp b/src/common/snippets/tests/src/pass/mha_tokenization.cpp
index 7b5ef49541d..e6d85987416 100644
--- a/src/common/snippets/tests/src/pass/mha_tokenization.cpp
+++ b/src/common/snippets/tests/src/pass/mha_tokenization.cpp
@@ -2,13 +2,16 @@
// SPDX-License-Identifier: Apache-2.0
//
+#include "snippets/pass/mha_tokenization.hpp"
+
#include <gtest/gtest.h>
+
#include <pass/mha_tokenization.hpp>
#include <subgraph_mha.hpp>
-#include "snippets/pass/tokenization.hpp"
-#include "snippets/pass/mha_tokenization.hpp"
+
#include "snippets/pass/common_optimizations.hpp"
#include "snippets/pass/extract_reshapes_from_mha.hpp"
+#include "snippets/pass/tokenization.hpp"
namespace ov {
namespace test {
@@ -24,44 +27,124 @@ void TokenizeMHASnippetsTests::run() {
}
TEST_F(TokenizeMHASnippetsTests, smoke_Snippets_MHA_2D_Static) {
- const auto &f = MHA2DFunction(std::vector<PartialShape>{{12, 64}, {64, 12}, {12, 12}, {12, 64}},
- std::vector<ov::element::Type>({ov::element::f32, ov::element::f32, ov::element::f32, ov::element::f32}));
+ const auto& f = MHA2DFunction(
+ std::vector<PartialShape>{{12, 64}, {64, 12}, {12, 12}, {12, 64}},
+ std::vector<ov::element::Type>({ov::element::f32, ov::element::f32, ov::element::f32, ov::element::f32}));
+ execute_and_validate_function(*this, f);
+}
+
+TEST_F(TokenizeMHASnippetsTests, smoke_Snippets_MHA_2D_Static_Const_B) {
+ const auto& f = MHA2DFunction(
+ std::vector<PartialShape>{{12, 64}, {64, 12}, {12, 12}, {12, 64}},
+ std::vector<ov::element::Type>({ov::element::f32, ov::element::f32, ov::element::f32, ov::element::f32}));
execute_and_validate_function(*this, f);
}
TEST_F(TokenizeMHASnippetsTests, smoke_Snippets_MHA_2D_Dynamic) {
- const auto &f = MHA2DFunction(std::vector<PartialShape>{{-1, -1}, {-1, -1}, {-1, -1}, {-1, -1}},
- std::vector<ov::element::Type>({ov::element::f32, ov::element::f32, ov::element::f32, ov::element::f32}));
+ const auto& f = MHA2DFunction(
+ std::vector<PartialShape>{{-1, -1}, {-1, -1}, {-1, -1}, {-1, -1}},
+ std::vector<ov::element::Type>({ov::element::f32, ov::element::f32, ov::element::f32, ov::element::f32}));
execute_and_validate_function(*this, f);
}
TEST_F(TokenizeMHASnippetsTests, smoke_Snippets_MHA_2D_Partially_Dynamic) {
- const auto &f = MHA2DFunction(std::vector<PartialShape>{{12, 64}, {64, 12}, {-1, 12}, {12, 64}},
- std::vector<ov::element::Type>({ov::element::f32, ov::element::f32, ov::element::f32, ov::element::f32}));
+ const auto& f = MHA2DFunction(
+ std::vector<PartialShape>{{12, 64}, {64, 12}, {-1, 12}, {12, 64}},
+ std::vector<ov::element::Type>({ov::element::f32, ov::element::f32, ov::element::f32, ov::element::f32}));
+ execute_and_validate_function(*this, f);
+}
+
+TEST_F(TokenizeMHASnippetsTests, smoke_Snippets_MHA_2D_Partially_Dynamic_Const_B) {
+ const auto& f = MHA2DFunction(
+ std::vector<PartialShape>{{12, 64}, {64, 12}, {-1, 12}, {12, 64}},
+ std::vector<ov::element::Type>({ov::element::f32, ov::element::f32, ov::element::f32, ov::element::f32}));
execute_and_validate_function(*this, f);
}
TEST_F(TokenizeMHASnippetsTests, smoke_Snippets_MHA_4D) {
- const auto &f = MHAFunction(std::vector<PartialShape>{{1, 128, 12, 64}, {1, 128, 12, 64}, {1, 12, 128, 128}, {1, 128, 12, 64}},
- std::vector<ov::element::Type>({ov::element::f32, ov::element::f32, ov::element::f32, ov::element::f32}));
+ const auto& f = MHAFunction(
+ std::vector<PartialShape>{{1, 128, 12, 64}, {1, 128, 12, 64}, {1, 12, 128, 128}, {1, 128, 12, 64}},
+ std::vector<ov::element::Type>({ov::element::f32, ov::element::f32, ov::element::f32, ov::element::f32}));
+ execute_and_validate_function(*this, f);
+}
+
+TEST_F(TokenizeMHASnippetsTests, smoke_Snippets_MHA_4D_Const_B) {
+ const auto& f = MHAConstBFunction(
+ std::vector<PartialShape>{{1, 128, 12, 64}, {1, 128, 12, 64}, {1, 12, 128, 128}, {1, 128, 12, 64}},
+ std::vector<ov::element::Type>({ov::element::f32, ov::element::f32, ov::element::f32, ov::element::f32}),
+ true,
+ true,
+ true,
+ true);
+ execute_and_validate_function(*this, f);
+}
+
+TEST_F(TokenizeMHASnippetsTests, smoke_Snippets_MHA_4D_MatMul1_Const_B_Are_Wei_Blocked) {
+ // This pattern maps to the LIR shape flow from lir_179_MarkLoops_data_flow_out.xml:
+ // Brgemm(Q, K) -> Softmax -> Brgemm(Softmax, V_const), where only MatMul1 B is constant.
+ const auto& f = MHAConstBFunction(
+ std::vector<PartialShape>{{1, 300, 8, 32}, {1, 300, 8, 32}, {1, 8, 300, 300}, {1, 300, 8, 32}},
+ std::vector<ov::element::Type>({ov::element::f32, ov::element::f32, ov::element::f32, ov::element::f32}),
+ false,
+ false,
+ false,
+ true);
execute_and_validate_function(*this, f);
}
TEST_F(TokenizeMHASnippetsTests, smoke_Snippets_MHA_4D_Dynamic) {
- const auto &f = MHAFunction(std::vector<PartialShape>{{-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}},
- std::vector<ov::element::Type>({ov::element::f32, ov::element::f32, ov::element::f32, ov::element::f32}), true, false);
+ const auto& f = MHAFunction(
+ std::vector<PartialShape>{{-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}},
+ std::vector<ov::element::Type>({ov::element::f32, ov::element::f32, ov::element::f32, ov::element::f32}),
+ true,
+ false);
execute_and_validate_function(*this, f);
}
TEST_F(TokenizeMHASnippetsTests, smoke_Snippets_MHA_4D_Partially_Dynamic) {
- const auto &f = MHAFunction(std::vector<PartialShape>{{1, 128, 12, 64}, {1, 128, 12, 64}, {1, 1, -1, 1}, {1, 128, 12, 64}},
- std::vector<ov::element::Type>({ov::element::f32, ov::element::f32, ov::element::f32, ov::element::f32}), true, false);
+ const auto& f = MHAFunction(
+ std::vector<PartialShape>{{1, 128, 12, 64}, {1, 128, 12, 64}, {1, 1, -1, 1}, {1, 128, 12, 64}},
+ std::vector<ov::element::Type>({ov::element::f32, ov::element::f32, ov::element::f32, ov::element::f32}),
+ true,
+ false);
+ execute_and_validate_function(*this, f);
+}
+
+TEST_F(TokenizeMHASnippetsTests, smoke_Snippets_MHA_4D_Partially_Dynamic_Const_B) {
+ const auto& f = MHAConstBFunction(
+ std::vector<PartialShape>{{1, 128, 12, 64}, {1, 128, 12, 64}, {1, 1, -1, 1}, {1, 128, 12, 64}},
+ std::vector<ov::element::Type>({ov::element::f32, ov::element::f32, ov::element::f32, ov::element::f32}),
+ true,
+ false,
+ true,
+ true);
execute_and_validate_function(*this, f);
}
TEST_F(TokenizeMHASnippetsTests, smoke_Snippets_MHA_4D_Partially_Dynamic_Disabled_Dynamic) {
- const auto &f = MHAFunction(std::vector<PartialShape>{{1, 128, 12, 64}, {1, 128, 12, 64}, {1, 1, -1, 1}, {1, 128, 12, 64}},
- std::vector<ov::element::Type>({ov::element::f32, ov::element::f32, ov::element::f32, ov::element::f32}), true, false);
+ const auto& f = MHAFunction(
+ std::vector<PartialShape>{{1, 128, 12, 64}, {1, 128, 12, 64}, {1, 1, -1, 1}, {1, 128, 12, 64}},
+ std::vector<ov::element::Type>({ov::element::f32, ov::element::f32, ov::element::f32, ov::element::f32}),
+ true,
+ false);
+ model = f.getOriginal();
+ common_config = ov::snippets::pass::CommonOptimizations::Config(1, true);
+ mha_config = ov::snippets::pass::TokenizeMHASnippets::Config(
+ ov::snippets::pass::TokenizationConfig(std::numeric_limits<size_t>::max()),
+ true,
+ false,
+ {3, 4});
+ run();
+}
+
+TEST_F(TokenizeMHASnippetsTests, smoke_Snippets_MHA_4D_Partially_Dynamic_Disabled_Dynamic_Const_B) {
+ const auto& f = MHAConstBFunction(
+ std::vector<PartialShape>{{1, 128, 12, 64}, {1, 128, 12, 64}, {1, 1, -1, 1}, {1, 128, 12, 64}},
+ std::vector<ov::element::Type>({ov::element::f32, ov::element::f32, ov::element::f32, ov::element::f32}),
+ true,
+ false,
+ true,
+ true);
model = f.getOriginal();
common_config = ov::snippets::pass::CommonOptimizations::Config(1, true);
mha_config = ov::snippets::pass::TokenizeMHASnippets::Config(
@@ -73,160 +156,309 @@ TEST_F(TokenizeMHASnippetsTests, smoke_Snippets_MHA_4D_Partially_Dynamic_Disable
}
TEST_F(TokenizeMHASnippetsTests, smoke_Snippets_MHA_4D_Dynamic_M) {
- const auto &f = MHAFunction(std::vector<PartialShape>{{1, -1, 12, 64}, {1, 128, 12, 64}, {1, 12, -1, 128}, {1, 128, 12, 64}},
- std::vector<ov::element::Type>({ov::element::f32, ov::element::f32, ov::element::f32, ov::element::f32}), true, false);
+ const auto& f = MHAFunction(
+ std::vector<PartialShape>{{1, -1, 12, 64}, {1, 128, 12, 64}, {1, 12, -1, 128}, {1, 128, 12, 64}},
+ std::vector<ov::element::Type>({ov::element::f32, ov::element::f32, ov::element::f32, ov::element::f32}),
+ true,
+ false);
+ execute_and_validate_function(*this, f);
+}
+
+TEST_F(TokenizeMHASnippetsTests, smoke_Snippets_MHA_4D_Dynamic_M_Const_B) {
+ const auto& f = MHAConstBFunction(
+ std::vector<PartialShape>{{1, -1, 12, 64}, {1, 128, 12, 64}, {1, 12, -1, 128}, {1, 128, 12, 64}},
+ std::vector<ov::element::Type>({ov::element::f32, ov::element::f32, ov::element::f32, ov::element::f32}),
+ true,
+ false,
+ true,
+ true);
execute_and_validate_function(*this, f);
}
TEST_F(TokenizeMHASnippetsTests, smoke_Snippets_MHA_4D_Dynamic_K) {
- const auto &f = MHAFunction(std::vector<PartialShape>{{1, 128, 12, -1}, {1, 128, 12, -1}, {1, 12, 128, 128}, {1, 128, 12, 64}},
- std::vector<ov::element::Type>({ov::element::f32, ov::element::f32, ov::element::f32, ov::element::f32}), true, false);
+ const auto& f = MHAFunction(
+ std::vector<PartialShape>{{1, 128, 12, -1}, {1, 128, 12, -1}, {1, 12, 128, 128}, {1, 128, 12, 64}},
+ std::vector<ov::element::Type>({ov::element::f32, ov::element::f32, ov::element::f32, ov::element::f32}),
+ true,
+ false);
execute_and_validate_function(*this, f);
}
TEST_F(TokenizeMHASnippetsTests, smoke_Snippets_MHA_3D) {
- const auto &f = MHAFunction(std::vector<PartialShape>{{128, 12, 64}, {128, 12, 64}, {12, 128, 128}, {128, 12, 64}},
- std::vector<ov::element::Type>({ov::element::f32, ov::element::f32, ov::element::f32, ov::element::f32}));
+ const auto& f = MHAFunction(
+ std::vector<PartialShape>{{128, 12, 64}, {128, 12, 64}, {12, 128, 128}, {128, 12, 64}},
+ std::vector<ov::element::Type>({ov::element::f32, ov::element::f32, ov::element::f32, ov::element::f32}));
+ execute_and_validate_function(*this, f);
+}
+
+TEST_F(TokenizeMHASnippetsTests, smoke_Snippets_MHA_3D_Const_B) {
+ const auto& f = MHAConstBFunction(
+ std::vector<PartialShape>{{128, 12, 64}, {128, 12, 64}, {12, 128, 128}, {128, 12, 64}},
+ std::vector<ov::element::Type>({ov::element::f32, ov::element::f32, ov::element::f32, ov::element::f32}),
+ true,
+ true,
+ true,
+ true);
execute_and_validate_function(*this, f);
}
TEST_F(TokenizeMHASnippetsTests, smoke_Snippets_MHA_3D_Dynamic) {
- const auto &f = MHAFunction(std::vector<PartialShape>{{-1, -1, -1}, {-1, -1, -1}, {-1, -1, -1}, {-1, -1, -1}},
- std::vector<ov::element::Type>({ov::element::f32, ov::element::f32, ov::element::f32, ov::element::f32}), true, false);
+ const auto& f = MHAFunction(
+ std::vector<PartialShape>{{-1, -1, -1}, {-1, -1, -1}, {-1, -1, -1}, {-1, -1, -1}},
+ std::vector<ov::element::Type>({ov::element::f32, ov::element::f32, ov::element::f32, ov::element::f32}),
+ true,
+ false);
execute_and_validate_function(*this, f);
}
TEST_F(TokenizeMHASnippetsTests, smoke_Snippets_MHA_with_MatMul0_Transpose) {
- const auto &f = MHAMatMul0TransposeFunction(std::vector<PartialShape>{{1, 128, 12, 64}, {1, 128, 12, 64}, {1, 12, 128, 128}, {1, 128, 12, 64}},
- std::vector<ov::element::Type>({ov::element::f32, ov::element::f32, ov::element::f32, ov::element::f32}));
+ const auto& f = MHAMatMul0TransposeFunction(
+ std::vector<PartialShape>{{1, 128, 12, 64}, {1, 128, 12, 64}, {1, 12, 128, 128}, {1, 128, 12, 64}},
+ std::vector<ov::element::Type>({ov::element::f32, ov::element::f32, ov::element::f32, ov::element::f32}));
+ execute_and_validate_function(*this, f);
+}
+
+TEST_F(TokenizeMHASnippetsTests, smoke_Snippets_MHA_with_MatMul0_Transpose_Const_B) {
+ const auto& f = MHAMatMul0TransposeFunction(
+ std::vector<PartialShape>{{1, 128, 12, 64}, {1, 128, 12, 64}, {1, 12, 128, 128}, {1, 128, 12, 64}},
+ std::vector<ov::element::Type>({ov::element::f32, ov::element::f32, ov::element::f32, ov::element::f32}),
+ true);
execute_and_validate_function(*this, f);
}
TEST_F(TokenizeMHASnippetsTests, smoke_Snippets_MHA_with_MatMul0_Transpose_Dynamic) {
- const auto &f = MHAMatMul0TransposeFunction(std::vector<PartialShape>{{-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}},
- std::vector<ov::element::Type>({ov::element::f32, ov::element::f32, ov::element::f32, ov::element::f32}),
- false);
+ const auto& f = MHAMatMul0TransposeFunction(
+ std::vector<PartialShape>{{-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}},
+ std::vector<ov::element::Type>({ov::element::f32, ov::element::f32, ov::element::f32, ov::element::f32}),
+ false);
execute_and_validate_function(*this, f);
}
TEST_F(TokenizeMHASnippetsTests, smoke_Snippets_MHA_with_int_Matmuls) {
- const auto &f = MHAINT8MatMulTypeRelaxedFunction(std::vector<PartialShape>{{1, 128, 12, 64}, {1, 128, 12, 64}, {1, 12, 128, 128}, {1, 128, 12, 64}});
+ const auto& f = MHAINT8MatMulTypeRelaxedFunction(
+ std::vector<PartialShape>{{1, 128, 12, 64}, {1, 128, 12, 64}, {1, 12, 128, 128}, {1, 128, 12, 64}});
execute_and_validate_function(*this, f);
}
TEST_F(TokenizeMHASnippetsTests, smoke_Snippets_MHA_Transpose_extraction) {
- const auto& f = MHATransposedInputFunction(std::vector<PartialShape>{{1, 128, 12, 64}, {1, 128, 12, 64}, {1, 128, 12, 64}}, true);
+ const auto& f =
+ MHATransposedInputFunction(std::vector<PartialShape>{{1, 128, 12, 64}, {1, 128, 12, 64}, {1, 128, 12, 64}},
+ true);
execute_and_validate_function(*this, f);
}
TEST_F(TokenizeMHASnippetsTests, smoke_Snippets_MHA_Dynamic_Transpose_extraction) {
- const auto& f = MHATransposedInputFunction(std::vector<PartialShape>{{-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}}, true);
+ const auto& f =
+ MHATransposedInputFunction(std::vector<PartialShape>{{-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}},
+ true);
execute_and_validate_function(*this, f);
}
TEST_F(TokenizeMHASnippetsTests, smoke_Snippets_MHA_Transpose_extraction_and_unsupported_existing_transpose) {
- const auto& f = MHATransposedInputFunction(std::vector<PartialShape>{{1, 128, 12, 64}, {1, 12, 64, 128}, {1, 128, 12, 64}}, true,
- std::vector<int64_t>{0, 3, 1, 2});
+ const auto& f =
+ MHATransposedInputFunction(std::vector<PartialShape>{{1, 128, 12, 64}, {1, 12, 64, 128}, {1, 128, 12, 64}},
+ true,
+ std::vector<int64_t>{0, 3, 1, 2});
execute_and_validate_function(*this, f);
}
TEST_F(TokenizeMHASnippetsTests, smoke_Snippets_MHA_Dynamic_Transpose_extraction_and_unsupported_existing_transpose) {
- const auto& f = MHATransposedInputFunction(std::vector<PartialShape>{{-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}}, true,
- std::vector<int64_t>{0, 3, 1, 2});
+ const auto& f =
+ MHATransposedInputFunction(std::vector<PartialShape>{{-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}},
+ true,
+ std::vector<int64_t>{0, 3, 1, 2});
execute_and_validate_function(*this, f);
}
TEST_F(TokenizeMHASnippetsTests, smoke_Snippets_MHA_Transpose_fusion) {
- const auto& f = MHATransposedInputFunction(std::vector<PartialShape>{{1, 128, 12, 64}, {1, 64, 128, 12}, {1, 128, 12, 64}}, false,
- std::vector<int64_t>{0, 2, 1, 3});
+ const auto& f =
+ MHATransposedInputFunction(std::vector<PartialShape>{{1, 128, 12, 64}, {1, 64, 128, 12}, {1, 128, 12, 64}},
+ false,
+ std::vector<int64_t>{0, 2, 1, 3});
execute_and_validate_function(*this, f);
}
TEST_F(TokenizeMHASnippetsTests, smoke_Snippets_MHA_Dynamic_Transpose_fusion) {
- const auto& f = MHATransposedInputFunction(std::vector<PartialShape>{{-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}}, false,
- std::vector<int64_t>{0, 2, 1, 3});
+ const auto& f =
+ MHATransposedInputFunction(std::vector<PartialShape>{{-1, -1, -1, -1}, {-1, -1, -1, -1}, {-1, -1, -1, -1}},
+ false,
+ std::vector<int64_t>{0, 2, 1, 3});
execute_and_validate_function(*this, f);
}
TEST_F(TokenizeMHASnippetsTests, smoke_Snippets_MHA3D_SplitM) {
- const auto& f = MHASplitMFunction(std::vector<PartialShape>{{128, 12, 64}, {128, 12, 64}, {12, 128, 128}, {128, 12, 64}},
- std::vector<ov::element::Type>({ov::element::f32, ov::element::f32, ov::element::f32, ov::element::f32}),
- std::vector<Shape>{{2, 64, 12, 64}, {12, 1, 64, 128}, {12, 2, 64, 128}, {1, 128, 12, 64}, {128, 12, 64}},
- false);
+ const auto& f = MHASplitMFunction(
+ std::vector<PartialShape>{{128, 12, 64}, {128, 12, 64}, {12, 128, 128}, {128, 12, 64}},
+ std::vector<ov::element::Type>({ov::element::f32, ov::element::f32, ov::element::f32, ov::element::f32}),
+ std::vector<Shape>{{2, 64, 12, 64}, {12, 1, 64, 128}, {12, 2, 64, 128}, {1, 128, 12, 64}, {128, 12, 64}},
+ false);
+ common_config = get_default_common_optimizations_config();
+ common_config.set_concurrency(24);
+ execute_and_validate_function(*this, f);
+}
+
+TEST_F(TokenizeMHASnippetsTests, smoke_Snippets_MHA3D_SplitM_Const_B) {
+ const auto& f = MHASplitMFunction(
+ std::vector<PartialShape>{{128, 12, 64}, {128, 12, 64}, {12, 128, 128}, {128, 12, 64}},
+ std::vector<ov::element::Type>({ov::element::f32, ov::element::f32, ov::element::f32, ov::element::f32}),
+ std::vector<Shape>{{2, 64, 12, 64}, {12, 1, 64, 128}, {12, 2, 64, 128}, {1, 128, 12, 64}, {128, 12, 64}},
+ false);
common_config = get_default_common_optimizations_config();
common_config.set_concurrency(24);
execute_and_validate_function(*this, f);
}
TEST_F(TokenizeMHASnippetsTests, smoke_Snippets_MHA3D_SplitM_withMul) {
- const auto& f = MHASplitMFunction(std::vector<PartialShape>{{128, 12, 64}, {128, 12, 64}, {12, 128, 128}, {128, 12, 64}},
- std::vector<ov::element::Type>({ov::element::f32, ov::element::f32, ov::element::f32, ov::element::f32}),
- std::vector<Shape>{{4, 32, 12, 64}, {12, 1, 64, 128}, {12, 4, 32, 128}, {1, 128, 12, 64}, {128, 12, 64}},
- true);
+ const auto& f = MHASplitMFunction(
+ std::vector<PartialShape>{{128, 12, 64}, {128, 12, 64}, {12, 128, 128}, {128, 12, 64}},
+ std::vector<ov::element::Type>({ov::element::f32, ov::element::f32, ov::element::f32, ov::element::f32}),
+ std::vector<Shape>{{4, 32, 12, 64}, {12, 1, 64, 128}, {12, 4, 32, 128}, {1, 128, 12, 64}, {128, 12, 64}},
+ true);
+ common_config = get_default_common_optimizations_config();
+ common_config.set_concurrency(16);
+ execute_and_validate_function(*this, f);
+}
+
+TEST_F(TokenizeMHASnippetsTests, smoke_Snippets_MHA3D_SplitM_withMul_Const_B) {
+ const auto& f = MHASplitMFunction(
+ std::vector<PartialShape>{{128, 12, 64}, {128, 12, 64}, {12, 128, 128}, {128, 12, 64}},
+ std::vector<ov::element::Type>({ov::element::f32, ov::element::f32, ov::element::f32, ov::element::f32}),
+ std::vector<Shape>{{4, 32, 12, 64}, {12, 1, 64, 128}, {12, 4, 32, 128}, {1, 128, 12, 64}, {128, 12, 64}},
+ true);
common_config = get_default_common_optimizations_config();
common_config.set_concurrency(16);
execute_and_validate_function(*this, f);
}
TEST_F(TokenizeMHASnippetsTests, smoke_Snippets_MHA4D_SplitM) {
- const auto& f = MHASplitMFunction(std::vector<PartialShape>{{1, 384, 16, 64}, {1, 384, 16, 64}, {1, 1, 1, 384}, {1, 384, 16, 64}},
- std::vector<ov::element::Type>({ov::element::f32, ov::element::f32, ov::element::f32, ov::element::f32}),
- std::vector<Shape>{{1, 12, 32, 16, 64}, {1, 16, 1, 64, 384}, {1, 1, 1, 1, 384}, {1, 1, 384, 16, 64}, {1, 384, 16, 64}},
- false);
+ const auto& f = MHASplitMFunction(
+ std::vector<PartialShape>{{1, 384, 16, 64}, {1, 384, 16, 64}, {1, 1, 1, 384}, {1, 384, 16, 64}},
+ std::vector<ov::element::Type>({ov::element::f32, ov::element::f32, ov::element::f32, ov::element::f32}),
+ std::vector<Shape>{{1, 12, 32, 16, 64},
+ {1, 16, 1, 64, 384},
+ {1, 1, 1, 1, 384},
+ {1, 1, 384, 16, 64},
+ {1, 384, 16, 64}},
+ false);
+ common_config = get_default_common_optimizations_config();
+ common_config.set_concurrency(60);
+ execute_and_validate_function(*this, f);
+}
+
+TEST_F(TokenizeMHASnippetsTests, smoke_Snippets_MHA4D_SplitM_Const_B) {
+ const auto& f = MHASplitMFunction(
+ std::vector<PartialShape>{{1, 384, 16, 64}, {1, 384, 16, 64}, {1, 1, 1, 384}, {1, 384, 16, 64}},
+ std::vector<ov::element::Type>({ov::element::f32, ov::element::f32, ov::element::f32, ov::element::f32}),
+ std::vector<Shape>{{1, 12, 32, 16, 64},
+ {1, 16, 1, 64, 384},
+ {1, 1, 1, 1, 384},
+ {1, 1, 384, 16, 64},
+ {1, 384, 16, 64}},
+ false);
common_config = get_default_common_optimizations_config();
common_config.set_concurrency(60);
execute_and_validate_function(*this, f);
}
TEST_F(TokenizeMHASnippetsTests, smoke_Snippets_MHA4D_SplitM_withMul) {
- const auto& f = MHASplitMFunction(std::vector<PartialShape>{{1, 384, 16, 64}, {1, 384, 16, 64}, {1, 1, 1, 384}, {1, 384, 16, 64}},
- std::vector<ov::element::Type>({ov::element::f32, ov::element::f32, ov::element::f32, ov::element::f32}),
- std::vector<Shape>{{1, 12, 32, 16, 64}, {1, 16, 1, 64, 384}, {1, 1, 1, 1, 384}, {1, 1, 384, 16, 64}, {1, 384, 16, 64}},
- true);
+ const auto& f = MHASplitMFunction(
+ std::vector<PartialShape>{{1, 384, 16, 64}, {1, 384, 16, 64}, {1, 1, 1, 384}, {1, 384, 16, 64}},
+ std::vector<ov::element::Type>({ov::element::f32, ov::element::f32, ov::element::f32, ov::element::f32}),
+ std::vector<Shape>{{1, 12, 32, 16, 64},
+ {1, 16, 1, 64, 384},
+ {1, 1, 1, 1, 384},
+ {1, 1, 384, 16, 64},
+ {1, 384, 16, 64}},
+ true);
+ common_config = get_default_common_optimizations_config();
+ common_config.set_concurrency(60);
+ execute_and_validate_function(*this, f);
+}
+
+TEST_F(TokenizeMHASnippetsTests, smoke_Snippets_MHA4D_SplitM_withMul_Const_B) {
+ const auto& f = MHASplitMFunction(
+ std::vector<PartialShape>{{1, 384, 16, 64}, {1, 384, 16, 64}, {1, 1, 1, 384}, {1, 384, 16, 64}},
+ std::vector<ov::element::Type>({ov::element::f32, ov::element::f32, ov::element::f32, ov::element::f32}),
+ std::vector<Shape>{{1, 12, 32, 16, 64},
+ {1, 16, 1, 64, 384},
+ {1, 1, 1, 1, 384},
+ {1, 1, 384, 16, 64},
+ {1, 384, 16, 64}},
+ true);
common_config = get_default_common_optimizations_config();
common_config.set_concurrency(60);
execute_and_validate_function(*this, f);
}
TEST_F(TokenizeMHASnippetsTests, smoke_Snippets_MHAWOTranspose_SplitM) {
- const auto& f = MHAWOTransposeSplitMFunction(std::vector<PartialShape>{{10, 9216, 128}, {10, 128, 9216}, {10, 9216, 128}},
- std::vector<ov::element::Type>({ov::element::f32, ov::element::f32, ov::element::f32}),
- std::vector<Shape>{{10, 18, 512, 128}, {10, 1, 128, 9216}, {10, 1, 9216, 128}, {10, 9216, 128}});
+ const auto& f = MHAWOTransposeSplitMFunction(
+ std::vector<PartialShape>{{10, 9216, 128}, {10, 128, 9216}, {10, 9216, 128}},
+ std::vector<ov::element::Type>({ov::element::f32, ov::element::f32, ov::element::f32}),
+ std::vector<Shape>{{10, 18, 512, 128}, {10, 1, 128, 9216}, {10, 1, 9216, 128}, {10, 9216, 128}});
+ common_config = get_default_common_optimizations_config();
+ common_config.set_concurrency(18);
+ execute_and_validate_function(*this, f);
+}
+
+TEST_F(TokenizeMHASnippetsTests, smoke_Snippets_MHAWOTranspose_SplitM_Const_B) {
+ const auto& f = MHAWOTransposeSplitMFunction(
+ std::vector<PartialShape>{{10, 9216, 128}, {10, 128, 9216}, {10, 9216, 128}},
+ std::vector<ov::element::Type>({ov::element::f32, ov::element::f32, ov::element::f32}),
+ std::vector<Shape>{{10, 18, 512, 128}, {10, 1, 128, 9216}, {10, 1, 9216, 128}, {10, 9216, 128}});
common_config = get_default_common_optimizations_config();
common_config.set_concurrency(18);
execute_and_validate_function(*this, f);
}
TEST_F(TokenizeMHASnippetsTests, smoke_Snippets_MHA_SplitM_AlmostAllThreads) {
- const auto& f = MHAWOTransposeSplitMFunction(std::vector<PartialShape>{{5, 30, 32}, {5, 32, 30}, {5, 30, 32}},
- std::vector<ov::element::Type>({ov::element::f32, ov::element::f32, ov::element::f32}),
- std::vector<Shape>{{5, 10, 3, 32}, {5, 1, 32, 30}, {5, 1, 30, 32}, {5, 30, 32}});
+ const auto& f = MHAWOTransposeSplitMFunction(
+ std::vector<PartialShape>{{5, 30, 32}, {5, 32, 30}, {5, 30, 32}},
+ std::vector<ov::element::Type>({ov::element::f32, ov::element::f32, ov::element::f32}),
+ std::vector<Shape>{{5, 10, 3, 32}, {5, 1, 32, 30}, {5, 1, 30, 32}, {5, 30, 32}});
+ common_config = get_default_common_optimizations_config();
+ common_config.set_concurrency(32);
+ execute_and_validate_function(*this, f);
+}
+
+TEST_F(TokenizeMHASnippetsTests, smoke_Snippets_MHA_SplitM_AlmostAllThreads_Const_B) {
+ const auto& f = MHAWOTransposeSplitMFunction(
+ std::vector<PartialShape>{{5, 30, 32}, {5, 32, 30}, {5, 30, 32}},
+ std::vector<ov::element::Type>({ov::element::f32, ov::element::f32, ov::element::f32}),
+ std::vector<Shape>{{5, 10, 3, 32}, {5, 1, 32, 30}, {5, 1, 30, 32}, {5, 30, 32}});
common_config = get_default_common_optimizations_config();
common_config.set_concurrency(32);
execute_and_validate_function(*this, f);
}
TEST_F(TokenizeMHASnippetsTests, smoke_Snippets_MHA_4D_SplitM_DynamicParameter) {
- const auto &f = MHAFunction(std::vector<PartialShape>{{1, 128, 16, 64}, {1, 128, 16, 64}, {1, 16, 128, -1}, {1, 128, 16, 64}},
- std::vector<ov::element::Type>({ov::element::f32, ov::element::f32, ov::element::f32, ov::element::f32}), false, false);
+ const auto& f = MHAFunction(
+ std::vector<PartialShape>{{1, 128, 16, 64}, {1, 128, 16, 64}, {1, 16, 128, -1}, {1, 128, 16, 64}},
+ std::vector<ov::element::Type>({ov::element::f32, ov::element::f32, ov::element::f32, ov::element::f32}),
+ false,
+ false);
common_config = get_default_common_optimizations_config();
common_config.set_concurrency(32);
execute_and_validate_function(*this, f);
}
TEST_F(TokenizeMHASnippetsTests, smoke_Snippets_MHASelect_SplitM) {
- const auto& f = MHASelectSplitMFunction(std::vector<PartialShape>{{8, 512, 18}, {8, 18, 64}, {1, 512, 64}, {1, 1, 64}, {8, 64, 512}},
- std::vector<Shape>{{8, 2, 256, 18}, {8, 1, 18, 64}, {1, 2, 256, 64}, {1, 1, 1, 64},
- {8, 1, 64, 512}, {8, 512, 512}});
+ const auto& f = MHASelectSplitMFunction(
+ std::vector<PartialShape>{{8, 512, 18}, {8, 18, 64}, {1, 512, 64}, {1, 1, 64}, {8, 64, 512}},
+ std::vector<Shape>{{8, 2, 256, 18},
+ {8, 1, 18, 64},
+ {1, 2, 256, 64},
+ {1, 1, 1, 64},
+ {8, 1, 64, 512},
+ {8, 512, 512}});
common_config = get_default_common_optimizations_config();
common_config.set_concurrency(16);
execute_and_validate_function(*this, f);
}
TEST_F(TokenizeMHASnippetsTests, smoke_Snippets_MHASelect_SplitM_ScalarParams) {
- const auto& f = MHASelectSplitMFunction(std::vector<PartialShape>{{8, 512, 18}, {8, 18, 64}, {1}, {64}, {8, 64, 512}},
- std::vector<Shape>{{8, 2, 256, 18}, {8, 1, 18, 64}, {}, {},
- {8, 1, 64, 512}, {8, 512, 512}});
+ const auto& f = MHASelectSplitMFunction(
+ std::vector<PartialShape>{{8, 512, 18}, {8, 18, 64}, {1}, {64}, {8, 64, 512}},
+ std::vector<Shape>{{8, 2, 256, 18}, {8, 1, 18, 64}, {}, {}, {8, 1, 64, 512}, {8, 512, 512}});
common_config = get_default_common_optimizations_config();
common_config.set_concurrency(16);
execute_and_validate_function(*this, f);
@@ -237,7 +469,8 @@ TEST_F(TokenizeMHASnippetsTests, smoke_Snippets_MHA_Reshape_extraction) {
{400, 80, 196},
{400, 14, 14, 14},
{400, 14, 14, 1, 14},
- {400, 196, 80}}, true);
+ {400, 196, 80}},
+ true);
execute_and_validate_function(*this, f);
}
diff --git a/src/plugins/intel_cpu/src/transformations/snippets/x64/pass/lowered/external_repacking_adjuster.cpp b/src/plugins/intel_cpu/src/transformations/snippets/x64/pass/lowered/external_repacking_adjuster.cpp
index bd3c00cfa2c..dae2907e84e 100644
--- a/src/plugins/intel_cpu/src/transformations/snippets/x64/pass/lowered/external_repacking_adjuster.cpp
+++ b/src/plugins/intel_cpu/src/transformations/snippets/x64/pass/lowered/external_repacking_adjuster.cpp
@@ -12,6 +12,7 @@
#include <functional>
#include <memory>
#include <numeric>
+#include <utility>
#include "cache/multi_cache.h"
#include "cpu_shape.h"
@@ -43,19 +44,23 @@ BrgemmExternalRepackingAdjuster::BrgemmExternalRepackingAdjuster(const ov::snipp
const auto& cpu_config = ov::as_type_ptr<CPURuntimeConfig>(m_configurator->get_config());
const auto& input_repackers = cpu_config->input_repackers;
const auto& params = linear_ir->get_parameters();
- for (const auto& [idx, _] : input_repackers) {
+ for (const auto& [idx, input_repacker] : input_repackers) {
OPENVINO_ASSERT(idx < params.size(), "Incorrect index of repacked input");
- m_executors[idx] = create_executor(params[idx], configurator->get_cache());
+ auto config = RepackedInputConfig{get_kernel_config(params[idx])};
+ // `desc() == nullptr` means repacking has to be executed at runtime.
+ // If descriptor is already set, weights were prepacked beforehand and only offsets must be adjusted.
+ if (!input_repacker.desc()) {
+ config.executor = create_executor(config.kernel_config, configurator->get_cache());
+ config.needs_runtime_repacking = true;
+ }
+ m_repacked_inputs.emplace(idx, std::move(config));
}
- OPENVINO_ASSERT(input_repackers.size() == m_executors.size(), "Incorrect count of repacked inputs");
+ OPENVINO_ASSERT(input_repackers.size() == m_repacked_inputs.size(), "Incorrect count of repacked inputs");
}
-BrgemmExternalRepackingAdjuster::RepackExecutorPtr BrgemmExternalRepackingAdjuster::create_executor(
- const ov::snippets::lowered::ExpressionPtr& param,
- const ov::intel_cpu::MultiCacheWeakPtr& cache) {
- RepackExecutorPtr executor = nullptr;
-
+BrgemmCopyBKernelConfig BrgemmExternalRepackingAdjuster::get_kernel_config(
+ const ov::snippets::lowered::ExpressionPtr& param) {
const auto& shape_infer_consumers = ov::snippets::utils::get_first_child_shape_infer_expr_seq(param);
const auto& out =
shape_infer_consumers.empty() ? param->get_output_port(0) : shape_infer_consumers.back()->get_output_port(0);
@@ -71,13 +76,16 @@ BrgemmExternalRepackingAdjuster::RepackExecutorPtr BrgemmExternalRepackingAdjust
if (brgemm_config.with_wei_repacking() && consumer.get_index() == 1) {
OPENVINO_ASSERT(brgemm_config.with_compensations() == false,
"External repacking for BrgemmCPU with compensations is not supported.");
- const auto kernel_config = BrgemmCopyBKernelConfig(brgemm_config);
- executor = std::make_shared<BrgemmCopyBKernelExecutor>(cache, kernel_config);
- break;
+ return BrgemmCopyBKernelConfig(brgemm_config);
}
}
- OPENVINO_ASSERT(executor, "The executor of the repacked input must be inited!");
- return executor;
+ OPENVINO_THROW("The config of repacked input must be initialized");
+}
+
+BrgemmExternalRepackingAdjuster::RepackExecutorPtr BrgemmExternalRepackingAdjuster::create_executor(
+ const BrgemmCopyBKernelConfig& kernel_config,
+ const ov::intel_cpu::MultiCacheWeakPtr& cache) {
+ return std::make_shared<BrgemmCopyBKernelExecutor>(cache, kernel_config);
}
CpuBlockedMemoryDescPtr BrgemmExternalRepackingAdjuster::get_desc(const ov::snippets::VectorDims& planar_shape,
@@ -121,9 +129,14 @@ bool BrgemmExternalRepackingAdjuster::run(const snippets::lowered::LinearIR& lin
const auto& cpu_config = ov::as_type_ptr<CPURuntimeConfig>(m_configurator->get_config());
size_t data_size = 0;
- for (const auto& p : m_executors) {
- const auto& i = p.first;
- const auto& executor = p.second;
+ bool has_runtime_repacking = false;
+ for (const auto& [i, repacked_input] : m_repacked_inputs) {
+ if (!repacked_input.needs_runtime_repacking) {
+ continue;
+ }
+ has_runtime_repacking = true;
+ const auto& executor = repacked_input.executor;
+ OPENVINO_ASSERT(executor, "Input marked for runtime repacking has no executor");
const auto& shape = cpu_config->io_shapes[i];
const auto& layout = cpu_config->io_layouts[i];
@@ -156,18 +169,25 @@ bool BrgemmExternalRepackingAdjuster::run(const snippets::lowered::LinearIR& lin
data_size += src_data + dst_data;
}
- const auto cache_size = dnnl::utils::get_cache_size(1, true) + dnnl::utils::get_cache_size(2, true);
- const auto fit_into_cache = data_size < cache_size;
- // Heuristic: If external repacking data doesn't fit in the caches L1 and L2,
- // external repacking should be executed in seperate parallel section before kernel execution.
- cpu_config->repacking_impl_type = fit_into_cache ? CPURuntimeConfig::RepackingImplType::IN_PARALLEL
- : CPURuntimeConfig::RepackingImplType::SEPARATE;
+ if (!has_runtime_repacking) {
+ cpu_config->repacking_impl_type = CPURuntimeConfig::RepackingImplType::NONE;
+ } else {
+ const auto cache_size = dnnl::utils::get_cache_size(1, true) + dnnl::utils::get_cache_size(2, true);
+ const auto fit_into_cache = data_size < cache_size;
+ // Heuristic: If external repacking data doesn't fit in the caches L1 and L2,
+ // external repacking should be executed in seperate parallel section before kernel execution.
+ cpu_config->repacking_impl_type = fit_into_cache ? CPURuntimeConfig::RepackingImplType::IN_PARALLEL
+ : CPURuntimeConfig::RepackingImplType::SEPARATE;
+ }
const auto is_impl_parallel = cpu_config->repacking_impl_type == CPURuntimeConfig::RepackingImplType::IN_PARALLEL;
- for (const auto& p : m_executors) {
- const auto& i = p.first;
- const auto& executor = p.second;
+ for (const auto& [i, repacked_input] : m_repacked_inputs) {
+ if (!repacked_input.needs_runtime_repacking) {
+ continue;
+ }
+ const auto& executor = repacked_input.executor;
+ OPENVINO_ASSERT(executor, "Input marked for runtime repacking has no executor");
const auto& shape = cpu_config->io_shapes[i];
const auto& layout = cpu_config->io_layouts[i];
@@ -244,7 +264,40 @@ bool BrgemmExternalRepackingAdjuster::run(const snippets::lowered::LinearIR& lin
}
const auto out_offsets = cpu_config->io_data_offsets[i];
- input_repacker = InputRepacker(p.second->get_kernel(), dst_desc, in_offsets, out_offsets);
+ input_repacker = InputRepacker(executor->get_kernel(), dst_desc, in_offsets, out_offsets);
+ }
+
+ // Inputs prepacked during model preparation do not need runtime repacking kernels.
+ // For them, we only adjust blocked output offsets for current runtime shapes.
+ for (const auto& [i, repacked_input] : m_repacked_inputs) {
+ if (repacked_input.needs_runtime_repacking) {
+ continue;
+ }
+ const auto& config = repacked_input.kernel_config;
+ const auto& shape = cpu_config->io_shapes[i];
+ const auto& layout = cpu_config->io_layouts[i];
+ const auto& prc = linear_ir.get_parameters()[i]->get_node()->get_output_element_type(0);
+ const auto planar_shape = ov::snippets::utils::get_planar_vdims(shape, layout);
+ const auto dst_desc = get_desc(planar_shape,
+ prc,
+ config.get_wei_K_blk(),
+ config.get_wei_N_blk(),
+ config.are_wei_blocked(),
+ config.is_transposed_B());
+
+ const auto blocked_dims = dst_desc->getBlockDims();
+ const auto inner_blocks_num = blocked_dims.size() - planar_shape.size();
+ const auto rank = cpu_config->io_data_offsets[i].size() + inner_blocks_num;
+ OPENVINO_ASSERT(rank >= blocked_dims.size(), "Incorrect target rank for dst offsets");
+
+ ov::snippets::VectorDims shape_for_offset(rank - blocked_dims.size(), 1);
+ shape_for_offset.insert(shape_for_offset.end(), blocked_dims.begin(), blocked_dims.end());
+
+ ov::snippets::VectorDims dst_offsets;
+ ov::snippets::utils::init_strides(shape_for_offset, rank, prc.size(), 0, dst_offsets);
+ dst_offsets.resize(dst_offsets.size() - inner_blocks_num);
+ cpu_config->io_data_offsets[i] = std::move(dst_offsets);
+ cpu_config->input_repackers.erase(i);
}
return true;
diff --git a/src/plugins/intel_cpu/src/transformations/snippets/x64/pass/lowered/external_repacking_adjuster.hpp b/src/plugins/intel_cpu/src/transformations/snippets/x64/pass/lowered/external_repacking_adjuster.hpp
index 0febb9cc1b2..b285e5b6ad5 100644
--- a/src/plugins/intel_cpu/src/transformations/snippets/x64/pass/lowered/external_repacking_adjuster.hpp
+++ b/src/plugins/intel_cpu/src/transformations/snippets/x64/pass/lowered/external_repacking_adjuster.hpp
@@ -34,11 +34,17 @@ public:
bool run(const snippets::lowered::LinearIR& linear_ir) override;
bool applicable() const override {
- return !m_executors.empty();
+ return !m_repacked_inputs.empty();
}
private:
using RepackExecutorPtr = std::shared_ptr<BrgemmCopyBKernelExecutor>;
+ struct RepackedInputConfig {
+ BrgemmCopyBKernelConfig kernel_config;
+ RepackExecutorPtr executor = nullptr;
+ bool needs_runtime_repacking = false;
+ };
+
static CpuBlockedMemoryDescPtr get_desc(const ov::snippets::VectorDims& planar_shape,
const ov::element::Type& prc,
size_t wei_k_blk,
@@ -52,11 +58,12 @@ private:
size_t N,
size_t K);
- static RepackExecutorPtr create_executor(const ov::snippets::lowered::ExpressionPtr& param,
+ static BrgemmCopyBKernelConfig get_kernel_config(const ov::snippets::lowered::ExpressionPtr& param);
+ static RepackExecutorPtr create_executor(const BrgemmCopyBKernelConfig& kernel_config,
const ov::intel_cpu::MultiCacheWeakPtr& cache);
static const size_t brgemm_kernel_rank;
- std::unordered_map<size_t, RepackExecutorPtr> m_executors;
+ std::unordered_map<size_t, RepackedInputConfig> m_repacked_inputs;
};
} // namespace ov::intel_cpu::pass
diff --git a/src/plugins/intel_cpu/src/transformations/snippets/x64/pass/repack_matmul_weights.cpp b/src/plugins/intel_cpu/src/transformations/snippets/x64/pass/repack_matmul_weights.cpp
index c599dc89c08..d3f4b16fec5 100644
--- a/src/plugins/intel_cpu/src/transformations/snippets/x64/pass/repack_matmul_weights.cpp
+++ b/src/plugins/intel_cpu/src/transformations/snippets/x64/pass/repack_matmul_weights.cpp
@@ -13,6 +13,7 @@
#include "cpu_memory.h"
#include "cpu_types.h"
#include "dnnl_extension_utils.h"
+#include "memory_desc/cpu_blocked_memory_desc.h"
#include "memory_desc/cpu_memory_desc_utils.h"
#include "memory_desc/dnnl_memory_desc.h"
#include "nodes/executors/dnnl/dnnl_utils.hpp"
@@ -40,7 +41,7 @@ DnnlMemoryDescPtr RepackMatMulWeights::get_src_desc(const VectorDims& shape,
std::make_shared<CpuBlockedMemoryDesc>(brgemm_config.orig_wei_dt(), planar_shape, shape, layout));
}
-DnnlMemoryDescPtr RepackMatMulWeights::get_dst_desc(const Shape& shape, const BrgemmConfig& brgemm_config) {
+CpuBlockedMemoryDescPtr RepackMatMulWeights::get_dst_cpu_desc(const Shape& shape, const BrgemmConfig& brgemm_config) {
const auto [blocked_dims, blocked_order] =
brgemm_utils::repacking::get_wei_blocked_shape(shape.getStaticDims(),
brgemm_config.wei_dt(),
@@ -48,8 +49,11 @@ DnnlMemoryDescPtr RepackMatMulWeights::get_dst_desc(const Shape& shape, const Br
brgemm_config.wei_n_blk(),
brgemm_config.are_wei_blocked());
- return MemoryDescUtils::convertToDnnlMemoryDesc(
- std::make_shared<CpuBlockedMemoryDesc>(brgemm_config.wei_dt(), shape, blocked_dims, blocked_order));
+ return std::make_shared<CpuBlockedMemoryDesc>(brgemm_config.wei_dt(), shape, blocked_dims, blocked_order);
+}
+
+DnnlMemoryDescPtr RepackMatMulWeights::get_dst_desc(const Shape& shape, const BrgemmConfig& brgemm_config) {
+ return MemoryDescUtils::convertToDnnlMemoryDesc(get_dst_cpu_desc(shape, brgemm_config));
}
bool RepackMatMulWeights::run_on_model(const std::shared_ptr<ov::Model>& model) {
@@ -106,12 +110,10 @@ bool RepackMatMulWeights::run_on_model(const std::shared_ptr<ov::Model>& model)
m_context->getWeightsCache(),
nullptr,
m_context->getCpuParallel()->get_thread_pool());
- weights_idxs.insert(i);
- }
- // Removed already repacked inputs: remaining inputs will be repacked in runtime configurator on inference stage
- for (const auto& weight_idx : weights_idxs) {
- m_input_repackers.erase(weight_idx);
+ m_input_repackers[i] =
+ InputRepacker(nullptr, get_dst_cpu_desc(src_mem_desc->getShape(), brgemm_config), {}, {});
+ weights_idxs.insert(i);
}
return !weights_idxs.empty();
diff --git a/src/plugins/intel_cpu/src/transformations/snippets/x64/pass/repack_matmul_weights.hpp b/src/plugins/intel_cpu/src/transformations/snippets/x64/pass/repack_matmul_weights.hpp
index e12d7e923ea..c8108ca4a45 100644
--- a/src/plugins/intel_cpu/src/transformations/snippets/x64/pass/repack_matmul_weights.hpp
+++ b/src/plugins/intel_cpu/src/transformations/snippets/x64/pass/repack_matmul_weights.hpp
@@ -41,6 +41,8 @@ private:
static DnnlMemoryDescPtr get_src_desc(const VectorDims& shape,
const VectorDims& layout,
const brgemm_utils::BrgemmConfig& brgemm_config);
+ static CpuBlockedMemoryDescPtr get_dst_cpu_desc(const Shape& shape,
+ const brgemm_utils::BrgemmConfig& brgemm_config);
static DnnlMemoryDescPtr get_dst_desc(const Shape& shape, const brgemm_utils::BrgemmConfig& brgemm_config);
const GraphContext::CPtr m_context;
diff --git a/src/plugins/intel_cpu/tests/functional/shared_tests_instances/snippets/mha.cpp b/src/plugins/intel_cpu/tests/functional/shared_tests_instances/snippets/mha.cpp
index 53def099a0f..a19cc2f2ba9 100644
--- a/src/plugins/intel_cpu/tests/functional/shared_tests_instances/snippets/mha.cpp
+++ b/src/plugins/intel_cpu/tests/functional/shared_tests_instances/snippets/mha.cpp
@@ -52,6 +52,10 @@ std::vector<std::vector<InputShape>> transposedShape_4D(bool with_static = true,
return shapes;
}
+std::vector<std::vector<InputShape>> transposedShape_4D_matmul1_const_b() {
+ return SNIPPETS_TESTS_STATIC_SHAPES({{1, 300, 8, 32}, {1, 300, 8, 32}, {1, 8, 300, 300}, {1, 300, 8, 32}});
+}
+
std::vector<std::vector<InputShape>> transposedShape_3D(bool with_dynamic = true) {
auto shapes = SNIPPETS_TESTS_STATIC_SHAPES({{128, 12, 64}, {128, 12, 64}, {12, 128, 128}, {128, 12, 64}},
{{68, 6, 92}, {68, 6, 92}, {1, 68, 68}, {68, 6, 92}},
@@ -99,6 +103,21 @@ INSTANTIATE_TEST_SUITE_P(smoke_Snippets_MHA_4D,
::testing::Values(CPUTestUtils::empty_plugin_config)),
MHA::getTestCaseName);
+INSTANTIATE_TEST_SUITE_P(smoke_Snippets_MHA_4D_MatMul1_Const_B_Are_Wei_Blocked,
+ MHAConstB,
+ ::testing::Combine(::testing::ValuesIn(transposedShape_4D_matmul1_const_b()),
+ ::testing::ValuesIn(precision_f32(4)),
+ ::testing::Values(ov::element::f32),
+ ::testing::Values(false),
+ ::testing::Values(false),
+ ::testing::Values(true),
+ ::testing::Values(MHA::default_thread_count),
+ ::testing::Values(expected_nodes_mha_4d_f32),
+ ::testing::Values(2), // decomposed Transpose + MHA
+ ::testing::Values(ov::test::utils::DEVICE_CPU),
+ ::testing::Values(CPUTestUtils::empty_plugin_config)),
+ MHAConstB::getTestCaseName);
+
INSTANTIATE_TEST_SUITE_P(smoke_Snippets_MHA_4D_WithScalarMul,
MHA,
::testing::Combine(::testing::ValuesIn(transposedShape_4D(true, false)),
diff --git a/src/tests/functional/plugin/shared/include/snippets/mha.hpp b/src/tests/functional/plugin/shared/include/snippets/mha.hpp
index 0e2214c41fd..d8cb49d16fc 100644
--- a/src/tests/functional/plugin/shared/include/snippets/mha.hpp
+++ b/src/tests/functional/plugin/shared/include/snippets/mha.hpp
@@ -23,6 +23,20 @@ typedef std::tuple<std::vector<InputShape>, // Input shapes
>
MHAParams;
+typedef std::tuple<std::vector<InputShape>, // Input shapes
+ std::vector<ov::element::Type>, // Input Element types
+ ov::element::Type, // Inference precision
+ bool, // With Multiply
+ bool, // MatMul0 with const B
+ bool, // MatMul1 with const B
+ size_t, // Thread count
+ size_t, // Expected num nodes
+ size_t, // Expected num subgraphs
+ std::string, // Target Device
+ ov::AnyMap // Config
+ >
+ MHAConstBParams;
+
typedef std::tuple<std::vector<InputShape>, // Input shapes
std::vector<ov::element::Type>, // Input Element types
ov::element::Type, // Inference precision
@@ -32,9 +46,9 @@ typedef std::tuple<std::vector<InputShape>, // Input shapes
std::string, // Target Device
ov::AnyMap // Config
>
-MHAWithDynamicMulParams;
+ MHAWithDynamicMulParams;
-class MHABase : virtual public SnippetsTestsCommon {
+class MHABase : virtual public SnippetsTestsCommon {
public:
constexpr static size_t default_thread_count = 0;
@@ -44,23 +58,42 @@ protected:
void generate_inputs(const std::vector<ov::Shape>& targetInputStaticShapes) override;
void init_thresholds() override;
virtual std::shared_ptr<SnippetsFunctionBase> get_subgraph() const = 0;
- virtual void init_params(std::vector<InputShape>& input_shapes, ov::element::Type& prc, ov::AnyMap& additional_config) = 0;
+ virtual void init_params(std::vector<InputShape>& input_shapes,
+ ov::element::Type& prc,
+ ov::AnyMap& additional_config) = 0;
size_t m_thread_count;
std::vector<ov::element::Type> m_input_types;
};
-class MHA : public testing::WithParamInterface<ov::test::snippets::MHAParams>,
- virtual public MHABase {
+class MHA : public testing::WithParamInterface<ov::test::snippets::MHAParams>, virtual public MHABase {
public:
static std::string getTestCaseName(const testing::TestParamInfo<ov::test::snippets::MHAParams>& obj);
protected:
std::shared_ptr<SnippetsFunctionBase> get_subgraph() const override;
- void init_params(std::vector<InputShape>& input_shapes, ov::element::Type& prc, ov::AnyMap& additional_config) override;
+ void init_params(std::vector<InputShape>& input_shapes,
+ ov::element::Type& prc,
+ ov::AnyMap& additional_config) override;
+ void init_thresholds() override;
+
+ bool m_with_mul = false;
+};
+
+class MHAConstB : public testing::WithParamInterface<ov::test::snippets::MHAConstBParams>, virtual public MHABase {
+public:
+ static std::string getTestCaseName(const testing::TestParamInfo<ov::test::snippets::MHAConstBParams>& obj);
+
+protected:
+ std::shared_ptr<SnippetsFunctionBase> get_subgraph() const override;
+ void init_params(std::vector<InputShape>& input_shapes,
+ ov::element::Type& prc,
+ ov::AnyMap& additional_config) override;
void init_thresholds() override;
bool m_with_mul = false;
+ bool m_const_b_matmul0 = false;
+ bool m_const_b_matmul1 = false;
};
class MHA2D : public MHA {
@@ -131,7 +164,9 @@ public:
protected:
std::shared_ptr<SnippetsFunctionBase> get_subgraph() const override;
- void init_params(std::vector<InputShape>& input_shapes, ov::element::Type& prc, ov::AnyMap& additional_config) override;
+ void init_params(std::vector<InputShape>& input_shapes,
+ ov::element::Type& prc,
+ ov::AnyMap& additional_config) override;
};
class MHASharedKV : public MHA {
diff --git a/src/tests/functional/plugin/shared/src/snippets/mha.cpp b/src/tests/functional/plugin/shared/src/snippets/mha.cpp
index 1ce8110782f..70e6941d9c5 100644
--- a/src/tests/functional/plugin/shared/src/snippets/mha.cpp
+++ b/src/tests/functional/plugin/shared/src/snippets/mha.cpp
@@ -35,8 +35,9 @@ void MHABase::generate_inputs(const std::vector<ov::Shape>& targetInputStaticSha
in_data.start_from = bf16_precision ? 0 : -1;
in_data.range = 2;
in_data.resolution = 256;
- tensor =
- ov::test::utils::create_and_fill_tensor(model_input.get_element_type(), targetInputStaticShapes[i], in_data);
+ tensor = ov::test::utils::create_and_fill_tensor(model_input.get_element_type(),
+ targetInputStaticShapes[i],
+ in_data);
inputs.insert({model_input.get_node_shared_ptr(), tensor});
}
}
@@ -102,10 +103,13 @@ std::string MHA::getTestCaseName(const testing::TestParamInfo<ov::test::snippets
return result.str();
}
-std::string MHAWithDynamicMul::getTestCaseName(const testing::TestParamInfo<ov::test::snippets::MHAWithDynamicMulParams>& obj) {
+std::string MHAConstB::getTestCaseName(const testing::TestParamInfo<ov::test::snippets::MHAConstBParams>& obj) {
const auto& [input_shapes,
elem_types,
prc,
+ with_mul,
+ const_b_matmul0,
+ const_b_matmul1,
thread_count,
num_nodes,
num_subgraphs,
@@ -117,6 +121,9 @@ std::string MHAWithDynamicMul::getTestCaseName(const testing::TestParamInfo<ov::
result << "IS[" << i << "]=" << input_shapes[i] << "_";
for (size_t i = 0; i < elem_types.size(); i++)
result << "T[" << i << "]=" << elem_types[i] << "_";
+ result << "Mul=" << with_mul << "_";
+ result << "ConstBMatMul0=" << const_b_matmul0 << "_";
+ result << "ConstBMatMul1=" << const_b_matmul1 << "_";
result << "ThreadNum=" << thread_count << "_";
result << "PRC=" << prc << "_";
result << "#N=" << num_nodes << "_";
@@ -132,18 +139,100 @@ std::string MHAWithDynamicMul::getTestCaseName(const testing::TestParamInfo<ov::
return result.str();
}
-void MHA::init_params(std::vector<InputShape>& input_shapes, ov::element::Type& prc, ov::AnyMap& additional_config) {
- std::tie(input_shapes, m_input_types, prc, m_with_mul, m_thread_count, ref_num_nodes, ref_num_subgraphs,
- targetDevice, additional_config) = this->GetParam();
+std::string MHAWithDynamicMul::getTestCaseName(
+ const testing::TestParamInfo<ov::test::snippets::MHAWithDynamicMulParams>& obj) {
+ const auto& [input_shapes,
+ elem_types,
+ prc,
+ thread_count,
+ num_nodes,
+ num_subgraphs,
+ target_device,
+ additional_config] = obj.param;
+
+ std::ostringstream result;
+ for (size_t i = 0; i < input_shapes.size(); i++)
+ result << "IS[" << i << "]=" << input_shapes[i] << "_";
+ for (size_t i = 0; i < elem_types.size(); i++)
+ result << "T[" << i << "]=" << elem_types[i] << "_";
+ result << "ThreadNum=" << thread_count << "_";
+ result << "PRC=" << prc << "_";
+ result << "#N=" << num_nodes << "_";
+ result << "#S=" << num_subgraphs << "_";
+ result << "targetDevice=" << target_device << "_";
+
+ if (!additional_config.empty()) {
+ result << "_PluginConf";
+ for (auto& item : additional_config) {
+ result << "_" << item.first << "=" << item.second.as<std::string>();
+ }
+ }
+ return result.str();
}
-void MHAWithDynamicMul::init_params(std::vector<InputShape>& input_shapes, ov::element::Type& prc, ov::AnyMap& additional_config) {
- std::tie(input_shapes, m_input_types, prc, m_thread_count, ref_num_nodes, ref_num_subgraphs, targetDevice, additional_config) = this->GetParam();
+void MHA::init_params(std::vector<InputShape>& input_shapes, ov::element::Type& prc, ov::AnyMap& additional_config) {
+ std::tie(input_shapes,
+ m_input_types,
+ prc,
+ m_with_mul,
+ m_thread_count,
+ ref_num_nodes,
+ ref_num_subgraphs,
+ targetDevice,
+ additional_config) = this->GetParam();
+}
+
+void MHAConstB::init_params(std::vector<InputShape>& input_shapes,
+ ov::element::Type& prc,
+ ov::AnyMap& additional_config) {
+ std::tie(input_shapes,
+ m_input_types,
+ prc,
+ m_with_mul,
+ m_const_b_matmul0,
+ m_const_b_matmul1,
+ m_thread_count,
+ ref_num_nodes,
+ ref_num_subgraphs,
+ targetDevice,
+ additional_config) = this->GetParam();
+}
+
+void MHAWithDynamicMul::init_params(std::vector<InputShape>& input_shapes,
+ ov::element::Type& prc,
+ ov::AnyMap& additional_config) {
+ std::tie(input_shapes,
+ m_input_types,
+ prc,
+ m_thread_count,
+ ref_num_nodes,
+ ref_num_subgraphs,
+ targetDevice,
+ additional_config) = this->GetParam();
}
std::shared_ptr<SnippetsFunctionBase> MHA::get_subgraph() const {
- bool is_with_reshape = std::all_of(inputDynamicShapes.begin(), inputDynamicShapes.end(), [](const PartialShape& ps){ return ps.is_static(); });
- return std::make_shared<ov::test::snippets::MHAFunction>(inputDynamicShapes, m_input_types, m_with_mul, is_with_reshape);
+ bool is_with_reshape =
+ std::all_of(inputDynamicShapes.begin(), inputDynamicShapes.end(), [](const PartialShape& ps) {
+ return ps.is_static();
+ });
+ return std::make_shared<ov::test::snippets::MHAFunction>(inputDynamicShapes,
+ m_input_types,
+ m_with_mul,
+ is_with_reshape);
+}
+
+std::shared_ptr<SnippetsFunctionBase> MHAConstB::get_subgraph() const {
+ const bool is_with_reshape =
+ std::all_of(inputDynamicShapes.begin(), inputDynamicShapes.end(), [](const PartialShape& ps) {
+ return ps.is_static();
+ });
+ return std::make_shared<ov::test::snippets::MHAConstBFunction>(inputDynamicShapes,
+ m_input_types,
+ m_with_mul,
+ is_with_reshape,
+ m_const_b_matmul0,
+ m_const_b_matmul1);
}
std::shared_ptr<SnippetsFunctionBase> MHA2D::get_subgraph() const {
@@ -161,6 +250,17 @@ void MHA::init_thresholds() {
}
}
+void MHAConstB::init_thresholds() {
+ MHABase::init_thresholds();
+ auto precision_hint = configuration.count(ov::hint::inference_precision.name())
+ ? configuration.at(ov::hint::inference_precision.name())
+ : ov::element::f32;
+ if (m_input_types.size() > 1 && m_input_types[0] == ov::element::bf16 && precision_hint == ov::element::f32) {
+ rel_threshold = 0.01f;
+ abs_threshold = 0.0078125f;
+ }
+}
+
void MHASelect::generate_inputs(const std::vector<ov::Shape>& targetInputStaticShapes) {
inputs.clear();
auto model_inputs = function->inputs();
@@ -260,6 +360,12 @@ TEST_P(MHA, CompareWithRefImpl) {
validateNumSubgraphs();
}
+TEST_P(MHAConstB, CompareWithRefImpl) {
+ SKIP_IF_CURRENT_TEST_IS_DISABLED()
+ run();
+ validateNumSubgraphs();
+}
+
TEST_P(MHA2D, CompareWithRefImpl) {
SKIP_IF_CURRENT_TEST_IS_DISABLED()
run();
diff --git a/src/tests/ov_helpers/ov_snippets_models/include/subgraph_mha.hpp b/src/tests/ov_helpers/ov_snippets_models/include/subgraph_mha.hpp
index 49102e54ffe..aabcb532f36 100644
--- a/src/tests/ov_helpers/ov_snippets_models/include/subgraph_mha.hpp
+++ b/src/tests/ov_helpers/ov_snippets_models/include/subgraph_mha.hpp
@@ -57,6 +57,39 @@ protected:
const std::vector<ov::element::Type> precisions;
};
+class MHAConstBFunction : public SnippetsFunctionBase {
+public:
+ explicit MHAConstBFunction(const std::vector<PartialShape>& inputShapes,
+ const std::vector<ov::element::Type>& precisions,
+ bool with_mul = true,
+ bool with_reshape = true,
+ bool const_b_matmul0 = false,
+ bool const_b_matmul1 = false)
+ : SnippetsFunctionBase(inputShapes),
+ with_mul(with_mul),
+ with_reshape(with_reshape),
+ const_b_matmul0(const_b_matmul0),
+ const_b_matmul1(const_b_matmul1),
+ precisions(precisions) {
+ OPENVINO_ASSERT(input_shapes.size() == 4, "Got invalid number of input shapes");
+ OPENVINO_ASSERT(precisions.size() == 4, "Got invalid number of input precisions");
+ OPENVINO_ASSERT(!const_b_matmul0 || input_shapes[1].is_static(),
+ "MHAConstBFunction expects static shape for MatMul0 constant B");
+ OPENVINO_ASSERT(!const_b_matmul1 || input_shapes[3].is_static(),
+ "MHAConstBFunction expects static shape for MatMul1 constant B");
+ }
+protected:
+ std::shared_ptr<ov::Model> initOriginal() const override;
+ std::shared_ptr<ov::Model> initReference() const override;
+ void validate_function(const std::shared_ptr<Model>& f) const override;
+
+ const bool with_mul = true;
+ const bool with_reshape = true;
+ const bool const_b_matmul0 = false;
+ const bool const_b_matmul1 = false;
+ const std::vector<ov::element::Type> precisions;
+};
+
class MHA2DFunction : public SnippetsFunctionBase {
public:
explicit MHA2DFunction(const std::vector<PartialShape>& inputShapes, const std::vector<ov::element::Type>& precisions)
diff --git a/src/tests/ov_helpers/ov_snippets_models/src/subgraph_mha.cpp b/src/tests/ov_helpers/ov_snippets_models/src/subgraph_mha.cpp
index 3768e41e23c..3dd7492720f 100644
--- a/src/tests/ov_helpers/ov_snippets_models/src/subgraph_mha.cpp
+++ b/src/tests/ov_helpers/ov_snippets_models/src/subgraph_mha.cpp
@@ -52,12 +52,34 @@ std::vector<int64_t> get_decomposed_order_after_split_m(size_t rank) {
}
} // namespace
-std::shared_ptr<ov::Model> MHAFunction::initOriginal() const {
+std::shared_ptr<ov::Model> init_mha_original(const std::vector<PartialShape>& input_shapes,
+ const std::vector<ov::element::Type>& precisions,
+ bool with_mul,
+ bool with_reshape,
+ bool const_b_matmul0,
+ bool const_b_matmul1) {
auto transpose0Param = std::make_shared<ov::opset1::Parameter>(precisions[0], input_shapes[0]);
- auto transpose1Param = std::make_shared<ov::opset1::Parameter>(precisions[1], input_shapes[1]);
auto addParam = std::make_shared<ov::opset1::Parameter>(precisions[2], input_shapes[2]);
- auto transpose2Param = std::make_shared<ov::opset1::Parameter>(precisions[3], input_shapes[3]);
- ov::ParameterVector ngraphParam = {transpose0Param, transpose1Param, addParam, transpose2Param};
+ ov::ParameterVector ngraphParam = {transpose0Param};
+
+ std::shared_ptr<ov::Node> transpose1Input;
+ if (const_b_matmul0) {
+ transpose1Input = ov::test::utils::make_constant(precisions[1], input_shapes[1].to_shape());
+ } else {
+ auto transpose1Param = std::make_shared<ov::opset1::Parameter>(precisions[1], input_shapes[1]);
+ transpose1Input = transpose1Param;
+ ngraphParam.push_back(transpose1Param);
+ }
+ ngraphParam.push_back(addParam);
+
+ std::shared_ptr<ov::Node> transpose2Input;
+ if (const_b_matmul1) {
+ transpose2Input = ov::test::utils::make_constant(precisions[3], input_shapes[3].to_shape());
+ } else {
+ auto transpose2Param = std::make_shared<ov::opset1::Parameter>(precisions[3], input_shapes[3]);
+ transpose2Input = transpose2Param;
+ ngraphParam.push_back(transpose2Param);
+ }
const auto rank = input_shapes[0].size();
const auto fusion_order = get_fusion_order(rank);
@@ -69,7 +91,7 @@ std::shared_ptr<ov::Model> MHAFunction::initOriginal() const {
const auto transpose3Const = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{rank}, fusion_order);
const auto transpose0 = std::make_shared<ov::op::v1::Transpose>(transpose0Param, transpose0Const);
- const auto transpose1 = std::make_shared<ov::op::v1::Transpose>(transpose1Param, transpose1Const);
+ const auto transpose1 = std::make_shared<ov::op::v1::Transpose>(transpose1Input, transpose1Const);
std::shared_ptr<ov::Node> matmul_parent1 = transpose1;
if (with_mul) {
ov::Shape shape(rank, 1);
@@ -87,10 +109,12 @@ std::shared_ptr<ov::Model> MHAFunction::initOriginal() const {
const auto interm_shape = add->get_output_shape(0);
const auto batch =
std::accumulate(interm_shape.cbegin(), interm_shape.cbegin() + (rank - 1), 1, std::multiplies<size_t>());
- const auto reshape0ConstData = std::vector<int64_t>{ batch, -1 };
+ const auto reshape0ConstData = std::vector<int64_t>{batch, -1};
const auto reshape1ConstData = interm_shape;
- const auto reshape0Const = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{reshape0ConstData.size()}, reshape0ConstData);
- const auto reshape1Const = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{reshape1ConstData.size()}, reshape1ConstData);
+ const auto reshape0Const =
+ ov::op::v0::Constant::create(ov::element::i64, ov::Shape{reshape0ConstData.size()}, reshape0ConstData);
+ const auto reshape1Const =
+ ov::op::v0::Constant::create(ov::element::i64, ov::Shape{reshape1ConstData.size()}, reshape1ConstData);
const auto reshape0 = std::make_shared<ov::opset1::Reshape>(add, reshape0Const, true);
const auto softMax = std::make_shared<ov::opset1::Softmax>(reshape0, 1);
@@ -101,20 +125,41 @@ std::shared_ptr<ov::Model> MHAFunction::initOriginal() const {
softmax_out = softMax->output(0);
}
- const auto transpose2 = std::make_shared<ov::op::v1::Transpose>(transpose2Param, transpose2Const);
+ const auto transpose2 = std::make_shared<ov::op::v1::Transpose>(transpose2Input, transpose2Const);
const auto matMul1 = std::make_shared<ov::op::v0::MatMul>(softmax_out, transpose2);
const auto transpose3 = std::make_shared<ov::op::v1::Transpose>(matMul1, transpose3Const);
ov::ResultVector results{std::make_shared<ov::opset1::Result>(transpose3)};
return std::make_shared<ov::Model>(results, ngraphParam, "mha");
}
-std::shared_ptr<ov::Model> MHAFunction::initReference() const {
+
+std::shared_ptr<ov::Model> init_mha_reference(const std::vector<PartialShape>& input_shapes,
+ const std::vector<ov::element::Type>& precisions,
+ bool with_mul,
+ bool const_b_matmul0,
+ bool const_b_matmul1) {
auto data0 = std::make_shared<ov::opset1::Parameter>(precisions[0], input_shapes[0]);
- auto data1 = std::make_shared<ov::opset1::Parameter>(precisions[1], input_shapes[1]);
auto data2 = std::make_shared<ov::opset1::Parameter>(precisions[2], input_shapes[2]);
- auto data3 = std::make_shared<ov::opset1::Parameter>(precisions[3], input_shapes[3]);
+ ov::ParameterVector ngraphParams = {data0};
- ov::ParameterVector ngraphParams = {data0, data1, data2, data3};
+ std::shared_ptr<ov::Node> data1;
+ if (const_b_matmul0) {
+ data1 = ov::test::utils::make_constant(precisions[1], input_shapes[1].to_shape());
+ } else {
+ auto data1Param = std::make_shared<ov::opset1::Parameter>(precisions[1], input_shapes[1]);
+ data1 = data1Param;
+ ngraphParams.push_back(data1Param);
+ }
+ ngraphParams.push_back(data2);
+
+ std::shared_ptr<ov::Node> data3;
+ if (const_b_matmul1) {
+ data3 = ov::test::utils::make_constant(precisions[3], input_shapes[3].to_shape());
+ } else {
+ auto data3Param = std::make_shared<ov::opset1::Parameter>(precisions[3], input_shapes[3]);
+ data3 = data3Param;
+ ngraphParams.push_back(data3Param);
+ }
const auto rank = input_shapes[0].size();
const auto fusion_order = get_fusion_order(rank);
@@ -129,7 +174,6 @@ std::shared_ptr<ov::Model> MHAFunction::initReference() const {
if (transpose1->get_output_partial_shape(0).is_static()) {
shape[rank - 3] = transpose1->get_output_shape(0)[rank - 3];
}
-
const auto mulConst = ov::test::utils::make_constant(precisions[1], shape);
subgraph_parent1 = std::make_shared<ov::op::v1::Multiply>(transpose1, mulConst);
}
@@ -137,10 +181,11 @@ std::shared_ptr<ov::Model> MHAFunction::initReference() const {
OutputVector subgraph_inputs = {data0, subgraph_parent1, data2, data3};
auto transpose0Param = std::make_shared<ov::opset1::Parameter>(precisions[0], input_shapes[0]);
- auto brgemm1Param = std::make_shared<ov::opset1::Parameter>(subgraph_parent1->get_element_type(), subgraph_parent1->get_output_partial_shape(0));
+ auto brgemm1Param = std::make_shared<ov::opset1::Parameter>(subgraph_parent1->get_element_type(),
+ subgraph_parent1->get_output_partial_shape(0));
auto addParam = std::make_shared<ov::opset1::Parameter>(precisions[2], input_shapes[2]);
- auto transpose2Param = std::make_shared<ov::opset1::Parameter>(precisions[3], input_shapes[3]);
-
+ auto transpose2Param =
+ std::make_shared<ov::opset1::Parameter>(data3->get_element_type(), data3->get_output_partial_shape(0));
ov::ParameterVector subgraph_params = {transpose0Param, brgemm1Param, addParam, transpose2Param};
const auto transpose0Const = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{rank}, fusion_order);
@@ -148,7 +193,6 @@ std::shared_ptr<ov::Model> MHAFunction::initReference() const {
const auto transpose3Const = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{rank}, fusion_order);
const auto transpose0 = std::make_shared<ov::op::v1::Transpose>(transpose0Param, transpose0Const);
-
const auto matMul0 = std::make_shared<ov::op::v0::MatMul>(transpose0, brgemm1Param);
const auto add = std::make_shared<ov::op::v1::Add>(matMul0, addParam);
const auto softMax = std::make_shared<ov::opset1::Softmax>(add, rank - 1);
@@ -157,7 +201,6 @@ std::shared_ptr<ov::Model> MHAFunction::initReference() const {
const auto transpose3 = std::make_shared<ov::op::v1::Transpose>(matMul1, transpose3Const);
const auto snippets_result = std::make_shared<ov::snippets::op::Result>(transpose3);
-
auto subgraph = std::make_shared<ov::snippets::op::Subgraph>(
subgraph_inputs,
std::make_shared<ov::Model>(OutputVector{snippets_result}, subgraph_params));
@@ -165,6 +208,35 @@ std::shared_ptr<ov::Model> MHAFunction::initReference() const {
return std::make_shared<ov::Model>(OutputVector{subgraph}, ngraphParams);
}
+std::shared_ptr<ov::Model> MHAFunction::initOriginal() const {
+ return init_mha_original(input_shapes, precisions, with_mul, with_reshape, false, false);
+}
+
+std::shared_ptr<ov::Model> MHAFunction::initReference() const {
+ return init_mha_reference(input_shapes, precisions, with_mul, false, false);
+}
+
+std::shared_ptr<ov::Model> MHAConstBFunction::initOriginal() const {
+ return init_mha_original(input_shapes, precisions, with_mul, with_reshape, const_b_matmul0, const_b_matmul1);
+}
+
+std::shared_ptr<ov::Model> MHAConstBFunction::initReference() const {
+ return init_mha_reference(input_shapes, precisions, with_mul, const_b_matmul0, const_b_matmul1);
+}
+
+void MHAConstBFunction::validate_function(const std::shared_ptr<Model>& f) const {
+ OPENVINO_ASSERT(f != nullptr, "The test requires Model to be defined");
+ std::vector<PartialShape> expected_shapes = {input_shapes[0]};
+ if (!const_b_matmul0) {
+ expected_shapes.push_back(input_shapes[1]);
+ }
+ expected_shapes.push_back(input_shapes[2]);
+ if (!const_b_matmul1) {
+ expected_shapes.push_back(input_shapes[3]);
+ }
+ validate_params_shape(expected_shapes, f->get_parameters());
+}
+
std::shared_ptr<ov::Model> MHA2DFunction::initOriginal() const {
auto param0 = std::make_shared<ov::opset1::Parameter>(precisions[0], input_shapes[0]);
auto param1 = std::make_shared<ov::opset1::Parameter>(precisions[1], input_shapes[1]);