Skip to content

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

[Snippets][CPU] Support v3 NUMPY Broadcast in MHA tokenization along with v1 (#37349)

Arseniy Obolenskiye0178fff1a58
diff --git a/src/common/snippets/include/snippets/utils/tokenization_utils.hpp b/src/common/snippets/include/snippets/utils/tokenization_utils.hpp
index 1380c5ccdf1..53327b7a178 100644
--- a/src/common/snippets/include/snippets/utils/tokenization_utils.hpp
+++ b/src/common/snippets/include/snippets/utils/tokenization_utils.hpp
@@ -17,6 +17,9 @@
#include "snippets/pass/tokenization.hpp"
namespace ov::snippets::utils {
+
+bool is_numpy_broadcast(const std::shared_ptr<const ov::Node>& node);
+
/**
* @brief Tokenizes a node into Subgraph. 2 options are possible (depending on config's values and internal logic)L
* 1. The node is wrapped in a trivial Subgraph which contains only this node
diff --git a/src/common/snippets/src/pass/broadcast_to_movebroadcast.cpp b/src/common/snippets/src/pass/broadcast_to_movebroadcast.cpp
index aed76a869a9..0fad8496bd3 100644
--- a/src/common/snippets/src/pass/broadcast_to_movebroadcast.cpp
+++ b/src/common/snippets/src/pass/broadcast_to_movebroadcast.cpp
@@ -11,13 +11,12 @@
#include "openvino/core/node.hpp"
#include "openvino/core/node_output.hpp"
#include "openvino/core/rt_info.hpp"
-#include "openvino/core/type.hpp"
#include "openvino/op/broadcast.hpp"
-#include "openvino/op/util/attr_types.hpp"
#include "openvino/pass/pattern/matcher.hpp"
#include "openvino/pass/pattern/op/wrap_type.hpp"
#include "snippets/itt.hpp"
#include "snippets/op/broadcastmove.hpp"
+#include "snippets/utils/tokenization_utils.hpp"
ov::snippets::pass::BroadcastToMoveBroadcast::BroadcastToMoveBroadcast() {
MATCHER_SCOPE(BroadcastToMoveBroadcast);
@@ -27,14 +26,8 @@ ov::snippets::pass::BroadcastToMoveBroadcast::BroadcastToMoveBroadcast() {
auto callback = [](ov::pass::pattern::Matcher& m) {
OV_ITT_SCOPED_TASK(ov::pass::itt::domains::SnippetsTransform, "Snippets::op::BroadcastToMoveBroadcast")
auto root = m.get_match_root();
- if (auto broadcast_v1 = ov::as_type_ptr<const ov::op::v1::Broadcast>(root)) {
- if (broadcast_v1->get_broadcast_spec().m_type != ov::op::AutoBroadcastType::NUMPY) {
- return false;
- }
- } else if (auto broadcast_v3 = ov::as_type_ptr<const ov::op::v3::Broadcast>(root)) {
- if (broadcast_v3->get_broadcast_spec().m_type != ov::op::BroadcastType::NUMPY) {
- return false;
- }
+ if (!ov::snippets::utils::is_numpy_broadcast(root)) {
+ return false;
}
const auto target_shape = root->get_output_partial_shape(0);
diff --git a/src/common/snippets/src/pass/collapse_subgraph.cpp b/src/common/snippets/src/pass/collapse_subgraph.cpp
index 1e4286648d7..4206d7d0225 100644
--- a/src/common/snippets/src/pass/collapse_subgraph.cpp
+++ b/src/common/snippets/src/pass/collapse_subgraph.cpp
@@ -23,7 +23,6 @@
#include "openvino/core/validation_util.hpp"
#include "openvino/op/abs.hpp"
#include "openvino/op/add.hpp"
-#include "openvino/op/broadcast.hpp"
#include "openvino/op/ceiling.hpp"
#include "openvino/op/clamp.hpp"
#include "openvino/op/constant.hpp"
@@ -71,7 +70,6 @@
#include "openvino/op/tanh.hpp"
#include "openvino/op/transpose.hpp"
#include "openvino/op/util/arithmetic_reductions_keep_dims.hpp"
-#include "openvino/op/util/attr_types.hpp"
#include "openvino/op/xor.hpp"
#include "openvino/opsets/opset1.hpp"
#include "openvino/pass/matcher_pass.hpp"
@@ -206,17 +204,6 @@ auto is_supported_op(const std::shared_ptr<const Node>& n) -> bool {
return *axis == (rank - 1);
};
- auto is_supported_broadcast_op = [](const std::shared_ptr<const Node>& n) -> bool {
- // Broadcast is supported only for MHA tokenization where there are needed and special checks
- if (auto broadcast_v1 = ov::as_type_ptr<const ov::op::v1::Broadcast>(n)) {
- return broadcast_v1->get_broadcast_spec().m_type == ov::op::AutoBroadcastType::NUMPY;
- }
- if (auto broadcast_v3 = ov::as_type_ptr<const ov::op::v3::Broadcast>(n)) {
- return broadcast_v3->get_broadcast_spec().m_type == ov::op::BroadcastType::NUMPY;
- }
- return false;
- };
-
auto is_supported_reduce_op = [](const std::shared_ptr<const Node>& n) -> bool {
if (ov::is_type_any_of<const ov::op::v1::ReduceMax, const ov::op::v1::ReduceSum>(n)) {
const auto& reduce_base = ov::as_type_ptr<const ov::op::util::ArithmeticReductionKeepDims>(n);
@@ -237,7 +224,7 @@ auto is_supported_op(const std::shared_ptr<const Node>& n) -> bool {
return is_supported_fq_op(n) || is_supported_unary_eltwise_op(n) || is_supported_binary_eltwise_op(n) ||
is_supported_ternary_eltwise_op(n) || is_supported_transpose(n) || is_supported_softmax(n) ||
- is_supported_matmul(n) || is_supported_broadcast_op(n) || is_supported_reduce_op(n);
+ is_supported_matmul(n) || ov::snippets::utils::is_numpy_broadcast(n) || is_supported_reduce_op(n);
}
auto has_supported_in_out(const std::shared_ptr<const Node>& n) -> bool {
diff --git a/src/common/snippets/src/pass/mha_tokenization.cpp b/src/common/snippets/src/pass/mha_tokenization.cpp
index 1af92368515..9314dabe2cc 100644
--- a/src/common/snippets/src/pass/mha_tokenization.cpp
+++ b/src/common/snippets/src/pass/mha_tokenization.cpp
@@ -93,12 +93,10 @@ void tokenize_broadcast(const std::shared_ptr<ov::Node>& interm_op, ov::NodeVect
auto skip_last_dim = [](const ov::PartialShape& shape) {
return ov::PartialShape(std::vector<ov::Dimension>{shape.begin(), shape.end() - 1});
};
-
for (auto input : interm_op->inputs()) {
- auto broadcast = ov::as_type_ptr<ov::opset1::Broadcast>(input.get_source_output().get_node_shared_ptr());
+ const auto broadcast = input.get_source_output().get_node_shared_ptr();
// TODO: Can we reuse AppropriateForSubgraph here? Seems like it's huge check for Broadcast
- if (broadcast && broadcast->get_broadcast_spec().m_type == ov::op::AutoBroadcastType::NUMPY &&
- broadcast->get_output_target_inputs(0).size() == 1) {
+ if (ov::snippets::utils::is_numpy_broadcast(broadcast) && broadcast->get_output_target_inputs(0).size() == 1) {
// TODO: Add support of Broadcast with ShapeOf subgraph on second input
if (!ov::is_type<ov::op::v0::Constant>(broadcast->input_value(1).get_node_shared_ptr())) {
continue;
diff --git a/src/common/snippets/src/utils/tokenization_utils.cpp b/src/common/snippets/src/utils/tokenization_utils.cpp
index c26cb633456..bb873828943 100644
--- a/src/common/snippets/src/utils/tokenization_utils.cpp
+++ b/src/common/snippets/src/utils/tokenization_utils.cpp
@@ -38,6 +38,7 @@
#include "openvino/op/result.hpp"
#include "openvino/op/transpose.hpp"
#include "openvino/op/util/attr_types.hpp"
+#include "openvino/op/util/broadcast_base.hpp"
#include "openvino/opsets/opset1.hpp"
#include "snippets/op/result.hpp"
#include "snippets/op/subgraph.hpp"
@@ -49,6 +50,11 @@
namespace ov::snippets::utils {
+bool is_numpy_broadcast(const std::shared_ptr<const ov::Node>& node) {
+ const auto broadcast = ov::as_type_ptr<const ov::op::util::BroadcastBase>(node);
+ return broadcast && broadcast->get_broadcast_spec().m_type == ov::op::BroadcastType::NUMPY;
+}
+
namespace {
// copy_runtime_info drops the non-copyable DisablePrecisionConversion attribute; propagate it
// explicitly from the tokenized nodes to the Subgraph so precision markup (e.g. kept-in-fp32 sin/cos
diff --git a/src/common/snippets/tests/src/pass/mha_tokenization.cpp b/src/common/snippets/tests/src/pass/mha_tokenization.cpp
index c3b90bdb7ba..fe69e81f9a4 100644
--- a/src/common/snippets/tests/src/pass/mha_tokenization.cpp
+++ b/src/common/snippets/tests/src/pass/mha_tokenization.cpp
@@ -68,6 +68,16 @@ TEST_F(TokenizeMHASnippetsTests, smoke_Snippets_MHA_4D) {
execute_and_validate_function(*this, f);
}
+TEST_F(TokenizeMHASnippetsTests, smoke_Snippets_MHA_4D_V3_Broadcast) {
+ const auto& f = MHAFunction(
+ std::vector<PartialShape>{{1, 128, 12, 64}, {1, 128, 12, 64}, {1, 1, 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);
+ 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}},
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 34e4c328213..48f2df6a973 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
@@ -56,6 +56,10 @@ 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_4D_v3_broadcast() {
+ return SNIPPETS_TESTS_STATIC_SHAPES({{1, 128, 12, 64}, {1, 128, 12, 64}, {1, 1, 128, 128}, {1, 128, 12, 64}});
+}
+
std::vector<std::vector<InputShape>> twoConstBShape_4D() {
return SNIPPETS_TESTS_STATIC_SHAPES({{1, 300, 8, 32}, // Q1
{1, 300, 8, 32}, // K1
@@ -127,6 +131,18 @@ INSTANTIATE_TEST_SUITE_P(smoke_Snippets_MHA_4D_MatMul1_Const_B_Are_Wei_Blocked,
::testing::Values(CPUTestUtils::empty_plugin_config)),
MHAConstB::getTestCaseName);
+INSTANTIATE_TEST_SUITE_P(smoke_Snippets_MHA_4D_V3_Broadcast,
+ MHAWithBroadcast,
+ ::testing::Combine(::testing::ValuesIn(transposedShape_4D_v3_broadcast()),
+ ::testing::ValuesIn(precision_f32(4)),
+ ::testing::Values(ov::element::f32),
+ ::testing::Values(false),
+ ::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)),
+ MHAWithBroadcast::getTestCaseName);
+
// Ticket: CVS-180477
INSTANTIATE_TEST_SUITE_P(smoke_Snippets_MHA_4D_TwoConstB_StaticShapesCacheCollisionRegression,
MHATwoConstB,
diff --git a/src/tests/functional/plugin/shared/include/snippets/mha.hpp b/src/tests/functional/plugin/shared/include/snippets/mha.hpp
index 13362632eea..e13d0118f02 100644
--- a/src/tests/functional/plugin/shared/include/snippets/mha.hpp
+++ b/src/tests/functional/plugin/shared/include/snippets/mha.hpp
@@ -97,6 +97,11 @@ protected:
std::shared_ptr<SnippetsFunctionBase> get_subgraph() const override;
};
+class MHAWithBroadcast : public MHA {
+protected:
+ std::shared_ptr<SnippetsFunctionBase> get_subgraph() const override;
+};
+
class MHASelect : public MHA {
protected:
void generate_inputs(const std::vector<ov::Shape>& targetInputStaticShapes) override;
diff --git a/src/tests/functional/plugin/shared/src/snippets/mha.cpp b/src/tests/functional/plugin/shared/src/snippets/mha.cpp
index d25f749915c..a602e6a7b48 100644
--- a/src/tests/functional/plugin/shared/src/snippets/mha.cpp
+++ b/src/tests/functional/plugin/shared/src/snippets/mha.cpp
@@ -264,6 +264,19 @@ std::shared_ptr<SnippetsFunctionBase> MHA2D::get_subgraph() const {
return std::make_shared<ov::test::snippets::MHA2DFunction>(inputDynamicShapes, m_input_types);
}
+std::shared_ptr<SnippetsFunctionBase> MHAWithBroadcast::get_subgraph() const {
+ const auto with_mul = std::get<3>(this->GetParam());
+ const bool is_with_reshape =
+ std::all_of(inputDynamicShapes.begin(), inputDynamicShapes.end(), [](const ov::PartialShape& ps) {
+ return ps.is_static();
+ });
+ return std::make_shared<ov::test::snippets::MHAFunction>(inputDynamicShapes,
+ m_input_types,
+ with_mul,
+ is_with_reshape,
+ is_with_reshape);
+}
+
void MHASelect::generate_inputs(const std::vector<ov::Shape>& targetInputStaticShapes) {
inputs.clear();
auto model_inputs = function->inputs();
@@ -377,6 +390,12 @@ TEST_P(MHA2D, CompareWithRefImpl) {
run();
}
+TEST_P(MHAWithBroadcast, CompareWithRefImpl) {
+ SKIP_IF_CURRENT_TEST_IS_DISABLED()
+ run();
+ validateNumSubgraphs();
+}
+
TEST_P(MHASelect, CompareWithRefImpl) {
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 0ad42c45a26..199066dd9e8 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
@@ -30,30 +30,42 @@ namespace snippets {
* \ /
* Transpose0[0,2,1,3] Multiply [with_mul = true]
* \ /
- * MatMul0
- * \ /
- * Add
- * Reshape0
+ * MatMul0 AddParam
+ * \ |
+ * Add <--- Broadcast [with_broadcast = true]
+ * |
+ * Reshape0 [with_reshape = true]
+ * |
* Softmax
- * Reshape1 Transpose2[0,2,1,3]
- * \ /
+ * |
+ * Reshape1 [with_reshape = true] Transpose2[0,2,1,3]
+ * \ /
* MatMul1
* Transpose3[0,2,1,3]
*/
class MHAFunction : public SnippetsFunctionBase {
public:
- explicit MHAFunction(const std::vector<PartialShape>& inputShapes, const std::vector<ov::element::Type>& precisions,
- bool with_mul = true, bool with_reshape = true)
- : SnippetsFunctionBase(inputShapes), with_mul(with_mul), with_reshape(with_reshape), precisions(precisions) {
+ explicit MHAFunction(const std::vector<PartialShape>& inputShapes,
+ const std::vector<ov::element::Type>& precisions,
+ bool with_mul = true,
+ bool with_reshape = true,
+ bool with_broadcast = false)
+ : SnippetsFunctionBase(inputShapes),
+ with_mul(with_mul),
+ with_reshape(with_reshape),
+ with_broadcast(with_broadcast),
+ 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");
}
+
protected:
std::shared_ptr<ov::Model> initOriginal() const override;
std::shared_ptr<ov::Model> initReference() const override;
const bool with_mul = true;
const bool with_reshape = true;
+ const bool with_broadcast = false;
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 a8ad0274af3..17fe274a6ba 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
@@ -8,6 +8,7 @@
#include "common_test_utils/node_builders/constant.hpp"
#include "common_test_utils/node_builders/fake_quantize.hpp"
#include "fake_quantize_helper.hpp"
+#include "openvino/op/broadcast.hpp"
#include "openvino/opsets/opset1.hpp"
#include "ov_lpt_models/common/builders.hpp"
#include "ov_ops/type_relaxed.hpp"
@@ -57,7 +58,8 @@ std::shared_ptr<ov::Model> init_mha_original(const std::vector<PartialShape>& in
bool with_mul,
bool with_reshape,
bool const_b_matmul0,
- bool const_b_matmul1) {
+ bool const_b_matmul1,
+ bool with_broadcast) {
auto transpose0Param = std::make_shared<ov::opset1::Parameter>(precisions[0], input_shapes[0]);
auto addParam = std::make_shared<ov::opset1::Parameter>(precisions[2], input_shapes[2]);
ov::ParameterVector ngraphParam = {transpose0Param};
@@ -102,7 +104,15 @@ std::shared_ptr<ov::Model> init_mha_original(const std::vector<PartialShape>& in
matmul_parent1 = std::make_shared<ov::op::v1::Multiply>(transpose1, mulConst);
}
const auto matMul0 = std::make_shared<ov::op::v0::MatMul>(transpose0, matmul_parent1);
- const auto add = std::make_shared<ov::op::v1::Add>(matMul0, addParam);
+ auto add_input = addParam->output(0);
+ if (with_broadcast) {
+ OPENVINO_ASSERT(matMul0->get_output_partial_shape(0).is_static(),
+ "MHAFunction with broadcast requires a static MatMul output shape");
+ const auto target_shape =
+ ov::op::v0::Constant::create(ov::element::i64, ov::Shape{rank}, matMul0->get_output_shape(0));
+ add_input = std::make_shared<ov::op::v3::Broadcast>(add_input, target_shape, ov::op::BroadcastType::NUMPY);
+ }
+ const auto add = std::make_shared<ov::op::v1::Add>(matMul0, add_input);
auto softmax_out = add->output(0);
if (with_reshape) {
@@ -137,7 +147,8 @@ std::shared_ptr<ov::Model> init_mha_reference(const std::vector<PartialShape>& i
const std::vector<ov::element::Type>& precisions,
bool with_mul,
bool const_b_matmul0,
- bool const_b_matmul1) {
+ bool const_b_matmul1,
+ bool with_broadcast = false) {
auto data0 = std::make_shared<ov::opset1::Parameter>(precisions[0], input_shapes[0]);
auto data2 = std::make_shared<ov::opset1::Parameter>(precisions[2], input_shapes[2]);
ov::ParameterVector ngraphParams = {data0};
@@ -194,7 +205,15 @@ std::shared_ptr<ov::Model> init_mha_reference(const std::vector<PartialShape>& i
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);
+ auto add_input = addParam->output(0);
+ if (with_broadcast) {
+ OPENVINO_ASSERT(matMul0->get_output_partial_shape(0).is_static(),
+ "MHAFunction with broadcast requires a static MatMul output shape");
+ const auto target_shape =
+ ov::op::v0::Constant::create(ov::element::i64, ov::Shape{rank}, matMul0->get_output_shape(0));
+ add_input = std::make_shared<ov::op::v3::Broadcast>(add_input, target_shape, ov::op::BroadcastType::NUMPY);
+ }
+ const auto add = std::make_shared<ov::op::v1::Add>(matMul0, add_input);
const auto softMax = std::make_shared<ov::opset1::Softmax>(add, rank - 1);
const auto transpose2 = std::make_shared<ov::op::v1::Transpose>(transpose2Param, transpose2Const);
const auto matMul1 = std::make_shared<ov::op::v0::MatMul>(softMax, transpose2);
@@ -209,15 +228,21 @@ std::shared_ptr<ov::Model> init_mha_reference(const std::vector<PartialShape>& i
}
std::shared_ptr<ov::Model> MHAFunction::initOriginal() const {
- return init_mha_original(input_shapes, precisions, with_mul, with_reshape, false, false);
+ return init_mha_original(input_shapes, precisions, with_mul, with_reshape, false, false, with_broadcast);
}
std::shared_ptr<ov::Model> MHAFunction::initReference() const {
- return init_mha_reference(input_shapes, precisions, with_mul, false, false);
+ return init_mha_reference(input_shapes, precisions, with_mul, false, false, with_broadcast);
}
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);
+ return init_mha_original(input_shapes,
+ precisions,
+ with_mul,
+ with_reshape,
+ const_b_matmul0,
+ const_b_matmul1,
+ false);
}
std::shared_ptr<ov::Model> MHAConstBFunction::initReference() const {