Skip to content

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

[Snippets] Remove node friendly name dependency from tokenization (#37742)

Arseniy Obolenskiyc4c9fecc5aef
diff --git a/src/common/snippets/src/utils/tokenization_utils.cpp b/src/common/snippets/src/utils/tokenization_utils.cpp
index bb873828943..8ebdbf2a57a 100644
--- a/src/common/snippets/src/utils/tokenization_utils.cpp
+++ b/src/common/snippets/src/utils/tokenization_utils.cpp
@@ -272,28 +272,11 @@ bool tokenize_node(const std::shared_ptr<ov::Node>& node, const ov::snippets::pa
for (size_t i = 0; i < input_body_parameters.size(); ++i) {
auto found = std::find(external_inputs.begin(), external_inputs.end(), subgraph->input_value(i));
if (found != external_inputs.end()) {
- // Todo: here we rely on friendly_name uniqueness. Propose a different algorithm.
- size_t current_input_index = body_parameters.size();
- for (size_t p_ind = 0; p_ind < body_parameters.size(); p_ind++) {
- const auto& p = body_parameters[p_ind];
- // unite two body parameters from two input subgraphs only if:
- // 1. two input subgraphs are connected to the same parent node/subgraph,
- // 2. and connected to the same output port of this parent node/subgraph.
- if (p->get_friendly_name() == found->get_node_shared_ptr()->get_friendly_name() &&
- external_inputs[p_ind] == *found) {
- current_input_index = p_ind;
- break;
- }
- }
-
- if (current_input_index < body_parameters.size()) {
- remark(13) << "replacing " << *found << " " << current_input_index << " with "
- << body_parameters[current_input_index] << '\n';
- f->replace_parameter(i, body_parameters[current_input_index]);
- } else {
- external_inputs.push_back(subgraph->input_value(i));
- body_parameters.push_back(input_body_parameters[i]);
- }
+ const auto current_input_index =
+ static_cast<size_t>(std::distance(external_inputs.begin(), found));
+ remark(13) << "replacing " << *found << " " << current_input_index << " with "
+ << body_parameters[current_input_index] << '\n';
+ f->replace_parameter(i, body_parameters[current_input_index]);
} else if (is_recurrent(subgraph->input_value(i))) {
remark(13) << "ternary merge is conducted " << subgraph->input_value(i).get_node_shared_ptr()
<< '\n';
diff --git a/src/common/snippets/tests/src/pass/collapse_subgraph.cpp b/src/common/snippets/tests/src/pass/collapse_subgraph.cpp
index 0510f8c0703..e374f9fdf9c 100644
--- a/src/common/snippets/tests/src/pass/collapse_subgraph.cpp
+++ b/src/common/snippets/tests/src/pass/collapse_subgraph.cpp
@@ -2,13 +2,16 @@
// SPDX-License-Identifier: Apache-2.0
//
+#include "snippets/pass/collapse_subgraph.hpp"
+
#include <gtest/gtest.h>
+
#include <pass/collapse_subgraph.hpp>
-#include <subgraph_simple.hpp>
-#include <subgraph_fq.hpp>
+#include <subgraph_common_parent.hpp>
#include <subgraph_converts.hpp>
-#include "snippets/pass/tokenization.hpp"
-#include "snippets/pass/collapse_subgraph.hpp"
+#include <subgraph_fq.hpp>
+#include <subgraph_simple.hpp>
+
#include "utils.hpp"
namespace ov {
@@ -79,6 +82,11 @@ TEST_F(CollapseSubgraphTests, smoke_Snippets_ThreeFQFunction) {
execute_and_validate_function(*this, f);
}
+TEST_F(CollapseSubgraphTests, smoke_Snippets_CommonParentTokenization) {
+ const auto& f = CommonParentTokenizationFunction(std::vector<PartialShape>{{3}});
+ execute_and_validate_function(*this, f);
+}
+
} // namespace snippets
} // namespace test
} // namespace ov
diff --git a/src/tests/ov_helpers/ov_snippets_models/include/subgraph_common_parent.hpp b/src/tests/ov_helpers/ov_snippets_models/include/subgraph_common_parent.hpp
new file mode 100644
index 00000000000..b4b61ba63a8
--- /dev/null
+++ b/src/tests/ov_helpers/ov_snippets_models/include/subgraph_common_parent.hpp
@@ -0,0 +1,38 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#pragma once
+
+#include "snippets_helpers.hpp"
+
+namespace ov {
+namespace test {
+namespace snippets {
+
+// Graph that exercises parameter deduplication at external input indices 0
+// and 1 during tokenization.
+//
+// param0 -> VariadicSplit(axis=0, lengths=[1, 2])
+// split->output(1) + split->output(0) -> Add
+// Add2 -> Result
+// split->output(0) * split->output(1) -> Mul
+//
+// The Add branch inserts external inputs in [1, 0] order. The Multiply branch
+// visits them in [0, 1] order, so tokenization must reuse parameters by both
+// external input indices.
+class CommonParentTokenizationFunction : public SnippetsFunctionBase {
+public:
+ explicit CommonParentTokenizationFunction(const std::vector<PartialShape>& inputShapes)
+ : SnippetsFunctionBase(inputShapes) {
+ OPENVINO_ASSERT(input_shapes.size() == 1, "Got invalid number of input shapes");
+ }
+
+protected:
+ std::shared_ptr<ov::Model> initOriginal() const override;
+ std::shared_ptr<ov::Model> initReference() const override;
+};
+
+} // namespace snippets
+} // namespace test
+} // namespace ov
diff --git a/src/tests/ov_helpers/ov_snippets_models/src/subgraph_common_parent.cpp b/src/tests/ov_helpers/ov_snippets_models/src/subgraph_common_parent.cpp
new file mode 100644
index 00000000000..187e7c4dc1f
--- /dev/null
+++ b/src/tests/ov_helpers/ov_snippets_models/src/subgraph_common_parent.cpp
@@ -0,0 +1,67 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "subgraph_common_parent.hpp"
+
+#include "openvino/op/add.hpp"
+#include "openvino/op/constant.hpp"
+#include "openvino/op/multiply.hpp"
+#include "openvino/op/parameter.hpp"
+#include "openvino/op/result.hpp"
+#include "openvino/op/variadic_split.hpp"
+#include "snippets/op/result.hpp"
+#include "snippets/op/subgraph.hpp"
+
+namespace ov {
+namespace test {
+namespace snippets {
+
+std::shared_ptr<ov::Model> CommonParentTokenizationFunction::initOriginal() const {
+ auto data0 = std::make_shared<op::v0::Parameter>(precision, input_shapes[0]);
+ const auto axis = op::v0::Constant::create(element::i64, Shape{}, {0});
+ const auto split_lengths = op::v0::Constant::create(element::i64, Shape{2}, {1, 2});
+ auto split = std::make_shared<op::v1::VariadicSplit>(data0, axis, split_lengths);
+
+ auto split_out0 = split->output(0);
+ auto split_out1 = split->output(1);
+
+ // The first branch orders the shared inputs as [split_out1, split_out0].
+ // The second branch uses [split_out0, split_out1], so merging it must
+ // reuse body parameters at external input indices 1 and 0 respectively.
+ auto add = std::make_shared<op::v1::Add>(split_out1, split_out0);
+ auto mul = std::make_shared<op::v1::Multiply>(split_out0, split_out1);
+ auto add2 = std::make_shared<op::v1::Add>(add, mul);
+
+ return std::make_shared<ov::Model>(OutputVector{add2}, ParameterVector{data0});
+}
+
+std::shared_ptr<ov::Model> CommonParentTokenizationFunction::initReference() const {
+ auto data0 = std::make_shared<op::v0::Parameter>(precision, input_shapes[0]);
+ const auto axis = op::v0::Constant::create(element::i64, Shape{}, {0});
+ const auto split_lengths = op::v0::Constant::create(element::i64, Shape{2}, {1, 2});
+ auto split = std::make_shared<op::v1::VariadicSplit>(data0, axis, split_lengths);
+
+ auto split_out0 = split->output(0);
+ auto split_out1 = split->output(1);
+
+ // Tokenization orders the merged Subgraph external inputs as
+ // [split_out1, split_out0] from the first Add branch.
+ auto indata0 = std::make_shared<op::v0::Parameter>(precision, split_out1.get_shape());
+ auto indata1 = std::make_shared<op::v0::Parameter>(precision, split_out0.get_shape());
+
+ auto add = std::make_shared<op::v1::Add>(indata0, indata1);
+ auto mul = std::make_shared<op::v1::Multiply>(indata1, indata0);
+ auto add2 = std::make_shared<op::v1::Add>(add, mul);
+
+ auto snippets_result = std::make_shared<ov::snippets::op::Result>(add2);
+ auto subgraph = std::make_shared<ov::snippets::op::Subgraph>(
+ OutputVector{split_out1, split_out0},
+ std::make_shared<ov::Model>(OutputVector{snippets_result}, ParameterVector{indata0, indata1}));
+
+ return std::make_shared<ov::Model>(OutputVector{subgraph}, ParameterVector{data0});
+}
+
+} // namespace snippets
+} // namespace test
+} // namespace ov