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] PropagateSubtensors pass removed (#32316)
Vladislav Golubev4ed60823b242
diff --git a/src/common/snippets/include/snippets/lowered/pass/brgemm_blocking.hpp b/src/common/snippets/include/snippets/lowered/pass/brgemm_blocking.hppindex 29b68c11815..44e318b6be9 100644--- a/src/common/snippets/include/snippets/lowered/pass/brgemm_blocking.hpp+++ b/src/common/snippets/include/snippets/lowered/pass/brgemm_blocking.hpp@@ -32,8 +32,6 @@ namespace ov::snippets::lowered::pass {class BrgemmBlockingBase {public:virtual ~BrgemmBlockingBase() = default;- static snippets::lowered::SpecificIterationHandlers get_default_blocking_loop_handlers(size_t work_amount,- size_t block_size);protected:/**diff --git a/src/common/snippets/include/snippets/lowered/pass/propagate_subtensors.hpp b/src/common/snippets/include/snippets/lowered/pass/propagate_subtensors.hppdeleted file mode 100644index 4a5640d3dbc..00000000000--- a/src/common/snippets/include/snippets/lowered/pass/propagate_subtensors.hpp+++ /dev/null@@ -1,34 +0,0 @@-// Copyright (C) 2023 Intel Corporation-// SPDX-License-Identifier: Apache-2.0-//--#pragma once--#include <cstddef>-#include <memory>--#include "openvino/core/rtti.hpp"-#include "snippets/lowered/linear_ir.hpp"-#include "snippets/lowered/pass/pass.hpp"--namespace ov::snippets::lowered::pass {-/**- * @interface UpdateSubtensors- * @brief The pass updates subtensors of all operations in Loop based on tail size.- * Firstly, the pass updates subtensors of all Loop input ports.- * After that, shape inference infrastructure is used to update subtensors of all ops in Loop body- * @param m_offset - offset which must be set- * @ingroup snippets- */-class UpdateSubtensors : public pass::RangedPass {-public:- explicit UpdateSubtensors(size_t tail_size);- OPENVINO_RTTI("UpdateSubtensors", "", RangedPass);- bool run(LinearIR& linear_ir, LinearIR::constExprIt begin, LinearIR::constExprIt end) override;- std::shared_ptr<pass::PassBase> merge(const std::shared_ptr<pass::PassBase>& other) override;--private:- size_t m_tail_size;-};--} // namespace ov::snippets::lowered::passdiff --git a/src/common/snippets/src/lowered/pass/brgemm_blocking.cpp b/src/common/snippets/src/lowered/pass/brgemm_blocking.cppindex a08c53085ff..a6bc4ea0d5d 100644--- a/src/common/snippets/src/lowered/pass/brgemm_blocking.cpp+++ b/src/common/snippets/src/lowered/pass/brgemm_blocking.cpp@@ -17,27 +17,13 @@#include "snippets/lowered/loop_info.hpp"#include "snippets/lowered/loop_manager.hpp"#include "snippets/lowered/loop_port.hpp"-#include "snippets/lowered/pass/propagate_subtensors.hpp"#include "snippets/lowered/specific_loop_iter_handlers.hpp"-#include "snippets/lowered/specific_loop_iter_types.hpp"#include "snippets/utils/utils.hpp"namespace ov::snippets::lowered::pass {using namespace ov::snippets::utils;using PortType = LoopPort::Type;-lowered::SpecificIterationHandlers BrgemmBlockingBase::get_default_blocking_loop_handlers(size_t work_amount,- size_t block_size) {- OPENVINO_ASSERT(block_size != 0, "block size must be non zero");- SpecificIterationHandlers handlers;- const auto tail_size =- utils::is_dynamic_value(work_amount) ? utils::get_dynamic_value<size_t>() : work_amount % block_size;- if (tail_size != 0) {- handlers.register_pass<lowered::SpecificLoopIterType::LAST_ITER, lowered::pass::UpdateSubtensors>(tail_size);- }- return handlers;-}-bool BrgemmBlockingBase::blocking_loop_exists(const lowered::LoopManagerPtr& loop_manager,const ExpressionPtr& brgemm_expr) {auto check_port = [&](const LoopPort& p) {@@ -87,14 +73,17 @@ void BrgemmBlockingBase::mark_k_blocking(const LoopManagerPtr& loop_manager,loop_manager->get_loop_info<UnifiedLoopInfo>(id)->set_handlers(get_k_loop_handlers(k, block_size_k));}-SpecificIterationHandlers BrgemmBlockingBase::get_m_loop_handlers(size_t work_amount, size_t block_size) const {- return get_default_blocking_loop_handlers(work_amount, block_size);+SpecificIterationHandlers BrgemmBlockingBase::get_m_loop_handlers([[maybe_unused]] size_t work_amount,+ [[maybe_unused]] size_t block_size) const {+ return {};}-SpecificIterationHandlers BrgemmBlockingBase::get_n_loop_handlers(size_t work_amount, size_t block_size) const {- return get_default_blocking_loop_handlers(work_amount, block_size);+SpecificIterationHandlers BrgemmBlockingBase::get_n_loop_handlers([[maybe_unused]] size_t work_amount,+ [[maybe_unused]] size_t block_size) const {+ return {};}-SpecificIterationHandlers BrgemmBlockingBase::get_k_loop_handlers(size_t work_amount, size_t block_size) const {- return get_default_blocking_loop_handlers(work_amount, block_size);+SpecificIterationHandlers BrgemmBlockingBase::get_k_loop_handlers([[maybe_unused]] size_t work_amount,+ [[maybe_unused]] size_t block_size) const {+ return {};}std::tuple<size_t, size_t, size_t> BrgemmBlockingBase::get_blocking_params(const ExpressionPtr& brgemm_expr) const {diff --git a/src/common/snippets/src/lowered/pass/propagate_subtensors.cpp b/src/common/snippets/src/lowered/pass/propagate_subtensors.cppdeleted file mode 100644index 0824762c04a..00000000000--- a/src/common/snippets/src/lowered/pass/propagate_subtensors.cpp+++ /dev/null@@ -1,221 +0,0 @@-// Copyright (C) 2023 Intel Corporation-// SPDX-License-Identifier: Apache-2.0-//--#include "snippets/lowered/pass/propagate_subtensors.hpp"--#include <algorithm>-#include <cstddef>-#include <cstdint>-#include <iterator>-#include <map>-#include <memory>-#include <vector>--#include "openvino/core/except.hpp"-#include "openvino/core/type.hpp"-#include "snippets/itt.hpp"-#include "snippets/lowered/linear_ir.hpp"-#include "snippets/lowered/loop_info.hpp"-#include "snippets/lowered/loop_manager.hpp"-#include "snippets/lowered/pass/pass.hpp"-#include "snippets/lowered/port_descriptor.hpp"-#include "snippets/op/broadcastload.hpp"-#include "snippets/op/broadcastmove.hpp"-#include "snippets/op/loop.hpp"-#include "snippets/shape_types.hpp"-#include "snippets/utils/utils.hpp"--namespace ov::snippets::lowered::pass {-namespace {--// The algorithm uses the following special values in subtensors/shapes:-// 1. Dynamic value in subtensor/shape : SIZE_MAX-// 2. Full dimension in subtensor : SIZE_MAX - 1-// 3. Default value of `new_dim_value` : SIZE_MAX - 2-// 4. `Forced` special dynamic value : SIZE_MAX - 3-//-// We have to introduce `FORCED_DYNAMIC_VALUE` to distinguish `new_dim_value = DYNAMIC`-// from the real dynamic values in subtensors and shapes and force this value in subtensors.-// For example, there is Brgemm with the following info in the tail Loop:-// Input 0: shape [?, ?], existing subtensor [32, FULL_DIM]-// Input 1: shape [?, ?], existing subtensor [FULL_DIM, FULL_DIM]-// Output : shape [?, ?], existing subtensor [32, FULL_DIM]-// If the user wants to force `?` in the place of `32` in subtensors, the steps will be:-// 1. Set `?` to subtensor and shape of Input 0 :-// shape [?, ?] (shape has not been changed!), new subtensor [?, FULL_DIM]-// 2. Make shape inference of Brgemm and get Output:-// shape [?, ?] (shape has not been changed!), existing subtensor [FULL_DIM, FULL_DIM]-// 3. Update subtensor on output using shape:-// new_subtensor[i] = std::min(planar_shape[i], subtensor[i]); // i = 0: std::min(SIZE_MAX(?), 32)-// new subtensor [32, FULL_DIM] - has not been changed! But should be [?, FULL_DIM]-// Conclusion: we have to distinguish forced dynamic value with existing dynamic values in shape and subtensor--constexpr size_t NEW_DEFAULT_VALUE = SIZE_MAX - 2;-constexpr size_t FORCED_DYNAMIC_VALUE = SIZE_MAX - 3;--void propagate_updated_subtensor_through_loop(const LinearIR& linear_ir,- const LoopInfoPtr& loop_info,- LinearIR::container::const_iterator begin,- LinearIR::container::const_iterator end,- bool most_outer_loop,- size_t new_dim_value = NEW_DEFAULT_VALUE) {- // Marks the forced dynamic value- new_dim_value = utils::is_dynamic_value(new_dim_value) ? FORCED_DYNAMIC_VALUE : new_dim_value;- OPENVINO_ASSERT(snippets::utils::implication(most_outer_loop, new_dim_value != NEW_DEFAULT_VALUE),- "if the updated subtensor propagation was called for the outer loop, new_dim_value must not be "- "equal to default value");-- std::map<lowered::PortDescriptorPtr, snippets::VectorDims> original_shapes;- // First step: set new dim value to the corresponding input_ports' dimensions- if (most_outer_loop) {- for (const auto& port : loop_info->get_input_ports()) {- if (port.is_processed()) {- const auto& expr = port.get_expr_port()->get_expr();- const auto& desc = port.get_expr_port()->get_descriptor_ptr();- if (port.get_dim_idx() < desc->get_subtensor().size()) {- desc->set_subtensor_dim(port.get_dim_idx(), new_dim_value);- }-- const auto parent_desc = expr->get_input_port_connector(port.get_expr_port()->get_index())- ->get_source()- .get_descriptor_ptr();- const auto& parent_shape = parent_desc->get_shape();- if (original_shapes.find(parent_desc) == original_shapes.end()) {- original_shapes[parent_desc] = parent_shape;- }- auto new_shape = parent_shape;- new_shape[*(desc->get_layout().rbegin() + port.get_dim_idx())] = new_dim_value;- parent_desc->set_shape(new_shape);- }- }- }-- auto update_only_dim_idx_with_subtensor_value = [&](const LoopPort& port) {- if (port.is_processed()) {- const auto desc = port.get_expr_port()->get_descriptor_ptr();- const auto expr = port.get_expr_port()->get_expr();- const auto parent_desc =- expr->get_input_port_connector(port.get_expr_port()->get_index())->get_source().get_descriptor_ptr();-- const auto& parent_shape = parent_desc->get_shape();- const auto& desc_subtensor = desc->get_subtensor();- if (port.get_dim_idx() < desc_subtensor.size()) {- if (original_shapes.find(parent_desc) == original_shapes.end()) {- original_shapes[parent_desc] = parent_shape;- }- auto new_shape = parent_shape;- new_shape[*(desc->get_layout().rbegin() + port.get_dim_idx())] =- *(desc_subtensor.rbegin() + port.get_dim_idx());- parent_desc->set_shape(new_shape);- }- }- };-- auto update_subtensors = [](const std::vector<PortDescriptorPtr>& descs, bool is_input) {- for (const auto& desc : descs) {- const auto& subtensor = desc->get_subtensor();- if (!subtensor.empty()) {- auto planar_dims = is_input- ? snippets::utils::get_planar_vdims(desc->get_shape(), desc->get_layout())- : snippets::utils::get_preordered_vdims(desc->get_shape(), desc->get_layout());- const size_t subtensor_start = planar_dims.size() - subtensor.size();- VectorDims new_subtensor(planar_dims.begin() + subtensor_start, planar_dims.end());- for (size_t i = 0; i < new_subtensor.size(); ++i) {- // If user forces dynamic value to set in subtensor, set real dynamic dimension using- // `get_dynamic_value<size_t>()`- new_subtensor[i] = [&]() -> size_t {- if (new_subtensor[i] == FORCED_DYNAMIC_VALUE) {- return utils::get_dynamic_value<size_t>();- }- if (utils::is_full_dim_value(subtensor[i])) {- return subtensor[i];- }- return std::min(new_subtensor[i], subtensor[i]);- }();- }- desc->set_subtensor(new_subtensor);- }- }- };-- auto shape_inference_end_it = end;- const bool loop_by_last_dim = loop_info->get_dim_idx() == 0;- // Subtensors are updated using shape inference infrastructure:- // For inner loops propagation function is called recursively- for (auto expr_it = begin; expr_it != end; expr_it++) {- const auto expr = *expr_it;- if (ov::is_type<snippets::op::LoopEnd>(expr->get_node())) {- continue;- }- if (auto loop_begin = ov::as_type_ptr<snippets::op::LoopBegin>(expr->get_node())) {- const auto loop_end = loop_begin->get_loop_end();- const auto inner_loop_info = linear_ir.get_loop_manager()->get_loop_info(loop_end->get_id());- const auto inner_begin = std::next(expr_it);- const auto inner_end = linear_ir.find_after(inner_begin, linear_ir.get_expr_by_node(loop_end));-- // The corresponding shapes of inner loops input ports must be updated using existing subtensor values- if (!most_outer_loop) {- for (const auto& port : loop_info->get_input_ports()) {- update_only_dim_idx_with_subtensor_value(port);- }- }- propagate_updated_subtensor_through_loop(linear_ir, inner_loop_info, inner_begin, inner_end, false);- expr_it = inner_end;- continue;- }- if ((ov::is_type_any_of<snippets::op::BroadcastMove, snippets::op::BroadcastLoad>(- expr_it->get()->get_node())) &&- loop_by_last_dim) {- // WA: we have to break subtensor propagation if we try to propagate new last dim through Broadcast nodes- // which broadcast last dim in original dimension value anyway- // This workaround might be avoided if blocked shape are used for tail size propagation- shape_inference_end_it = expr_it;- break;- }- expr->updateShapes();- update_subtensors(expr->get_input_port_descriptors(), true);- update_subtensors(expr->get_output_port_descriptors(), false);- }-- // After subtensor propagation, the original shapes must be restored- for (const auto& elem : original_shapes) {- elem.first->set_shape(elem.second);- }- for (auto expr_it = begin; expr_it != shape_inference_end_it; expr_it++) {- const auto& expr = *expr_it;- if (ov::is_type<snippets::op::LoopBase>(expr->get_node())) {- continue;- }- expr->updateShapes();- }-}-} // namespace--UpdateSubtensors::UpdateSubtensors(size_t tail_size) : RangedPass(), m_tail_size(tail_size) {}--bool UpdateSubtensors::run(LinearIR& linear_ir, LinearIR::constExprIt begin, LinearIR::constExprIt end) {- const auto& last_expr = *end;- const auto last_node = last_expr->get_node();- const auto loop_end = ov::as_type_ptr<op::LoopEnd>(last_node);- OPENVINO_ASSERT(loop_end, "the last operation in range must be LoopEnd");-- const auto& loop_manager = linear_ir.get_loop_manager();- const auto& loop_info = loop_manager->get_loop_info(loop_end->get_id());- propagate_updated_subtensor_through_loop(linear_ir, loop_info, begin, end, true, m_tail_size);- return true;-}--std::shared_ptr<pass::PassBase> UpdateSubtensors::merge(const std::shared_ptr<pass::PassBase>& other) {- if (!other) {- return shared_from_this();- }- const auto casted_pass = ov::as_type_ptr<UpdateSubtensors>(other);- size_t merged_size = 0;- if (!casted_pass || !ov::snippets::utils::merge_dynamic_dim(merged_size, m_tail_size, casted_pass->m_tail_size)) {- return nullptr;- }- return std::make_shared<UpdateSubtensors>(merged_size);-}--} // namespace ov::snippets::lowered::passdiff --git a/src/common/snippets/src/lowered/specific_loop_iter_handlers.cpp b/src/common/snippets/src/lowered/specific_loop_iter_handlers.cppindex da859123fa0..1b69144c597 100644--- a/src/common/snippets/src/lowered/specific_loop_iter_handlers.cpp+++ b/src/common/snippets/src/lowered/specific_loop_iter_handlers.cpp@@ -8,7 +8,6 @@#include <utility>#include "snippets/lowered/pass/iter_handler.hpp"-#include "snippets/lowered/pass/propagate_subtensors.hpp"#include "snippets/utils/utils.hpp"namespace ov::snippets::lowered {@@ -28,7 +27,6 @@ SpecificIterationHandlers::SpecificIterationHandlers(size_t loop_work_amount,}if (last_iter_increment != 0) {m_last_iter_handlers.register_pass<lowered::pass::UpdateMemoryAccessCounts>(last_iter_increment);- m_last_iter_handlers.register_pass<lowered::pass::UpdateSubtensors>(last_iter_increment);// Last Iterations of Loop processed last dimensions with Eltwise nodes inside should have increment = 1if (last_iter_increment == 1) {m_last_iter_handlers.register_pass<lowered::pass::SetLoopIncrementOne>();diff --git a/src/plugins/intel_cpu/tests/unit/snippets_transformations/aarch64/lowered/gemm_blocking.cpp b/src/plugins/intel_cpu/tests/unit/snippets_transformations/aarch64/lowered/gemm_blocking.cppindex e540bc79b0f..516f2fe97eb 100644--- a/src/plugins/intel_cpu/tests/unit/snippets_transformations/aarch64/lowered/gemm_blocking.cpp+++ b/src/plugins/intel_cpu/tests/unit/snippets_transformations/aarch64/lowered/gemm_blocking.cpp@@ -36,8 +36,7 @@ void create_gemm_loop_infos(const LinearIRPtr& linear_ir,std::vector<LoopPort>{LoopPort::create<PortType::NotProcessed>(gemm_expr->get_input_port(0)),LoopPort::create<PortType::Incremented>(gemm_expr->get_input_port(1), 0)},std::vector<LoopPort>{LoopPort::create<PortType::Incremented>(gemm_expr->get_output_port(0), 0)},- false,- BrgemmBlockingBase::get_default_blocking_loop_handlers(n, n_blk)));+ false));}if (m_block) {std::vector<LoopPort> entries{LoopPort::create<PortType::Incremented>(gemm_expr->get_input_port(0), 1),@@ -46,8 +45,7 @@ void create_gemm_loop_infos(const LinearIRPtr& linear_ir,std::make_shared<ov::snippets::lowered::UnifiedLoopInfo>(m, m_blk,entries,std::vector<LoopPort>{LoopPort::create<PortType::Incremented>(gemm_expr->get_output_port(0), 1)},- false,- BrgemmBlockingBase::get_default_blocking_loop_handlers(m, m_blk)));+ false));}}diff --git a/src/plugins/intel_cpu/tests/unit/snippets_transformations/x64/lowered/brgemm_blocking.cpp b/src/plugins/intel_cpu/tests/unit/snippets_transformations/x64/lowered/brgemm_blocking.cppindex 6be688c97d5..df9af21196a 100644--- a/src/plugins/intel_cpu/tests/unit/snippets_transformations/x64/lowered/brgemm_blocking.cpp+++ b/src/plugins/intel_cpu/tests/unit/snippets_transformations/x64/lowered/brgemm_blocking.cpp@@ -31,7 +31,7 @@ using PortDescriptor = ov::snippets::modifier::MemoryAccess::PortDescriptor;namespace {enum class BACKEND_TYPE{CPU, TPP};SpecificIterationHandlers get_k_loop_handlers(size_t work_amount, size_t block_size, BACKEND_TYPE backend = BACKEND_TYPE::CPU) {- auto handlers = BrgemmBlockingBase::get_default_blocking_loop_handlers(work_amount, block_size);+ SpecificIterationHandlers handlers;switch (backend) {#ifdef SNIPPETS_LIBXSMM_TPPcase BACKEND_TYPE::TPP:@@ -72,8 +72,7 @@ void create_brgemm_loop_infos(const LinearIRPtr& linear_ir,std::vector<LoopPort>{LoopPort::create<PortType::NotProcessed>(brgemm_expr->get_input_port(0)),LoopPort::create<PortType::Incremented>(brgemm_expr->get_input_port(1), 0)},std::vector<LoopPort>{LoopPort::create<PortType::Incremented>(brgemm_expr->get_output_port(0), 0)},- false,- BrgemmBlockingBase::get_default_blocking_loop_handlers(n, n_block)));+ false));}if (m_block) {std::vector<LoopPort> entries{LoopPort::create<PortType::Incremented>(brgemm_expr->get_input_port(0), 1)};@@ -83,8 +82,7 @@ void create_brgemm_loop_infos(const LinearIRPtr& linear_ir,std::make_shared<ov::snippets::lowered::UnifiedLoopInfo>(m, m_blk,entries,std::vector<LoopPort>{LoopPort::create<PortType::Incremented>(brgemm_expr->get_output_port(0), 1)},- false,- BrgemmBlockingBase::get_default_blocking_loop_handlers(m, m_block)));+ false));}}@@ -461,9 +459,8 @@ TEST_F(BrgemmCPUBlockingTest, AMX) {std::vector<LoopPort> entries {LoopPort::create<PortType::Incremented>(brgemm_expr->get_input_port(0), 1),LoopPort::create<PortType::NotProcessed>(brgemm_expr->get_input_port(1))};std::vector<LoopPort> exits {LoopPort::create<PortType::Incremented>(brgemm_expr->get_output_port(0), 1)};- auto handlers = BrgemmBlockingBase::get_default_blocking_loop_handlers(m, m_blk);- linear_ir_ref->get_loop_manager()->- add_loop_info(std::make_shared<ov::snippets::lowered::UnifiedLoopInfo>(m, m_blk, entries, exits, false, handlers));+ linear_ir_ref->get_loop_manager()->add_loop_info(+ std::make_shared<ov::snippets::lowered::UnifiedLoopInfo>(m, m_blk, entries, exits, false));brgemm_expr->set_loop_ids({0});scratch.first->get()->set_loop_ids({0});auto result = linear_ir_ref->push_node<ov::opset10::Result>(brgemm.second);@@ -501,9 +498,8 @@ TEST_F(BrgemmCPUBlockingTest, AMX_FC) {std::vector<LoopPort> entries {LoopPort::create<PortType::Incremented>(brgemm_expr->get_input_port(0), 1),LoopPort::create<PortType::NotProcessed>(brgemm_expr->get_input_port(1))};std::vector<LoopPort> exits {LoopPort::create<PortType::Incremented>(brgemm_expr->get_output_port(0), 1)};- auto handlers = BrgemmBlockingBase::get_default_blocking_loop_handlers(m, m_blk);- linear_ir_ref->get_loop_manager()->- add_loop_info(std::make_shared<ov::snippets::lowered::UnifiedLoopInfo>(m, m_blk, entries, exits, false, handlers));+ linear_ir_ref->get_loop_manager()->add_loop_info(+ std::make_shared<ov::snippets::lowered::UnifiedLoopInfo>(m, m_blk, entries, exits, false));brgemm_expr->set_loop_ids({0});scratch.first->get()->set_loop_ids({0});auto result = linear_ir_ref->push_node<ov::opset10::Result>(brgemm.second);