From 6af22d24115dfad08e1802cdd464be77bdaba620 Mon Sep 17 00:00:00 2001 From: Vladislav Golubev Date: Fri, 2 Feb 2024 13:17:35 +0100 Subject: [PATCH] ReduceSum/Max::make removed --- .../snippets/include/snippets/op/reduce.hpp | 13 +----- src/common/snippets/src/op/reduce.cpp | 40 ++++++------------- .../src/pass/reduce_to_snippets_reduce.cpp | 5 ++- .../src/pass/softmax_decomposition.cpp | 6 ++- .../src/lowered/pass/buffer_allocation.cpp | 6 ++- .../lowered/buffer_allocation.cpp | 6 ++- 6 files changed, 29 insertions(+), 47 deletions(-) diff --git a/src/common/snippets/include/snippets/op/reduce.hpp b/src/common/snippets/include/snippets/op/reduce.hpp index 547b2ad18af0cc..49cfb3aef53461 100644 --- a/src/common/snippets/include/snippets/op/reduce.hpp +++ b/src/common/snippets/include/snippets/op/reduce.hpp @@ -27,6 +27,7 @@ class ReduceBase : public ov::op::Op { bool visit_attributes(AttributeVisitor& visitor) override; void validate_and_infer_types() override; size_t get_axis() const { return m_axis; } + static void compute_and_set_reduce_subtensors(const std::shared_ptr& reduce); protected: size_t m_axis = 0; @@ -38,12 +39,6 @@ class ReduceSum : public ReduceBase { ReduceSum(const Output& x, size_t axis) : ReduceBase(x, axis) {} ReduceSum() = default; std::shared_ptr clone_with_new_inputs(const OutputVector& new_args) const override; - /** - * @brief Creates ReduceSum operation, computes and sets subtensors to input/output PortDescriptors - * @param x Reduce input - * @param axis Reduce axis - */ - static std::shared_ptr make(const Output& x, size_t axis); }; class ReduceMax : public ReduceBase { @@ -52,12 +47,6 @@ class ReduceMax : public ReduceBase { ReduceMax(const Output& x, size_t axis) : ReduceBase(x, axis) {} ReduceMax() = default; std::shared_ptr clone_with_new_inputs(const OutputVector& new_args) const override; - /** - * @brief Creates ReduceMax operation, computes and sets subtensors to input/output PortDescriptors - * @param x Reduce input - * @param axis Reduce axis - */ - static std::shared_ptr make(const Output& x, size_t axis); }; } // namespace op diff --git a/src/common/snippets/src/op/reduce.cpp b/src/common/snippets/src/op/reduce.cpp index 20b01ad3c281a0..69472dc4ee69d1 100644 --- a/src/common/snippets/src/op/reduce.cpp +++ b/src/common/snippets/src/op/reduce.cpp @@ -10,21 +10,6 @@ namespace ov { namespace snippets { namespace op { -namespace { -void compute_and_set_reduce_subtensors(const std::shared_ptr& reduce) { - OPENVINO_ASSERT(reduce->get_input_partial_shape(0).rank().is_static(), - "Subtensors can be automatically calculated only for reduce with static rank."); - const auto reduce_rank = reduce->get_input_partial_shape(0).size(); - const auto axis = reduce->get_axis(); - - std::vector subtensor(reduce_rank, 1); - for (size_t i = axis; i < reduce_rank; ++i) - subtensor[i] = lowered::PortDescriptor::ServiceDimensions::FULL_DIM; - lowered::PortDescriptorUtils::set_port_descriptor_ptr(reduce->input(0), std::make_shared(reduce->input(0), subtensor)); - lowered::PortDescriptorUtils::set_port_descriptor_ptr(reduce->output(0), std::make_shared(reduce->output(0), subtensor)); -} -} // namespace - ReduceBase::ReduceBase(const Output& x, size_t axis) : Op({x}), m_axis(axis) { constructor_validate_and_infer_types(); } @@ -40,30 +25,31 @@ void ReduceBase::validate_and_infer_types() { set_output_type(0, get_input_element_type(0), result_shape); } +void ReduceBase::compute_and_set_reduce_subtensors(const std::shared_ptr& reduce) { + OPENVINO_ASSERT(reduce->get_input_partial_shape(0).rank().is_static(), + "Subtensors can be automatically calculated only for reduce with static rank."); + const auto reduce_rank = reduce->get_input_partial_shape(0).size(); + const auto axis = reduce->get_axis(); + + std::vector subtensor(reduce_rank, 1); + for (size_t i = axis; i < reduce_rank; ++i) + subtensor[i] = lowered::PortDescriptor::ServiceDimensions::FULL_DIM; + lowered::PortDescriptorUtils::set_port_descriptor_ptr(reduce->input(0), std::make_shared(reduce->input(0), subtensor)); + lowered::PortDescriptorUtils::set_port_descriptor_ptr(reduce->output(0), std::make_shared(reduce->output(0), subtensor)); +} + std::shared_ptr ReduceSum::clone_with_new_inputs(const OutputVector& new_args) const { INTERNAL_OP_SCOPE(ReduceSum); check_new_args_count(this, new_args); return std::make_shared(new_args.at(0), m_axis); } -std::shared_ptr ReduceSum::make(const Output& x, size_t axis) { - const auto reduce = std::make_shared(x, axis); - compute_and_set_reduce_subtensors(reduce); - return reduce; -} - std::shared_ptr ReduceMax::clone_with_new_inputs(const OutputVector& new_args) const { INTERNAL_OP_SCOPE(ReduceMax); check_new_args_count(this, new_args); return std::make_shared(new_args.at(0), m_axis); } -std::shared_ptr ReduceMax::make(const Output& x, size_t axis) { - const auto reduce = std::make_shared(x, axis); - compute_and_set_reduce_subtensors(reduce); - return reduce; -} - } // namespace op } // namespace snippets } // namespace ov diff --git a/src/common/snippets/src/pass/reduce_to_snippets_reduce.cpp b/src/common/snippets/src/pass/reduce_to_snippets_reduce.cpp index 99184424676455..a477f5709594ca 100644 --- a/src/common/snippets/src/pass/reduce_to_snippets_reduce.cpp +++ b/src/common/snippets/src/pass/reduce_to_snippets_reduce.cpp @@ -37,11 +37,12 @@ snippets::pass::ReduceToSnippetsReduce::ReduceToSnippetsReduce() { std::shared_ptr snippets_reduce = nullptr; if (ov::is_type(reduce)) - snippets_reduce = ov::snippets::op::ReduceSum::make(data_input, axis); + snippets_reduce = std::make_shared(data_input, axis); else if (ov::is_type(reduce)) - snippets_reduce = ov::snippets::op::ReduceMax::make(data_input, axis); + snippets_reduce = std::make_shared(data_input, axis); else OPENVINO_THROW("Reduce ", reduce, " can't be converted to snippets opset."); + ov::snippets::op::ReduceBase::compute_and_set_reduce_subtensors(snippets_reduce); ov::replace_node(reduce, snippets_reduce); snippets_reduce->set_friendly_name(reduce->get_friendly_name()); diff --git a/src/common/snippets/src/pass/softmax_decomposition.cpp b/src/common/snippets/src/pass/softmax_decomposition.cpp index de7451d45e3d24..8c7cf8e5609369 100644 --- a/src/common/snippets/src/pass/softmax_decomposition.cpp +++ b/src/common/snippets/src/pass/softmax_decomposition.cpp @@ -42,11 +42,13 @@ SoftmaxDecomposition::SoftmaxDecomposition() { } const auto& softmax_input = softmax->input_value(0); - const auto reduce_max = ov::snippets::op::ReduceMax::make(softmax_input, axis); + const auto reduce_max = std::make_shared(softmax_input, axis); + ov::snippets::op::ReduceBase::compute_and_set_reduce_subtensors(reduce_max); const auto subtract = std::make_shared(softmax_input, reduce_max); const auto exp = std::make_shared(subtract); - const auto reduce_sum = ov::snippets::op::ReduceSum::make(exp, axis); + const auto reduce_sum = std::make_shared(exp, axis); + ov::snippets::op::ReduceBase::compute_and_set_reduce_subtensors(reduce_sum); const auto power = std::make_shared(reduce_sum, -1.f); const auto multiply = std::make_shared(exp, power); 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 d4631c26084947..a6f243181b54e9 100644 --- a/src/common/snippets/tests/src/lowered/pass/buffer_allocation.cpp +++ b/src/common/snippets/tests/src/lowered/pass/buffer_allocation.cpp @@ -142,11 +142,13 @@ std::shared_ptr MHABufferAllocationTest::GetModel() const { const auto relu1 = std::make_shared(matmul0); // Decomposed Softmax - const auto reduce_max = ov::snippets::op::ReduceMax::make(relu1, 3); + const auto reduce_max = std::make_shared(relu1, 3); + ov::snippets::op::ReduceBase::compute_and_set_reduce_subtensors(reduce_max); const auto subtract = std::make_shared(relu1, reduce_max); const auto exp = std::make_shared(subtract); - const auto reduce_sum = ov::snippets::op::ReduceSum::make(exp, 3); + const auto reduce_sum = std::make_shared(exp, 3); + ov::snippets::op::ReduceBase::compute_and_set_reduce_subtensors(reduce_sum); const auto power = std::make_shared(reduce_sum, -1.f); const auto multiply = std::make_shared(exp, power); diff --git a/src/plugins/intel_cpu/tests/unit/snippets_transformations/lowered/buffer_allocation.cpp b/src/plugins/intel_cpu/tests/unit/snippets_transformations/lowered/buffer_allocation.cpp index 746ecfb3fb762b..b66ce1919f6d23 100644 --- a/src/plugins/intel_cpu/tests/unit/snippets_transformations/lowered/buffer_allocation.cpp +++ b/src/plugins/intel_cpu/tests/unit/snippets_transformations/lowered/buffer_allocation.cpp @@ -156,11 +156,13 @@ class MHABF16AMXBufferAllocationTest : public BufferAllocationCPUTest { const auto relu1 = std::make_shared(brgemm_cpu0); // Decomposed Softmax - const auto reduce_max = ov::snippets::op::ReduceMax::make(relu1, 3); + const auto reduce_max = std::make_shared(relu1, 3); + ov::snippets::op::ReduceBase::compute_and_set_reduce_subtensors(reduce_max); const auto subtract = std::make_shared(relu1, reduce_max); const auto exp = std::make_shared(subtract); - const auto reduce_sum = ov::snippets::op::ReduceSum::make(exp, 3); + const auto reduce_sum = std::make_shared(exp, 3); + ov::snippets::op::ReduceBase::compute_and_set_reduce_subtensors(reduce_sum); const auto power = std::make_shared(reduce_sum, -1.f); const auto multiply = std::make_shared(exp, power);