Skip to content

Commit

Permalink
Extract to the separate pass
Browse files Browse the repository at this point in the history
  • Loading branch information
aobolensk committed Jan 14, 2025
1 parent 3ebeb0e commit f2ab0f0
Show file tree
Hide file tree
Showing 4 changed files with 175 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,71 +114,9 @@ class BrgemmBlocking : public snippets::lowered::pass::RangedPass, public Brgemm
size_t m_block, n_block, k_block;
std::tie(m_block, n_block, k_block) = get_blocking_params(brgemm_expr);
modified = mark_blocking_loops(linear_ir, expr_it, m_block, n_block, k_block);
#ifdef SNIPPETS_DEBUG_CAPS
dump_params_to_csv(brgemm_expr, linear_ir);
#endif // SNIPPETS_DEBUG_CAPS
}
return modified;
}

#ifdef SNIPPETS_DEBUG_CAPS
void dump_params_to_csv(const ov::snippets::lowered::ExpressionPtr& brgemm_expr,
const snippets::lowered::LinearIR& linear_ir) {
auto debug_config = linear_ir.get_config().debug_config;
const auto brgemm = ov::as_type_ptr<BRGEMM_TYPE>(brgemm_expr->get_node());
OPENVINO_ASSERT(brgemm, "Brgemm is nullptr!");
auto csv_path = linear_ir.get_config().debug_config.dumpParams.csv_path;
std::stringstream ss;
ss << brgemm_expr->get_node()->get_friendly_name() << ",";
for (size_t i = 0; i < brgemm->get_input_size(); ++i) {
ss << brgemm->get_input_element_type(i);
if (i != brgemm->get_input_size() - 1) {
ss << ";";
}
}
ss << ",";
for (size_t i = 0; i < brgemm->get_output_size(); ++i) {
ss << brgemm->get_output_element_type(i);
if (i != brgemm->get_output_size() - 1) {
ss << ";";
}
}
ss << ",";
for (const auto& input : brgemm->inputs()) {
const auto& shape = input.get_partial_shape();
for (const auto& dim : shape) {
ss << dim << " ";
}
ss.seekp(-1, ss.cur);
ss << ";";
}
ss.seekp(-1, ss.cur);
ss << ",";
for (const auto& output : brgemm->outputs()) {
const auto& shape = output.get_partial_shape();
for (const auto& dim : shape) {
ss << dim << " ";
}
ss.seekp(-1, ss.cur);
ss << ";";
}
ss.seekp(-1, ss.cur);
ss << ",";
int64_t M, N, K;
std::tie(M, N, K) = get_brgemm_dimensions(brgemm_expr);
ss << M << "," << N << "," << K << ",";
int64_t m_block = brgemm_expr->get_input_port_descriptor(0)->get_subtensor().front();
int64_t n_block = brgemm_expr->get_input_port_descriptor(1)->get_subtensor().back();
int64_t k_block = brgemm_expr->get_input_port_descriptor(0)->get_subtensor().back();
ss << m_block << "," << n_block << "," << k_block;
ss << '\n';
std::ofstream csv_file(csv_path, std::ios_base::app);
if (csv_file.seekp(0, std::ios_base::end).tellp() == 0) {
csv_file << "name,in_type,out_type,in_shapes,out_shapes,M,N,K,m_block,n_block,k_block\n";
}
csv_file << ss.str();
}
#endif // SNIPPETS_DEBUG_CAPS
};
} // namespace pass
} // namespace lowered
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
// Copyright (C) 2025 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//

#pragma once

#include <fstream>

#include "snippets/itt.hpp"
#include "snippets/lowered/loop_manager.hpp"
#include "snippets/lowered/specific_loop_iter_handlers.hpp"
#include "snippets/lowered/pass/iter_handler.hpp"
#include "snippets/op/brgemm.hpp"

