Skip to content

Commit

Permalink
[Snippets] Fix sporadic issue in MHAParallelWAOptimizer (openvinotool…
Browse files Browse the repository at this point in the history
…kit#26660)

### Details:
- *Currently, `loops_to_split` in `MHAParallelWAOptimizer` are stored in
unordered_map, so elements order is not determined. This sporadically
leads to the situation when loop last iteration has work_amount more
than main body's increment. This might lead to failures*
- *In this PR, 'loops_to_split' are stored in vector, so loop
information updates are always called for expanded loop infos in
determined order: FIRST_ITER->MAIN_BODY->LAST_ITER*
- *Also, the corresponding assert is added to
`InsertSpecificIterations::get_decomposed_loop_work_amount` in order to
throw an exception on early stage in case of incorrect configuration.
This assert also allows to cover the changes by the existing tests (some
of them fail if assert is added but the fix is not applied)*

### Tickets:
 - *N/A*
  • Loading branch information
v-Golubev authored Sep 19, 2024
1 parent bdc0110 commit f0c5d2f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/common/snippets/include/snippets/runtime_configurator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,13 +179,13 @@ class RuntimeConfigurator {
static std::unordered_set<size_t> find_unsqueezed_params(
const ov::snippets::lowered::LinearIRCPtr& linear_ir,
const std::unordered_set<snippets::lowered::ExpressionPtr>& brgemms);
static std::unordered_set<ov::snippets::lowered::ExpandedLoopInfoPtr> find_loops_to_split(
static std::vector<ov::snippets::lowered::ExpandedLoopInfoPtr> find_loops_to_split(
const ov::snippets::lowered::LinearIRCPtr& linear_ir,
const std::unordered_set<size_t>& unsqueezed_params);

RuntimeConfigurator* configurator = nullptr;

std::unordered_set<ov::snippets::lowered::ExpandedLoopInfoPtr> loops_to_split{};
std::vector<ov::snippets::lowered::ExpandedLoopInfoPtr> loops_to_split{};
std::unordered_set<size_t> unsqueezed_params{};
std::vector<std::vector<size_t>> optimized_layouts{};
std::vector<size_t> m_dim_idces{};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,12 @@ size_t InsertSpecificIterations::get_decomposed_loop_work_amount(const UnifiedLo
return is_dynamic ? remaining_work_amount : increment;
case (SpecificLoopIterType::MAIN_BODY):
return is_dynamic ? remaining_work_amount : (remaining_work_amount / increment) * increment;
case (SpecificLoopIterType::LAST_ITER):
case (SpecificLoopIterType::LAST_ITER): {
OPENVINO_ASSERT(is_dynamic || remaining_work_amount < unified_loop_info->get_increment(),
"Last iter work amount (", remaining_work_amount,
") must be less than the UnifiedLoopInfo's increment: ", unified_loop_info->get_increment());
return remaining_work_amount;
}
default:
OPENVINO_THROW("Unknown SpecificLoopIterType!");
}
Expand Down
15 changes: 10 additions & 5 deletions src/common/snippets/src/runtime_configurator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -477,21 +477,21 @@ std::unordered_set<size_t> RuntimeConfigurator::MHAParallelWAOptimizer::find_uns
return unsqueezed_params;
}

std::unordered_set<ExpandedLoopInfoPtr> RuntimeConfigurator::MHAParallelWAOptimizer::find_loops_to_split(
std::vector<ExpandedLoopInfoPtr> RuntimeConfigurator::MHAParallelWAOptimizer::find_loops_to_split(
const lowered::LinearIRCPtr& linear_ir,
const std::unordered_set<size_t>& unsqueezed_params) {
const auto loop_manager = linear_ir->get_loop_manager();
std::unordered_set<ExpandedLoopInfoPtr> loops_to_split;
std::set<size_t> loop_idces_to_split;
std::vector<size_t> prev_loop_idces;

auto add_loops_to_split = [&](const ExpressionPtr& expr) {
auto add_loop_idx_to_split = [&](const ExpressionPtr& expr) {
const auto& loop_idces = expr->get_loop_ids();
if (loop_idces != prev_loop_idces) {
prev_loop_idces = loop_idces;
for (const auto& loop_id : loop_idces) {
const auto expanded_loop_info = loop_manager->get_loop_info<ExpandedLoopInfo>(loop_id);
if (expanded_loop_info->get_dim_idx() == m_dim_idx) {
loops_to_split.insert(expanded_loop_info);
loop_idces_to_split.insert(loop_id);
}
}
}
Expand All @@ -505,8 +505,13 @@ std::unordered_set<ExpandedLoopInfoPtr> RuntimeConfigurator::MHAParallelWAOptimi
// Ops after non related params mustn't be traversed
if (unsqueezed_params.count(i++))
continue;
utils::visit_path(param, visited, add_loops_to_split, false);
utils::visit_path(param, visited, add_loop_idx_to_split, false);
}

const auto& loops_map = linear_ir->get_loop_manager()->get_map();
std::vector<ExpandedLoopInfoPtr> loops_to_split;
for (const auto& id : loop_idces_to_split)
loops_to_split.push_back(ov::as_type_ptr<ExpandedLoopInfo>(loops_map.at(id)));
return loops_to_split;
}

Expand Down

0 comments on commit f0c5d2f

Please sign in to comment.