OpenVINO™ is an open source toolkit for optimizing and deploying AI inference
C++ aicomputer-visiondeep-learningdeploy-aidiffusion-modelsgenerative-aigood-first-issueinferencellm-inferencenatural-language-processingnlpopenvinooptimize-aiperformance-boostrecommendation-systemspeech-recognitionstable-diffusiontransformersyolo
[Snippets] Tokenization config refactoring (#32057)
Vladislav Golubeved17b199e0e5
diff --git a/src/common/snippets/include/snippets/pass/collapse_subgraph.hpp b/src/common/snippets/include/snippets/pass/collapse_subgraph.hppindex e1278c676e5..0806e52f208 100644--- a/src/common/snippets/include/snippets/pass/collapse_subgraph.hpp+++ b/src/common/snippets/include/snippets/pass/collapse_subgraph.hpp@@ -41,7 +41,7 @@ namespace ov::snippets::pass {class TokenizeSnippets : public ov::pass::MatcherPass {public:OPENVINO_MATCHER_PASS_RTTI("snippets::pass::TokenizeSnippets");- explicit TokenizeSnippets(const SnippetsTokenization::Config& config);+ explicit TokenizeSnippets(const TokenizationConfig& config);static bool AppropriateForSubgraph(const std::shared_ptr<const Node>& node);diff --git a/src/common/snippets/include/snippets/pass/common_optimizations.hpp b/src/common/snippets/include/snippets/pass/common_optimizations.hppindex e9c1f11088b..ebda54f9841 100644--- a/src/common/snippets/include/snippets/pass/common_optimizations.hpp+++ b/src/common/snippets/include/snippets/pass/common_optimizations.hpp@@ -5,7 +5,6 @@#pragma once#include "openvino/pass/matcher_pass.hpp"-#include "snippets/pass/tokenization.hpp"namespace ov::snippets::pass {@@ -18,7 +17,34 @@ class CommonOptimizations : public ov::pass::MatcherPass {public:OPENVINO_MATCHER_PASS_RTTI("snippets::pass::CommonOptimizations");- explicit CommonOptimizations(const SnippetsTokenization::Config& config);++ /**+ * @interface Config+ * @brief Configuration for CommonOptimizations pass+ * @ingroup snippets+ */+ struct Config {+ Config(size_t concurrency, bool split_m_dimension)+ : m_concurrency(concurrency),+ m_split_m_dimension(split_m_dimension) {+ OPENVINO_ASSERT(concurrency > 0, "Concurrency should be greater than 0");+ }++ [[nodiscard]] size_t get_concurrency() const {+ return m_concurrency;+ }++ [[nodiscard]] bool get_split_m_dimension() const {+ return m_split_m_dimension;+ }++ private:+ size_t m_concurrency = 0;+ // True if "SplitDimensionM" optimization is enabled.+ bool m_split_m_dimension = true;+ };++ explicit CommonOptimizations(const Config& config);};} // namespace ov::snippets::passdiff --git a/src/common/snippets/include/snippets/pass/fc_tokenization.hpp b/src/common/snippets/include/snippets/pass/fc_tokenization.hppindex 77822745689..2c1ea706b9b 100644--- a/src/common/snippets/include/snippets/pass/fc_tokenization.hpp+++ b/src/common/snippets/include/snippets/pass/fc_tokenization.hpp@@ -17,7 +17,7 @@ namespace ov::snippets::pass {class TokenizeFCSnippets : public ov::pass::MatcherPass {public:OPENVINO_MATCHER_PASS_RTTI("snippets::pass::TokenizeFCSnippets");- explicit TokenizeFCSnippets(const SnippetsTokenization::Config& config);+ explicit TokenizeFCSnippets(const TokenizationConfig& config);};} // namespace ov::snippets::passdiff --git a/src/common/snippets/include/snippets/pass/gated_mlp_tokenization.hpp b/src/common/snippets/include/snippets/pass/gated_mlp_tokenization.hppindex 6dc7120f174..c84d85e3051 100644--- a/src/common/snippets/include/snippets/pass/gated_mlp_tokenization.hpp+++ b/src/common/snippets/include/snippets/pass/gated_mlp_tokenization.hpp@@ -27,7 +27,7 @@ namespace ov::snippets::pass {class TokenizeGatedMLPSnippets : public ov::pass::MatcherPass {public:OPENVINO_MATCHER_PASS_RTTI("snippets::pass::TokenizeGatedMLPSnippets");- explicit TokenizeGatedMLPSnippets(const SnippetsTokenization::Config& config);+ explicit TokenizeGatedMLPSnippets(const TokenizationConfig& config);};} // namespace ov::snippets::passdiff --git a/src/common/snippets/include/snippets/pass/mha_tokenization.hpp b/src/common/snippets/include/snippets/pass/mha_tokenization.hppindex 8e82bfe5a7e..9d2de5286b5 100644--- a/src/common/snippets/include/snippets/pass/mha_tokenization.hpp+++ b/src/common/snippets/include/snippets/pass/mha_tokenization.hpp@@ -11,7 +11,7 @@#include "openvino/opsets/opset1.hpp"#include "openvino/pass/matcher_pass.hpp"-#include "snippets/pass/tokenization.hpp"+#include "snippets/pass/tokenization_config.hpp"namespace ov::snippets::pass {@@ -45,7 +45,49 @@ namespace ov::snippets::pass {class TokenizeMHASnippets : public ov::pass::MatcherPass {public:OPENVINO_MATCHER_PASS_RTTI("snippets::pass::TokenizeMHASnippets");- explicit TokenizeMHASnippets(const SnippetsTokenization::Config& config);++ /**+ * @interface Config+ * @brief Configuration for TokenizeMHASnippets pass+ * @ingroup snippets+ */+ struct Config : public TokenizationConfig {+ Config(const TokenizationConfig& tokenization_config,+ bool enable_transpose_on_output,+ bool dyn_mha_token,+ std::set<size_t> mha_transpose_ranks)+ : TokenizationConfig(tokenization_config),+ m_mha_token_enable_transpose_on_output(enable_transpose_on_output),+ m_is_dynamic_mha_token_enabled(dyn_mha_token),+ m_mha_supported_transpose_ranks(std::move(mha_transpose_ranks)) {}++ [[nodiscard]] bool get_mha_token_enable_transpose_on_output() const {+ return m_mha_token_enable_transpose_on_output;+ }++ [[nodiscard]] bool is_dynamic_mha_token_enabled() const {+ return m_is_dynamic_mha_token_enabled;+ }++ [[nodiscard]] const std::set<size_t>& get_mha_supported_transpose_ranks() const {+ return m_mha_supported_transpose_ranks;+ }++ private:+ // False if Transpose on output isn't tokenized in MHA Tokenization.+ // Otherwise, it may be fused into Subgraph if possible+ // TODO [111813]: Remove please when the ticket 111813 is implemented+ bool m_mha_token_enable_transpose_on_output = true;+ // If True, MHA pattern with dynamic nodes will be tokenized+ // Otherwise dynamic MHA won't be tokenized+ bool m_is_dynamic_mha_token_enabled = true;+ // Set of supported Transpose shape ranks for tokenization in MHATokenization pass.+ // Note that in general Snippets support Transpose of any ranks.+ // But at the moment Transpose is used only in MHA pattern where 3D and 4D tensors are supported.+ std::set<size_t> m_mha_supported_transpose_ranks = {3, 4};+ };++ explicit TokenizeMHASnippets(const Config& config);static std::vector<int32_t> get_fusion_transpose_order(size_t rank);static std::vector<int32_t> get_decomposed_transpose_order(size_t rank);diff --git a/src/common/snippets/include/snippets/pass/mlp_seq_tokenization.hpp b/src/common/snippets/include/snippets/pass/mlp_seq_tokenization.hppindex 356998ee692..8483d537c83 100644--- a/src/common/snippets/include/snippets/pass/mlp_seq_tokenization.hpp+++ b/src/common/snippets/include/snippets/pass/mlp_seq_tokenization.hpp@@ -8,9 +8,11 @@#include <memory>#include "openvino/core/descriptor/tensor.hpp"+#include "openvino/core/except.hpp"#include "openvino/core/node.hpp"+#include "openvino/op/matmul.hpp"#include "openvino/pass/matcher_pass.hpp"-#include "snippets/pass/tokenization.hpp"+#include "snippets/pass/tokenization_config.hpp"namespace ov::snippets::pass {@@ -32,7 +34,37 @@ namespace ov::snippets::pass {class TokenizeMLPSeqSnippets : public ov::pass::MatcherPass {public:OPENVINO_MATCHER_PASS_RTTI("snippets::pass::TokenizeMLPSeqSnippets");- explicit TokenizeMLPSeqSnippets(const SnippetsTokenization::Config& config);++ /**+ * @interface Config+ * @brief Configuration for TokenizeMLPSeqSnippets pass+ * @ingroup snippets+ */+ struct Config : public TokenizationConfig {+ using CanBeFusedAsPostOpPred = std::function<bool(const std::shared_ptr<const ov::op::v0::MatMul>&,+ const std::shared_ptr<const ov::Node>&)>;++ static bool postops_are_not_supported([[maybe_unused]] const std::shared_ptr<const ov::op::v0::MatMul>& matmul,+ [[maybe_unused]] const std::shared_ptr<const ov::Node>& postop) {+ return false;+ }++ explicit Config(const TokenizationConfig& tokenization_config,+ CanBeFusedAsPostOpPred can_be_fused_as_postop = postops_are_not_supported)+ : TokenizationConfig(tokenization_config),+ m_can_be_fused_as_postop(std::move(can_be_fused_as_postop)) {}++ [[nodiscard]] const CanBeFusedAsPostOpPred& get_can_be_fused_as_postop() const {+ OPENVINO_ASSERT(m_can_be_fused_as_postop, "m_can_be_fused_as_postop mustn't be nullptr");+ return m_can_be_fused_as_postop;+ }++ private:+ // Predicate that checks if the node can be fused as MatMul post-op.+ CanBeFusedAsPostOpPred m_can_be_fused_as_postop = postops_are_not_supported;+ };++ explicit TokenizeMLPSeqSnippets(const Config& config);private:static bool is_matmul_supported(const std::shared_ptr<ov::Node>& node);diff --git a/src/common/snippets/include/snippets/pass/tokenization.hpp b/src/common/snippets/include/snippets/pass/tokenization.hppindex a18813120db..701bba13522 100644--- a/src/common/snippets/include/snippets/pass/tokenization.hpp+++ b/src/common/snippets/include/snippets/pass/tokenization.hpp@@ -17,6 +17,10 @@#include "openvino/op/matmul.hpp"#include "openvino/pass/pass.hpp"#include "snippets/op/subgraph.hpp"+#include "snippets/pass/common_optimizations.hpp"+#include "snippets/pass/mha_tokenization.hpp"+#include "snippets/pass/mlp_seq_tokenization.hpp"+#include "snippets/pass/tokenization_config.hpp"namespace ov::snippets::pass {@@ -70,94 +74,21 @@ class SnippetsTokenization : public ov::pass::ModelPass {public:OPENVINO_MODEL_PASS_RTTI("snippets::pass::SnippetsTokenization");- /**- * @interface Config- * @brief Allow to adjust tokenization passes- * @ingroup snippets- */- struct Config {- using CanBeFusedAsPostOpPred = std::function<bool(const std::shared_ptr<const ov::op::v0::MatMul>&,- const std::shared_ptr<const ov::Node>&)>;-- Config(size_t concurrency,- size_t data_ptr_gpr_count,- bool split_m_dimension,- bool enable_transpose_on_output,- bool dyn_mha_token,- std::set<size_t> mha_transpose_ranks,- CanBeFusedAsPostOpPred can_be_fused_as_postop = nullptr)- : m_concurrency(concurrency),- m_data_ptr_gpr_count(data_ptr_gpr_count),- m_split_m_dimension(split_m_dimension),- m_mha_token_enable_transpose_on_output(enable_transpose_on_output),- m_is_dynamic_mha_token_enabled(dyn_mha_token),- m_mha_supported_transpose_ranks(std::move(mha_transpose_ranks)),- m_can_be_fused_as_postop(std::move(can_be_fused_as_postop)) {- OPENVINO_ASSERT(concurrency > 0, "Concurrency should be greater than 0");- OPENVINO_ASSERT(data_ptr_gpr_count > 0, "data_ptr_gpr_count should be greater than 0");- }-- void set_concurrency(size_t concur) {- m_concurrency = concur;- }-- [[nodiscard]] size_t get_concurrency() const {- return m_concurrency;- }-- [[nodiscard]] size_t get_data_ptr_gpr_count() const {- return m_data_ptr_gpr_count;- }-- [[nodiscard]] bool get_split_m_dimension() const {- return m_split_m_dimension;- }-- [[nodiscard]] bool get_mha_token_enable_transpose_on_output() const {- return m_mha_token_enable_transpose_on_output;- }-- [[nodiscard]] bool is_dynamic_mha_token_enabled() const {- return m_is_dynamic_mha_token_enabled;- }-- [[nodiscard]] std::set<size_t> get_mha_supported_transpose_ranks() const {- return m_mha_supported_transpose_ranks;- }-- [[nodiscard]] const CanBeFusedAsPostOpPred& get_can_be_fused_as_postop() const {- return m_can_be_fused_as_postop;- }-- private:- size_t m_concurrency = 0;- // The number of gpr that can be used as data pointers for data nodes (Parameter (and non-Scalar Constants),- // Result, Buffers with the same ID)- size_t m_data_ptr_gpr_count = 0;- // True if "SplitDimensionM" optimization is enabled. Otherwise, it's disabled.- bool m_split_m_dimension = true;- // False if Transpose on output isn't tokenized in MHA Tokenization.- // Otherwise, it may be fused into Subgraph if possible- // TODO [111813]: Remove please when the ticket 111813 is implemented- bool m_mha_token_enable_transpose_on_output = true;- // If True, MHA pattern with dynamic nodes will be tokenized- // Otherwise dynamic MHA won't be tokenized- // Currently, the flag can be set to `True` only for testing purposes.- bool m_is_dynamic_mha_token_enabled = true;- // Set of supported Transpose shape ranks for tokenization in MHATokenization pass.- // Note that in general Snippets support Transpose of any ranks.- // But at the moment Transpose is used only in MHA pattern where 3D and 4D tensors are supported.- std::set<size_t> m_mha_supported_transpose_ranks = {3, 4};- // Predicate that checks if the node can be fused as MatMul post-op.- // It is currently used only in TokenizeMLPSeqSnippets- CanBeFusedAsPostOpPred m_can_be_fused_as_postop = nullptr;- };-- explicit SnippetsTokenization(Config config) : m_config(std::move(config)) {}+ explicit SnippetsTokenization(TokenizationConfig config,+ CommonOptimizations::Config common_config,+ TokenizeMHASnippets::Config mha_config,+ TokenizeMLPSeqSnippets::Config mlp_seq_config)+ : m_tokenization_config(config),+ m_common_optimizations_config(common_config),+ m_mha_config(std::move(mha_config)),+ m_mlp_seq_config(std::move(mlp_seq_config)) {}bool run_on_model(const std::shared_ptr<ov::Model>& m) override;private:- Config m_config;+ TokenizationConfig m_tokenization_config;+ CommonOptimizations::Config m_common_optimizations_config;+ TokenizeMHASnippets::Config m_mha_config;+ TokenizeMLPSeqSnippets::Config m_mlp_seq_config;};} // namespace ov::snippets::passdiff --git a/src/common/snippets/include/snippets/pass/tokenization_config.hpp b/src/common/snippets/include/snippets/pass/tokenization_config.hppnew file mode 100644index 00000000000..7009cb1661f--- /dev/null+++ b/src/common/snippets/include/snippets/pass/tokenization_config.hpp@@ -0,0 +1,51 @@+// Copyright (C) 2018-2025 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#pragma once++#include <cstddef>++#include "openvino/core/except.hpp"++namespace ov::snippets::pass {++/**+ * @interface TokenizationConfig+ * @brief Base configuration for tokenization passes containing common GPR management logic+ * @ingroup snippets+ */+struct TokenizationConfig {+ explicit TokenizationConfig(size_t available_gprs_count) : m_available_gprs_count(available_gprs_count) {+ OPENVINO_ASSERT(available_gprs_count > 0, "available_gprs_count should be greater than 0");+ }++ /**+ * @brief Checks if the available GPRs count is sufficient for the given requirements.+ * @param io_count Number of input/output,+ * each of which requires GPR allocated throughout the life of the kernel.+ * @param expected_buffer_reg_groups Number of unique buffer register groups,+ * each of which requires GPR allocated throughout the life of the kernel.+ * @param expected_maximal_loop_depth Each loop uses GPR for work amount storage.+ * For the expressions covered with all `expected_maximal_loop_depth` loops,+ * `expected_maximal_loop_depth` GPRS must be alive+ * @param is_dynamic Indicates whether the subgraph is dynamic.+ * It affects the number of available GPRs:+ * in static case, abi_param2 is used to pass precomputed offsets to the kernel.+ * @return true if the available GPRs are sufficient; false otherwise.+ */+ [[nodiscard]] bool is_gprs_count_sufficient(const size_t io_count,+ const size_t expected_buffer_reg_groups,+ const size_t expected_maximal_loop_depth,+ bool is_dynamic = false) const {+ const auto available_gprs_count = is_dynamic ? m_available_gprs_count : m_available_gprs_count - 1;+ return (io_count + expected_buffer_reg_groups + expected_maximal_loop_depth) <= available_gprs_count;+ }++protected:+ // The total number of GPRs that can be used inside snippets kernel+ // (data pointers for Parameters/Results/Buffers, as well as loop work amounts)+ size_t m_available_gprs_count = 0;+};++} // namespace ov::snippets::passdiff --git a/src/common/snippets/include/snippets/utils/tokenization_utils.hpp b/src/common/snippets/include/snippets/utils/tokenization_utils.hppindex 9b5dca6446f..915b9958212 100644--- a/src/common/snippets/include/snippets/utils/tokenization_utils.hpp+++ b/src/common/snippets/include/snippets/utils/tokenization_utils.hpp@@ -24,8 +24,7 @@ namespace ov::snippets::utils {* @param config tokenization config which regulates* @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);+bool tokenize_node(const std::shared_ptr<ov::Node>& node, const ov::snippets::pass::TokenizationConfig& 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)@@ -40,4 +39,15 @@ bool tokenize_node(const std::shared_ptr<ov::Node>& node,*/std::shared_ptr<ov::snippets::op::Subgraph> tokenize_ordered_nodes(const ov::NodeVector& ordered_ops,bool are_shared_internal_params_allowed = false);++/**+ * @brief Calculates the potential number of body parameters that would be required for a given operation.+ * Body parameters are created for Snippets node in 2 cases:+ * 1. The input is not a Constant node+ * 2. The input is a Constant node but it is not scalar+ * @note This function assumes that 0'th input of the operation is already counted, so it is ignored here+ * @param op The operation node to analyze+ * @return The estimated number of body parameters needed for this operation+ */+size_t get_potential_body_params(const std::shared_ptr<ov::Node>& op);} // namespace ov::snippets::utilsdiff --git a/src/common/snippets/src/pass/collapse_subgraph.cpp b/src/common/snippets/src/pass/collapse_subgraph.cppindex 16a65d58f2d..2b07787ffbb 100644--- a/src/common/snippets/src/pass/collapse_subgraph.cpp+++ b/src/common/snippets/src/pass/collapse_subgraph.cpp@@ -82,6 +82,7 @@#include "snippets/pass/fq_decomposition.hpp"#include "snippets/pass/fuse_transpose_brgemm.hpp"#include "snippets/pass/tokenization.hpp"+#include "snippets/pass/tokenization_config.hpp"#include "snippets/pass/transpose_decomposition.hpp"#include "snippets/remarks.hpp"#include "snippets/utils/tokenization_utils.hpp"@@ -281,7 +282,7 @@ bool TokenizeSnippets::AppropriateForSubgraph(const std::shared_ptr<const Node>&snippets::op::Subgraph::check_broadcast(node);}-TokenizeSnippets::TokenizeSnippets(const SnippetsTokenization::Config& config) {+TokenizeSnippets::TokenizeSnippets(const TokenizationConfig& config) {MATCHER_SCOPE(TokenizeSnippets);auto label = ov::pass::pattern::any_input([](const ov::Output<ov::Node>& out) {diff --git a/src/common/snippets/src/pass/common_optimizations.cpp b/src/common/snippets/src/pass/common_optimizations.cppindex b7540b34fe3..add2ed07179 100644--- a/src/common/snippets/src/pass/common_optimizations.cpp+++ b/src/common/snippets/src/pass/common_optimizations.cpp@@ -20,7 +20,6 @@#include "snippets/pass/softmax_reshape_elimination.hpp"#include "snippets/pass/split_dimension_m.hpp"#include "snippets/pass/subgraph_manager.hpp"-#include "snippets/pass/tokenization.hpp"#include "snippets/pass/transform_convert.hpp"#include "snippets/pass/validate.hpp"@@ -30,7 +29,7 @@ namespace ov::snippets::pass {if (enabled) \manager.register_pass<pass>(__VA_ARGS__);-CommonOptimizations::CommonOptimizations(const SnippetsTokenization::Config& config) {+CommonOptimizations::CommonOptimizations(const CommonOptimizations::Config& config) {MATCHER_SCOPE(CommonOptimizations);ov::graph_rewrite_callback callback = [&](ov::pass::pattern::Matcher& m) {OV_ITT_SCOPED_TASK(ov::pass::itt::domains::SnippetsTransform, "Snippets::CommonOptimizations");diff --git a/src/common/snippets/src/pass/fc_tokenization.cpp b/src/common/snippets/src/pass/fc_tokenization.cppindex d2ee9a68b58..44059955608 100644--- a/src/common/snippets/src/pass/fc_tokenization.cpp+++ b/src/common/snippets/src/pass/fc_tokenization.cpp@@ -13,10 +13,10 @@#include "openvino/pass/pattern/op/wrap_type.hpp"#include "openvino/util/pp.hpp"#include "snippets/itt.hpp"-#include "snippets/pass/tokenization.hpp"+#include "snippets/pass/tokenization_config.hpp"#include "snippets/utils/tokenization_utils.hpp"-ov::snippets::pass::TokenizeFCSnippets::TokenizeFCSnippets(const SnippetsTokenization::Config& config) {+ov::snippets::pass::TokenizeFCSnippets::TokenizeFCSnippets(const TokenizationConfig& config) {MATCHER_SCOPE(TokenizeFCSnippets);// TODO: extend constant path coverage// Ticket: 153480diff --git a/src/common/snippets/src/pass/gated_mlp_tokenization.cpp b/src/common/snippets/src/pass/gated_mlp_tokenization.cppindex 85f18f22877..65bd7ba66a7 100644--- a/src/common/snippets/src/pass/gated_mlp_tokenization.cpp+++ b/src/common/snippets/src/pass/gated_mlp_tokenization.cpp@@ -4,6 +4,8 @@#include "snippets/pass/gated_mlp_tokenization.hpp"+#include <algorithm>+#include <cstddef>#include <memory>#include "openvino/core/except.hpp"@@ -30,6 +32,7 @@#include "snippets/op/brgemm.hpp"#include "snippets/pass/collapse_subgraph.hpp"#include "snippets/pass/tokenization.hpp"+#include "snippets/pass/tokenization_config.hpp"#include "snippets/utils/tokenization_utils.hpp"#include "snippets/utils/utils.hpp"@@ -70,7 +73,7 @@ Predicate fc_predicate(bool is_down) {} // namespace-TokenizeGatedMLPSnippets::TokenizeGatedMLPSnippets(const SnippetsTokenization::Config& config) {+TokenizeGatedMLPSnippets::TokenizeGatedMLPSnippets(const TokenizationConfig& config) {MATCHER_SCOPE(TokenizeGatedMLPSnippets);using namespace ov::pass;using namespace ov::op::util;@@ -107,22 +110,27 @@ TokenizeGatedMLPSnippets::TokenizeGatedMLPSnippets(const SnippetsTokenization::Creturn false;}- static const auto body_params_count = 5; // 2xinput + 3x fc- static const auto body_result_count = 1; // one output- static const auto reg_group_count = 5; // upper-bound of possible buffer count-- // TODO [75567]: move this plugin-specific constraint to the plugin callback- if (body_params_count + body_result_count + reg_group_count > config.get_data_ptr_gpr_count()) {- return false;- }-- const auto ordered_ops = ov::NodeVector{fc_gate, fc_up, act, mul, fc_down};const bool allow_shared_params = [&]() {const auto mm_gate = ov::as_type_ptr<ov::op::v0::MatMul>(fc_gate);const auto mm_up = ov::as_type_ptr<ov::op::v0::MatMul>(fc_up);OPENVINO_ASSERT(mm_gate && mm_up, "fc_gate and fc_up must have MatMul type");return mm_gate->get_transpose_a() == mm_up->get_transpose_a();}();+ // 1x data input (can be shared or not) + 3x matmul weights + 1x result+ const size_t io_count = (allow_shared_params ? 4 : 5) + 1;+ static constexpr size_t n_reg_group = 3;+ // Loop depth could reach 3 because of SplitLoops optimization+ static constexpr size_t n_loops_depth = 3;+ const auto ordered_ops = ov::NodeVector{fc_gate, fc_up, act, mul, fc_down};+ const bool is_dynamic = std::any_of(ordered_ops.begin(), ordered_ops.end(), [](const std::shared_ptr<Node>& n) {+ return n->is_dynamic();+ });++ // TODO [75567]: move this plugin-specific constraint to the plugin callback+ if (!config.is_gprs_count_sufficient(io_count, n_reg_group, n_loops_depth, is_dynamic)) {+ return false;+ }+const auto subgraph = ov::snippets::utils::tokenize_ordered_nodes(ordered_ops, allow_shared_params);// mark the Subgraph as Completed to not allow Snippets to include any nodes into this Subgraph in commondiff --git a/src/common/snippets/src/pass/mha_tokenization.cpp b/src/common/snippets/src/pass/mha_tokenization.cppindex ea254e470af..4254b0a1f03 100644--- a/src/common/snippets/src/pass/mha_tokenization.cpp+++ b/src/common/snippets/src/pass/mha_tokenization.cpp@@ -19,7 +19,6 @@#include "openvino/core/dimension.hpp"#include "openvino/core/node.hpp"#include "openvino/core/node_vector.hpp"-#include "openvino/core/shape.hpp"#include "openvino/core/type.hpp"#include "openvino/core/type/element_type.hpp"#include "openvino/core/validation_util.hpp"@@ -36,17 +35,17 @@#include "openvino/util/pp.hpp"#include "snippets/itt.hpp"#include "snippets/op/brgemm.hpp"-#include "snippets/op/subgraph.hpp"#include "snippets/pass/collapse_subgraph.hpp"#include "snippets/pass/explicit_transpose_matmul_inputs.hpp"#include "snippets/pass/tokenization.hpp"#include "snippets/utils/tokenization_utils.hpp"#include "snippets/utils/utils.hpp"+using namespace ov::snippets::utils;+namespace {bool is_supported_tensor(const ov::descriptor::Tensor& t) {- return t.get_partial_shape().rank().is_static() &&- ov::snippets::utils::any_of(t.get_partial_shape().size(), 2LU, 3LU, 4LU);+ return t.get_partial_shape().rank().is_static() && any_of(t.get_partial_shape().size(), 2LU, 3LU, 4LU);}bool is_supported_intermediate_op(const std::shared_ptr<ov::Node>& node) {@@ -157,24 +156,9 @@ bool tokenize_reshape_around_softmax(std::shared_ptr<ov::Node>& interm_op,return true;}-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);- if (!constant || (ov::shape_size(input.get_shape()) != 1 && !ov::is_type<ov::op::v0::FakeQuantize>(op) &&- !ov::snippets::op::Subgraph::constant_input_should_be_inside_body(op))) {- count++;- }- }- return count;-}-bool update_intermediate_supported_ops(std::shared_ptr<ov::Node>& interm_op,ov::NodeVector& ordered_ops,- size_t& hidden_virtual_ports_count,- size_t& potential_body_params_count) {+ size_t& n_potential_body_params) {while (is_supported_intermediate_op(interm_op)) {// All supported intermediate ops have only one output portif (interm_op->get_output_target_inputs(0).size() != 1) {@@ -184,14 +168,6 @@ bool update_intermediate_supported_ops(std::shared_ptr<ov::Node>& interm_op,// Check for supported ops on branches: Broadcast/Elementwise (for example, dequantize ops)if (interm_op->get_input_size() > 1) {tokenize_broadcast(interm_op, ordered_ops);-- // To avoid unsupported number of non-scalar Constants in the future after FakeQuantize decomposition- // (plugin specific limitation) we should calculate potential number of non-scalar Constants for- // FakeQuantize that will be moved up from body.- if (const auto fq_node = ov::as_type_ptr<ov::op::v0::FakeQuantize>(interm_op)) {- hidden_virtual_ports_count += ov::snippets::utils::get_non_scalar_constant_count_for_fq(fq_node);- }-auto is_supported_branch_op = [&ordered_ops](const std::shared_ptr<ov::Node>& op) {return is_supported_intermediate_op(op) &&ov::snippets::pass::GetSnippetsNodeType(op) !=@@ -224,7 +200,7 @@ bool update_intermediate_supported_ops(std::shared_ptr<ov::Node>& interm_op,}}- potential_body_params_count += get_potential_body_params(interm_op);+ n_potential_body_params += get_potential_body_params(interm_op);ordered_ops.push_back(interm_op);interm_op = interm_op->get_output_target_inputs(0).begin()->get_node()->shared_from_this();@@ -262,7 +238,7 @@ bool ov::snippets::pass::TokenizeMHASnippets::is_matmul0_supported(const std::shreturn matmul_prc != element::dynamic;}-ov::snippets::pass::TokenizeMHASnippets::TokenizeMHASnippets(const SnippetsTokenization::Config& config) {+ov::snippets::pass::TokenizeMHASnippets::TokenizeMHASnippets(const Config& config) {MATCHER_SCOPE(TokenizeMHASnippets);auto m_matmul0 =@@ -275,12 +251,9 @@ ov::snippets::pass::TokenizeMHASnippets::TokenizeMHASnippets(const SnippetsTokenauto& pattern_to_output = m.get_pattern_value_map();// Queries + Key + Values = 3 standard inputs of MHA- size_t potential_body_params_count = 3;- // 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.- size_t hidden_virtual_ports_count = 0;+ // Also, some Constants may be extracted outside of Subgraph body,+ // so they should be taken into account as parameters+ size_t n_potential_body_params = 3;// The count of potential unique Buffers - it's hidden virtual ports as well// We should go through Subgraph and calculate potential non-inplace Buffers count.// Example:@@ -298,12 +271,11 @@ ov::snippets::pass::TokenizeMHASnippets::TokenizeMHASnippets(const SnippetsToken// (to avoid performance regressions due to scalar calculations).// But operations after Transpose1 and before MatMul0 will be fused into one loop as well (look at// first point)- size_t uniqie_buffer_reg_group_count = 1; // After MatMul0 there is always one Buffer+ size_t n_buffer_reg_groups = 1; // After MatMul0 there is always one Buffer+ // Loop depth could reach 3 because of SplitLoops optimization+ static constexpr size_t n_loops_depth = 3;ov::NodeVector ordered_ops;- /* ======== Matcher Pass ========== */-- /****** Skeleton ******//* Skeleton on MHA-pattern is:* \ /* MatMul0@@ -328,10 +300,7 @@ ov::snippets::pass::TokenizeMHASnippets::TokenizeMHASnippets(const SnippetsTokenauto interm_op = matmul0->get_output_target_inputs(0).begin()->get_node()->shared_from_this();// Add supported operations which are between MatMul0 and Softmax to ordered_ops- if (!update_intermediate_supported_ops(interm_op,- ordered_ops,- hidden_virtual_ports_count,- potential_body_params_count)) {+ if (!update_intermediate_supported_ops(interm_op, ordered_ops, n_potential_body_params)) {return false;}@@ -369,10 +338,7 @@ ov::snippets::pass::TokenizeMHASnippets::TokenizeMHASnippets(const SnippetsToken}// Add supported operations which are between Softmax and MatMul1 to ordered_ops- if (!update_intermediate_supported_ops(interm_op,- ordered_ops,- hidden_virtual_ports_count,- potential_body_params_count)) {+ if (!update_intermediate_supported_ops(interm_op, ordered_ops, n_potential_body_params)) {return false;}@@ -398,11 +364,9 @@ ov::snippets::pass::TokenizeMHASnippets::TokenizeMHASnippets(const SnippetsTokenconst auto matmul0_prc =op::Brgemm::get_output_type(matmul0->get_input_element_type(0), matmul0->get_input_element_type(1));if (matmul1->get_input_element_type(0).size() != matmul0_prc.size()) {- uniqie_buffer_reg_group_count++;+ n_buffer_reg_groups++;}- /***********************/-/***** Transposes *****//* There may be Transpose and Reshape ops on inputs and outputs of MHA-pattern skeleton* We can add them into Subgraph body@@ -464,20 +428,12 @@ ov::snippets::pass::TokenizeMHASnippets::TokenizeMHASnippets(const SnippetsTokenbreak;}- // To avoid unsupported number of non-scalar Constants in the future after FakeQuantize decomposition- // (plugin specific limitation) we should calculate potential number of non-scalar Constants for- // FakeQuantize that will be moved up from body.- if (const auto fq_node = ov::as_type_ptr<ov::op::v0::FakeQuantize>(child)) {- hidden_virtual_ports_count += ov::snippets::utils::get_non_scalar_constant_count_for_fq(fq_node);- }- potential_body_params_count += get_potential_body_params(child);-+ n_potential_body_params += get_potential_body_params(child);+ const size_t n_child_consumers = child->get_output_target_inputs(0).size();// TODO [75567]: move this plugin-specific constraint to the plugin callback- // We cannot collapse op to Subgraph if count of potential Parameter and Result count is- // higher 12- if (potential_body_params_count + child->get_output_target_inputs(0).size() +- hidden_virtual_ports_count + uniqie_buffer_reg_group_count >- 12) {+ if (!config.is_gprs_count_sufficient(n_potential_body_params + n_child_consumers,+ n_buffer_reg_groups,+ n_loops_depth)) {break;}@@ -486,7 +442,7 @@ ov::snippets::pass::TokenizeMHASnippets::TokenizeMHASnippets(const SnippetsTokenhas_matmul1_has_ops_on_output = true;}if (has_matmul1_has_ops_on_output) {- uniqie_buffer_reg_group_count++;+ n_buffer_reg_groups++;}// At the moment Snippets don't support nodes between MatMul1 and Transpose3 due to Loop and strided@@ -507,57 +463,26 @@ ov::snippets::pass::TokenizeMHASnippets::TokenizeMHASnippets(const SnippetsToken}}- /**********************/-- /* ================================ */-- /* ======= Support checks ========= */-- // TODO [75567]: move this plugin-specific constraint to the plugin callback- const auto last_node = ordered_ops.back();- const auto io_count =- potential_body_params_count + last_node->get_output_size() + hidden_virtual_ports_count;- const auto data_count = io_count + uniqie_buffer_reg_group_count;- auto available_regs = config.get_data_ptr_gpr_count();- // [150148, 150149] Currently Snippets don't have mechanism of spilling registers on stack.- // Due to this limitation we have to skip tokenization of some subgraphs- // if we need more registers than we have on the target machine.- // `config.get_data_ptr_gpr_count()` provides available data registers count (including- // parameters, results and buffers) after excluding 2 registers for work amounts. However,- // MHA Subgraph has `SplitLoops` optimization which adds outermost blocked Loop by M. This- // Loop requires the separate own register for `work_amount` also. Thus, we have to- // decrement `available_regs` count in MHA case. Need to notice that in general we have- // enough count of available registers. But in rare cases (when there are a lot of- // parameters/results, the heuristic value of their number is `5`) the count of available- // registers might be not enough and we have to not tokenize these subgraphs. So only for- // these rare cases we decrement `available_regs` value.- if (io_count > 5) {- available_regs--;- }-- if (data_count > available_regs) {+ /* ======= Plugin related checks ========= */+ const bool is_dynamic =+ std::any_of(ordered_ops.cbegin(), ordered_ops.cend(), [](const std::shared_ptr<Node>& n) {+ return n->is_dynamic();+ });+ if (is_dynamic && !config.is_dynamic_mha_token_enabled()) {return false;}- // If backend doesn't enable dynamic MHA tokenization, return false- if (!config.is_dynamic_mha_token_enabled()) {- if (std::any_of(ordered_ops.cbegin(), ordered_ops.cend(), [](const std::shared_ptr<ov::Node>& op) {- return op->is_dynamic();- })) {- return false;- }+ // TODO [75567]: move this plugin-specific constraint to the plugin callback+ const auto io_count = n_potential_body_params + ordered_ops.back()->get_output_size();+ if (!config.is_gprs_count_sufficient(io_count, n_buffer_reg_groups, n_loops_depth, is_dynamic)) {+ return false;}- /* ================================ */-- const auto subgraph = ov::snippets::utils::tokenize_ordered_nodes(ordered_ops);-+ const auto subgraph = tokenize_ordered_nodes(ordered_ops);// mark the Subgraph as Completed to not allow Snippets to include any nodes into the MHA Subgraph in common// TokenizationSetSnippetsSubgraphType(subgraph, SnippetsSubgraphType::Completed);return true;-- /* ================================ */});}diff --git a/src/common/snippets/src/pass/mlp_seq_tokenization.cpp b/src/common/snippets/src/pass/mlp_seq_tokenization.cppindex 919c6d7c124..e0b2f84bd49 100644--- a/src/common/snippets/src/pass/mlp_seq_tokenization.cpp+++ b/src/common/snippets/src/pass/mlp_seq_tokenization.cpp@@ -12,7 +12,6 @@#include "openvino/core/except.hpp"#include "openvino/core/node.hpp"#include "openvino/core/node_vector.hpp"-#include "openvino/core/shape.hpp"#include "openvino/core/type.hpp"#include "openvino/core/type/element_type.hpp"#include "openvino/core/validation_util.hpp"@@ -29,7 +28,6 @@#include "openvino/util/pp.hpp"#include "snippets/itt.hpp"#include "snippets/op/brgemm.hpp"-#include "snippets/op/subgraph.hpp"#include "snippets/pass/collapse_subgraph.hpp"#include "snippets/pass/tokenization.hpp"#include "snippets/utils/tokenization_utils.hpp"@@ -37,31 +35,12 @@namespace ov::snippets::pass {-namespace {+using namespace ov::snippets::utils;+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) {- if (const auto fq = ov::as_type_ptr<ov::op::v0::FakeQuantize>(op)) {- return ov::snippets::utils::get_non_scalar_constant_count_for_fq(fq);- }- 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);- if (!is_scalar && !should_be_inside_body) {- count++;- }- }- return count;-}-} // namespaceconst size_t TokenizeMLPSeqSnippets::m_rank = 2;@@ -104,7 +83,7 @@ bool TokenizeMLPSeqSnippets::is_supported_intermediate_op(const std::shared_ptr<ov::op::v0::FakeQuantize>(node);}-TokenizeMLPSeqSnippets::TokenizeMLPSeqSnippets(const SnippetsTokenization::Config& config) {+TokenizeMLPSeqSnippets::TokenizeMLPSeqSnippets(const Config& config) {MATCHER_SCOPE(TokenizeMLPSeqSnippets);auto constant = ov::pass::pattern::wrap_type<ov::op::v0::Constant>();@@ -116,14 +95,8 @@ TokenizeMLPSeqSnippets::TokenizeMLPSeqSnippets(const SnippetsTokenization::ConfiOV_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;+ // Two inputs of first MatMul + Result+ size_t io_count = 3;ov::NodeVector ordered_ops;/* ======== Matcher Pass ========== */@@ -152,12 +125,17 @@ TokenizeMLPSeqSnippets::TokenizeMLPSeqSnippets(const SnippetsTokenization::Confiif (transformation_callback(matmul0)) {return false;}+ const auto& out_shape = matmul0->get_output_partial_shape(0);+ OPENVINO_ASSERT(out_shape.rank().is_static() && out_shape.size() >= 2,+ "MatMul out shape must be 2D at least");+ const auto& m_dim = *(out_shape.rbegin() + 1);+ bool is_dynamic = matmul0->is_dynamic();// 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 (auto fq = ov::as_type_ptr<ov::op::v0::FakeQuantize>(matmul0->get_input_node_shared_ptr(0))) {if (has_one_consumer(fq)) {- potential_body_params_count += ov::snippets::utils::get_non_scalar_constant_count_for_fq(fq);+ is_dynamic = is_dynamic || fq->is_dynamic();+ io_count += get_non_scalar_constant_count_for_fq(fq);ordered_ops.push_back(fq);}}@@ -166,16 +144,14 @@ TokenizeMLPSeqSnippets::TokenizeMLPSeqSnippets(const SnippetsTokenization::Confi// Tokenize Sequence while we can do itstd::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();-bool postops_fusion_possible = true;std::shared_ptr<ov::op::v0::MatMul> cur_matmul = matmul0;while (has_one_consumer(prev_op)) {- auto current_potential_body_params_count = potential_body_params_count;+ auto current_io_count = io_count;if (is_matmul_supported(interm_op) && !transformation_callback(interm_op)) {// +1 for weights- current_potential_body_params_count++;+ current_io_count++;// If MatMul is the first in the sequence, postops fusion status is resetpostops_fusion_possible = true;cur_matmul = ov::as_type_ptr<ov::op::v0::MatMul>(interm_op);@@ -183,10 +159,9 @@ TokenizeMLPSeqSnippets::TokenizeMLPSeqSnippets(const SnippetsTokenization::Confi} else if (is_supported_intermediate_op(interm_op)) {// Intermediate op contributes to the body params count only if can't be fused as post-op// or if a previous node between MatMul and this op is not supported by post-op fusion- if (!postops_fusion_possible || config.get_can_be_fused_as_postop() == nullptr ||- !config.get_can_be_fused_as_postop()(cur_matmul, interm_op)) {+ if (!postops_fusion_possible || !config.get_can_be_fused_as_postop()(cur_matmul, interm_op)) {postops_fusion_possible = false;- current_potential_body_params_count += get_potential_body_params(interm_op);+ current_io_count += get_potential_body_params(interm_op);}} else {// Unsupported op@@ -194,8 +169,14 @@ TokenizeMLPSeqSnippets::TokenizeMLPSeqSnippets(const SnippetsTokenization::Confi}// 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) {+ // Heuiristic count of possible buffers - upper-bound value+ static constexpr size_t n_buffer_reg_groups = 2;+ const bool small_m = m_dim.is_static() && (m_dim.get_length() <= 32);+ // Loop depth could reach 3 because of SplitLoops optimization.+ // If M is small enough, SplitLoops is not needed+ const size_t loops_depth = small_m ? 2 : 3;+ is_dynamic = is_dynamic || interm_op->is_dynamic();+ if (!config.is_gprs_count_sufficient(current_io_count, n_buffer_reg_groups, loops_depth, is_dynamic)) {break;}@@ -204,7 +185,7 @@ TokenizeMLPSeqSnippets::TokenizeMLPSeqSnippets(const SnippetsTokenization::Confiinterm_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;+ io_count = current_io_count;}// Currently, sequence of MLP should contain 2 MatMuls at least@@ -216,10 +197,7 @@ TokenizeMLPSeqSnippets::TokenizeMLPSeqSnippets(const SnippetsTokenization::Confireturn false;}- /* ====== Subgraph creation ======= */-- const auto subgraph = ov::snippets::utils::tokenize_ordered_nodes(ordered_ops);-+ const auto subgraph = tokenize_ordered_nodes(ordered_ops);// mark the Subgraph as Completed to not allow Snippets to include any nodes into this Subgraph in common// TokenizationSetSnippetsSubgraphType(subgraph, SnippetsSubgraphType::Completed);diff --git a/src/common/snippets/src/pass/tokenization.cpp b/src/common/snippets/src/pass/tokenization.cppindex 5dc1bb0667d..cdae95c61cc 100644--- a/src/common/snippets/src/pass/tokenization.cpp+++ b/src/common/snippets/src/pass/tokenization.cpp@@ -95,16 +95,16 @@ bool SnippetsTokenization::run_on_model(const std::shared_ptr<ov::Model>& m) {// 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<TokenizeGatedMLPSnippets>(m_config);- manager.register_pass<TokenizeMLPSeqSnippets>(m_config);+ manager.register_pass<TokenizeMHASnippets>(m_mha_config);+ manager.register_pass<TokenizeGatedMLPSnippets>(m_tokenization_config);+ manager.register_pass<TokenizeMLPSeqSnippets>(m_mlp_seq_config);auto tokenization_passes = manager.register_pass<ov::pass::GraphRewrite>();tokenization_passes->add_matcher<TokenizeGNSnippets>();- tokenization_passes->add_matcher<TokenizeFCSnippets>(m_config);- tokenization_passes->add_matcher<TokenizeSnippets>(m_config);+ tokenization_passes->add_matcher<TokenizeFCSnippets>(m_tokenization_config);+ tokenization_passes->add_matcher<TokenizeSnippets>(m_tokenization_config);- manager.register_pass<CommonOptimizations>(m_config);+ manager.register_pass<CommonOptimizations>(m_common_optimizations_config);manager.run_passes(m);// Returning value is false because pass::Manager always apply Validation pass if function was changed.diff --git a/src/common/snippets/src/utils/tokenization_utils.cpp b/src/common/snippets/src/utils/tokenization_utils.cppindex 77807f572ac..f58fbf47b29 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/opsets/opset1.hpp"#include "snippets/op/subgraph.hpp"#include "snippets/pass/tokenization.hpp"+#include "snippets/pass/tokenization_config.hpp"#include "snippets/remarks.hpp"#include "snippets/utils/utils.hpp"@@ -80,7 +81,7 @@ auto outputs_are_not_broadcastable(const std::shared_ptr<const Node>& node) -> b}} // namespace-bool tokenize_node(const std::shared_ptr<ov::Node>& node, const SnippetsTokenization::Config& config) {+bool tokenize_node(const std::shared_ptr<ov::Node>& node, const TokenizationConfig& config) {const auto getFusedNames = [](const std::shared_ptr<Node>& n) -> std::string {auto rt_info = n->get_rt_info();auto it = rt_info.find("originalLayersNames");@@ -387,13 +388,15 @@ bool tokenize_node(const std::shared_ptr<ov::Node>& node, const SnippetsTokeniza}// The each data node (Parameter (and non-Scalar Constants), Result, Buffers with the same ID) requires the own- // unique GPR. At the moment, CPU Plugin has limitation for GPR registers: there are 12 available GPRs, and one of- // them must be reserved for runtime parameters, so only 11 can be used during kernel execution. This limitation- // will be resolved once generator supports gprs spills [75622].+ // unique GPR. At the moment, CPU Plugin has limitation for GPR registers.+ // This limitation will be resolved once generator supports gprs spills [75622].// TODO [75567]: move this plugin-specific constraint to the plugin callback+ const auto io_count = body_parameters.size() + body_results.size() + hidden_data_count;const auto unique_buffer_count = op::Subgraph::get_estimated_buffer_count(ops_for_buffer_count);- const size_t max_data_ptr_count = config.get_data_ptr_gpr_count();- if (body_parameters.size() + body_results.size() + hidden_data_count + unique_buffer_count > max_data_ptr_count) {+ // This helper is usually used for eltwises tokenization, where maximal loops depth is always 2+ // (no additional loops optimizations are applied)+ static constexpr size_t loops_depth = 2;+ if (!config.is_gprs_count_sufficient(io_count, unique_buffer_count, loops_depth)) {const std::string message_reset ="new subgraph is created. Impossible to schedule subgraph with " + std::to_string(body_parameters.size()) +" inputs, " + std::to_string(body_results.size()) + " outputs and " + std::to_string(hidden_data_count) +@@ -538,4 +541,26 @@ std::shared_ptr<ov::snippets::op::Subgraph> tokenize_ordered_nodes(const ov::Nodreturn subgraph;}+size_t get_potential_body_params(const std::shared_ptr<ov::Node>& op) {+ // To avoid unsupported number of non-scalar Constants in the future after FakeQuantize decomposition+ // (plugin specific limitation) we should calculate potential number of non-scalar Constants for+ // FakeQuantize that will be moved up from body.+ if (const auto fq = ov::as_type_ptr<ov::op::v0::FakeQuantize>(op)) {+ return ov::snippets::utils::get_non_scalar_constant_count_for_fq(fq);+ }+ 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);+ if (!is_scalar && !should_be_inside_body) {+ count++;+ }+ }+ return count;+}+} // namespace ov::snippets::utilsdiff --git a/src/common/snippets/tests/CMakeLists.txt b/src/common/snippets/tests/CMakeLists.txtindex 114200f647b..8298a43428b 100644--- a/src/common/snippets/tests/CMakeLists.txt+++ b/src/common/snippets/tests/CMakeLists.txt@@ -29,10 +29,12 @@ ov_build_target_faster(${TARGET_NAME}add_library(snippets_test_utils STATIC ${CMAKE_CURRENT_SOURCE_DIR}/include/lowering_utils.hpp${CMAKE_CURRENT_SOURCE_DIR}/include/lir_test_utils.hpp+ ${CMAKE_CURRENT_SOURCE_DIR}/include/utils.hpp${CMAKE_CURRENT_SOURCE_DIR}/include/lir_comparator.hpp${CMAKE_CURRENT_SOURCE_DIR}/include/lowered/pass/buffer_allocation.hpp${CMAKE_CURRENT_SOURCE_DIR}/src/lowering_utils.cpp${CMAKE_CURRENT_SOURCE_DIR}/src/lir_test_utils.cpp+ ${CMAKE_CURRENT_SOURCE_DIR}/src/utils.cpp${CMAKE_CURRENT_SOURCE_DIR}/src/lir_comparator.cpp${CMAKE_CURRENT_SOURCE_DIR}/src/lowered/pass/buffer_allocation.cpp)target_include_directories(snippets_test_utils PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)diff --git a/src/common/snippets/tests/include/pass/collapse_subgraph.hpp b/src/common/snippets/tests/include/pass/collapse_subgraph.hppindex 478310cc4e4..bc6b9fc39d0 100644--- a/src/common/snippets/tests/include/pass/collapse_subgraph.hpp+++ b/src/common/snippets/tests/include/pass/collapse_subgraph.hpp@@ -6,6 +6,9 @@#include <common_test_utils/ov_test_utils.hpp>+#include "snippets/pass/tokenization_config.hpp"+#include "utils.hpp"+namespace ov {namespace test {namespace snippets {@@ -13,6 +16,9 @@ namespace snippets {class CollapseSubgraphTests : public TransformationTestsF {public:virtual void run();++protected:+ ov::snippets::pass::TokenizationConfig config = get_default_tokenization_config();};} // namespace snippetsdiff --git a/src/common/snippets/tests/include/pass/gated_mlp_tokenization.hpp b/src/common/snippets/tests/include/pass/gated_mlp_tokenization.hppindex 65acf41924b..1bd146aba93 100644--- a/src/common/snippets/tests/include/pass/gated_mlp_tokenization.hpp+++ b/src/common/snippets/tests/include/pass/gated_mlp_tokenization.hpp@@ -6,7 +6,8 @@#include <common_test_utils/ov_test_utils.hpp>-#include "snippets/pass/tokenization.hpp"+#include "snippets/pass/common_optimizations.hpp"+#include "snippets/pass/tokenization_config.hpp"#include "utils.hpp"namespace ov {@@ -18,7 +19,8 @@ public:virtual void run();protected:- ov::snippets::pass::SnippetsTokenization::Config config = get_default_tokenization_config();+ ov::snippets::pass::TokenizationConfig base_config = get_default_tokenization_config();+ ov::snippets::pass::CommonOptimizations::Config common_config = get_default_common_optimizations_config();};} // namespace snippetsdiff --git a/src/common/snippets/tests/include/pass/mha_tokenization.hpp b/src/common/snippets/tests/include/pass/mha_tokenization.hppindex 4b9aa82d83b..4b372f638a1 100644--- a/src/common/snippets/tests/include/pass/mha_tokenization.hpp+++ b/src/common/snippets/tests/include/pass/mha_tokenization.hpp@@ -6,7 +6,8 @@#include <common_test_utils/ov_test_utils.hpp>-#include "snippets/pass/tokenization.hpp"+#include "snippets/pass/common_optimizations.hpp"+#include "snippets/pass/mha_tokenization.hpp"#include "utils.hpp"namespace ov {@@ -18,7 +19,8 @@ public:virtual void run();protected:- ov::snippets::pass::SnippetsTokenization::Config config = get_default_tokenization_config();+ ov::snippets::pass::TokenizeMHASnippets::Config mha_config = get_default_mha_config();+ ov::snippets::pass::CommonOptimizations::Config common_config = get_default_common_optimizations_config();};} // namespace snippetsdiff --git a/src/common/snippets/tests/include/pass/mlp_seq_tokenization.hpp b/src/common/snippets/tests/include/pass/mlp_seq_tokenization.hppindex 60ef2a5cf02..9294fa78dcb 100644--- a/src/common/snippets/tests/include/pass/mlp_seq_tokenization.hpp+++ b/src/common/snippets/tests/include/pass/mlp_seq_tokenization.hpp@@ -6,7 +6,8 @@#include <common_test_utils/ov_test_utils.hpp>-#include "snippets/pass/tokenization.hpp"+#include "snippets/pass/common_optimizations.hpp"+#include "snippets/pass/mlp_seq_tokenization.hpp"#include "utils.hpp"namespace ov {@@ -18,7 +19,8 @@ public:virtual void run();protected:- ov::snippets::pass::SnippetsTokenization::Config config = get_default_tokenization_config();+ ov::snippets::pass::TokenizeMLPSeqSnippets::Config mlp_seq_config = get_default_mlp_seq_config();+ ov::snippets::pass::CommonOptimizations::Config common_config = get_default_common_optimizations_config();};} // namespace snippetsdiff --git a/src/common/snippets/tests/include/utils.hpp b/src/common/snippets/tests/include/utils.hppindex 4c283bc413b..8a5350ea97d 100644--- a/src/common/snippets/tests/include/utils.hpp+++ b/src/common/snippets/tests/include/utils.hpp@@ -1,18 +1,22 @@-// Copyright (C) 2024 Intel Corporation+// Copyright (C) 2018-2025 Intel Corporation// SPDX-License-Identifier: Apache-2.0//#pragma once-#include "snippets/pass/tokenization.hpp"+#include "snippets/pass/common_optimizations.hpp"+#include "snippets/pass/mha_tokenization.hpp"+#include "snippets/pass/mlp_seq_tokenization.hpp"+#include "snippets/pass/tokenization_config.hpp"namespace ov {namespace test {namespace snippets {-static ov::snippets::pass::SnippetsTokenization::Config get_default_tokenization_config() {- return { 1, std::numeric_limits<size_t>::max(), true, true, true, { 3, 4 }};-}+ov::snippets::pass::TokenizationConfig get_default_tokenization_config();+ov::snippets::pass::CommonOptimizations::Config get_default_common_optimizations_config();+ov::snippets::pass::TokenizeMHASnippets::Config get_default_mha_config();+ov::snippets::pass::TokenizeMLPSeqSnippets::Config get_default_mlp_seq_config();} // namespace snippets} // namespace testdiff --git a/src/common/snippets/tests/src/lowering_utils.cpp b/src/common/snippets/tests/src/lowering_utils.cppindex 71f129e86be..58a32d8a1c8 100644--- a/src/common/snippets/tests/src/lowering_utils.cpp+++ b/src/common/snippets/tests/src/lowering_utils.cpp@@ -175,7 +175,7 @@ std::shared_ptr<ov::snippets::op::Subgraph>std::shared_ptr<ov::snippets::op::Subgraph> LoweringTests::getTokenizedSubgraph(const std::shared_ptr<Model> &f) {// Perform tokenizationov::pass::Manager m;- ov::snippets::pass::SnippetsTokenization::Config config = get_default_tokenization_config();+ ov::snippets::pass::TokenizationConfig config = get_default_tokenization_config();m.register_pass<ov::snippets::pass::EnumerateNodes>();m.register_pass<ov::snippets::pass::TokenizeGNSnippets>();m.register_pass<ov::snippets::pass::TokenizeSnippets>(config);diff --git a/src/common/snippets/tests/src/pass/collapse_subgraph.cpp b/src/common/snippets/tests/src/pass/collapse_subgraph.cppindex 88cc59b29e5..85c782ed214 100644--- a/src/common/snippets/tests/src/pass/collapse_subgraph.cpp+++ b/src/common/snippets/tests/src/pass/collapse_subgraph.cpp@@ -17,7 +17,6 @@ namespace snippets {void CollapseSubgraphTests::run() {ASSERT_TRUE(model);- ov::snippets::pass::SnippetsTokenization::Config config = get_default_tokenization_config();manager.register_pass<ov::snippets::pass::EnumerateNodes>();manager.register_pass<ov::snippets::pass::TokenizeSnippets>(config);// todo: This is a temporary work-around. remove when MatMul tokenization is supported through general pipelinediff --git a/src/common/snippets/tests/src/pass/fake_quantize_decomposition_test.cpp b/src/common/snippets/tests/src/pass/fake_quantize_decomposition_test.cppindex d0aba0fc4ab..5d1301d7c9b 100644--- a/src/common/snippets/tests/src/pass/fake_quantize_decomposition_test.cpp+++ b/src/common/snippets/tests/src/pass/fake_quantize_decomposition_test.cpp@@ -18,7 +18,7 @@ namespace snippets {class FakeQuantizeDecompositionTest : public TransformationTestsF {public:void register_passes() {- ov::snippets::pass::SnippetsTokenization::Config config = get_default_tokenization_config();+ ov::snippets::pass::CommonOptimizations::Config config = get_default_common_optimizations_config();manager.register_pass<ov::snippets::pass::CommonOptimizations>(config);}diff --git a/src/common/snippets/tests/src/pass/gated_mlp_tokenization.cpp b/src/common/snippets/tests/src/pass/gated_mlp_tokenization.cppindex b8e643b9618..c29e1ba9155 100644--- a/src/common/snippets/tests/src/pass/gated_mlp_tokenization.cpp+++ b/src/common/snippets/tests/src/pass/gated_mlp_tokenization.cpp@@ -21,8 +21,8 @@ namespace snippets {void TokenizeGatedMLPSnippetsTests::run() {ASSERT_TRUE(model);manager.register_pass<ov::snippets::pass::EnumerateNodes>();- manager.register_pass<ov::snippets::pass::TokenizeGatedMLPSnippets>(config);- manager.register_pass<ov::snippets::pass::CommonOptimizations>(config);+ manager.register_pass<ov::snippets::pass::TokenizeGatedMLPSnippets>(base_config);+ manager.register_pass<ov::snippets::pass::CommonOptimizations>(common_config);disable_rt_info_check();// To avoid Transpose extraction from MatMulsdiff --git a/src/common/snippets/tests/src/pass/mha_tokenization.cpp b/src/common/snippets/tests/src/pass/mha_tokenization.cppindex a28ee1eb8a4..cc0419c8755 100644--- a/src/common/snippets/tests/src/pass/mha_tokenization.cpp+++ b/src/common/snippets/tests/src/pass/mha_tokenization.cpp@@ -18,8 +18,8 @@ void TokenizeMHASnippetsTests::run() {ASSERT_TRUE(model);manager.register_pass<ov::snippets::pass::ExtractReshapesFromMHA>();manager.register_pass<ov::snippets::pass::EnumerateNodes>();- manager.register_pass<ov::snippets::pass::TokenizeMHASnippets>(config);- manager.register_pass<ov::snippets::pass::CommonOptimizations>(config);+ manager.register_pass<ov::snippets::pass::TokenizeMHASnippets>(mha_config);+ manager.register_pass<ov::snippets::pass::CommonOptimizations>(common_config);disable_rt_info_check();}@@ -63,7 +63,12 @@ TEST_F(TokenizeMHASnippetsTests, smoke_Snippets_MHA_4D_Partially_Dynamic_Disableconst 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();- config = ov::snippets::pass::SnippetsTokenization::Config{1, std::numeric_limits<size_t>::max(), true, true, false, { 3, 4 }};+ 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();}@@ -148,7 +153,7 @@ TEST_F(TokenizeMHASnippetsTests, smoke_Snippets_MHA3D_SplitM) {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);- config.set_concurrency(24);+ common_config = ov::snippets::pass::CommonOptimizations::Config(24, true);execute_and_validate_function(*this, f);}@@ -157,7 +162,7 @@ TEST_F(TokenizeMHASnippetsTests, smoke_Snippets_MHA3D_SplitM_withMul) {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);- config.set_concurrency(16);+ common_config = ov::snippets::pass::CommonOptimizations::Config(16, true);execute_and_validate_function(*this, f);}@@ -166,7 +171,7 @@ TEST_F(TokenizeMHASnippetsTests, smoke_Snippets_MHA4D_SplitM) {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);- config.set_concurrency(60);+ common_config = ov::snippets::pass::CommonOptimizations::Config(60, true);execute_and_validate_function(*this, f);}@@ -175,7 +180,7 @@ TEST_F(TokenizeMHASnippetsTests, smoke_Snippets_MHA4D_SplitM_withMul) {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);- config.set_concurrency(60);+ common_config = ov::snippets::pass::CommonOptimizations::Config(60, true);execute_and_validate_function(*this, f);}@@ -183,7 +188,7 @@ 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}});- config.set_concurrency(18);+ common_config = ov::snippets::pass::CommonOptimizations::Config(18, true);execute_and_validate_function(*this, f);}@@ -191,14 +196,14 @@ 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}});- config.set_concurrency(32);+ common_config = ov::snippets::pass::CommonOptimizations::Config(32, true);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);- config.set_concurrency(32);+ common_config = ov::snippets::pass::CommonOptimizations::Config(32, true);execute_and_validate_function(*this, f);}@@ -206,7 +211,7 @@ 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}});- config.set_concurrency(16);+ common_config = ov::snippets::pass::CommonOptimizations::Config(16, true);execute_and_validate_function(*this, f);}@@ -214,7 +219,7 @@ 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}});- config.set_concurrency(16);+ common_config = ov::snippets::pass::CommonOptimizations::Config(16, true);execute_and_validate_function(*this, f);}diff --git a/src/common/snippets/tests/src/pass/mlp_seq_tokenization.cpp b/src/common/snippets/tests/src/pass/mlp_seq_tokenization.cppindex 7ce78c2d314..2d94e9f6b71 100644--- a/src/common/snippets/tests/src/pass/mlp_seq_tokenization.cpp+++ b/src/common/snippets/tests/src/pass/mlp_seq_tokenization.cpp@@ -19,8 +19,8 @@ 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);+ manager.register_pass<ov::snippets::pass::TokenizeMLPSeqSnippets>(mlp_seq_config);+ manager.register_pass<ov::snippets::pass::CommonOptimizations>(common_config);disable_rt_info_check();}class TokenizeMLPSeqSnippetsParamTests : public TokenizeMLPSeqSnippetsTests,diff --git a/src/common/snippets/tests/src/utils.cpp b/src/common/snippets/tests/src/utils.cppnew file mode 100644index 00000000000..cadb1879d6e--- /dev/null+++ b/src/common/snippets/tests/src/utils.cpp@@ -0,0 +1,44 @@+// Copyright (C) 2018-2025 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#include "utils.hpp"++#include <limits>++#include "snippets/pass/common_optimizations.hpp"+#include "snippets/pass/mha_tokenization.hpp"+#include "snippets/pass/mlp_seq_tokenization.hpp"+#include "snippets/pass/tokenization_config.hpp"++namespace ov {+namespace test {+namespace snippets {+using namespace ov::snippets::pass;++TokenizationConfig get_default_tokenization_config() {+ static const TokenizationConfig conf(std::numeric_limits<size_t>::max());+ return conf;+}++CommonOptimizations::Config get_default_common_optimizations_config() {+ static const CommonOptimizations::Config conf(1, true);+ return conf;+}++TokenizeMHASnippets::Config get_default_mha_config() {+ static const TokenizeMHASnippets::Config conf(TokenizationConfig(std::numeric_limits<size_t>::max()),+ true,+ true,+ {3, 4});+ return conf;+}++TokenizeMLPSeqSnippets::Config get_default_mlp_seq_config() {+ static const TokenizeMLPSeqSnippets::Config conf(get_default_tokenization_config());+ return conf;+}++} // namespace snippets+} // namespace test+} // namespace ovdiff --git a/src/plugins/intel_cpu/src/transformations/transformation_pipeline.cpp b/src/plugins/intel_cpu/src/transformations/transformation_pipeline.cppindex 551490a3d77..201f11b8587 100644--- a/src/plugins/intel_cpu/src/transformations/transformation_pipeline.cpp+++ b/src/plugins/intel_cpu/src/transformations/transformation_pipeline.cpp@@ -161,6 +161,7 @@#include "snippets/pass/mha_tokenization.hpp"#include "snippets/pass/mlp_seq_tokenization.hpp"#include "snippets/pass/tokenization.hpp"+#include "snippets/pass/tokenization_config.hpp"// Misc#include "nodes/fake_quantize.h"@@ -1151,6 +1152,7 @@ void Transformations::PostLpt() {}void Transformations::MainSnippets() {+ using namespace snippets::pass;// Disable MainSnippets for int8 models on arm platforms due to performance issues// Ticket: 163408#if defined(OPENVINO_ARCH_ARM) || defined(OPENVINO_ARCH_ARM64)@@ -1180,6 +1182,33 @@ void Transformations::MainSnippets() {// TODO [123659] Implement common logic to split optimization and limitation conditionsconst auto ignoreCallback = config.snippetsMode == Config::SnippetsMode::IgnoreCallback;+#if defined(OPENVINO_ARCH_ARM64)+ // ARM has 32 gprs, but the following registers should be excluded from available registers:+ // - abi_param1: used for runtime parameters+ // - X18: platform register+ // - 2 (SP, X29) stack related registers+ // - 3 (X_TMP_0, X_TMP_1, X_DEFAULT_ADDR) registers for temporary use+ size_t available_gprs_count = 25;+#elif defined(OPENVINO_ARCH_X86_64)+ // X64 has 16 gprs, but the following registers should be excluded from available registers:+ // - abi_param1: used for runtime parameters+ // - RSP: stack related register+ size_t available_gprs_count = 14;+#else+ size_t available_gprs_count = 0;+#endif+ TokenizationConfig tokenization_config(available_gprs_count);++ size_t concurrency = config.streamExecutorConfig.get_threads_per_stream();+ if (concurrency == 0) {+ concurrency = parallel_get_max_threads();+ }+ // The optimization "SplitDimensionM" depends on target machine (thread count).+ // To avoid uncontrolled behavior in tests, we disabled the optimization when there is+ // Config::SnippetsMode::IgnoreCallback+ bool split_m_dimension = !ignoreCallback;+ CommonOptimizations::Config common_optimizations_config(concurrency, split_m_dimension);+// [111813]: At the moment Snippets supports Transpose on output of MHA pattern only if it is an one node between// MatMul and Result. However there may be Convert [f32->bf16] before Result since:// - bf16 Brgemm has f32 output;@@ -1187,24 +1216,19 @@ void Transformations::MainSnippets() {// To avoid situations when Transpose is not alone node between MatMul and Result,// Plugin disables Transpose tokenization on outputbool mha_token_enable_transpose_on_output = any_of(config.inferencePrecision, element::f32, element::dynamic);- size_t concurrency = config.streamExecutorConfig.get_threads_per_stream();- if (concurrency == 0) {- concurrency = parallel_get_max_threads();- }-// Runtime caching should be enabled in case of dynamic Subgraphs in CPU Plugin: to reduce overheads of// ShapeInference and CodeGeneration If runtime cache capacity is zero, it means that rtCache won't be used and we// shouldn't tokenize dynamic Subgraphs - it will lead to performance degradationsbool is_dynamic_mha_token_enabled = config.snippetsCacheCapacity != 0;+ // [122706] Some 3D MHA Patterns have perf regressions when Transpose op is tokenized+ std::set<size_t> mha_supported_transpose_ranks = {4};+ TokenizeMHASnippets::Config mha_config(tokenization_config,+ mha_token_enable_transpose_on_output,+ is_dynamic_mha_token_enabled,+ mha_supported_transpose_ranks);#if defined(OPENVINO_ARCH_ARM64)- // ARM has 32 gprs. After excluding 2 registers for work amounts, 1 register for runtime parameters, 1 platform- // register, 3 registers for temporary use, and 2 stack related registers, it has 23 remaining registers.- size_t data_ptr_gpr_count = 23;- snippets::pass::SnippetsTokenization::Config::CanBeFusedAsPostOpPred supported_as_postop = nullptr;+ TokenizeMLPSeqSnippets::Config mlp_seq_config(tokenization_config);#elif defined(OPENVINO_ARCH_X86_64)- // X64 has 16 gprs. After excluding 2 registers for work amounts, 1 register for runtime parameters,- // and 2 stack related registers, it has 11 remaining registers.- size_t data_ptr_gpr_count = 11;auto supported_as_postop = [this](const std::shared_ptr<const ov::op::v0::MatMul>& matmul,const std::shared_ptr<const ov::Node>& node) {if (!pass::FuseBrgemmCPUPostops::can_be_fused_as_postop(node)) {@@ -1219,23 +1243,10 @@ void Transformations::MainSnippets() {}return pass::FuseBrgemmCPUPostops::brgemm_can_fuse_postop(input_precision);};+ TokenizeMLPSeqSnippets::Config mlp_seq_config(tokenization_config, supported_as_postop);#else- size_t data_ptr_gpr_count = 0;- snippets::pass::SnippetsTokenization::Config::CanBeFusedAsPostOpPred supported_as_postop = nullptr;+ TokenizeMLPSeqSnippets::Config mlp_seq_config(tokenization_config);#endif- // The optimization "SplitDimensionM" depends on target machine (thread count).- // To avoid uncontrolled behavior in tests, we disabled the optimization when there is- // Config::SnippetsMode::IgnoreCallback- bool split_m_dimension = !ignoreCallback;- // [122706] Some 3D MHA Patterns have perf regressions when Transpose op is tokenized- std::set<size_t> mha_supported_transpose_ranks = {4};- snippets::pass::SnippetsTokenization::Config tokenization_config(concurrency,- data_ptr_gpr_count,- split_m_dimension,- mha_token_enable_transpose_on_output,- is_dynamic_mha_token_enabled,- mha_supported_transpose_ranks,- supported_as_postop);ov::pass::Manager snippetsManager("CPU:Snippets");snippetsManager.set_per_pass_validation(false);@@ -1243,10 +1254,15 @@ void Transformations::MainSnippets() {if (!ignoreCallback) {CPU_REGISTER_PASS_ARM64(snippetsManager, SnippetsMarkSkipped);CPU_REGISTER_PASS_X64(snippetsManager, SnippetsMarkSkipped, config.inferencePrecision == ov::element::bf16);- CPU_DISABLE_PASS_COMMON(snippetsManager, snippets::pass::TokenizeFCSnippets);- CPU_DISABLE_PASS_COMMON(snippetsManager, snippets::pass::TokenizeGatedMLPSnippets);+ CPU_DISABLE_PASS_COMMON(snippetsManager, TokenizeFCSnippets);+ CPU_DISABLE_PASS_COMMON(snippetsManager, TokenizeGatedMLPSnippets);}- CPU_REGISTER_PASS_COMMON(snippetsManager, snippets::pass::SnippetsTokenization, tokenization_config);+ CPU_REGISTER_PASS_COMMON(snippetsManager,+ SnippetsTokenization,+ tokenization_config,+ common_optimizations_config,+ mha_config,+ mlp_seq_config);#if defined(OPENVINO_ARCH_X86_64) || defined(OPENVINO_ARCH_ARM64)// Currently, Snippets don't provide efficient execution for single token inference in LLM case.@@ -1276,8 +1292,8 @@ void Transformations::MainSnippets() {#endifif (!isMHASupported) {- CPU_DISABLE_PASS_COMMON(snippetsManager, snippets::pass::TokenizeMHASnippets);- CPU_DISABLE_PASS_COMMON(snippetsManager, snippets::pass::ExtractReshapesFromMHA);+ CPU_DISABLE_PASS_COMMON(snippetsManager, TokenizeMHASnippets);+ CPU_DISABLE_PASS_COMMON(snippetsManager, ExtractReshapesFromMHA);}#if !defined(SNIPPETS_LIBXSMM_TPP) && defined(OPENVINO_ARCH_X86_64)@@ -1291,7 +1307,7 @@ void Transformations::MainSnippets() {#endifif (!isMlpSeqSupported) {- CPU_DISABLE_PASS_COMMON(snippetsManager, snippets::pass::TokenizeMLPSeqSnippets);+ CPU_DISABLE_PASS_COMMON(snippetsManager, TokenizeMLPSeqSnippets);}#if defined(OPENVINO_ARCH_X86_64)@@ -1332,8 +1348,8 @@ void Transformations::MainSnippets() {}// Ticket 160154: enable tokenization for MHA with insufficient parallel work amountconst auto is_unsupported_parallel_work_amount =- static_cast<size_t>(parallel_work_amount.get_length()) < tokenization_config.get_concurrency() &&- !ov::snippets::pass::SplitDimensionM::can_be_optimized(n, tokenization_config.get_concurrency());+ static_cast<size_t>(parallel_work_amount.get_length()) < common_optimizations_config.get_concurrency() &&+ !SplitDimensionM::can_be_optimized(n, common_optimizations_config.get_concurrency());return is_unsupported_parallel_work_amount;};#endif // OPENVINO_ARCH_X86_64@@ -1472,7 +1488,7 @@ void Transformations::MainSnippets() {const auto& pshape = child->get_input_partial_shape(0);return is_unsupported_parallel_work_amount(n, pshape);},- snippets::pass::TokenizeMHASnippets);+ TokenizeMHASnippets);CPU_SET_CALLBACK_X64(snippetsManager,[&](const std::shared_ptr<const ov::Node>& n) -> bool {@@ -1494,14 +1510,14 @@ void Transformations::MainSnippets() {return true;return output_shape[1].is_dynamic() || output_shape[1].get_length() > 256;},- snippets::pass::TokenizeMLPSeqSnippets);+ TokenizeMLPSeqSnippets);CPU_SET_CALLBACK_X64(snippetsManager,[&](const std::shared_ptr<const ov::Node>& n) -> bool {return !is_supported_matmul(n) ||is_unsupported_parallel_work_amount(n, n->get_output_partial_shape(0));},- snippets::pass::ExtractReshapesFromMHA);+ ExtractReshapesFromMHA);}CPU_SET_CALLBACK_COMMON(@@ -1522,7 +1538,7 @@ void Transformations::MainSnippets() {return true;return !has_supported_tensors(n);},- snippets::pass::TokenizeSnippets);+ TokenizeSnippets);auto mm_supports_transpose_b = [this]([[maybe_unused]] const std::shared_ptr<const ov::Node>& n) -> bool {[[maybe_unused]] const auto& inferencePrecision = config.inferencePrecision;@@ -1570,7 +1586,7 @@ void Transformations::MainSnippets() {[&mm_supports_transpose_b](const std::shared_ptr<const ov::Node>& n) -> bool {return mm_supports_transpose_b(n);},- snippets::pass::ExplicitTransposeMatMulInputs);+ ExplicitTransposeMatMulInputs);snippetsManager.run_passes(model);}diff --git a/src/plugins/intel_cpu/tests/unit/snippets_transformations/aarch64/snipptes_mark_skipped.cpp b/src/plugins/intel_cpu/tests/unit/snippets_transformations/aarch64/snipptes_mark_skipped.cppindex 6395759380b..81031cccd9b 100644--- a/src/plugins/intel_cpu/tests/unit/snippets_transformations/aarch64/snipptes_mark_skipped.cpp+++ b/src/plugins/intel_cpu/tests/unit/snippets_transformations/aarch64/snipptes_mark_skipped.cpp@@ -2,13 +2,16 @@// SPDX-License-Identifier: Apache-2.0//#include <gtest/gtest.h>-#include <subgraph_customizable.hpp>+#include <snippets_helpers.hpp>+#include <subgraph_customizable.hpp>#include <transformations/snippets/aarch64/pass/snippets_mark_skipped.hpp>+#include "openvino/core/visibility.hpp"#include "openvino/opsets/opset1.hpp"-#include "snippets/pass/tokenization.hpp"#include "snippets/pass/collapse_subgraph.hpp"+#include "snippets/pass/tokenization.hpp"+#include "snippets/pass/tokenization_config.hpp"namespace ov {namespace test {@@ -18,44 +21,43 @@ class SnippetsMarkSkippedTests : public TransformationTestsF {public:void run() {ASSERT_TRUE(model);- ov::snippets::pass::SnippetsTokenization::Config config = { 1, 23, true, true, true, { 3, 4 }};+ ov::snippets::pass::TokenizationConfig config(23);manager.register_pass<ov::intel_cpu::SnippetsMarkSkipped>();manager.register_pass<ov::snippets::pass::EnumerateNodes>();manager.register_pass<ov::snippets::pass::TokenizeSnippets>(config);- //// todo: This is a temporary work-around. remove when MatMul tokenization is supported through general pipelinemanager.get_pass_config()->set_callback<ov::snippets::pass::TokenizeSnippets>(- [](const std::shared_ptr<const ov::Node>& n) -> bool {- return ov::is_type<const ov::op::v0::MatMul>(n);- });+ [](const std::shared_ptr<const ov::Node>& n) -> bool {+ return ov::is_type<const ov::op::v0::MatMul>(n);+ });}};TEST_F(SnippetsMarkSkippedTests, smoke_Snippets_SkipConvFused_ConvMulRelu) {- std::vector<std::shared_ptr<Node>> eltwiseOps {std::make_shared<ov::op::v1::Multiply>(),- std::make_shared<ov::op::v0::Relu>(),- std::make_shared<ov::op::v0::Relu>()};- std::vector<PartialShape> inputShapes {{1, 2, 16, 16}, {1, 2, 1, 16}};- const auto &f = ConvMulActivationFunction(inputShapes, eltwiseOps);+ std::vector<std::shared_ptr<Node>> eltwiseOps{std::make_shared<ov::op::v1::Multiply>(),+ std::make_shared<ov::op::v0::Relu>(),+ std::make_shared<ov::op::v0::Relu>()};+ std::vector<PartialShape> inputShapes{{1, 2, 16, 16}, {1, 2, 1, 16}};+ const auto& f = ConvMulActivationFunction(inputShapes, eltwiseOps);// Fully tokenizable, since Mul isn't fused into Convolutionexecute_and_validate_function(*this, f);}TEST_F(SnippetsMarkSkippedTests, smoke_Snippets_SkipConvFused_ConvSumRelu) {- std::vector<std::shared_ptr<Node>> eltwiseOps {std::make_shared<ov::op::v1::Add>(),- std::make_shared<ov::op::v0::Relu>(),- std::make_shared<ov::op::v0::Relu>()};- std::vector<PartialShape> inputShapes {{1, 2, 16, 16}, {1, 2, 1, 16}};- const auto &f = ConvMulActivationFunction(inputShapes, eltwiseOps);+ std::vector<std::shared_ptr<Node>> eltwiseOps{std::make_shared<ov::op::v1::Add>(),+ std::make_shared<ov::op::v0::Relu>(),+ std::make_shared<ov::op::v0::Relu>()};+ std::vector<PartialShape> inputShapes{{1, 2, 16, 16}, {1, 2, 1, 16}};+ const auto& f = ConvMulActivationFunction(inputShapes, eltwiseOps);// Fully tokenizable, since Add isn't fused into Convolutionexecute_and_validate_function(*this, f);}TEST_F(SnippetsMarkSkippedTests, smoke_Snippets_SkipConvFused_ConvBiasRelu) {- std::vector<std::shared_ptr<Node>> eltwiseOps {std::make_shared<ov::op::v1::Add>(),- std::make_shared<ov::op::v0::Relu>()};- std::vector<PartialShape> inputShapes {{1, 2, 16, 16}};- const auto &f = ConvBiasActivationFunction(inputShapes, eltwiseOps);+ std::vector<std::shared_ptr<Node>> eltwiseOps{std::make_shared<ov::op::v1::Add>(),+ std::make_shared<ov::op::v0::Relu>()};+ std::vector<PartialShape> inputShapes{{1, 2, 16, 16}};+ const auto& f = ConvBiasActivationFunction(inputShapes, eltwiseOps);model = f.getOriginal();// Not tokenizable, since Bias + Relu can be fused into Convolutionmodel_ref = f.getOriginal();@@ -63,21 +65,21 @@ TEST_F(SnippetsMarkSkippedTests, smoke_Snippets_SkipConvFused_ConvBiasRelu) {}TEST_F(SnippetsMarkSkippedTests, smoke_Snippets_SkipConvFused_ConvBiasTwoRelu) {- std::vector<std::shared_ptr<Node>> eltwiseOps {std::make_shared<ov::op::v1::Add>(),- std::make_shared<ov::op::v0::Relu>(),- std::make_shared<ov::op::v0::Relu>()};- std::vector<PartialShape> inputShapes {{1, 2, 16, 16}};- const auto &f = ConvBiasTwoActivationFunction(inputShapes, eltwiseOps);+ std::vector<std::shared_ptr<Node>> eltwiseOps{std::make_shared<ov::op::v1::Add>(),+ std::make_shared<ov::op::v0::Relu>(),+ std::make_shared<ov::op::v0::Relu>()};+ std::vector<PartialShape> inputShapes{{1, 2, 16, 16}};+ const auto& f = ConvBiasTwoActivationFunction(inputShapes, eltwiseOps);// Partially tokenizable, since Bias and first Relu can be fused into Convolutionexecute_and_validate_function(*this, f);}TEST_F(SnippetsMarkSkippedTests, smoke_Snippets_SkipMatMulFused_MatMulBiasTwoRelu) {- std::vector<std::shared_ptr<Node>> eltwiseOps {std::make_shared<ov::op::v1::Add>(),- std::make_shared<ov::op::v0::Relu>(),- std::make_shared<ov::op::v0::Relu>()};- std::vector<PartialShape> inputShapes {{1, 2, 2, 16}, {16, 4}};- const auto &f = MatMulTwoActivationFunction(inputShapes, eltwiseOps);+ std::vector<std::shared_ptr<Node>> eltwiseOps{std::make_shared<ov::op::v1::Add>(),+ std::make_shared<ov::op::v0::Relu>(),+ std::make_shared<ov::op::v0::Relu>()};+ std::vector<PartialShape> inputShapes{{1, 2, 2, 16}, {16, 4}};+ const auto& f = MatMulTwoActivationFunction(inputShapes, eltwiseOps);model = f.getOriginal();// Not tokenizable, since Bias and two Relu can be fused into MatMulmodel_ref = f.getOriginal();@@ -85,11 +87,11 @@ TEST_F(SnippetsMarkSkippedTests, smoke_Snippets_SkipMatMulFused_MatMulBiasTwoRel}TEST_F(SnippetsMarkSkippedTests, smoke_Snippets_SkipMatMulFused_MatMulBiasReluDiv) {- std::vector<std::shared_ptr<Node>> eltwiseOps {std::make_shared<ov::op::v1::Add>(),- std::make_shared<ov::op::v0::Relu>(),- std::make_shared<ov::op::v1::Divide>()};- std::vector<PartialShape> inputShapes {{1, 2, 2, 16}, {16, 4}, {1, 2, 2, 4}};- const auto &f = MatMulBiasActivationBinaryFunction(inputShapes, eltwiseOps);+ std::vector<std::shared_ptr<Node>> eltwiseOps{std::make_shared<ov::op::v1::Add>(),+ std::make_shared<ov::op::v0::Relu>(),+ std::make_shared<ov::op::v1::Divide>()};+ std::vector<PartialShape> inputShapes{{1, 2, 2, 16}, {16, 4}, {1, 2, 2, 4}};+ const auto& f = MatMulBiasActivationBinaryFunction(inputShapes, eltwiseOps);// There will one Subgraph with Divide since Bias and Relu can be fused into MatMulexecute_and_validate_function(*this, f);}diff --git a/src/plugins/intel_cpu/tests/unit/snippets_transformations/x64/fake_quantize_tokenization_test.cpp b/src/plugins/intel_cpu/tests/unit/snippets_transformations/x64/fake_quantize_tokenization_test.cppindex 7486bb08827..d86cd6086a2 100644--- a/src/plugins/intel_cpu/tests/unit/snippets_transformations/x64/fake_quantize_tokenization_test.cpp+++ b/src/plugins/intel_cpu/tests/unit/snippets_transformations/x64/fake_quantize_tokenization_test.cpp@@ -8,6 +8,7 @@#include "common_test_utils/ov_test_utils.hpp"#include "snippets/pass/fq_decomposition.hpp"#include "snippets/pass/tokenization.hpp"+#include "snippets/pass/tokenization_config.hpp"#include "snippets/pass/collapse_subgraph.hpp"#include "fake_quantize_helper.hpp"#include "snippets/op/subgraph.hpp"@@ -21,7 +22,7 @@ namespace snippets {class FakeQuantizeTokenizationTest : public TransformationTestsF {public:void register_passes() {- ov::snippets::pass::SnippetsTokenization::Config config = { 1, std::numeric_limits<size_t>::max(), true, true, true, { 3, 4 }};+ ov::snippets::pass::TokenizationConfig config(std::numeric_limits<size_t>::max());manager.register_pass<ov::intel_cpu::SnippetsMarkSkipped>();manager.register_pass<ov::snippets::pass::EnumerateNodes>();manager.register_pass<ov::snippets::pass::TokenizeSnippets>(config);diff --git a/src/plugins/intel_cpu/tests/unit/snippets_transformations/x64/snipptes_mark_skipped.cpp b/src/plugins/intel_cpu/tests/unit/snippets_transformations/x64/snipptes_mark_skipped.cppindex 4a624bfa510..cfddd33c1c0 100644--- a/src/plugins/intel_cpu/tests/unit/snippets_transformations/x64/snipptes_mark_skipped.cpp+++ b/src/plugins/intel_cpu/tests/unit/snippets_transformations/x64/snipptes_mark_skipped.cpp@@ -8,6 +8,7 @@#include <transformations/snippets/x64/pass/snippets_mark_skipped.hpp>#include "openvino/opsets/opset1.hpp"#include "snippets/pass/tokenization.hpp"+#include "snippets/pass/tokenization_config.hpp"#include "snippets/pass/collapse_subgraph.hpp"namespace ov {@@ -18,11 +19,10 @@ class SnippetsMarkSkippedTests : public TransformationTestsF {public:void run() {ASSERT_TRUE(model);- ov::snippets::pass::SnippetsTokenization::Config config = { 1, 11, true, true, true, { 3, 4 }};+ ov::snippets::pass::TokenizationConfig config(11);manager.register_pass<ov::intel_cpu::SnippetsMarkSkipped>();manager.register_pass<ov::snippets::pass::EnumerateNodes>();manager.register_pass<ov::snippets::pass::TokenizeSnippets>(config);- //// todo: This is a temporary work-around. remove when MatMul tokenization is supported through general pipelinemanager.get_pass_config()->set_callback<ov::snippets::pass::TokenizeSnippets>([](const std::shared_ptr<const ov::Node>& n) -> bool {