Skip to content

Commit a7e8fe4

Browse files
authored
[Snippets][CPU][TESTS] Refactor BufferAllocation test to reduce code duplication (#31220)
### Tickets: related to 74841
1 parent 99625ee commit a7e8fe4

File tree

4 files changed

+93
-133
lines changed

4 files changed

+93
-133
lines changed

src/common/snippets/tests/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,10 @@ ov_build_target_faster(${TARGET_NAME}
3030
add_library(snippets_test_utils STATIC ${CMAKE_CURRENT_SOURCE_DIR}/include/lowering_utils.hpp
3131
${CMAKE_CURRENT_SOURCE_DIR}/include/lir_test_utils.hpp
3232
${CMAKE_CURRENT_SOURCE_DIR}/include/lir_comparator.hpp
33+
${CMAKE_CURRENT_SOURCE_DIR}/include/lowered/pass/buffer_allocation.hpp
3334
${CMAKE_CURRENT_SOURCE_DIR}/src/lowering_utils.cpp
3435
${CMAKE_CURRENT_SOURCE_DIR}/src/lir_test_utils.cpp
35-
${CMAKE_CURRENT_SOURCE_DIR}/src/lir_comparator.cpp)
36+
${CMAKE_CURRENT_SOURCE_DIR}/src/lir_comparator.cpp
37+
${CMAKE_CURRENT_SOURCE_DIR}/src/lowered/pass/buffer_allocation.cpp)
3638
target_include_directories(snippets_test_utils PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
3739
target_link_libraries(snippets_test_utils PRIVATE common_test_utils ov_snippets_models)

src/common/snippets/tests/include/lowered/pass/buffer_allocation.hpp

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,19 @@
88

99
#include "snippets/op/brgemm.hpp"
1010
#include "snippets/lowered/pass/pass.hpp"
11+
#include "snippets/shape_inference/shape_inference.hpp"
1112

1213
namespace ov {
1314
namespace test {
1415
namespace snippets {
1516

1617
typedef std::tuple<
17-
bool, // Optimized pipeline
18-
bool, // With SplitLoops opt
19-
size_t, // Expected Buffer size in bytes
20-
size_t, // Expected unique Buffer reg group count
21-
size_t // Expected unique Buffer cluster count
18+
std::vector<ov::PartialShape>, // Input shapes
19+
bool, // Optimized pipeline
20+
bool, // With SplitLoops opt
21+
size_t, // Expected Buffer size in bytes
22+
size_t, // Expected unique Buffer reg group count
23+
size_t // Expected unique Buffer cluster count
2224
> BufferAllocationParams;
2325

2426
class BufferAllocationTest : public testing::TestWithParam<BufferAllocationParams> {
@@ -28,16 +30,23 @@ class BufferAllocationTest : public testing::TestWithParam<BufferAllocationParam
2830

2931
protected:
3032
void SetUp() override;
31-
void ApplyTransformations(const std::shared_ptr<ov::snippets::lowered::pass::PassConfig>& pass_config);
33+
virtual void ApplyTransformations(const std::shared_ptr<ov::snippets::lowered::pass::PassConfig>& pass_config);
3234
void Validate();
3335

34-
virtual std::shared_ptr<ov::Model> GetModel() const = 0;
36+
virtual std::shared_ptr<ov::Model> GetModel(const std::vector<ov::PartialShape>& shapes) const = 0;
3537
virtual std::shared_ptr<ov::snippets::lowered::pass::PassConfig> GetPassConfig();
38+
virtual std::shared_ptr<ov::snippets::IShapeInferSnippetsFactory> GetShapeInferFactory() const;
39+
virtual std::vector<ov::snippets::lowered::pass::PassPipeline::PositionedPassLowered> getBackendSpecificPasses();
3640

3741
static void MarkOp(const std::shared_ptr<ov::Node>& node, const std::vector<size_t>& subtensor);
42+
static void MarkOp(const std::shared_ptr<ov::Node>& node,
43+
const std::vector<std::vector<size_t>>& in_subtensors,
44+
const std::vector<std::vector<size_t>>& out_subtensors);
3845

3946
ov::snippets::lowered::LinearIR m_linear_ir;
4047

48+
std::vector<ov::PartialShape> m_shapes;
49+
4150
size_t m_expected_size = 0;
4251
size_t m_expected_reg_group_count = 0;
4352
size_t m_expected_cluster_count = 0;
@@ -51,7 +60,7 @@ class BufferAllocationTest : public testing::TestWithParam<BufferAllocationParam
5160

5261
class EltwiseBufferAllocationTest : public BufferAllocationTest {
5362
protected:
54-
std::shared_ptr<ov::Model> GetModel() const override;
63+
std::shared_ptr<ov::Model> GetModel(const std::vector<ov::PartialShape>& shapes) const override;
5564
};
5665

5766
} // namespace snippets

src/common/snippets/tests/src/lowered/pass/buffer_allocation.cpp

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66

77
#include "openvino/opsets/opset1.hpp"
88
#include "snippets/op/buffer.hpp"
9+
#include "snippets/op/subgraph.hpp"
910
#include "snippets/lowered/linear_ir.hpp"
11+
#include "snippets/pass/positioned_pass.hpp"
1012
#include "snippets/lowered/pass/mark_loops.hpp"
1113
#include "snippets/lowered/pass/init_loops.hpp"
1214
#include "snippets/lowered/pass/insert_load_store.hpp"
@@ -25,12 +27,14 @@ namespace test {
2527
namespace snippets {
2628

2729
std::string BufferAllocationTest::getTestCaseName(testing::TestParamInfo<ov::test::snippets::BufferAllocationParams> obj) {
30+
std::vector<ov::PartialShape> shapes;
2831
bool is_optimized, with_split_loops;
2932
size_t expected_size, expected_reg_group_count, expected_cluster_count;
3033

31-
std::tie(is_optimized, with_split_loops, expected_size, expected_reg_group_count, expected_cluster_count) = obj.param;
34+
std::tie(shapes, is_optimized, with_split_loops, expected_size, expected_reg_group_count, expected_cluster_count) = obj.param;
3235

3336
std::ostringstream result;
37+
result << "Shapes=" << ov::test::utils::partialShape2str(shapes) << "_";
3438
result << "Opt=" << ov::test::utils::bool2str(is_optimized) << "_";
3539
result << "Split=" << ov::test::utils::bool2str(with_split_loops) << "_";
3640
result << "ExpBufferSize=" << expected_size << "_";
@@ -40,13 +44,12 @@ std::string BufferAllocationTest::getTestCaseName(testing::TestParamInfo<ov::tes
4044
}
4145

4246
void BufferAllocationTest::SetUp() {
43-
std::tie(m_is_buffer_optimized, m_with_split_loops, m_expected_size, m_expected_reg_group_count, m_expected_cluster_count) = this->GetParam();
47+
std::tie(m_shapes, m_is_buffer_optimized, m_with_split_loops, m_expected_size,
48+
m_expected_reg_group_count, m_expected_cluster_count) = this->GetParam();
4449

45-
const auto body = GetModel();
46-
m_linear_ir = ov::snippets::lowered::LinearIR(body, std::make_shared<ov::snippets::IShapeInferSnippetsFactory>());
50+
const auto body = GetModel(m_shapes);
51+
m_linear_ir = ov::snippets::lowered::LinearIR(body, GetShapeInferFactory());
4752
m_linear_ir.set_loop_depth(m_loop_depth);
48-
// When Subgraph::control_flow_transformations become public method,
49-
// please use this method instead of ApplyTransformations
5053
ApplyTransformations(GetPassConfig());
5154
}
5255

@@ -57,6 +60,10 @@ std::shared_ptr<ov::snippets::lowered::pass::PassConfig> BufferAllocationTest::G
5760
return config;
5861
}
5962

63+
std::shared_ptr<ov::snippets::IShapeInferSnippetsFactory> BufferAllocationTest::GetShapeInferFactory() const {
64+
return std::make_shared<ov::snippets::IShapeInferSnippetsFactory>();
65+
}
66+
6067
void BufferAllocationTest::MarkOp(const std::shared_ptr<ov::Node>& node, const std::vector<size_t>& subtensor) {
6168
for (const auto& input : node->inputs())
6269
ov::snippets::lowered::PortDescriptorUtils::set_port_descriptor_ptr(
@@ -66,6 +73,23 @@ void BufferAllocationTest::MarkOp(const std::shared_ptr<ov::Node>& node, const s
6673
output, std::make_shared<ov::snippets::lowered::PortDescriptor>(output, subtensor));
6774
}
6875

76+
void BufferAllocationTest::MarkOp(const std::shared_ptr<ov::Node>& node,
77+
const std::vector<std::vector<size_t>>& in_subtensors,
78+
const std::vector<std::vector<size_t>>& out_subtensors) {
79+
OPENVINO_ASSERT(in_subtensors.size() == node->inputs().size(), "Incorrect count of input subtensors");
80+
OPENVINO_ASSERT(out_subtensors.size() == node->outputs().size(), "Incorrect count of output subtensors");
81+
for (size_t i = 0; i < node->inputs().size(); ++i) {
82+
const auto& input = node->input(i);
83+
ov::snippets::lowered::PortDescriptorUtils::set_port_descriptor_ptr(
84+
input, std::make_shared<ov::snippets::lowered::PortDescriptor>(input, in_subtensors[i]));
85+
}
86+
for (size_t i = 0; i < node->outputs().size(); ++i) {
87+
const auto& output = node->output(i);
88+
ov::snippets::lowered::PortDescriptorUtils::set_port_descriptor_ptr(
89+
output, std::make_shared<ov::snippets::lowered::PortDescriptor>(output, out_subtensors[i]));
90+
}
91+
}
92+
6993
void BufferAllocationTest::ApplyTransformations(const std::shared_ptr<ov::snippets::lowered::pass::PassConfig>& pass_config) {
7094
ov::snippets::lowered::pass::PassPipeline pipeline(pass_config);
7195
pipeline.register_pass<ov::snippets::lowered::pass::MarkLoops>(m_vector_size);
@@ -77,8 +101,13 @@ void BufferAllocationTest::ApplyTransformations(const std::shared_ptr<ov::snippe
77101
pipeline.register_pass<ov::snippets::lowered::pass::InitLoops>();
78102
pipeline.register_pass<ov::snippets::lowered::pass::InsertLoops>();
79103
pipeline.register_pass<ov::snippets::lowered::pass::AllocateBuffers>(m_is_buffer_optimized);
104+
pipeline.register_positioned_passes(getBackendSpecificPasses());
80105
pipeline.run(m_linear_ir);
81106
}
107+
std::vector<ov::snippets::lowered::pass::PassPipeline::PositionedPassLowered>
108+
BufferAllocationTest::getBackendSpecificPasses() {
109+
return {};
110+
}
82111

83112
void BufferAllocationTest::Validate() {
84113
std::set<size_t> reg_groups, clusters;
@@ -91,12 +120,13 @@ void BufferAllocationTest::Validate() {
91120
EXPECT_EQ(m_linear_ir.get_static_buffer_scratchpad_size(), m_expected_size);
92121
}
93122

94-
std::shared_ptr<ov::Model> EltwiseBufferAllocationTest::GetModel() const {
123+
std::shared_ptr<ov::Model> EltwiseBufferAllocationTest::GetModel(const std::vector<ov::PartialShape>& shapes) const {
95124
const auto subtensor_eltwise = std::vector<size_t>{1, m_vector_size};
96125
const auto subtensor_buffer = std::vector<size_t>(2, ov::snippets::utils::get_full_dim_value());
97126

98-
const auto parameter0 = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::PartialShape({1, 3, 100, 100}));
99-
const auto parameter1 = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::PartialShape({1, 3, 100, 100}));
127+
const auto parameter0 = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, shapes[0]);
128+
const auto parameter1 = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, shapes[0]);
129+
100130
const auto add = std::make_shared<ov::op::v1::Add>(parameter0, parameter1);
101131
const auto buffer0 = std::make_shared<ov::snippets::op::Buffer>(add);
102132
const auto relu = std::make_shared<ov::op::v0::Relu>(buffer0);
@@ -121,6 +151,7 @@ namespace BufferAllocationTest_Instances {
121151

122152
INSTANTIATE_TEST_SUITE_P(smoke_Snippets_BufferAllocation_EltwiseNotOptimized, EltwiseBufferAllocationTest,
123153
::testing::Combine(
154+
::testing::Values(std::vector<ov::PartialShape>{{1, 3, 100, 100}}),
124155
::testing::Values(false),
125156
::testing::Values(false), // in this test it doesn't make sense
126157
::testing::Values(80000), // Each Buffer has own allocated memory
@@ -130,6 +161,7 @@ INSTANTIATE_TEST_SUITE_P(smoke_Snippets_BufferAllocation_EltwiseNotOptimized, El
130161

131162
INSTANTIATE_TEST_SUITE_P(smoke_Snippets_BufferAllocation_EltwiseOptimized, EltwiseBufferAllocationTest,
132163
::testing::Combine(
164+
::testing::Values(std::vector<ov::PartialShape>{{1, 3, 100, 100}}),
133165
::testing::Values(true),
134166
::testing::Values(false), // in this test it doesn't make sense
135167
::testing::Values(40000), // Two Buffer reuse memory

0 commit comments

Comments
 (0)