namespace ov {
namespace snippets {
namespace lowered {
namespace pass {

/**
* @interface BrgemmDebugParamsBase
* @brief Base class for Brgemm debug parameters dump pass
* @ingroup snippets
*/
class BrgemmDebugParamsBase {
public:
static snippets::lowered::SpecificIterationHandlers get_default_blocking_loop_handlers(size_t work_amount, size_t block_size);

protected:
/**
* @interface get_brgemm_dimensions
* @brief Extract current dimensions M,N,K of `brgemm_expr`
* @param brgemm_expr Brgemm expression
* @return tuple in format (M, N, K)
*/
static std::tuple<size_t, size_t, size_t> get_brgemm_dimensions(const ov::snippets::lowered::ExpressionPtr& brgemm_expr);
};

/**
* @interface BrgemmDebugParams
* @brief Base class for brgemm blocking passes
* @ingroup snippets
*/
template <typename BRGEMM_TYPE,
typename std::enable_if<std::is_base_of<ov::snippets::op::Brgemm, BRGEMM_TYPE>::value, bool>::type = true>
class BrgemmDebugParams : public snippets::lowered::pass::RangedPass, public BrgemmDebugParamsBase {
public:
OPENVINO_RTTI("BrgemmDebugParams", "", RangedPass);

bool run(snippets::lowered::LinearIR& linear_ir,
snippets::lowered::LinearIR::constExprIt begin,
snippets::lowered::LinearIR::constExprIt end) override final { // NOLINT
OV_ITT_SCOPED_TASK(ov::pass::itt::domains::SnippetsTransform, "Snippets::BrgemmDebugParams")
#ifdef SNIPPETS_DEBUG_CAPS
for (auto expr_it = begin; expr_it != end; expr_it++) {
const auto& brgemm_expr = *expr_it;
const auto brgemm = ov::as_type_ptr<BRGEMM_TYPE>(brgemm_expr->get_node());
if (brgemm) {
dump_params_to_csv(brgemm_expr, linear_ir);
}
}
#endif // SNIPPETS_DEBUG_CAPS
return false;
}

#ifdef SNIPPETS_DEBUG_CAPS
void dump_params_to_csv(const ov::snippets::lowered::ExpressionPtr& brgemm_expr,
const snippets::lowered::LinearIR& linear_ir) {
auto debug_config = linear_ir.get_config().debug_config;
const auto brgemm = ov::as_type_ptr<BRGEMM_TYPE>(brgemm_expr->get_node());
OPENVINO_ASSERT(brgemm, "Brgemm is nullptr!");
auto csv_path = linear_ir.get_config().debug_config.dumpParams.csv_path;
std::stringstream ss;
ss << brgemm_expr->get_node()->get_friendly_name() << ",";
for (size_t i = 0; i < brgemm->get_input_size(); ++i) {
ss << brgemm->get_input_element_type(i);
if (i != brgemm->get_input_size() - 1) {
ss << ";";
}
}
ss << ",";
for (size_t i = 0; i < brgemm->get_output_size(); ++i) {
ss << brgemm->get_output_element_type(i);
if (i != brgemm->get_output_size() - 1) {
ss << ";";
}
}
ss << ",";
for (const auto& input : brgemm->inputs()) {
const auto& shape = input.get_partial_shape();
for (const auto& dim : shape) {
ss << dim << " ";
}
ss.seekp(-1, ss.cur);
ss << ";";
}
ss.seekp(-1, ss.cur);
ss << ",";
for (const auto& output : brgemm->outputs()) {
const auto& shape = output.get_partial_shape();
for (const auto& dim : shape) {
ss << dim << " ";
}
ss.seekp(-1, ss.cur);
ss << ";";
}
ss.seekp(-1, ss.cur);
ss << ",";
int64_t M, N, K;
std::tie(M, N, K) = get_brgemm_dimensions(brgemm_expr);
ss << M << "," << N << "," << K << ",";
int64_t m_block = brgemm_expr->get_input_port_descriptor(0)->get_subtensor().front();
int64_t n_block = brgemm_expr->get_input_port_descriptor(1)->get_subtensor().back();
int64_t k_block = brgemm_expr->get_input_port_descriptor(0)->get_subtensor().back();
ss << m_block << "," << n_block << "," << k_block;
ss << '\n';
std::ofstream csv_file(csv_path, std::ios_base::app);
if (csv_file.seekp(0, std::ios_base::end).tellp() == 0) {
csv_file << "name,in_type,out_type,in_shapes,out_shapes,M,N,K,m_block,n_block,k_block\n";
}
csv_file << ss.str();
}
#endif // SNIPPETS_DEBUG_CAPS
};
} // namespace pass
} // namespace lowered
} // namespace snippets
} // namespace ov
42 changes: 42 additions & 0 deletions src/common/snippets/src/lowered/pass/brgemm_debug_params.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright (C) 2025 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//

