Skip to content

Commit

Permalink
Moved all the logic in ParallelWAOptimizer class
Browse files Browse the repository at this point in the history
  • Loading branch information
v-Golubev committed Aug 2, 2024
1 parent 5fdf022 commit dbd8ad7
Show file tree
Hide file tree
Showing 4 changed files with 196 additions and 147 deletions.
19 changes: 17 additions & 2 deletions src/common/snippets/include/snippets/runtime_configurator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#pragma once

#include "snippets/lowered/linear_ir.hpp"
#include "snippets/lowered/loop_info.hpp"
#include "snippets/kernel_executor_table.hpp"
#include "snippets/lowered/pass/pass.hpp"

Expand Down Expand Up @@ -96,11 +97,20 @@ class RuntimeConfigurator {
* @param linear_ir LinearIR
*/
virtual void init_tensor_rank(const lowered::LinearIRPtr& linear_ir) const;

struct UnifiedLoopInfoRtParams {
size_t work_amount = 0;
std::vector<int64_t> ptr_increments;
std::vector<int64_t> finalization_offsets;
};
using LoopInfoRuntimeParamsMap = std::unordered_map<lowered::UnifiedLoopInfoPtr, UnifiedLoopInfoRtParams>;
/**
* @brief Update Loop informations in LinearIR: Unified and ExpandedLoopInfo
* @param linear_ir LinearIR
* @param initializated_info_map Reference on a map [LoopInfo->RuntimeParams].
* Can be used to pass in the method loop infos which were already initialized, e.g. by parallel domain optimization
*/
void update_loop_info(const lowered::LinearIRPtr& linear_ir) const;
void update_loop_info(const lowered::LinearIRPtr& linear_ir, LoopInfoRuntimeParamsMap& initializated_info_map) const;
/**
* @brief Update Buffer scratchpad size and offsets if needed
* Note: `update_loop_info` must be called before
Expand All @@ -110,12 +120,17 @@ class RuntimeConfigurator {
/**
* @brief Calculate data offsets of LinearIR and update these values in RuntimeConfig
*/
void update_data_offsets() const;
void update_data_offsets(const std::vector<ov::snippets::VectorDims>& shapes = {},
const std::vector<std::vector<size_t>>& layouts = {}) const;
/**
* @brief Update latest input shapes
*/
void update_latest_shapes();

static void init_data_ptr_shifts(const lowered::UnifiedLoopInfoPtr& unified_loop_info,
std::vector<int64_t>& ptr_increments,
std::vector<int64_t>& finalization_offsets);

std::shared_ptr<RuntimeConfig> m_config = nullptr;

size_t m_io_num = 0;
Expand Down
60 changes: 28 additions & 32 deletions src/common/snippets/src/runtime_configurator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,6 @@
namespace ov {
namespace snippets {

namespace {
void init_data_ptr_shifts(const lowered::UnifiedLoopInfoPtr& unified_loop_info, std::vector<int64_t>& ptr_increments,
std::vector<int64_t>& finalization_offsets) {
const auto count = unified_loop_info->get_input_count() + unified_loop_info->get_output_count();
ptr_increments.resize(count);
finalization_offsets.resize(count);

size_t idx = 0;
unified_loop_info->iterate_through_descs(
[&ptr_increments, &finalization_offsets, &idx](const lowered::UnifiedLoopInfo::LoopPortDesc& desc) {
ptr_increments[idx] = desc.ptr_increment;
finalization_offsets[idx] = desc.finalization_offset;
++idx;
});
}
} // namespace

RuntimeConfigurator::RuntimeConfigurator(std::shared_ptr<RuntimeConfig> c) :
m_config(std::move(c)) {
OPENVINO_ASSERT(m_config, "Runtime config is nullptr!");
Expand Down Expand Up @@ -57,7 +40,8 @@ void RuntimeConfigurator::initialization(const lowered::LinearIRPtr& linear_ir)

void RuntimeConfigurator::update(const lowered::LinearIRPtr& linear_ir) {
if (linear_ir->is_dynamic()) {
update_loop_info(linear_ir);
LoopInfoRuntimeParamsMap initialized_info;
update_loop_info(linear_ir, initialized_info);
update_buffer_scratchpad_size(linear_ir);
}

Expand Down Expand Up @@ -143,15 +127,8 @@ void RuntimeConfigurator::init_buffer_info(const lowered::LinearIRPtr& linear_ir
m_dynamic_buffer_clusters = std::move(dynamic_buffer_clusters);
}

void RuntimeConfigurator::update_loop_info(const lowered::LinearIRPtr& linear_ir) const {
// Initialized UnifiedLoopInfo
struct CurrentUnifiedLoopInfo {
size_t current_work_amount = 0;
std::vector<int64_t> ptr_increments;
std::vector<int64_t> finalization_offsets;
};
std::unordered_map<lowered::UnifiedLoopInfoPtr, CurrentUnifiedLoopInfo> initializated_info_map;

void RuntimeConfigurator::update_loop_info(const lowered::LinearIRPtr& linear_ir,
LoopInfoRuntimeParamsMap& initializated_info_map) const {
const auto& loop_map = linear_ir->get_loop_manager()->get_map();
for (const auto& p : loop_map) {
const auto& expanded_loop_info = ov::as_type_ptr<lowered::ExpandedLoopInfo>(p.second);
Expand All @@ -163,12 +140,12 @@ void RuntimeConfigurator::update_loop_info(const lowered::LinearIRPtr& linear_ir
auto& current_info = initializated_info_map[current_unified_loop_info];
lowered::pass::InitLoops::update_runtime_parameters(current_unified_loop_info);

current_info.current_work_amount = current_unified_loop_info->get_work_amount();
current_info.work_amount = current_unified_loop_info->get_work_amount();
init_data_ptr_shifts(current_unified_loop_info, current_info.ptr_increments, current_info.finalization_offsets);
}

auto& initializated_info = initializated_info_map.at(current_unified_loop_info);
auto& current_work_amount = initializated_info.current_work_amount;
auto& current_work_amount = initializated_info.work_amount;
const auto& ptr_increments = initializated_info.ptr_increments;
const auto& finalization_offsets = initializated_info.finalization_offsets;

Expand Down Expand Up @@ -226,7 +203,10 @@ void RuntimeConfigurator::update_buffer_scratchpad_size(const lowered::LinearIRP
OPENVINO_ASSERT(!utils::is_dynamic_value(m_config->buffer_scratchpad_size), "Buffer scratchpad size must be defined!");
}

void RuntimeConfigurator::update_data_offsets() const {
void RuntimeConfigurator::update_data_offsets(const std::vector<ov::snippets::VectorDims>& shapes,
const std::vector<std::vector<size_t>>& layouts) const {
OPENVINO_ASSERT(shapes.empty() || shapes.size() == m_io_num);
OPENVINO_ASSERT(layouts.empty() || layouts.size() == m_io_num);
for (size_t i = 0; i < m_io_num; ++i) {
// offsets represent distance between consecutive elements of corresponding dimension.
// If a dim size == 1, then the next dim starts immediately and the stride is 0
Expand All @@ -236,11 +216,11 @@ void RuntimeConfigurator::update_data_offsets() const {
// case 2:
// shape: s0, s1, s2 == 1, s3
// offsets: s1*s3, s3, 0, 1
const auto& shape = m_io_descs[i]->get_shape();
const auto& shape = shapes.empty() ? m_io_descs[i]->get_shape() : shapes[i];
if (shape == m_latest_shapes[i])
continue;

const auto& layout = m_io_descs[i]->get_layout();
const auto& layout = layouts.empty() ? m_io_descs[i]->get_layout() : layouts[i];
auto& offsets = m_config->io_data_offsets[i];

offsets.resize(m_config->tensor_rank);
Expand Down Expand Up @@ -276,5 +256,21 @@ void RuntimeConfigurator::update_latest_shapes() {
}
}

void RuntimeConfigurator::init_data_ptr_shifts(const lowered::UnifiedLoopInfoPtr& unified_loop_info,
std::vector<int64_t>& ptr_increments,
std::vector<int64_t>& finalization_offsets) {
const auto count = unified_loop_info->get_input_count() + unified_loop_info->get_output_count();
ptr_increments.resize(count);
finalization_offsets.resize(count);

size_t idx = 0;
unified_loop_info->iterate_through_descs(
[&ptr_increments, &finalization_offsets, &idx](const lowered::UnifiedLoopInfo::LoopPortDesc& desc) {
ptr_increments[idx] = desc.ptr_increment;
finalization_offsets[idx] = desc.finalization_offset;
++idx;
});
}

} // namespace snippets
} // namespace ov
Loading

0 comments on commit dbd8ad7

Please sign in to comment.