Skip to content

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

[Snippets] Optimize buffer adjacency construction (#37080)

Arseniy Obolenskiyc5e83a021e59
diff --git a/src/common/snippets/include/snippets/lowered/pass/set_buffer_reg_group.hpp b/src/common/snippets/include/snippets/lowered/pass/set_buffer_reg_group.hpp
index 028c137d32f..cc436231812 100644
--- a/src/common/snippets/include/snippets/lowered/pass/set_buffer_reg_group.hpp
+++ b/src/common/snippets/include/snippets/lowered/pass/set_buffer_reg_group.hpp
@@ -6,6 +6,7 @@
#include <cstddef>
#include <map>
+#include <unordered_map>
#include <vector>
#include "openvino/core/rtti.hpp"
@@ -57,26 +58,21 @@ public:
private:
using BufferPool = std::vector<BufferExpressionPtr>;
using BufferMap = std::map<BufferExpressionPtr, UnifiedLoopInfo::LoopPortInfo>;
-
- /**
- * @brief Get Buffer Index in Buffer set
- * @param target the target Buffer expression
- * @param pool set of Buffers from the Linear IR
- * @return index of target Buffer expression in set
- */
- static size_t get_buffer_idx(const BufferExpressionPtr& target, const BufferPool& pool);
+ using BufferIndices = std::unordered_map<BufferExpressionPtr, size_t>;
/**
* @brief Create adjacency matrix for Buffer system. See comment in the method for more details.
* @param loop_manager the loop manager
* @param begin begin iterator
* @param end end iterator
* @param pool set of Buffers from the Linear IR
+ * @param buffer_indices map from Buffer expressions to adjacency matrix indices
* @return adjacency matrix where True value means that Buffers are adjacent and cannot have the same ID
*/
static std::vector<bool> create_adjacency_matrix(const LoopManagerPtr& loop_manager,
LinearIR::constExprIt begin,
LinearIR::constExprIt end,
- const BufferPool& pool);
+ const BufferPool& pool,
+ const BufferIndices& buffer_indices);
/**
* @brief Algorithm of Graph coloring where vertices are Buffers
* @param buffers set of Buffers from the Linear IR
@@ -95,12 +91,14 @@ private:
* for its
* @param rhs Pair where first value if Expression with second Buffer and second value is data pointer shift params
* for its
- * @param buffers set of Buffers from the Linear IR
+ * @param buffer_indices map from Buffer expressions to adjacency matrix indices
+ * @param buffer_count adjacency matrix row size
* @param adj Target adjacency matrix
*/
static void update_adj_matrix(const BufferMap::value_type& lhs,
const BufferMap::value_type& rhs,
- const BufferPool& buffers,
+ const BufferIndices& buffer_indices,
+ size_t buffer_count,
std::vector<bool>& adj);
/**
diff --git a/src/common/snippets/src/lowered/pass/set_buffer_reg_group.cpp b/src/common/snippets/src/lowered/pass/set_buffer_reg_group.cpp
index cadf1ce41ca..36ecf3affaf 100644
--- a/src/common/snippets/src/lowered/pass/set_buffer_reg_group.cpp
+++ b/src/common/snippets/src/lowered/pass/set_buffer_reg_group.cpp
@@ -5,7 +5,6 @@
#include "snippets/lowered/pass/set_buffer_reg_group.hpp"
#include <algorithm>
-#include <cassert>
#include <cstddef>
#include <functional>
#include <iterator>
@@ -32,12 +31,6 @@ inline size_t index(size_t col_num, size_t row, size_t col) {
}
} // namespace
-size_t SetBufferRegGroup::get_buffer_idx(const BufferExpressionPtr& target, const BufferPool& pool) {
- const auto iter = std::find(pool.cbegin(), pool.cend(), target);
- assert(iter != pool.cend() && "Buffer wasn't find in Buffer system of Subgraph");
- return std::distance(pool.cbegin(), iter);
-}
-
bool SetBufferRegGroup::can_be_in_one_reg_group(const UnifiedLoopInfo::LoopPortInfo& lhs_info,
const UnifiedLoopInfo::LoopPortInfo& rhs_info) {
const auto equal_element_type_sizes = lhs_info.desc.data_size == rhs_info.desc.data_size;
@@ -77,25 +70,26 @@ bool SetBufferRegGroup::are_adjacent(const BufferMap::value_type& lhs, const Buf
void SetBufferRegGroup::update_adj_matrix(const BufferMap::value_type& lhs,
const BufferMap::value_type& rhs,
- const BufferPool& buffers,
+ const BufferIndices& buffer_indices,
+ size_t buffer_count,
std::vector<bool>& adj) {
- const auto size = buffers.size();
- const auto lhs_idx = get_buffer_idx(lhs.first, buffers);
- const auto rhs_idx = get_buffer_idx(rhs.first, buffers);
+ const auto lhs_idx = buffer_indices.at(lhs.first);
+ const auto rhs_idx = buffer_indices.at(rhs.first);
// Already adjacent - skip
- if (adj[index(size, rhs_idx, lhs_idx)]) {
+ if (adj[index(buffer_count, rhs_idx, lhs_idx)]) {
return;
}
if (are_adjacent(lhs, rhs)) {
- adj[index(size, rhs_idx, lhs_idx)] = adj[index(size, lhs_idx, rhs_idx)] = true;
+ adj[index(buffer_count, rhs_idx, lhs_idx)] = adj[index(buffer_count, lhs_idx, rhs_idx)] = true;
}
}
std::vector<bool> SetBufferRegGroup::create_adjacency_matrix(const LoopManagerPtr& loop_manager,
LinearIR::constExprIt begin,
LinearIR::constExprIt end,
- const BufferPool& pool) {
+ const BufferPool& pool,
+ const BufferIndices& buffer_indices) {
// The sync point to check for adjacency is Loop because only in Loop we increment pointers.
// So if some Buffers in the one Loop have conflict (cannot be inplace: the different ptr increment and data sizes)
// they are called as adjacent
@@ -121,14 +115,14 @@ std::vector<bool> SetBufferRegGroup::create_adjacency_matrix(const LoopManagerPt
// these Buffers are adjacent
for (auto neighbour_it = std::next(buffer_it); neighbour_it != buffer_loop_neighbours.cend();
++neighbour_it) {
- update_adj_matrix(*buffer_it, *neighbour_it, pool, adj);
+ update_adj_matrix(*buffer_it, *neighbour_it, buffer_indices, size, adj);
}
// Buffers which are connected to the current Loop with zero ptr shifts and Buffers which are inside this
// Loop - must be adjacent: after each the Loop iteration GPR will be shifted using ptr increment of Buffer
// outside. But if inner Buffers have the same GPR - it means that these Buffers will work with shifted
// memory.
for (const auto& inner_it : buffers_loop_inside) {
- update_adj_matrix(*buffer_it, inner_it, pool, adj);
+ update_adj_matrix(*buffer_it, inner_it, buffer_indices, size, adj);
}
}
}
@@ -249,9 +243,14 @@ bool SetBufferRegGroup::run(LinearIR& linear_ir,
[](const BufferExpressionPtr& lhs, const BufferExpressionPtr& rhs) {
return lhs->get_exec_num() < rhs->get_exec_num();
});
+ BufferIndices buffer_indices;
+ buffer_indices.reserve(buffer_pool.size());
+ for (size_t i = 0; i < buffer_pool.size(); ++i) {
+ buffer_indices.emplace(buffer_pool[i], i);
+ }
// Creation of Adj matrix
- auto adj = create_adjacency_matrix(linear_ir.get_loop_manager(), begin, end, buffer_pool);
+ auto adj = create_adjacency_matrix(linear_ir.get_loop_manager(), begin, end, buffer_pool, buffer_indices);
// Graph coloring algorithm
const auto color_groups = coloring(buffer_pool, adj);
diff --git a/src/common/snippets/tests/include/lir_test_utils.hpp b/src/common/snippets/tests/include/lir_test_utils.hpp
index ad30f4705ec..4ed1cd6a896 100644
--- a/src/common/snippets/tests/include/lir_test_utils.hpp
+++ b/src/common/snippets/tests/include/lir_test_utils.hpp
@@ -13,6 +13,7 @@
#include "snippets/lowered/linear_ir.hpp"
#include "snippets/lowered/loop_info.hpp"
#include "snippets/lowered/pass/pass.hpp"
+#include "snippets/op/loop.hpp"
namespace ov {
namespace test {
@@ -68,6 +69,41 @@ void init_expr_descriptors(const ov::snippets::lowered::ExpressionPtr& expr,
const std::vector<ov::snippets::VectorDims>& subtensors = {},
const std::vector<ov::snippets::VectorDims>& layouts = {});
+inline ov::snippets::lowered::LinearIR::constExprIt insert_loop_end(
+ const std::shared_ptr<ov::snippets::lowered::LinearIR>& lir,
+ const ov::snippets::lowered::LinearIR::constExprIt& loop_begin_expr,
+ size_t loop_id,
+ const ov::snippets::lowered::LinearIR::constExprIt& pos) {
+ using namespace ov::snippets::lowered;
+
+ const auto& loop_manager = lir->get_loop_manager();
+ const auto loop_info = loop_manager->get_loop_info(loop_id);
+ const auto unified_info = ov::as_type_ptr<UnifiedLoopInfo>(loop_info);
+ OPENVINO_ASSERT(unified_info, "insert_loop_end expects UnifiedLoopInfo");
+
+ std::vector<PortConnectorPtr> loop_end_inputs;
+ loop_end_inputs.reserve(loop_info->get_input_count() + loop_info->get_output_count());
+ loop_info->iterate_through_ports([&loop_end_inputs](const LoopPort& port) {
+ loop_end_inputs.emplace_back(port.get_expr_port()->get_port_connector_ptr());
+ });
+ loop_end_inputs.emplace_back((*loop_begin_expr)->get_output_port_connector(0));
+
+ const auto loop_begin_node = ov::as_type_ptr<ov::snippets::op::LoopBegin>((*loop_begin_expr)->get_node());
+ OPENVINO_ASSERT(loop_begin_node, "The expression is not LoopBegin");
+ const auto loop_end = std::make_shared<ov::snippets::op::LoopEnd>(loop_begin_node,
+ unified_info->get_work_amount(),
+ unified_info->get_increment(),
+ unified_info->get_is_incremented(),
+ unified_info->get_ptr_increments(),
+ unified_info->get_finalization_offsets(),
+ unified_info->get_data_sizes(),
+ unified_info->get_input_count(),
+ unified_info->get_output_count(),
+ loop_id,
+ unified_info->is_parallel());
+ return lir->insert_node(loop_end, loop_end_inputs, {}, false, pos);
+}
+
using IOLoopPortDescs = std::pair<std::vector<ov::snippets::lowered::UnifiedLoopInfo::LoopPortDesc>,
std::vector<ov::snippets::lowered::UnifiedLoopInfo::LoopPortDesc>>;
/**
diff --git a/src/common/snippets/tests/src/lowered/pass/buffer_allocation.cpp b/src/common/snippets/tests/src/lowered/pass/buffer_allocation.cpp
index 9bd779b7748..eddb18d9aae 100644
--- a/src/common/snippets/tests/src/lowered/pass/buffer_allocation.cpp
+++ b/src/common/snippets/tests/src/lowered/pass/buffer_allocation.cpp
@@ -151,6 +151,43 @@ TEST_P(EltwiseBufferAllocationTest, BufferAllocation) {
Validate();
}
+class WideBranchBufferAllocationTest : public BufferAllocationTest {
+public:
+ static std::string getTestCaseName(const testing::TestParamInfo<BufferAllocationParams>& obj) {
+ return "Branches=" + std::to_string(std::get<0>(obj.param).size());
+ }
+
+protected:
+ std::shared_ptr<ov::Model> GetModel(const std::vector<ov::PartialShape>& shapes) const override {
+ const auto subtensor = std::vector<size_t>{1, m_vector_size};
+ const auto buffer_subtensor = std::vector<size_t>(2, ov::snippets::utils::get_full_dim_value());
+ ov::ParameterVector parameters;
+ ov::OutputVector branch_outputs;
+ parameters.reserve(shapes.size());
+ branch_outputs.reserve(shapes.size());
+
+ for (const auto& shape : shapes) {
+ const auto parameter = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, shape);
+ const auto relu = std::make_shared<ov::op::v0::Relu>(parameter);
+ const auto buffer = std::make_shared<ov::snippets::op::Buffer>(relu);
+ MarkOp(relu, subtensor);
+ MarkOp(buffer, buffer_subtensor);
+ parameters.push_back(parameter);
+ branch_outputs.push_back(buffer);
+ }
+
+ const auto result = std::make_shared<ov::snippets::op::Result>(
+ branch_outputs.front(),
+ ov::OutputVector(branch_outputs.begin() + 1, branch_outputs.end()));
+ return std::make_shared<ov::Model>(result, parameters);
+ }
+};
+
+TEST_P(WideBranchBufferAllocationTest, BufferAllocation) {
+ EXPECT_EQ(m_linear_ir.get_buffers().size(), m_shapes.size());
+ Validate();
+}
+
namespace BufferAllocationTest_Instances {
INSTANTIATE_TEST_SUITE_P(smoke_Snippets_BufferAllocation_EltwiseNotOptimized, EltwiseBufferAllocationTest,
@@ -173,6 +210,26 @@ INSTANTIATE_TEST_SUITE_P(smoke_Snippets_BufferAllocation_EltwiseOptimized, Eltwi
::testing::Values(1)), // Two Buffers are from the same luster
BufferAllocationTest::getTestCaseName);
+INSTANTIATE_TEST_SUITE_P(smoke_Snippets_BufferAllocation_WideBranch,
+ WideBranchBufferAllocationTest,
+ ::testing::Values(BufferAllocationParams{
+ std::vector<ov::PartialShape>(32, ov::PartialShape{1, 1, 16}), true, false, 2048, 1, 32},
+ BufferAllocationParams{
+ std::vector<ov::PartialShape>(64, ov::PartialShape{1, 1, 16}),
+ true,
+ false,
+ 4096,
+ 1,
+ 64},
+ BufferAllocationParams{
+ std::vector<ov::PartialShape>(128, ov::PartialShape{1, 1, 16}),
+ true,
+ false,
+ 8192,
+ 1,
+ 128}),
+ WideBranchBufferAllocationTest::getTestCaseName);
+
} // namespace BufferAllocationTest_Instances
} // namespace snippets
} // namespace test
diff --git a/src/common/snippets/tests/src/lowered/pass/set_buffer_reg_group.cpp b/src/common/snippets/tests/src/lowered/pass/set_buffer_reg_group.cpp
new file mode 100644
index 00000000000..040dc3f6206
--- /dev/null
+++ b/src/common/snippets/tests/src/lowered/pass/set_buffer_reg_group.cpp
@@ -0,0 +1,467 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "snippets/lowered/pass/set_buffer_reg_group.hpp"
+
+#include "lir_test_utils.hpp"
+#include "openvino/op/add.hpp"
+#include "openvino/op/parameter.hpp"
+#include "snippets/lowered/pass/mark_invariant_shape_path.hpp"
+#include "snippets/op/brgemm.hpp"
+#include "snippets/op/buffer.hpp"
+#include "snippets/op/loop.hpp"
+#include "snippets/op/result.hpp"
+
+namespace ov {
+namespace test {
+namespace snippets {
+
+using namespace ov::snippets::lowered;
+using namespace ov::snippets::lowered::pass;
+using ov::snippets::op::LoopBegin;
+using PortType = LoopPort::Type;
+using LoopPortDesc = UnifiedLoopInfo::LoopPortDesc;
+
+class SetBufferRegGroupTest : public LoweredPassTestsF {
+public:
+ SetBufferRegGroupTest() : LoweredPassTestsF() {
+ comparator.enable(LIRComparator::LIRCmpValues::EXPR_ATTRS);
+ }
+
+ void SetUp() override {
+ pipeline.register_pass<MarkInvariantShapePath>();
+ pipeline.register_pass<SetBufferRegGroup>();
+ }
+
+ ov::element::Type input_precision = ov::element::f32;
+ static constexpr size_t data_size = 4; // float32
+
+ static LoopPortDesc inc_desc(size_t work_amount) {
+ return LoopPortDesc(1, -static_cast<int64_t>(work_amount), data_size);
+ }
+ static LoopPortDesc not_proc_desc() {
+ return LoopPortDesc(0, 0, data_size);
+ }
+};
+
+/*
+ * Expected Control Flow Graph:
+ * LoopBegin (m)
+ * | LoopBegin (n1)
+ * | | Brgemm1
+ * | LoopEnd (n1)
+ * | Buffer1
+ * | LoopBegin (n_add)
+ * | | Add
+ * | LoopEnd (n_add)
+ * | Buffer2
+ * | LoopBegin (n2)
+ * | | Brgemm2
+ * | LoopEnd (n2)
+ * LoopEnd (m)
+ *
+ * Buffer1 and Buffer2 are both outside inner loops and share register group 0.
+ */
+TEST_F(SetBufferRegGroupTest, TwoBuffersSameRegGroup) {
+ const size_t m = 64;
+ const size_t n = 128;
+ const size_t k = 512;
+ const size_t m_block = 32;
+ const size_t n_block = 64;
+ const ov::Shape shape_a{1, 1, m, k};
+ const ov::Shape shape_b{1, 1, k, n};
+ const ov::Shape shape_c{1, 1, m, n};
+ const ov::Shape shape_d{1, 1, n, n};
+ const ov::snippets::VectorDims a_sub{m_block, ov::snippets::utils::get_full_dim_value()};
+ const ov::snippets::VectorDims b_sub{ov::snippets::utils::get_full_dim_value(), n_block};
+ const ov::snippets::VectorDims c_sub{m_block, n_block};
+
+ auto build_lir = [&](const std::shared_ptr<LinearIR>& lir) {
+ lir->set_loop_depth(2);
+ auto param0 = lir->push_node<ov::op::v0::Parameter>(input_precision, shape_a);
+ auto param1 = lir->push_node<ov::op::v0::Parameter>(input_precision, shape_b);
+ auto param2 = lir->push_node<ov::op::v0::Parameter>(input_precision, shape_c);
+ auto param3 = lir->push_node<ov::op::v0::Parameter>(input_precision, shape_d);
+ const auto& loop_manager = lir->get_loop_manager();
+
+ // Brgemm1 + n-loop
+ auto brgemm1 = lir->push_node<ov::snippets::op::Brgemm>(param0.second, param1.second);
+ init_expr_descriptors(*brgemm1.first, {a_sub, b_sub, c_sub});
+ const auto n1_loop = std::make_shared<UnifiedLoopInfo>(
+ n,
+ n_block,
+ std::vector<LoopPort>{LoopPort::create<PortType::NotProcessed>((*brgemm1.first)->get_input_port(0)),
+ LoopPort::create<PortType::Incremented>((*brgemm1.first)->get_input_port(1), 0)},
+ std::vector<LoopPort>{LoopPort::create<PortType::Incremented>((*brgemm1.first)->get_output_port(0), 0)},
+ std::vector<LoopPortDesc>{not_proc_desc(), inc_desc(n)},
+ std::vector<LoopPortDesc>{inc_desc(n)},
+ false);
+ const auto n1_id = loop_manager->add_loop_info(n1_loop);
+ auto lb1 = lir->insert_node(std::make_shared<LoopBegin>(false),
+ std::vector<PortConnectorPtr>{},
+ {},
+ false,
+ brgemm1.first);
+ auto le1 = insert_loop_end(lir, lb1, n1_id, lir->cend());
+
+ // Buffer1 at m-loop level
+ auto buffer1 = lir->push_node<ov::snippets::op::Buffer>(brgemm1.second);
+
+ // Add + n-loop
+ auto add = lir->push_node<ov::op::v1::Add>(buffer1.second, param2.second);
+ const auto an_loop = std::make_shared<UnifiedLoopInfo>(
+ n,
+ n_block,
+ std::vector<LoopPort>{LoopPort::create<PortType::Incremented>((*add.first)->get_input_port(0), 0),
+ LoopPort::create<PortType::Incremented>((*add.first)->get_input_port(1), 0)},
+ std::vector<LoopPort>{LoopPort::create<PortType::Incremented>((*add.first)->get_output_port(0), 0)},
+ std::vector<LoopPortDesc>{inc_desc(n), inc_desc(n)},
+ std::vector<LoopPortDesc>{inc_desc(n)},
+ false);
+ const auto an_id = loop_manager->add_loop_info(an_loop);
+ auto lb_an =
+ lir->insert_node(std::make_shared<LoopBegin>(false), std::vector<PortConnectorPtr>{}, {}, false, add.first);
+ auto le_an = insert_loop_end(lir, lb_an, an_id, lir->cend());
+
+ // Buffer2 at m-loop level
+ auto buffer2 = lir->push_node<ov::snippets::op::Buffer>(add.second);
+
+ // Brgemm2 + n-loop
+ auto brgemm2 = lir->push_node<ov::snippets::op::Brgemm>(buffer2.second, param3.second);
+ init_expr_descriptors(*brgemm2.first, {c_sub, b_sub, c_sub});
+ const auto n2_loop = std::make_shared<UnifiedLoopInfo>(
+ n,
+ n_block,
+ std::vector<LoopPort>{LoopPort::create<PortType::Incremented>((*brgemm2.first)->get_input_port(0), 0),
+ LoopPort::create<PortType::Incremented>((*brgemm2.first)->get_input_port(1), 0)},
+ std::vector<LoopPort>{LoopPort::create<PortType::Incremented>((*brgemm2.first)->get_output_port(0), 0)},
+ std::vector<LoopPortDesc>{inc_desc(n), inc_desc(n)},
+ std::vector<LoopPortDesc>{inc_desc(n)},
+ false);
+ const auto n2_id = loop_manager->add_loop_info(n2_loop);
+ auto lb2 = lir->insert_node(std::make_shared<LoopBegin>(false),
+ std::vector<PortConnectorPtr>{},
+ {},
+ false,
+ brgemm2.first);
+ auto le2 = insert_loop_end(lir, lb2, n2_id, lir->cend());
+
+ auto result = lir->push_node<ov::snippets::op::Result>(brgemm2.second);
+
+ // m-loop
+ const auto m_loop = std::make_shared<UnifiedLoopInfo>(
+ m,
+ m_block,
+ std::vector<LoopPort>{LoopPort::create<PortType::Incremented>((*brgemm1.first)->get_input_port(0), 1),
+ LoopPort::create<PortType::NotProcessed>((*brgemm1.first)->get_input_port(1)),
+ LoopPort::create<PortType::Incremented>((*add.first)->get_input_port(1), 1),
+ LoopPort::create<PortType::NotProcessed>((*brgemm2.first)->get_input_port(1))},
+ std::vector<LoopPort>{LoopPort::create<PortType::Incremented>((*brgemm2.first)->get_output_port(0), 1)},
+ std::vector<LoopPortDesc>{inc_desc(m), not_proc_desc(), inc_desc(m), not_proc_desc()},
+ std::vector<LoopPortDesc>{inc_desc(m)},
+ false);
+ const auto m_id = loop_manager->add_loop_info(m_loop);
+ auto lb_m = lir->insert_node(std::make_shared<LoopBegin>(false),
+ std::vector<PortConnectorPtr>{},
+ {},
+ false,
+ lir->cend());
+ auto le_m = insert_loop_end(lir, lb_m, m_id, lir->cend());
+
+ // Set loop IDs
+ (*lb_m)->set_loop_ids({m_id});
+ (*lb1)->set_loop_ids({m_id, n1_id});
+ (*brgemm1.first)->set_loop_ids({m_id, n1_id});
+ (*le1)->set_loop_ids({m_id, n1_id});
+ (*buffer1.first)->set_loop_ids({m_id});
+ (*lb_an)->set_loop_ids({m_id, an_id});
+ (*add.first)->set_loop_ids({m_id, an_id});
+ (*le_an)->set_loop_ids({m_id, an_id});
+ (*buffer2.first)->set_loop_ids({m_id});
+ (*lb2)->set_loop_ids({m_id, n2_id});
+ (*brgemm2.first)->set_loop_ids({m_id, n2_id});
+ (*le2)->set_loop_ids({m_id, n2_id});
+ (*le_m)->set_loop_ids({m_id});
+
+ return std::make_pair(ov::as_type_ptr<BufferExpression>(*buffer1.first),
+ ov::as_type_ptr<BufferExpression>(*buffer2.first));
+ };
+
+ build_lir(linear_ir);
+ const auto& [buffer1, buffer2] = build_lir(linear_ir_ref);
+ buffer1->set_reg_group(0);
+ buffer2->set_reg_group(0);
+}
+
+/*
+ * Expected Control Flow Graph:
+ * LoopBegin (m)
+ * | LoopBegin (n1)
+ * | | Brgemm1
+ * | | Buffer1
+ * | | Add
+ * | LoopEnd (n1)
+ * | Buffer2
+ * | LoopBegin (n2)
+ * | | Brgemm2
+ * | LoopEnd (n2)
+ * LoopEnd (m)
+ *
+ * Buffer1 is inside n1 while Buffer2 is outside it, so they use groups 0 and 1.
+ */
+TEST_F(SetBufferRegGroupTest, BufferInsideVsOutsideLoop) {
+ const size_t m = 64;
+ const size_t n = 128;
+ const size_t k = 512;
+ const size_t m_block = 32;
+ const size_t n_block = 64;
+ const ov::Shape shape_a{1, 1, m, k};
+ const ov::Shape shape_b{1, 1, k, n};
+ const ov::Shape shape_c{1, 1, m, n};
+ const ov::Shape shape_d{1, 1, n, n};
+ const ov::snippets::VectorDims a_sub{m_block, ov::snippets::utils::get_full_dim_value()};
+ const ov::snippets::VectorDims b_sub{ov::snippets::utils::get_full_dim_value(), n_block};
+ const ov::snippets::VectorDims c_sub{m_block, n_block};
+
+ auto build_lir = [&](const std::shared_ptr<LinearIR>& lir) {
+ lir->set_loop_depth(2);
+ auto param0 = lir->push_node<ov::op::v0::Parameter>(input_precision, shape_a);
+ auto param1 = lir->push_node<ov::op::v0::Parameter>(input_precision, shape_b);
+ auto param2 = lir->push_node<ov::op::v0::Parameter>(input_precision, shape_c);
+ auto param3 = lir->push_node<ov::op::v0::Parameter>(input_precision, shape_d);
+ const auto& loop_manager = lir->get_loop_manager();
+
+ // Brgemm1 + Buffer1 + Add inside n-loop
+ auto brgemm1 = lir->push_node<ov::snippets::op::Brgemm>(param0.second, param1.second);
+ init_expr_descriptors(*brgemm1.first, {a_sub, b_sub, c_sub});
+ auto buffer1 = lir->push_node<ov::snippets::op::Buffer>(brgemm1.second);
+ auto add = lir->push_node<ov::op::v1::Add>(buffer1.second, param2.second);
+
+ const auto n_loop = std::make_shared<UnifiedLoopInfo>(
+ n,
+ n_block,
+ std::vector<LoopPort>{LoopPort::create<PortType::NotProcessed>((*brgemm1.first)->get_input_port(0)),
+ LoopPort::create<PortType::Incremented>((*brgemm1.first)->get_input_port(1), 0),
+ LoopPort::create<PortType::Incremented>((*add.first)->get_input_port(1), 0)},
+ std::vector<LoopPort>{LoopPort::create<PortType::Incremented>((*add.first)->get_output_port(0), 0)},
+ std::vector<LoopPortDesc>{not_proc_desc(), inc_desc(n), inc_desc(n)},
+ std::vector<LoopPortDesc>{inc_desc(n)},
+ false);
+ const auto n_id = loop_manager->add_loop_info(n_loop);
+ auto lb_n = lir->insert_node(std::make_shared<LoopBegin>(false),
+ std::vector<PortConnectorPtr>{},
+ {},
+ false,
+ brgemm1.first);
+ auto le_n = insert_loop_end(lir, lb_n, n_id, lir->cend());
+
+ // Buffer2 outside n-loop, at m-loop level
+ auto buffer2 = lir->push_node<ov::snippets::op::Buffer>(add.second);
+
+ // Brgemm2 + n-loop
+ auto brgemm2 = lir->push_node<ov::snippets::op::Brgemm>(buffer2.second, param3.second);
+ init_expr_descriptors(*brgemm2.first, {c_sub, b_sub, c_sub});
+ const auto n2_loop = std::make_shared<UnifiedLoopInfo>(
+ n,
+ n_block,
+ std::vector<LoopPort>{LoopPort::create<PortType::Incremented>((*brgemm2.first)->get_input_port(0), 0),
+ LoopPort::create<PortType::Incremented>((*brgemm2.first)->get_input_port(1), 0)},
+ std::vector<LoopPort>{LoopPort::create<PortType::Incremented>((*brgemm2.first)->get_output_port(0), 0)},
+ std::vector<LoopPortDesc>{inc_desc(n), inc_desc(n)},
+ std::vector<LoopPortDesc>{inc_desc(n)},
+ false);
+ const auto n2_id = loop_manager->add_loop_info(n2_loop);
+ auto lb_n2 = lir->insert_node(std::make_shared<LoopBegin>(false),
+ std::vector<PortConnectorPtr>{},
+ {},
+ false,
+ brgemm2.first);
+ auto le_n2 = insert_loop_end(lir, lb_n2, n2_id, lir->cend());
+
+ auto result = lir->push_node<ov::snippets::op::Result>(brgemm2.second);
+
+ // m-loop
+ const auto m_loop = std::make_shared<UnifiedLoopInfo>(
+ m,
+ m_block,
+ std::vector<LoopPort>{LoopPort::create<PortType::Incremented>((*brgemm1.first)->get_input_port(0), 1),
+ LoopPort::create<PortType::NotProcessed>((*brgemm1.first)->get_input_port(1)),
+ LoopPort::create<PortType::Incremented>((*add.first)->get_input_port(1), 1),
+ LoopPort::create<PortType::NotProcessed>((*brgemm2.first)->get_input_port(1))},
+ std::vector<LoopPort>{LoopPort::create<PortType::Incremented>((*brgemm2.first)->get_output_port(0), 1)},
+ std::vector<LoopPortDesc>{inc_desc(m), not_proc_desc(), inc_desc(m), not_proc_desc()},
+ std::vector<LoopPortDesc>{inc_desc(m)},
+ false);
+ const auto m_id = loop_manager->add_loop_info(m_loop);
+ auto lb_m = lir->insert_node(std::make_shared<LoopBegin>(false),
+ std::vector<PortConnectorPtr>{},
+ {},
+ false,
+ lir->cend());
+ auto le_m = insert_loop_end(lir, lb_m, m_id, lir->cend());
+
+ (*lb_m)->set_loop_ids({m_id});
+ (*lb_n)->set_loop_ids({m_id, n_id});
+ (*brgemm1.first)->set_loop_ids({m_id, n_id});
+ (*buffer1.first)->set_loop_ids({m_id, n_id});
+ (*add.first)->set_loop_ids({m_id, n_id});
+ (*le_n)->set_loop_ids({m_id, n_id});
+ (*buffer2.first)->set_loop_ids({m_id});
+ (*lb_n2)->set_loop_ids({m_id, n2_id});
+ (*brgemm2.first)->set_loop_ids({m_id, n2_id});
+ (*le_n2)->set_loop_ids({m_id, n2_id});
+ (*le_m)->set_loop_ids({m_id});
+
+ return std::make_pair(ov::as_type_ptr<BufferExpression>(*buffer1.first),
+ ov::as_type_ptr<BufferExpression>(*buffer2.first));
+ };
+
+ build_lir(linear_ir);
+ const auto& [buffer1, buffer2] = build_lir(linear_ir_ref);
+ buffer1->set_reg_group(0);
+ buffer2->set_reg_group(1);
+}
+
+/*
+ * Expected Control Flow Graph:
+ * LoopBegin (m)
+ * | LoopBegin (n1)
+ * | | Brgemm1
+ * | | Buffer1
+ * | | Add1
+ * | | Buffer2
+ * | | Add2
+ * | LoopEnd (n1)
+ * | Buffer3
+ * | LoopBegin (n2)
+ * | | Brgemm2
+ * | LoopEnd (n2)
+ * LoopEnd (m)
+ *
+ * Buffer1 and Buffer2 share group 0 inside n1. Buffer3 is outside n1 and uses group 1.
+ */
+TEST_F(SetBufferRegGroupTest, ThreeBuffersInsideAndOutsideLoop) {
+ const size_t m = 64;
+ const size_t n = 128;
+ const size_t k = 512;
+ const size_t m_block = 32;
+ const size_t n_block = 64;
+ const ov::Shape shape_a{1, 1, m, k};
+ const ov::Shape shape_b{1, 1, k, n};
+ const ov::Shape shape_c{1, 1, m, n};
+ const ov::Shape shape_d{1, 1, n, n};
+ const ov::snippets::VectorDims a_sub{m_block, ov::snippets::utils::get_full_dim_value()};
+ const ov::snippets::VectorDims b_sub{ov::snippets::utils::get_full_dim_value(), n_block};
+ const ov::snippets::VectorDims c_sub{m_block, n_block};
+
+ auto build_lir = [&](const std::shared_ptr<LinearIR>& lir) {
+ lir->set_loop_depth(2);
+ auto param0 = lir->push_node<ov::op::v0::Parameter>(input_precision, shape_a);
+ auto param1 = lir->push_node<ov::op::v0::Parameter>(input_precision, shape_b);
+ auto param2 = lir->push_node<ov::op::v0::Parameter>(input_precision, shape_c);
+ auto param3 = lir->push_node<ov::op::v0::Parameter>(input_precision, shape_d);
+ const auto& loop_manager = lir->get_loop_manager();
+
+ // Brgemm1 + Buffer1 + Add1 + Buffer2 + Add2 all inside n-loop
+ auto brgemm1 = lir->push_node<ov::snippets::op::Brgemm>(param0.second, param1.second);
+ init_expr_descriptors(*brgemm1.first, {a_sub, b_sub, c_sub});
+ auto buffer1 = lir->push_node<ov::snippets::op::Buffer>(brgemm1.second);
+ auto add1 = lir->push_node<ov::op::v1::Add>(buffer1.second, param2.second);
+ auto buffer2 = lir->push_node<ov::snippets::op::Buffer>(add1.second);
+ auto add2 = lir->push_node<ov::op::v1::Add>(buffer2.second, param2.second);
+
+ const auto n_loop = std::make_shared<UnifiedLoopInfo>(
+ n,
+ n_block,
+ std::vector<LoopPort>{LoopPort::create<PortType::NotProcessed>((*brgemm1.first)->get_input_port(0)),
+ LoopPort::create<PortType::Incremented>((*brgemm1.first)->get_input_port(1), 0),
+ LoopPort::create<PortType::Incremented>((*add1.first)->get_input_port(1), 0),
+ LoopPort::create<PortType::Incremented>((*add2.first)->get_input_port(1), 0)},
+ std::vector<LoopPort>{LoopPort::create<PortType::Incremented>((*add2.first)->get_output_port(0), 0)},
+ std::vector<LoopPortDesc>{not_proc_desc(), inc_desc(n), inc_desc(n), inc_desc(n)},
+ std::vector<LoopPortDesc>{inc_desc(n)},
+ false);
+ const auto n_id = loop_manager->add_loop_info(n_loop);
+ auto lb_n = lir->insert_node(std::make_shared<LoopBegin>(false),
+ std::vector<PortConnectorPtr>{},
+ {},
+ false,
+ brgemm1.first);
+ auto le_n = insert_loop_end(lir, lb_n, n_id, lir->cend());
+
+ // Buffer3 outside n-loop, at m-loop level
+ auto buffer3 = lir->push_node<ov::snippets::op::Buffer>(add2.second);
+
+ // Brgemm2 + n-loop
+ auto brgemm2 = lir->push_node<ov::snippets::op::Brgemm>(buffer3.second, param3.second);
+ init_expr_descriptors(*brgemm2.first, {c_sub, b_sub, c_sub});
+ const auto n2_loop = std::make_shared<UnifiedLoopInfo>(
+ n,
+ n_block,
+ std::vector<LoopPort>{LoopPort::create<PortType::Incremented>((*brgemm2.first)->get_input_port(0), 0),
+ LoopPort::create<PortType::Incremented>((*brgemm2.first)->get_input_port(1), 0)},
+ std::vector<LoopPort>{LoopPort::create<PortType::Incremented>((*brgemm2.first)->get_output_port(0), 0)},
+ std::vector<LoopPortDesc>{inc_desc(n), inc_desc(n)},
+ std::vector<LoopPortDesc>{inc_desc(n)},
+ false);
+ const auto n2_id = loop_manager->add_loop_info(n2_loop);
+ auto lb_n2 = lir->insert_node(std::make_shared<LoopBegin>(false),
+ std::vector<PortConnectorPtr>{},
+ {},
+ false,
+ brgemm2.first);
+ auto le_n2 = insert_loop_end(lir, lb_n2, n2_id, lir->cend());
+
+ auto result = lir->push_node<ov::snippets::op::Result>(brgemm2.second);
+
+ // m-loop
+ const auto m_loop = std::make_shared<UnifiedLoopInfo>(
+ m,
+ m_block,
+ std::vector<LoopPort>{LoopPort::create<PortType::Incremented>((*brgemm1.first)->get_input_port(0), 1),
+ LoopPort::create<PortType::NotProcessed>((*brgemm1.first)->get_input_port(1)),
+ LoopPort::create<PortType::Incremented>((*add1.first)->get_input_port(1), 1),
+ LoopPort::create<PortType::Incremented>((*add2.first)->get_input_port(1), 1),
+ LoopPort::create<PortType::NotProcessed>((*brgemm2.first)->get_input_port(1))},
+ std::vector<LoopPort>{LoopPort::create<PortType::Incremented>((*brgemm2.first)->get_output_port(0), 1)},
+ std::vector<LoopPortDesc>{inc_desc(m), not_proc_desc(), inc_desc(m), inc_desc(m), not_proc_desc()},
+ std::vector<LoopPortDesc>{inc_desc(m)},
+ false);
+ const auto m_id = loop_manager->add_loop_info(m_loop);
+ auto lb_m = lir->insert_node(std::make_shared<LoopBegin>(false),
+ std::vector<PortConnectorPtr>{},
+ {},
+ false,
+ lir->cend());
+ auto le_m = insert_loop_end(lir, lb_m, m_id, lir->cend());
+
+ (*lb_m)->set_loop_ids({m_id});
+ (*lb_n)->set_loop_ids({m_id, n_id});
+ (*brgemm1.first)->set_loop_ids({m_id, n_id});
+ (*buffer1.first)->set_loop_ids({m_id, n_id});
+ (*add1.first)->set_loop_ids({m_id, n_id});
+ (*buffer2.first)->set_loop_ids({m_id, n_id});
+ (*add2.first)->set_loop_ids({m_id, n_id});
+ (*le_n)->set_loop_ids({m_id, n_id});
+ (*buffer3.first)->set_loop_ids({m_id});
+ (*lb_n2)->set_loop_ids({m_id, n2_id});
+ (*brgemm2.first)->set_loop_ids({m_id, n2_id});
+ (*le_n2)->set_loop_ids({m_id, n2_id});
+ (*le_m)->set_loop_ids({m_id});
+
+ return std::make_tuple(ov::as_type_ptr<BufferExpression>(*buffer1.first),
+ ov::as_type_ptr<BufferExpression>(*buffer2.first),
+ ov::as_type_ptr<BufferExpression>(*buffer3.first));
+ };
+
+ build_lir(linear_ir);
+ const auto& [buffer1, buffer2, buffer3] = build_lir(linear_ir_ref);
+ buffer1->set_reg_group(0);
+ buffer2->set_reg_group(0);
+ buffer3->set_reg_group(1);
+}
+
+} // namespace snippets
+} // namespace test
+} // namespace ov