#include "snippets/lowered/pass/brgemm_debug_params.hpp"

#include "snippets/itt.hpp"
#include "snippets/lowered/linear_ir.hpp"
#include "snippets/lowered/pass/pass.hpp"
#include "snippets/lowered/pass/iter_handler.hpp"
#include "snippets/snippets_isa.hpp"
#include "snippets/utils/utils.hpp"

namespace ov {
namespace snippets {
namespace lowered {
namespace pass {
using namespace ov::snippets::utils;

std::tuple<size_t, size_t, size_t> BrgemmDebugParamsBase::get_brgemm_dimensions(const ExpressionPtr& brgemm_expr) {
OPENVINO_ASSERT(brgemm_expr, "Brgemm expression is nullptr!");
const auto& in_0_desc = brgemm_expr->get_input_port_descriptor(0);
const auto& in_1_desc = brgemm_expr->get_input_port_descriptor(1);
const auto& out_desc = brgemm_expr->get_output_port_descriptor(0);

const auto& in_0_planar_dims = get_planar_vdims(in_0_desc->get_shape(), in_0_desc->get_layout());
const auto& in_1_planar_dims = get_planar_vdims(in_1_desc->get_shape(), in_1_desc->get_layout());
const auto& out_preordered_dims = get_preordered_vdims(out_desc->get_shape(), out_desc->get_layout());

const auto& m = *++out_preordered_dims.rbegin();
const auto& n = *out_preordered_dims.rbegin();
const auto& k0 = *in_0_planar_dims.rbegin();
const auto& k1 = *++in_1_planar_dims.rbegin();
size_t k = 0;
OPENVINO_ASSERT(utils::merge_dynamic_dim(k, k0, k1), "Brgemm input descriptors have incompatible K dimension value.");
return std::make_tuple(m, n, k);
}

} // namespace pass
} // namespace lowered
} // namespace snippets
} // namespace ov
5 changes: 5 additions & 0 deletions src/plugins/intel_cpu/src/nodes/subgraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "onednn/dnnl.h"
#include "openvino/core/parallel.hpp"
#include "shape_inference/custom/subgraph.hpp"
#include "snippets/lowered/pass/brgemm_debug_params.hpp"
#include "snippets/lowered/pass/init_loops.hpp"
#include "snippets/lowered/pass/insert_buffers.hpp"
#include "snippets/lowered/pass/insert_loops.hpp"
Expand Down Expand Up @@ -520,6 +521,10 @@ Subgraph::ControlFlowPasses Subgraph::getControlFlowPasses() const {
ov::snippets::lowered::pass::MarkLoops,
ov::intel_cpu::pass::BrgemmCPUBlocking);

SNIPPETS_REGISTER_PASS_RELATIVE(Place::After,
ov::intel_cpu::pass::BrgemmCPUBlocking,
ov::snippets::lowered::pass::BrgemmDebugParams<BrgemmCPU>);

SNIPPETS_REGISTER_PASS_RELATIVE(Place::After,
ov::snippets::lowered::pass::InitLoops,
ov::intel_cpu::pass::AdjustBrgemmCopyBLoopPorts);
Expand Down

0 comments on commit f2ab0f0

Please sign in to comment.