Skip to content

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

[Snippets] Implemented TokenizeMLPSeqSnippets (#30007)

Arseniy Obolenskiydb558ae9ad96
diff --git a/src/common/snippets/include/snippets/pass/mlp_seq_tokenization.hpp b/src/common/snippets/include/snippets/pass/mlp_seq_tokenization.hpp
new file mode 100644
index 00000000000..16e7f18b283
--- /dev/null
+++ b/src/common/snippets/include/snippets/pass/mlp_seq_tokenization.hpp
@@ -0,0 +1,45 @@
+// Copyright (C) 2025 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#pragma once
+
+#include "openvino/pass/matcher_pass.hpp"
+#include "snippets/pass/tokenization.hpp"
+
+namespace ov {
+namespace snippets {
+namespace pass {
+
+/**
+ * @interface TokenizeMLPSeqSnippets
+ * @brief The pass tokenizes sequence of MLPs into Subgraph
+ * Pattern:
+ * [Eltwise, FQ, Softmax] Constant
+ * | /
+ * MatMul0
+ * |
+ * [Eltwise, FQ, Softmax] Constant
+ * | /
+ * MatMul1
+ * |
+ * ...
+ * @ingroup snippets
+ */
+class TokenizeMLPSeqSnippets: public ov::pass::MatcherPass {
+public:
+ OPENVINO_MATCHER_PASS_RTTI("snippets::pass::TokenizeMLPSeqSnippets");
+ TokenizeMLPSeqSnippets(const SnippetsTokenization::Config& config);
+
+private:
+ static bool is_matmul_supported(const std::shared_ptr<ov::Node>& node);
+ static bool is_supported_softmax(const std::shared_ptr<ov::Node>& node);
+ static bool is_supported_intermediate_op(const std::shared_ptr<ov::Node>& node);
+ static bool is_tensor_supported(const ov::descriptor::Tensor& t);
+
+ static const size_t m_rank;
+};
+
+} // namespace pass
+} // namespace snippets
+} // namespace ov
diff --git a/src/common/snippets/include/snippets/pass/split_dimension_m.hpp b/src/common/snippets/include/snippets/pass/split_dimension_m.hpp
index 66de21d2f6a..8607b8e8eed 100644
--- a/src/common/snippets/include/snippets/pass/split_dimension_m.hpp
+++ b/src/common/snippets/include/snippets/pass/split_dimension_m.hpp
@@ -83,6 +83,9 @@ private:
void reshape_subgraph(const std::shared_ptr<op::Subgraph>& subgraph, const ov::Shape& shape, size_t batch_m_dim, size_t new_m_dim);
static size_t get_dim_M(const ov::Shape& shape) {
+ if (shape.size() < dim_M_index + 1) {
+ return 1;
+ }
return *(shape.rbegin() + dim_M_index);
}
diff --git a/src/common/snippets/include/snippets/utils/tokenization_utils.hpp b/src/common/snippets/include/snippets/utils/tokenization_utils.hpp
index 6612560cb2b..7702dd55e90 100644
--- a/src/common/snippets/include/snippets/utils/tokenization_utils.hpp
+++ b/src/common/snippets/include/snippets/utils/tokenization_utils.hpp
@@ -23,6 +23,14 @@ namespace utils {
* @return whether the node was tokenized or not
*/
bool tokenize_node(const std::shared_ptr<ov::Node>& node, const ov::snippets::pass::SnippetsTokenization::Config& config);
+/**
+ * @brief Tokenizes a list of nodes into Subgraph with the following rules:
+ * 1. The user is responsible for valid count of parameters, results and hidden virtual ports (constants)
+ * 2. The list of nodes cannot contain Subgraph ops
+ * @param ordered_ops node list which should be tokenized
+ * @return tokenized subgraph
+ */
+std::shared_ptr<ov::snippets::op::Subgraph> tokenize_ordered_nodes(const ov::NodeVector& ordered_ops);
} // namespace utils
} // namespace snippets
} // namespace ov
\ No newline at end of file
diff --git a/src/common/snippets/src/pass/extract_unsupported_transposes.cpp b/src/common/snippets/src/pass/extract_unsupported_transposes.cpp
index d509a7d4a88..ca26128afc6 100644
--- a/src/common/snippets/src/pass/extract_unsupported_transposes.cpp
+++ b/src/common/snippets/src/pass/extract_unsupported_transposes.cpp
@@ -37,8 +37,9 @@ bool ov::snippets::pass::ExtractUnsupportedTransposes::run_on_subgraph(const std
const auto is_brgemm_case = ov::is_type<opset1::MatMul>(transpose_child.get_node()->shared_from_this());
// If Transpose is supported (can be decomposed or fused into Brgemm), skip
// [116568]: It should be covered by TransposeDecomposition::is_supported or FuseTransposeBrgemm::is_supported
- if ((is_brgemm_case && TokenizeMHASnippets::get_fusion_transpose_order(order_value.size()) == order_value) ||
- (TokenizeMHASnippets::get_decomposed_transpose_order(order_value.size()) == order_value))
+ if (order_value.size() > 2 &&
+ ((is_brgemm_case && TokenizeMHASnippets::get_fusion_transpose_order(order_value.size()) == order_value) ||
+ (TokenizeMHASnippets::get_decomposed_transpose_order(order_value.size()) == order_value)))
continue;
// If the transpose isn't supported - we have to extract it from Subgraph
diff --git a/src/common/snippets/src/pass/mha_tokenization.cpp b/src/common/snippets/src/pass/mha_tokenization.cpp
index ae8355ac045..c98aac2461a 100644
--- a/src/common/snippets/src/pass/mha_tokenization.cpp
+++ b/src/common/snippets/src/pass/mha_tokenization.cpp
@@ -12,6 +12,7 @@
#include "snippets/pass/explicit_transpose_matmul_inputs.hpp"
#include "snippets/pass/mha_tokenization.hpp"
#include "snippets/utils/utils.hpp"
+#include "snippets/utils/tokenization_utils.hpp"
#include "transformations/utils/utils.hpp"
namespace {
@@ -455,95 +456,7 @@ ov::snippets::pass::TokenizeMHASnippets::TokenizeMHASnippets(const SnippetsToken
/* ================================ */
- /* ====== Subgraph creation ======= */
-
- ov::OutputVector body_inputs, subgraph_inputs;
- ov::ParameterVector body_parameters;
- ov::ResultVector body_results;
- std::vector<std::set<Input<Node>>> subgraph_result_inputs;
-
- auto create_body_inputs = [&](const std::shared_ptr<ov::Node>& node) -> void {
- for (size_t i = 0; i < node->get_input_size(); ++i) {
- const auto input = node->input(i);
- 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))) {
- // If Constant has one consumer - target node, we add Constant to body_inputs
- // If Constant has several consumers, we should check that all these consumers are inside Subgraph body
- // and if all of them are inside body, we can explicitly add Constant to the body_inputs, otherwise we should
- // make a copy and add copy of Constant to body_inputs
- // For example, this case is especially valid for Transposes nodes
- // (several Transposes have the same order so there can be the common Constant with this order)
- if (constant->get_output_target_inputs(0).size() == 1) {
- body_inputs.push_back(input.get_source_output());
- } else {
- const auto constant_consumers = constant->get_output_target_inputs(0);
- bool all_consumers_are_inside = std::all_of(constant_consumers.begin(), constant_consumers.end(),
- [&ordered_ops](const ov::Input<ov::Node>& input) {
- return std::find(ordered_ops.begin(), ordered_ops.end(),
- input.get_node()->shared_from_this()) != ordered_ops.end();
- });
- if (all_consumers_are_inside) {
- body_inputs.push_back(input.get_source_output());
- } else {
- const auto constant_copy = constant->clone_with_new_inputs({});
- node->set_argument(input.get_index(), constant_copy);
- body_inputs.push_back(constant_copy);
- }
- }
- } else if (std::find(ordered_ops.begin(), ordered_ops.end(), parent) == ordered_ops.end()) {
- 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_inputs.push_back(parameter->output(0));
-
- subgraph_inputs.push_back(input.get_source_output());
-
- node->input(i).replace_source_output(parameter);
- }
- }
- };
-
- for (const auto& op : ordered_ops) {
- create_body_inputs(op);
- op->clear_control_dependencies();
- fused_names += op->get_friendly_name() + ",";
- }
-
- for (const auto& output : last_node->outputs()) {
- subgraph_result_inputs.push_back(output.get_target_inputs());
- }
- for (const auto& output : last_node->outputs()) {
- body_results.push_back(std::make_shared<ov::opset1::Result>(last_node->output(output.get_index())));
- }
-
- if (body_results.size() != subgraph_result_inputs.size()) {
- OPENVINO_THROW("body results and node results size mismatch during subgraph collapse");
- }
-
- auto body = op::create_body(last_node->get_friendly_name(), body_results, body_parameters);
- auto subgraph = std::make_shared<op::Subgraph>(subgraph_inputs, body);
- // Copy runtime info from last node to subgraph - to copy topological order
- copy_runtime_info(last_node, subgraph);
- subgraph->set_friendly_name(last_node->get_friendly_name());
-
- for (size_t i = 0; i < subgraph->get_output_size(); ++i) {
- for (const auto& target_input : subgraph_result_inputs[i]) {
- target_input.replace_source_output(subgraph->output(i));
- }
- }
- op::update_out_tensor_name(subgraph);
-
- subgraph->validate_and_infer_types();
-
- auto act_body = subgraph->body_ptr();
- for (size_t i = 0; i < act_body->get_parameters().size(); i++) {
- act_body->get_parameters()[i]->set_friendly_name(body_parameters[i]->get_friendly_name());
- }
- subgraph->get_rt_info()["originalLayersNames"] = fused_names;
- subgraph->set_virtual_port_count(hidden_virtual_ports_count);
+ const auto subgraph = ov::snippets::utils::tokenize_ordered_nodes(ordered_ops);
// mark the Subgraph as Completed to not allow Snippets to include any nodes into the MHA Subgraph in common Tokenization
SetSnippetsSubgraphType(subgraph, SnippetsSubgraphType::Completed);
diff --git a/src/common/snippets/src/pass/mlp_seq_tokenization.cpp b/src/common/snippets/src/pass/mlp_seq_tokenization.cpp
new file mode 100644
index 00000000000..c1f092424d8
--- /dev/null
+++ b/src/common/snippets/src/pass/mlp_seq_tokenization.cpp
@@ -0,0 +1,200 @@
+// Copyright (C) 2025 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "snippets/pass/mlp_seq_tokenization.hpp"
+
+#include "snippets/itt.hpp"
+#include "snippets/op/brgemm.hpp"
+#include "snippets/op/subgraph.hpp"
+#include "snippets/pass/collapse_subgraph.hpp"
+#include "snippets/utils/utils.hpp"
+#include "snippets/utils/tokenization_utils.hpp"
+
+#include "openvino/core/rt_info.hpp"
+#include "openvino/core/validation_util.hpp"
+#include "openvino/pass/pattern/op/wrap_type.hpp"
+#include "transformations/utils/utils.hpp"
+
+namespace ov {
+namespace snippets {
+namespace pass {
+
+namespace {
+
+inline bool has_one_consumer(const std::shared_ptr<ov::Node>& node) {
+ return node->get_output_target_inputs(0).size() == 1;
+}
+
+size_t get_potential_body_params(const std::shared_ptr<ov::Node>& op) {
+ size_t count = 0;
+ for (size_t i = 1; i < op->get_input_size(); ++i) {
+ const auto input = op->input_value(i);
+ const auto parent = input.get_node_shared_ptr();
+ 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);
+ const auto is_fq_weight = constant && ov::is_type<ov::op::v0::FakeQuantize>(op);
+ if (!(is_scalar || should_be_inside_body || is_fq_weight)) {
+ count++;
+ }
+ }
+ if (const auto fq = ov::as_type_ptr<ov::op::v0::FakeQuantize>(op)) {
+ count += ov::snippets::utils::get_non_scalar_constant_count_for_fq(fq);
+ }
+ return count;
+}
+
+} // namespace
+
+const size_t TokenizeMLPSeqSnippets::m_rank = 2;
+
+bool TokenizeMLPSeqSnippets::is_tensor_supported(const ov::descriptor::Tensor& t) {
+ return t.get_partial_shape().rank().is_static() && t.get_partial_shape().size() <= m_rank;
+}
+
+bool TokenizeMLPSeqSnippets::is_matmul_supported(const std::shared_ptr<ov::Node>& node) {
+ const auto matmul = ov::as_type_ptr<ov::opset1::MatMul>(node);
+ if (!matmul || matmul->get_transpose_a() ||
+ !ov::is_type<ov::op::v0::Constant>(matmul->input_value(1).get_node_shared_ptr()) ||
+ !is_tensor_supported(matmul->get_input_tensor(0)) || !is_tensor_supported(matmul->get_input_tensor(1))) {
+ return false;
+ }
+
+ const auto prc = op::Brgemm::get_output_type(matmul->get_input_element_type(0), matmul->get_input_element_type(1));
+ return prc != element::dynamic;
+}
+
+bool TokenizeMLPSeqSnippets::is_supported_softmax(const std::shared_ptr<ov::Node>& node) {
+ int64_t axis = 0;
+ const auto rank = node->get_input_partial_shape(0).rank();
+ if (const auto softmax_v8 = ov::as_type_ptr<ov::op::v8::Softmax>(node)) {
+ axis = ov::util::try_normalize_axis(softmax_v8->get_axis(), rank, *node);
+ } else if (const auto softmax_v1 = ov::as_type_ptr<ov::op::v1::Softmax>(node)) {
+ axis = softmax_v1->get_axis();
+ } else {
+ return false;
+ }
+ return is_tensor_supported(node->get_input_tensor(0)) && axis == (rank.get_length() - 1);
+}
+
+bool TokenizeMLPSeqSnippets::is_supported_intermediate_op(const std::shared_ptr<ov::Node>& node) {
+ if (!ov::snippets::pass::TokenizeSnippets::AppropriateForSubgraph(node)) {
+ return false;
+ }
+ return is_supported_softmax(node) ||
+ ov::is_type_any_of<ov::op::util::UnaryElementwiseArithmetic,
+ ov::op::util::BinaryElementwiseArithmetic,
+ ov::op::v0::FakeQuantize>(node);
+}
+
+TokenizeMLPSeqSnippets::TokenizeMLPSeqSnippets(const SnippetsTokenization::Config& config) {
+ MATCHER_SCOPE(TokenizeMLPSeqSnippets);
+
+ auto constant = ov::pass::pattern::wrap_type<ov::op::v0::Constant>();
+ auto m_matmul0 = ov::pass::pattern::wrap_type<ov::opset1::MatMul>({ov::pass::pattern::any_input(), constant});
+
+ register_matcher(std::make_shared<ov::pass::pattern::Matcher>(m_matmul0, matcher_name),
+ [OV_CAPTURE_CPY_AND_THIS](ov::pass::pattern::Matcher &m) {
+ OV_ITT_SCOPED_TASK(ov::pass::itt::domains::SnippetsTransform, "Snippets::op::TokenizeMLPSeqSnippets")
+ auto& pattern_to_output = m.get_pattern_value_map();
+
+ // Two inputs of first MatMul
+ size_t potential_body_params_count = 2;
+ // After some transformations, a different number of Constants for some operations may be created
+ // than the actual number of Constants during tokenization.
+ // To avoid unsupported number of non-scalar Constants in the future (plugin specific limitation)
+ // we should calculate potential number of non-scalar Constants that will be moved up from body.
+ // Heuiristic count of possible buffers - upper-bound value
+ const size_t unique_buffer_reg_group_count = 2;
+ std::string fused_names;
+ ov::NodeVector ordered_ops;
+
+ /* ======== Matcher Pass ========== */
+
+ /****** Skeleton ******/
+ /* Skeleton on MLPSeq sequence-pattern is:
+ * MatMul0
+ * |
+ * Eltwise/FakeQuantize (1 or more)
+ * |
+ * MatMul1
+ * |
+ * Eltwise/FakeQuantize (1 or more)
+ * |
+ * MatMul2
+ * |
+ * ...
+ */
+
+ const auto matmul0 = ov::as_type_ptr<ov::opset1::MatMul>(pattern_to_output.at(m_matmul0).get_node_shared_ptr());
+ if (!is_matmul_supported(matmul0) || !has_one_consumer(matmul0)) {
+ return false;
+ }
+
+ if (transformation_callback(matmul0)) {
+ return false;
+ }
+
+ // Add possible FQ before matmul0
+ if (const auto fq = ov::as_type_ptr<ov::op::v0::FakeQuantize>(matmul0->input_value(0).get_node_shared_ptr())) {
+ if (has_one_consumer(fq)) {
+ potential_body_params_count += ov::snippets::utils::get_non_scalar_constant_count_for_fq(fq);
+ ordered_ops.push_back(fq);
+ }
+ }
+ ordered_ops.push_back(matmul0);
+
+ // Tokenize Sequence while we can do it
+ std::shared_ptr<ov::Node> prev_op = matmul0;
+ auto interm_op = prev_op->get_output_target_inputs(0).begin()->get_node()->shared_from_this();
+ auto available_regs = config.get_data_ptr_gpr_count();
+
+ while (has_one_consumer(prev_op)) {
+ auto current_potential_body_params_count = potential_body_params_count;
+
+ if (is_matmul_supported(interm_op) && !transformation_callback(interm_op)) {
+ // +1 for weights
+ current_potential_body_params_count++;
+ } else if (is_supported_intermediate_op(interm_op)) {
+ current_potential_body_params_count += get_potential_body_params(interm_op);
+ } else {
+ // Unsupported op
+ break;
+ }
+
+ // TODO [75567]: move this plugin-specific constraint to the plugin callback
+ // +1 - for result
+ if (current_potential_body_params_count + unique_buffer_reg_group_count + 1 > available_regs)
+ break;
+
+ ordered_ops.push_back(interm_op);
+ prev_op = interm_op;
+ interm_op = prev_op->get_output_target_inputs(0).begin()->get_node()->shared_from_this();
+
+ // Move counts
+ potential_body_params_count = current_potential_body_params_count;
+ }
+
+ // Currently, sequence of MLP should contain 2 MatMuls at least
+ size_t mm_count = 0;
+ for (const auto& op : ordered_ops) {
+ mm_count += (ov::is_type<ov::op::v0::MatMul>(op));
+ }
+ if (mm_count < 2)
+ return false;
+
+ /* ====== Subgraph creation ======= */
+
+ const auto subgraph = ov::snippets::utils::tokenize_ordered_nodes(ordered_ops);
+
+ // mark the Subgraph as Completed to not allow Snippets to include any nodes into this Subgraph in common Tokenization
+ SetSnippetsSubgraphType(subgraph, SnippetsSubgraphType::Completed);
+
+ return true;
+ });
+}
+
+} // namespace pass
+} // namespace snippets
+} // namespace ov
diff --git a/src/common/snippets/src/pass/tokenization.cpp b/src/common/snippets/src/pass/tokenization.cpp
index 1a062ea7867..7a19dc610df 100644
--- a/src/common/snippets/src/pass/tokenization.cpp
+++ b/src/common/snippets/src/pass/tokenization.cpp
@@ -13,6 +13,7 @@
#include "snippets/pass/fc_tokenization.hpp"
#include "snippets/pass/gn_tokenization.hpp"
#include "snippets/pass/mha_tokenization.hpp"
+#include "snippets/pass/mlp_seq_tokenization.hpp"
namespace ov {
namespace snippets {
@@ -82,10 +83,11 @@ bool SnippetsTokenization::run_on_model(const std::shared_ptr<ov::Model>& m) {
manager.register_pass<EnumerateNodes>();
manager.register_pass<ExtractReshapesFromMHA>();
- // This pass mustn't be registered in GraphRewrite with other tokenization passes because of 2 reasons:
- // 1. It has higher priority than other tokenization passes
- // 2. It changes the nodes after the matched root node
+ // The following passes mustn't be registered in GraphRewrite with other tokenization passes because of 2 reasons:
+ // 1. They have higher priority than other tokenization passes
+ // 2. They change the nodes after the matched root node
manager.register_pass<TokenizeMHASnippets>(m_config);
+ manager.register_pass<TokenizeMLPSeqSnippets>(m_config);
auto tokenization_passes = manager.register_pass<ov::pass::GraphRewrite>();
tokenization_passes->add_matcher<TokenizeGNSnippets>();
diff --git a/src/common/snippets/src/utils/tokenization_utils.cpp b/src/common/snippets/src/utils/tokenization_utils.cpp
index 039bd8e1653..037c954d356 100644
--- a/src/common/snippets/src/utils/tokenization_utils.cpp
+++ b/src/common/snippets/src/utils/tokenization_utils.cpp
@@ -395,6 +395,108 @@ bool tokenize_node(const std::shared_ptr<ov::Node>& node, const SnippetsTokeniza
return true;
}
+std::shared_ptr<ov::snippets::op::Subgraph> tokenize_ordered_nodes(const ov::NodeVector& ordered_ops) {
+ OPENVINO_ASSERT(!ordered_ops.empty(), "Nothing to be tokenized!");
+
+ ov::OutputVector body_inputs, subgraph_inputs;
+ ov::ParameterVector body_parameters;
+ ov::ResultVector body_results;
+ std::vector<std::set<Input<Node>>> subgraph_result_inputs;
+
+ auto create_body_inputs = [&](const std::shared_ptr<ov::Node>& node) -> void {
+ for (size_t i = 0; i < node->get_input_size(); ++i) {
+ const auto input = node->input(i);
+ 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))) {
+ // If Constant has one consumer - target node, we add Constant to body_inputs
+ // If Constant has several consumers, we should check that all these consumers are inside Subgraph body
+ // and if all of them are inside body, we can explicitly add Constant to the body_inputs, otherwise we should
+ // make a copy and add copy of Constant to body_inputs
+ // For example, this case is especially valid for Transposes nodes
+ // (several Transposes have the same order so there can be the common Constant with this order)
+ if (constant->get_output_target_inputs(0).size() == 1) {
+ body_inputs.push_back(input.get_source_output());
+ } else {
+ const auto constant_consumers = constant->get_output_target_inputs(0);
+ bool all_consumers_are_inside = std::all_of(constant_consumers.begin(), constant_consumers.end(),
+ [&ordered_ops](const ov::Input<ov::Node>& input) {
+ return std::find(ordered_ops.begin(), ordered_ops.end(),
+ input.get_node()->shared_from_this()) != ordered_ops.end();
+ });
+ if (all_consumers_are_inside) {
+ body_inputs.push_back(input.get_source_output());
+ } else {
+ const auto constant_copy = constant->clone_with_new_inputs({});
+ node->set_argument(input.get_index(), constant_copy);
+ body_inputs.push_back(constant_copy);
+ }
+ }
+ } else if (std::find(ordered_ops.begin(), ordered_ops.end(), parent) == ordered_ops.end()) {
+ 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_inputs.push_back(parameter->output(0));
+
+ subgraph_inputs.push_back(input.get_source_output());
+
+ node->input(i).replace_source_output(parameter);
+ }
+ }
+ };
+
+ size_t hidden_data_count = 0;
+ std::string fused_names;
+ auto last_node = ordered_ops.back();
+
+ for (const auto& op : ordered_ops) {
+ create_body_inputs(op);
+ op->clear_control_dependencies();
+ fused_names += op->get_friendly_name() + ",";
+
+ if (const auto fq_node = ov::as_type_ptr<ov::op::v0::FakeQuantize>(op)) {
+ hidden_data_count += ov::snippets::utils::get_non_scalar_constant_count_for_fq(fq_node);
+ }
+ }
+
+ for (const auto& output : last_node->outputs()) {
+ subgraph_result_inputs.push_back(output.get_target_inputs());
+ }
+ for (const auto& output : last_node->outputs()) {
+ body_results.push_back(std::make_shared<ov::opset1::Result>(last_node->output(output.get_index())));
+ }
+
+ if (body_results.size() != subgraph_result_inputs.size()) {
+ OPENVINO_THROW("body results and node results size mismatch during subgraph collapse");
+ }
+
+ auto body = op::create_body(last_node->get_friendly_name(), body_results, body_parameters);
+ auto subgraph = std::make_shared<op::Subgraph>(subgraph_inputs, body);
+ // Copy runtime info from last node to subgraph - to copy topological order
+ copy_runtime_info(last_node, subgraph);
+ subgraph->set_friendly_name(last_node->get_friendly_name());
+
+ for (size_t i = 0; i < subgraph->get_output_size(); ++i) {
+ for (const auto& target_input : subgraph_result_inputs[i]) {
+ target_input.replace_source_output(subgraph->output(i));
+ }
+ }
+ op::update_out_tensor_name(subgraph);
+
+ subgraph->validate_and_infer_types();
+
+ auto act_body = subgraph->body_ptr();
+ for (size_t i = 0; i < act_body->get_parameters().size(); i++) {
+ act_body->get_parameters()[i]->set_friendly_name(body_parameters[i]->get_friendly_name());
+ }
+ subgraph->get_rt_info()["originalLayersNames"] = fused_names;
+ subgraph->set_virtual_port_count(hidden_data_count);
+
+ return subgraph;
+}
+
} // namespace utils
} // namespace snippets
-} // namespace ov
\ No newline at end of file
+} // namespace ov
diff --git a/src/common/snippets/tests/include/pass/mlp_tokenization.hpp b/src/common/snippets/tests/include/pass/mlp_tokenization.hpp
new file mode 100644
index 00000000000..60ef2a5cf02
--- /dev/null
+++ b/src/common/snippets/tests/include/pass/mlp_tokenization.hpp
@@ -0,0 +1,26 @@
+// Copyright (C) 2025 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#pragma once
+
+#include <common_test_utils/ov_test_utils.hpp>
+
+#include "snippets/pass/tokenization.hpp"
+#include "utils.hpp"
+
+namespace ov {
+namespace test {
+namespace snippets {
+
+class TokenizeMLPSeqSnippetsTests : public TransformationTestsF {
+public:
+ virtual void run();
+
+protected:
+ ov::snippets::pass::SnippetsTokenization::Config config = get_default_tokenization_config();
+};
+
+} // namespace snippets
+} // namespace test
+} // namespace ov
diff --git a/src/common/snippets/tests/src/pass/mlp_tokenization.cpp b/src/common/snippets/tests/src/pass/mlp_tokenization.cpp
new file mode 100644
index 00000000000..577914e2432
--- /dev/null
+++ b/src/common/snippets/tests/src/pass/mlp_tokenization.cpp
@@ -0,0 +1,71 @@
+// Copyright (C) 2025 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include <gtest/gtest.h>
+
+#include <pass/mlp_tokenization.hpp>
+#include <subgraph_mlp_seq.hpp>
+
+#include "common_test_utils/common_utils.hpp"
+#include "snippets/pass/common_optimizations.hpp"
+#include "snippets/pass/mlp_seq_tokenization.hpp"
+#include "snippets/pass/tokenization.hpp"
+namespace ov {
+namespace test {
+namespace snippets {
+
+void TokenizeMLPSeqSnippetsTests::run() {
+ ASSERT_TRUE(model);
+ manager.register_pass<ov::snippets::pass::EnumerateNodes>();
+ manager.register_pass<ov::snippets::pass::TokenizeMLPSeqSnippets>(config);
+ manager.register_pass<ov::snippets::pass::CommonOptimizations>(config);
+ disable_rt_info_check();
+}
+class TokenizeMLPSnippetsParamTests : public TokenizeMLPSeqSnippetsTests,
+ public testing::WithParamInterface<std::tuple<std::vector<PartialShape>, ov::element::Type, int>> {
+protected:
+ void SetUp() override {
+ TransformationTestsF::SetUp();
+ auto params = GetParam();
+ auto shape = std::get<0>(params);
+ auto elem_type = std::get<1>(params);
+ auto hidden_layers = std::get<2>(params);
+
+ const auto& f = MLPSeqQuantizedTypeRelaxedFunction(shape,
+ std::vector<ov::element::Type>({elem_type}),
+ hidden_layers,
+ 128);
+ model = f.getOriginal();
+ model_ref = f.getReference();
+ }
+};
+
+TEST_P(TokenizeMLPSnippetsParamTests, TypeRelaxed_2D) {
+ run();
+}
+
+static std::string getTestCaseName(const testing::TestParamInfo<std::tuple<std::vector<PartialShape>, ov::element::Type, int>>& info) {
+ std::vector<PartialShape> shape = std::get<0>(info.param);
+ ov::element::Type elem_type = std::get<1>(info.param);
+ int hidden_layers = std::get<2>(info.param);
+ std::ostringstream result;
+ result << "InputShape=" << ov::test::utils::partialShape2str(shape) << "_";
+ result << "ElementType=" << elem_type.get_type_name() << "_";
+ result << "HiddenLayers=" << hidden_layers << "_";
+ result << "Precision=" << elem_type.get_type_name() << "_";
+ return result.str();
+}
+
+INSTANTIATE_TEST_SUITE_P(
+ smoke_Snippets_MLP_SEQ,
+ TokenizeMLPSnippetsParamTests,
+ testing::Combine(
+ testing::Values(std::vector<PartialShape>{{64, 64}}, std::vector<PartialShape>{{128, 128}}),
+ testing::Values(ov::element::f32, ov::element::u8),
+ testing::Values(1, 2, 3, 5, 7)),
+ getTestCaseName);
+
+} // namespace snippets
+} // namespace test
+} // namespace ov
diff --git a/src/plugins/intel_cpu/src/emitters/snippets/jit_snippets_call_args.hpp b/src/plugins/intel_cpu/src/emitters/snippets/jit_snippets_call_args.hpp
index 304cea42337..72a3a3931c8 100644
--- a/src/plugins/intel_cpu/src/emitters/snippets/jit_snippets_call_args.hpp
+++ b/src/plugins/intel_cpu/src/emitters/snippets/jit_snippets_call_args.hpp
@@ -45,7 +45,9 @@ struct jit_snippets_call_args {
// for all non-static data members. So we can keep them public or friend all control-flow emitters
loop_args_t* loop_args = nullptr;
amx_tile_config_t amx_tile_config;
- size_t buffer_offsets[SNIPPETS_MAX_DATA_PTR_COUNT] = {};
+ // Issue: 168073
+ // TODO: decrease max array size
+ size_t buffer_offsets[24] = {};
};
struct jit_snippets_call_args::loop_args_t {
diff --git a/src/plugins/intel_cpu/src/transformations/transformation_pipeline.cpp b/src/plugins/intel_cpu/src/transformations/transformation_pipeline.cpp
index b05924a11be..eca895dc6fe 100644
--- a/src/plugins/intel_cpu/src/transformations/transformation_pipeline.cpp
+++ b/src/plugins/intel_cpu/src/transformations/transformation_pipeline.cpp
@@ -169,6 +169,7 @@
#include "snippets/pass/fc_tokenization.hpp"
#include "snippets/pass/fq_decomposition.hpp"
#include "snippets/pass/mha_tokenization.hpp"
+#include "snippets/pass/mlp_seq_tokenization.hpp"
#include "snippets/pass/split_dimension_m.hpp"
#include "snippets/pass/tokenization.hpp"
#if defined(SNIPPETS_LIBXSMM_TPP)
@@ -1128,6 +1129,8 @@ void Transformations::MainSnippets() {
CPU_REGISTER_PASS_X64(snippetsManager, SnippetsMarkSkipped, config.inferencePrecision == ov::element::bf16);
#endif
CPU_DISABLE_PASS_COMMON(snippetsManager, snippets::pass::TokenizeFCSnippets);
+ // TODO: enable MLP SEQ tokenization as a part of 163370
+ CPU_DISABLE_PASS_COMMON(snippetsManager, snippets::pass::TokenizeMLPSeqSnippets);
}
CPU_REGISTER_PASS_COMMON(snippetsManager, snippets::pass::SnippetsTokenization, tokenization_config);
@@ -1140,14 +1143,14 @@ void Transformations::MainSnippets() {
ov::op::util::has_op_with_type<intel_cpu::ScaledDotProductAttentionWithKVCache>(model);
// CPU Plugin Subgraph supports f32, bf16, quantized and fp16(on avx_512_core_amx_fp16 target) BRGEMM
- const auto is_infer_prc_supported_by_MHA =
+ const auto is_infer_prc_supported_by_brgemm =
(dnnl::impl::cpu::x64::mayiuse(dnnl::impl::cpu::x64::avx2) &&
one_of(config.inferencePrecision, ov::element::f32, element::dynamic)) ||
(dnnl::impl::cpu::x64::mayiuse(dnnl::impl::cpu::x64::avx512_core) &&
one_of(config.inferencePrecision, ov::element::bf16, ov::element::f32, element::dynamic)) ||
(dnnl::impl::cpu::x64::mayiuse(dnnl::impl::cpu::x64::avx512_core_amx_fp16) &&
one_of(config.inferencePrecision, ov::element::f16));
- const bool isMHASupported = !is_LLM && is_infer_prc_supported_by_MHA;
+ const bool isMHASupported = !is_LLM && is_infer_prc_supported_by_brgemm;
#else
const bool isMHASupported = false;
#endif
@@ -1157,6 +1160,16 @@ void Transformations::MainSnippets() {
CPU_DISABLE_PASS_COMMON(snippetsManager, snippets::pass::ExtractReshapesFromMHA);
}
+#if !defined(SNIPPETS_LIBXSMM_TPP) && defined(OPENVINO_ARCH_X86_64)
+ const bool isMlpSeqSupported = is_infer_prc_supported_by_brgemm;
+#else
+ const bool isMlpSeqSupported = false;
+#endif
+
+ if (!isMlpSeqSupported) {
+ CPU_DISABLE_PASS_COMMON(snippetsManager, snippets::pass::TokenizeMLPSeqSnippets);
+ }
+
#if defined(OPENVINO_ARCH_X86_64)
auto is_supported_matmul = [this](const std::shared_ptr<const ov::Node>& n) {
const auto matmul = ov::as_type_ptr<const ov::op::v0::MatMul>(n);
@@ -1343,6 +1356,30 @@ void Transformations::MainSnippets() {
return is_unsupported_parallel_work_amount(n, pshape);
},
snippets::pass::TokenizeMHASnippets);
+ CPU_SET_CALLBACK_X64(
+ snippetsManager,
+ [&](const std::shared_ptr<const ov::Node>& n) -> bool {
+ if (!is_supported_matmul(n))
+ return true;
+ if (is_unsupported_parallel_work_amount(n, n->get_output_partial_shape(0)))
+ return true;
+
+ // We've only tested MLP sequence tokenization on small model shapes
+ // So we limit tokenization to sequences with small shapes to avoid unexpected behavior
+ // TODO: release these conditions after testing on larger models
+ const auto& input_shape = n->get_input_partial_shape(0);
+ if (input_shape.rank().is_dynamic() || input_shape.size() != 2)
+ return true;
+ if (input_shape.is_dynamic() || input_shape[0].get_length() > 8 || input_shape[1].get_length() > 256)
+ return true;
+ const auto& output_shape = n->get_output_partial_shape(0);
+ if (output_shape.rank().get_length() != 2)
+ return true;
+ if (output_shape[1].is_dynamic() || output_shape[1].get_length() > 256)
+ return true;
+ return false;
+ },
+ snippets::pass::TokenizeMLPSeqSnippets);
CPU_SET_CALLBACK_X64(
snippetsManager,
[&](const std::shared_ptr<const ov::Node>& n) -> bool {
diff --git a/src/plugins/intel_cpu/tests/functional/shared_tests_instances/skip_tests_config.cpp b/src/plugins/intel_cpu/tests/functional/shared_tests_instances/skip_tests_config.cpp
index 1f58b9e8581..6230303d5be 100644
--- a/src/plugins/intel_cpu/tests/functional/shared_tests_instances/skip_tests_config.cpp
+++ b/src/plugins/intel_cpu/tests/functional/shared_tests_instances/skip_tests_config.cpp
@@ -626,6 +626,7 @@ std::vector<std::string> disabledTestPatterns() {
retVector.emplace_back(R"(.*Snippets.*MHAFQ.*)");
retVector.emplace_back(R"(.*Snippets.*MHAINT8.*)");
retVector.emplace_back(R"(.*Snippets.*MHAQuant.*)");
+ retVector.emplace_back(R"(.*Snippets.*MLP.*Quantized.*)");
}
if (!ov::with_cpu_x86_avx512_core_amx_int8())
// TODO: Issue 92895
@@ -636,6 +637,7 @@ std::vector<std::string> disabledTestPatterns() {
retVector.emplace_back(R"(.*smoke_Snippets_EnforcePrecision_bf16.*)");
retVector.emplace_back(R"(.*smoke_Snippets_MHAWOTransposeEnforceBF16.*)");
retVector.emplace_back(R"(.*smoke_Snippets_MHA.*EnforceBF16.*)");
+ retVector.emplace_back(R"(.*smoke_Snippets_MLP.*bf16.*)");
retVector.emplace_back(R"(.*ConcatSDPTest.*bf16.*)");
}
// RNN/LSTM/GRU/AUGRU BF16 tests on avx512 core ISA
@@ -673,6 +675,7 @@ std::vector<std::string> disabledTestPatterns() {
retVector.emplace_back(R"(.*smoke_Snippets.*MHAFQ.*)");
retVector.emplace_back(R"(.*smoke_Snippets.*PrecisionPropagation_Convertion.*)");
retVector.emplace_back(R"(.*smoke_MHAQuant.*)");
+ retVector.emplace_back(R"(.*smoke_Snippets_MLP.*)");
if (!ov::with_cpu_x86_avx512_core_amx()) {
// Issue: 165178
retVector.emplace_back(R"(.*smoke_Snippets_Softmax/Softmax\.CompareWithRefImpl/IS=\[\]_TS=\(\(.*)");
diff --git a/src/plugins/intel_cpu/tests/functional/shared_tests_instances/snippets/mlp_seq.cpp b/src/plugins/intel_cpu/tests/functional/shared_tests_instances/snippets/mlp_seq.cpp
new file mode 100644
index 00000000000..bf5442dab8c
--- /dev/null
+++ b/src/plugins/intel_cpu/tests/functional/shared_tests_instances/snippets/mlp_seq.cpp
@@ -0,0 +1,95 @@
+// Copyright (C) 2025 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "snippets/mlp.hpp"
+
+#include "utils.hpp"
+
+namespace ov {
+namespace test {
+namespace snippets {
+
+namespace {
+
+std::vector<std::vector<InputShape>> inputShape_2D() {
+ auto shapes = SNIPPETS_TESTS_STATIC_SHAPES(
+ {{1, 64}},
+ {{2, 64}},
+ {{4, 64}},
+ {{8, 64}});
+ shapes.push_back({{PartialShape{-1, 64}, {{1, 64}, {8, 64}, {6, 64}, {8, 64}}}});
+ return shapes;
+}
+
+// Returns a vector of pairs where:
+// - The first element is the number of hidden layers in the MLP
+// - The second element is a pair: {expected number of subgraphs, expected number of nodes}
+std::vector<std::pair<size_t, std::pair<size_t, size_t>>> numHiddenLayersWithExpectations() {
+ return {
+ {1, {1, 1}},
+ {3, {2, 2}},
+ {5, {3, 3}},
+ };
+}
+
+std::vector<std::pair<size_t, std::pair<size_t, size_t>>> numHiddenLayersWithExpectationsBf16() {
+ return {
+ {1, {3, 9}},
+ {3, {3, 13}},
+ {5, {5, 18}},
+ };
+}
+
+std::vector<std::pair<size_t, std::pair<size_t, size_t>>> numHiddenLayersWithExpectationsQuantized() {
+ return {
+ {1, {1, 1}},
+ {3, {1, 1}},
+ {5, {2, 2}},
+ };
+}
+
+std::vector<size_t> hiddenMatmulSizes() {
+ return {64, 128, 256};
+}
+
+INSTANTIATE_TEST_SUITE_P(smoke_Snippets_MLP_SEQ_2D_f32,
+ MLPSeq,
+ ::testing::Combine(::testing::ValuesIn(inputShape_2D()),
+ ::testing::ValuesIn(precision_f32(1)),
+ ::testing::Values(ov::element::f32),
+ ::testing::Values(MLPSeq::default_thread_count),
+ ::testing::Values(ov::test::utils::DEVICE_CPU),
+ ::testing::Values(CPUTestUtils::empty_plugin_config),
+ ::testing::ValuesIn(numHiddenLayersWithExpectations()),
+ ::testing::ValuesIn(hiddenMatmulSizes())),
+ MLPSeq::getTestCaseName);
+
+INSTANTIATE_TEST_SUITE_P(smoke_Snippets_MLP_SEQ_2D_f32_prc_bf16,
+ MLPSeq,
+ ::testing::Combine(::testing::ValuesIn(inputShape_2D()),
+ ::testing::ValuesIn(precision_f32(1)),
+ ::testing::Values(ov::element::bf16),
+ ::testing::Values(MLPSeq::default_thread_count),
+ ::testing::Values(ov::test::utils::DEVICE_CPU),
+ ::testing::Values(CPUTestUtils::empty_plugin_config),
+ ::testing::ValuesIn(numHiddenLayersWithExpectationsBf16()),
+ ::testing::ValuesIn(hiddenMatmulSizes())),
+ MLPSeq::getTestCaseName);
+
+INSTANTIATE_TEST_SUITE_P(smoke_Snippets_MLP_SEQ_Quantized_2D_f32,
+ MLPSeqQuantized,
+ ::testing::Combine(::testing::ValuesIn(inputShape_2D()),
+ ::testing::ValuesIn(precision_f32(1)),
+ ::testing::Values(ov::element::f32),
+ ::testing::Values(MLPSeqQuantized::default_thread_count),
+ ::testing::Values(ov::test::utils::DEVICE_CPU),
+ ::testing::Values(CPUTestUtils::empty_plugin_config),
+ ::testing::ValuesIn(numHiddenLayersWithExpectationsQuantized()),
+ ::testing::ValuesIn(hiddenMatmulSizes())),
+ MLPSeqQuantized::getTestCaseName);
+
+} // namespace
+} // namespace snippets
+} // namespace test
+} // namespace ov
diff --git a/src/plugins/intel_cpu/tests/functional/shared_tests_instances/snippets/utils.hpp b/src/plugins/intel_cpu/tests/functional/shared_tests_instances/snippets/utils.hpp
index 6815cdab671..831a66cf7d2 100644
--- a/src/plugins/intel_cpu/tests/functional/shared_tests_instances/snippets/utils.hpp
+++ b/src/plugins/intel_cpu/tests/functional/shared_tests_instances/snippets/utils.hpp
@@ -30,6 +30,12 @@ static inline std::vector<std::vector<element::Type>> precision_f32(size_t count
return prc;
}
+static inline std::vector<std::vector<element::Type>> precision_i8(size_t count) {
+ std::vector<std::vector<element::Type>> prc;
+ prc.emplace_back(std::vector<element::Type>(count, element::i8));
+ return prc;
+}
+
static inline std::vector<std::vector<element::Type>> precision_bf16_if_supported(size_t count) {
std::vector<std::vector<element::Type>> prc;
if (is_bf16_supported_by_brgemm())
diff --git a/src/tests/functional/plugin/shared/include/snippets/mlp.hpp b/src/tests/functional/plugin/shared/include/snippets/mlp.hpp
new file mode 100644
index 00000000000..8d299b68544
--- /dev/null
+++ b/src/tests/functional/plugin/shared/include/snippets/mlp.hpp
@@ -0,0 +1,58 @@
+// Copyright (C) 2025 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#pragma once
+
+#include "shared_test_classes/base/snippets_test_utils.hpp"
+#include "snippets_helpers.hpp"
+
+namespace ov {
+namespace test {
+namespace snippets {
+
+typedef std::tuple<std::vector<InputShape>, // Input shapes
+ std::vector<ov::element::Type>, // Input Element types
+ ov::element::Type, // Inference precision
+ size_t, // Thread count
+ std::string, // Target Device
+ ov::AnyMap, // Config
+ std::pair<size_t, std::pair<size_t, size_t>>, // {number of hidden layers, {expected number of
+ // subgraphs, expected number of nodes}}
+ size_t // hidden matmul layers size
+ >
+ MLPSeqParams;
+
+class MLPSeqBase : virtual public SnippetsTestsCommon {
+public:
+ constexpr static size_t default_thread_count = 0;
+
+protected:
+ void SetUp() override;
+ void compile_model() override;
+ virtual std::shared_ptr<SnippetsFunctionBase> get_subgraph(size_t num_hidden_layers, size_t hidden_matmul_size) const = 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;
+ size_t m_num_hidden_layers, m_hidden_matmul_size;
+};
+
+class MLPSeq : public testing::WithParamInterface<ov::test::snippets::MLPSeqParams>,
+ virtual public MLPSeqBase {
+public:
+ static std::string getTestCaseName(testing::TestParamInfo<ov::test::snippets::MLPSeqParams> obj);
+
+protected:
+ std::shared_ptr<SnippetsFunctionBase> get_subgraph(size_t num_hidden_layers, size_t hidden_matmul_size) const override;
+ void init_params(std::vector<InputShape>& input_shapes, ov::element::Type& prc, ov::AnyMap& additional_config) override;
+};
+
+class MLPSeqQuantized : public MLPSeq {
+protected:
+ std::shared_ptr<SnippetsFunctionBase> get_subgraph(size_t num_hidden_layers, size_t hidden_matmul_size) const override;
+};
+
+} // namespace snippets
+} // namespace test
+} // namespace ov
diff --git a/src/tests/functional/plugin/shared/src/snippets/mha.cpp b/src/tests/functional/plugin/shared/src/snippets/mha.cpp
index 35463e507f8..3cb7fb75411 100644
--- a/src/tests/functional/plugin/shared/src/snippets/mha.cpp
+++ b/src/tests/functional/plugin/shared/src/snippets/mha.cpp
@@ -55,7 +55,6 @@ void MHABase::SetUp() {
inType = outType = prc;
setInferenceType(prc);
- init_thresholds();
}
void MHABase::init_thresholds() {
diff --git a/src/tests/functional/plugin/shared/src/snippets/mlp.cpp b/src/tests/functional/plugin/shared/src/snippets/mlp.cpp
new file mode 100644
index 00000000000..e904bb124dc
--- /dev/null
+++ b/src/tests/functional/plugin/shared/src/snippets/mlp.cpp
@@ -0,0 +1,121 @@
+// Copyright (C) 2025 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "snippets/mlp.hpp"
+
+#include <common_test_utils/ov_tensor_utils.hpp>
+
+#include "common_test_utils/common_utils.hpp"
+#include "functional_test_utils/skip_tests_config.hpp"
+#include "subgraph_mlp_seq.hpp"
+
+namespace ov {
+namespace test {
+namespace snippets {
+
+void MLPSeqBase::compile_model() {
+ if (m_thread_count != default_thread_count)
+ core->set_property(targetDevice, ov::inference_num_threads(m_thread_count));
+ SubgraphBaseTest::compile_model();
+}
+
+void MLPSeqBase::SetUp() {
+ std::vector<InputShape> input_shapes;
+ ov::element::Type prc;
+ ov::AnyMap additional_config;
+ init_params(input_shapes, prc, additional_config);
+ init_input_shapes(input_shapes);
+
+ const auto subgraph_model = get_subgraph(m_num_hidden_layers, m_hidden_matmul_size);
+ function = subgraph_model->getOriginal();
+
+ configuration.insert(additional_config.begin(), additional_config.end());
+ if (!configuration.count("SNIPPETS_MODE")) {
+ configuration.insert({"SNIPPETS_MODE", "IGNORE_CALLBACK"});
+ }
+
+ inType = outType = prc;
+ setInferenceType(prc);
+}
+
+std::string MLPSeq::getTestCaseName(testing::TestParamInfo<ov::test::snippets::MLPSeqParams> obj) {
+ auto [input_shapes,
+ elem_types,
+ prc,
+ thread_count,
+ target_device,
+ additional_config,
+ num_hidden_layers_with_expectations,
+ hidden_matmul_size] = obj.param;
+
+ auto [num_hidden_layers, num_subgraphs_and_nodes] = num_hidden_layers_with_expectations;
+ auto [num_subgraphs, num_nodes] = num_subgraphs_and_nodes;
+
+ 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>();
+ }
+ }
+ result << "#num_hidden_layers=" << num_hidden_layers << "_";
+ result << "#hidden_matmul_size=" << hidden_matmul_size << "_";
+ return result.str();
+}
+
+void MLPSeq::init_params(std::vector<InputShape>& input_shapes, ov::element::Type& prc, ov::AnyMap& additional_config) {
+ std::pair <size_t, std::pair<size_t, size_t>> num_hidden_layers_with_expectations;
+ std::tie(input_shapes,
+ m_input_types,
+ prc,
+ m_thread_count,
+ targetDevice,
+ additional_config,
+ num_hidden_layers_with_expectations,
+ m_hidden_matmul_size) = this->GetParam();
+ std::pair<size_t, size_t> ref_num_subgraphs_and_nodes;
+ std::tie(m_num_hidden_layers, ref_num_subgraphs_and_nodes) = num_hidden_layers_with_expectations;
+ std::tie(ref_num_subgraphs, ref_num_nodes) = ref_num_subgraphs_and_nodes;
+}
+
+std::shared_ptr<SnippetsFunctionBase> MLPSeq::get_subgraph(size_t num_hidden_layers, size_t hidden_matmul_size) const {
+ return std::make_shared<ov::test::snippets::MLPSeqFunction>(inputDynamicShapes,
+ m_input_types,
+ num_hidden_layers,
+ hidden_matmul_size);
+}
+
+std::shared_ptr<SnippetsFunctionBase> MLPSeqQuantized::get_subgraph(size_t num_hidden_layers,
+ size_t hidden_matmul_size) const {
+ return std::make_shared<ov::test::snippets::MLPSeqQuantizedFunction>(inputDynamicShapes,
+ m_input_types,
+ num_hidden_layers,
+ hidden_matmul_size);
+}
+
+TEST_P(MLPSeq, CompareWithRefImpl) {
+ SKIP_IF_CURRENT_TEST_IS_DISABLED()
+ run();
+ validateNumSubgraphs();
+}
+
+TEST_P(MLPSeqQuantized, CompareWithRefImpl) {
+ SKIP_IF_CURRENT_TEST_IS_DISABLED()
+ run();
+ validateNumSubgraphs();
+}
+
+} // namespace snippets
+} // namespace test
+} // namespace ov
diff --git a/src/tests/ov_helpers/ov_snippets_models/include/subgraph_mlp_seq.hpp b/src/tests/ov_helpers/ov_snippets_models/include/subgraph_mlp_seq.hpp
new file mode 100644
index 00000000000..7d3b7d9cbf0
--- /dev/null
+++ b/src/tests/ov_helpers/ov_snippets_models/include/subgraph_mlp_seq.hpp
@@ -0,0 +1,89 @@
+// Copyright (C) 2025 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#pragma once
+
+#include "snippets_helpers.hpp"
+
+/* The file contains graphs with different MLP sequential patterns:
+ * Skeleton of MLP sequential pattern is:
+ * Input
+ * |
+ * MatMul
+ * |
+ * Eltwise
+ * |
+ * MatMul
+ * |
+ * Output
+ */
+
+namespace ov::test::snippets {
+
+class MLPSeqFunction : public SnippetsFunctionBase {
+public:
+ explicit MLPSeqFunction(const std::vector<PartialShape>& inputShapes,
+ const std::vector<ov::element::Type>& precisions,
+ size_t num_hidden_layers,
+ size_t hidden_matmul_size)
+ : SnippetsFunctionBase(inputShapes),
+ precisions(precisions),
+ num_hidden_layers(num_hidden_layers),
+ hidden_matmul_size(hidden_matmul_size) {
+ OPENVINO_ASSERT(precisions.size() == 1, "MLPSeqFunction expects only precision");
+ }
+
+protected:
+ std::shared_ptr<ov::Model> initOriginal() const override;
+
+ const std::vector<ov::element::Type> precisions;
+ const size_t num_hidden_layers, hidden_matmul_size;
+};
+
+/* Graph:
+ * Input
+ * |
+ * FQ
+ * |
+ * MatMul
+ * |
+ * Eltwise
+ * |
+ * ...
+ * |
+ * Eltwise
+ * |
+ * FQ
+ * |
+ * MatMul
+ * |
+ * Output
+ */
+
+class MLPSeqQuantizedFunction : public MLPSeqFunction {
+public:
+ explicit MLPSeqQuantizedFunction(const std::vector<PartialShape>& inputShapes,
+ const std::vector<ov::element::Type>& precisions,
+ size_t num_hidden_layers,
+ size_t hidden_matmul_size)
+ : MLPSeqFunction(inputShapes, precisions, num_hidden_layers, hidden_matmul_size) {}
+
+protected:
+ std::shared_ptr<ov::Model> initOriginal() const override;
+};
+
+class MLPSeqQuantizedTypeRelaxedFunction : public MLPSeqFunction {
+public:
+ explicit MLPSeqQuantizedTypeRelaxedFunction(const std::vector<PartialShape>& inputShapes,
+ const std::vector<ov::element::Type>& precisions,
+ size_t num_hidden_layers,
+ size_t hidden_matmul_size)
+ : MLPSeqFunction(inputShapes, precisions, num_hidden_layers, hidden_matmul_size) {}
+
+protected:
+ std::shared_ptr<ov::Model> initOriginal() const override;
+ std::shared_ptr<ov::Model> initReference() const override;
+};
+
+} // namespace ov::test::snippets
diff --git a/src/tests/ov_helpers/ov_snippets_models/src/subgraph_mlp_seq.cpp b/src/tests/ov_helpers/ov_snippets_models/src/subgraph_mlp_seq.cpp
new file mode 100644
index 00000000000..8b5104f0060
--- /dev/null
+++ b/src/tests/ov_helpers/ov_snippets_models/src/subgraph_mlp_seq.cpp
@@ -0,0 +1,252 @@
+// Copyright (C) 2025 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "subgraph_mlp_seq.hpp"
+
+#include "snippets/op/subgraph.hpp"
+
+#include "openvino/opsets/opset15.hpp"
+#include "ov_lpt_models/common/builders.hpp"
+#include "ov_lpt_models/common/dequantization_operations.hpp"
+#include "ov_lpt_models/common/fake_quantize_on_data.hpp"
+
+namespace ov {
+namespace test {
+namespace snippets {
+
+std::shared_ptr<ov::Model> MLPSeqFunction::initOriginal() const {
+ auto A_param = std::make_shared<ov::op::v0::Parameter>(precisions[0], input_shapes[0]);
+ std::shared_ptr<Node> current = A_param;
+ float const_value = 0.1122f;
+
+ auto mlp_layer = [&](size_t m, size_t n) {
+ auto b_shape = ov::Shape{m, n};
+ auto b_row = ov::Shape{m};
+ auto B = std::make_shared<ov::op::v0::Constant>(ov::element::f32, b_shape, const_value);
+ current = std::make_shared<ov::op::v0::MatMul>(current, B, false, true);
+ auto constant = std::make_shared<ov::op::v0::Constant>(ov::element::f32, b_row, const_value);
+ current = std::make_shared<ov::op::v1::Add>(current, constant);
+ current = std::make_shared<ov::op::v0::Relu>(current);
+
+ const_value += 0.1122f;
+ };
+
+ mlp_layer(hidden_matmul_size, static_cast<unsigned long>(input_shapes[0][1].get_length()));
+ for (size_t i = 0; i < num_hidden_layers; ++i) {
+ mlp_layer(hidden_matmul_size, hidden_matmul_size);
+ }
+ mlp_layer(static_cast<unsigned long>(input_shapes[0][1].get_length()), hidden_matmul_size);
+
+ auto softmax = std::make_shared<ov::op::v8::Softmax>(current, 1);
+ auto result = std::make_shared<ov::op::v0::Result>(softmax);
+ return std::make_shared<Model>(ResultVector{result}, ParameterVector{A_param});
+}
+
+std::shared_ptr<ov::Model> MLPSeqQuantizedFunction::initOriginal() const {
+ auto A_param = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, input_shapes[0]);
+ std::shared_ptr<Node> current = A_param;
+
+ ov::builder::subgraph::FakeQuantizeOnData onData =
+ {256, {1, 1}, {0.f}, {2.55f}, {0.f}, {255.f}, ov::element::f32};
+ float const_value = 0.1122f;
+
+ auto mlp_layer = [&](size_t m, size_t n) {
+ auto b_shape = ov::Shape{m, n};
+ auto b_row = ov::Shape{m};
+ std::shared_ptr<Node> B = std::make_shared<ov::op::v0::Constant>(ov::element::i8, b_shape, const_value);
+ auto fp_const = std::make_shared<ov::op::v0::Constant>(ov::element::f32, b_shape, const_value);
+ B = std::make_shared<ov::op::v0::Convert>(B, ov::element::f32);
+ B = std::make_shared<ov::op::v1::Multiply>(B, fp_const);
+ current = ov::builder::subgraph::makeFakeQuantize(current, ov::element::f32, onData);
+ current = std::make_shared<ov::op::v0::MatMul>(current, B, false, true);
+ auto constant = std::make_shared<ov::op::v0::Constant>(ov::element::f32, b_row, const_value);
+ current = std::make_shared<ov::op::v1::Add>(current, constant);
+ current = std::make_shared<ov::op::v0::Relu>(current);
+
+ const_value += 0.1122f;
+ };
+
+ mlp_layer(hidden_matmul_size, static_cast<unsigned long>(input_shapes[0][1].get_length()));
+ for (size_t i = 0; i < num_hidden_layers; ++i) {
+ mlp_layer(hidden_matmul_size, hidden_matmul_size);
+ }
+ mlp_layer(static_cast<unsigned long>(input_shapes[0][1].get_length()), hidden_matmul_size);
+
+ auto softmax = std::make_shared<ov::op::v8::Softmax>(current, 1);
+
+ auto result = std::make_shared<ov::op::v0::Result>(softmax);
+ return std::make_shared<Model>(ResultVector{result}, ParameterVector{A_param});
+}
+
+std::shared_ptr<ov::Model> MLPSeqQuantizedTypeRelaxedFunction::initOriginal() const {
+ auto A_param = std::make_shared<ov::op::v0::Parameter>(precisions[0], input_shapes[0]);
+ std::shared_ptr<Node> A = A_param;
+ if (precisions[0] != ov::element::u8) {
+ A = std::make_shared<ov::op::v0::Convert>(A, ov::element::u8);
+ }
+
+ ov::builder::subgraph::FakeQuantizeOnData onData =
+ {256, {1, 1}, {0.f}, {2.55f}, {0.f}, {255.f}, ov::element::u8};
+ std::shared_ptr<Node> current = A;
+ float const_value = 0.1122f;
+
+ auto mlp_layer = [&](size_t m, size_t n) {
+ auto b_shape = ov::Shape{m, n};
+ auto b_row = ov::Shape{m};
+ current = ov::builder::subgraph::makeFakeQuantizeTypeRelaxed(current, ov::element::f32, onData);
+ auto B = std::make_shared<ov::op::v0::Constant>(ov::element::i8, b_shape, const_value);
+
+ current = std::make_shared<op::TypeRelaxed<ov::op::v0::MatMul>>(
+ std::vector<element::Type>{element::f32, element::f32},
+ std::vector<element::Type>{element::f32},
+ ov::op::TemporaryReplaceOutputType(current, element::f32).get(),
+ ov::op::TemporaryReplaceOutputType(B, element::f32).get(),
+ false,
+ true);
+
+ auto dq_scale = std::make_shared<ov::op::v0::Constant>(ov::element::f32, b_row, const_value);
+
+ current = std::make_shared<op::TypeRelaxed<ov::op::v1::Multiply>>(
+ std::vector<element::Type>{element::f32, element::f32},
+ std::vector<element::Type>{element::f32},
+ ov::op::TemporaryReplaceOutputType(current, element::f32).get(),
+ ov::op::TemporaryReplaceOutputType(dq_scale, element::f32).get());
+ auto constant = std::make_shared<ov::op::v0::Constant>(ov::element::f32, b_row, const_value);
+ current = std::make_shared<op::TypeRelaxed<ov::op::v1::Add>>(
+ std::vector<element::Type>{element::f32, element::f32},
+ std::vector<element::Type>{element::f32},
+ ov::op::TemporaryReplaceOutputType(current, element::f32).get(),
+ ov::op::TemporaryReplaceOutputType(constant, element::f32).get());
+ current = std::make_shared<ov::op::v0::Relu>(current);
+
+ const_value += 0.1122f;
+ };
+
+ mlp_layer(hidden_matmul_size, static_cast<unsigned long>(input_shapes[0][1].get_length()));
+ for (size_t i = 0; i < num_hidden_layers; ++i) {
+ mlp_layer(hidden_matmul_size, hidden_matmul_size);
+ }
+ mlp_layer(static_cast<unsigned long>(input_shapes[0][1].get_length()), hidden_matmul_size);
+
+ ov::builder::subgraph::FakeQuantizeOnData onData2 =
+ {256, {1, 1}, {0.f}, {2.55f}, {0.f}, {255.f}, ov::element::f32};
+ current = ov::builder::subgraph::makeFakeQuantizeTypeRelaxed(current, ov::element::f32, onData2);
+ auto softmax = std::make_shared<ov::op::v8::Softmax>(current, 1);
+ ov::builder::subgraph::FakeQuantizeOnData onData1 =
+ {256, {1, 1}, {0.f}, {2.55f}, {0.f}, {255.f}, ov::element::f32};
+ auto dq_A = ov::builder::subgraph::makeFakeQuantizeTypeRelaxed(softmax, ov::element::f32, onData1);
+
+ auto result = std::make_shared<ov::op::v0::Result>(dq_A);
+ return std::make_shared<Model>(ResultVector{result}, ParameterVector{A_param});
+}
+
+std::shared_ptr<ov::Model> MLPSeqQuantizedTypeRelaxedFunction::initReference() const {
+ auto A_param = std::make_shared<ov::op::v0::Parameter>(precisions[0], input_shapes[0]);
+ std::shared_ptr<Node> A = A_param;
+ if (precisions[0] != ov::element::u8) {
+ A = std::make_shared<ov::op::v0::Convert>(A, ov::element::u8);
+ }
+
+ // Create subgraph parameters (must be preserved even if similar constants exist).
+ auto sub_A = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, input_shapes[0]);
+ ov::ParameterVector subgraph_params = {sub_A};
+
+ ov::NodeVector subgraph_nodes = {A};
+
+ ov::builder::subgraph::FakeQuantizeOnData onData = {
+ 256, {1, 1}, {0.0f}, {2.55f}, {0.f}, {255.f}, ov::element::u8
+ };
+
+ auto decomposed_fq = [](const ov::Output<ov::Node>& input,
+ const ov::element::Type& out_precision,
+ float il, float ih, float scale) -> std::shared_ptr<ov::Node> {
+ auto input_low = ov::op::v0::Constant::create(input.get_element_type(), {1, 1}, {il});
+ auto input_high = ov::op::v0::Constant::create(input.get_element_type(), {1, 1}, {ih});
+ auto output_scale = ov::op::v0::Constant::create(input.get_element_type(), {1, 1}, {scale});
+ auto max_node = std::make_shared<ov::op::v1::Maximum>(input, input_low);
+ auto min_node = std::make_shared<ov::op::v1::Minimum>(max_node, input_high);
+ return std::make_shared<ov::op::v1::Multiply>(min_node, output_scale);
+ };
+
+ std::shared_ptr<ov::Node> current = sub_A;
+ current = std::make_shared<ov::snippets::op::ConvertSaturation>(current, ov::element::f32);
+
+ auto mlp_layer = [&](size_t m, size_t n, bool trans) {
+ auto b_shape = ov::Shape{m, n};
+ auto b_row = ov::Shape{m};
+ auto b_shape_trans = ov::Shape{n, m};
+
+ auto dq_shifts = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, b_row);
+ auto dq_shifts_const =
+ std::make_shared<ov::op::v0::Constant>(ov::element::f32, b_row, std::vector<float>{0.1122f});
+ auto dq_scales = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, b_row);
+ auto dq_scales_const =
+ std::make_shared<ov::op::v0::Constant>(ov::element::f32, b_row, std::vector<float>{0.1122f});
+
+ auto B = std::make_shared<ov::op::v0::Parameter>(ov::element::i8, b_shape_trans);
+ auto B_const = std::make_shared<ov::op::v0::Constant>(ov::element::i8, b_shape, std::vector<float>{0.1122f});
+ auto B_const_trans =
+ std::make_shared<ov::op::v1::Transpose>(B_const,
+ ov::op::v0::Constant::create(ov::element::i32, {2}, {1, 0}));
+ subgraph_params.push_back(B);
+ subgraph_nodes.push_back(B_const_trans);
+
+ current = decomposed_fq(current,
+ ov::element::u8,
+ onData.inputLowValues[0],
+ onData.inputHighValues[0],
+ 0.00346764503f);
+ current = std::make_shared<ov::snippets::op::ConvertSaturation>(current, ov::element::u8);
+
+ current = std::make_shared<op::TypeRelaxed<ov::op::v0::MatMul>>(
+ std::vector<ov::element::Type>{ov::element::f32, ov::element::f32},
+ std::vector<ov::element::Type>{ov::element::f32},
+ ov::op::TemporaryReplaceOutputType(current, ov::element::f32).get(),
+ ov::op::TemporaryReplaceOutputType(B, ov::element::f32).get(),
+ false,
+ trans);
+
+ subgraph_params.push_back(dq_scales);
+ subgraph_nodes.push_back(dq_scales_const);
+ current = std::make_shared<op::TypeRelaxed<ov::op::v1::Multiply>>(
+ std::vector<ov::element::Type>{ov::element::f32, ov::element::f32},
+ std::vector<ov::element::Type>{ov::element::f32},
+ ov::op::TemporaryReplaceOutputType(current, ov::element::f32).get(),
+ ov::op::TemporaryReplaceOutputType(dq_scales, ov::element::f32).get());
+
+ subgraph_params.push_back(dq_shifts);
+ subgraph_nodes.push_back(dq_shifts_const);
+ current = std::make_shared<op::TypeRelaxed<ov::op::v1::Add>>(
+ std::vector<ov::element::Type>{ov::element::f32, ov::element::f32},
+ std::vector<ov::element::Type>{ov::element::f32},
+ ov::op::TemporaryReplaceOutputType(current, ov::element::f32).get(),
+ ov::op::TemporaryReplaceOutputType(dq_shifts, ov::element::f32).get());
+ current = std::make_shared<ov::op::v0::Relu>(current);
+ };
+
+ mlp_layer(hidden_matmul_size, static_cast<unsigned long>(input_shapes[0][1].get_length()), false);
+ for (size_t i = 0; i < num_hidden_layers; ++i) {
+ mlp_layer(hidden_matmul_size, hidden_matmul_size, true);
+ }
+ mlp_layer(static_cast<unsigned long>(input_shapes[0][1].get_length()), hidden_matmul_size, false);
+
+ current = decomposed_fq(current, ov::element::f32, onData.inputLowValues[0], onData.inputHighValues[0], 0.00346764503f);
+ current = std::make_shared<ov::op::v1::Subtract>(current, ov::op::v0::Constant::create(ov::element::f32, {1, 1}, {0}));
+ current = std::make_shared<ov::op::v5::Round>(current, ov::op::v5::Round::RoundMode::HALF_TO_EVEN);
+ current = std::make_shared<ov::op::v8::Softmax>(current, 1);
+ current = decomposed_fq(current, ov::element::f32, onData.inputLowValues[0], onData.inputHighValues[0], 0.00346764503f);
+ current = std::make_shared<ov::op::v1::Subtract>(current, ov::op::v0::Constant::create(ov::element::f32, {1, 1}, {0}));
+ current = std::make_shared<ov::op::v5::Round>(current, ov::op::v5::Round::RoundMode::HALF_TO_EVEN);
+ auto result_subgraph = std::make_shared<ov::op::v0::Result>(current);
+ auto subgraph = std::make_shared<ov::snippets::op::Subgraph>(
+ subgraph_nodes,
+ std::make_shared<ov::Model>(ov::ResultVector{result_subgraph}, subgraph_params));
+ auto result = std::make_shared<ov::op::v0::Result>(subgraph);
+
+ return std::make_shared<ov::Model>(ov::ResultVector{result}, ov::ParameterVector{A_param});
+}
+
+} // namespace snippets
+} // namespace test
+} // namespace ov