Skip to content

Commit

Permalink
Address solution proposal
Browse files Browse the repository at this point in the history
  • Loading branch information
aobolensk committed Jan 14, 2025
1 parent cdb188e commit 1fe9e26
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 47 deletions.
18 changes: 9 additions & 9 deletions src/common/snippets/docs/debug_capabilities/parameters_dump.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ Examples:

Output example:
```csv
name,in_type,out_type,in_shapes,out_shapes,M,N,K,m_block,n_block,k_block
i8;i8;f32,i32,1 128 16 64;1 16 64 128;1 16 64 128,1 16 128 128,128,128,64,32,-2,-2
u8;i8,i32,1 16 128 128;1 16 128 64,1 16 128 64,128,64,128,32,-2,-2
i8;i8;f32,i32,2 68 6 92;2 6 92 68;2 6 92 68,2 6 68 68,68,68,92,32,-2,-2
u8;i8,i32,2 6 68 68;2 6 68 92,2 6 68 92,68,92,68,32,-2,-2
i8;i8;f32,i32,? ? ? 100;? ? 100 128;? ? 100 128,? ? ? 128,-1,128,100,32,-2,-2
u8;i8,i32,? ? ? 128;? ? 128 100,? ? ? 100,-1,100,128,32,-2,-2
i8;i8;f32,i32,? ? ? ?;? ? ? ?;? ? ? ?,? ? ? ?,-1,-1,-1,32,-2,-2
u8;i8,i32,? ? ? ?;? ? ? ?,? ? ? ?,-1,-1,-1,32,-2,-2
name,in_type,out_type,in_shapes,out_shapes,M,N,K,m_block,n_block,k_block,acc_max_time,avg_max_time
MatMul_437,i8;i8;f32,i32,1 128 16 64;1 16 64 128;1 16 64 128,1 16 128 128,128,128,64,32,-2,-2,157430,19678
MatMul_451,u8;i8,i32,1 16 128 128;1 16 128 64,1 16 128 64,128,64,128,32,-2,-2,96222,12027
MatMul_6069,i8;i8;f32,i32,2 68 6 92;2 6 92 68;2 6 92 68,2 6 68 68,68,68,92,32,-2,-2,1734,866
MatMul_6083,u8;i8,i32,2 6 68 68;2 6 68 92,2 6 68 92,68,92,68,32,-2,-2,1929,1929
MatMul_12488,i8;i8;f32,i32,? ? ? 100;? ? 100 128;? ? 100 128,? ? ? 128,-1,128,100,32,-2,-2,43105,43105
MatMul_12502,u8;i8,i32,? ? ? 128;? ? 128 100,? ? ? 100,-1,100,128,32,-2,-2,3328,2801
MatMul_18789,i8;i8;f32,i32,? ? ? ?;? ? ? ?;? ? ? ?,? ? ? ?,-1,-1,-1,32,-2,-2,6135,6135
MatMul_18803,u8;i8,i32,? ? ? ?;? ? ? ?,? ? ? ?,-1,-1,-1,32,-2,-2,2742,1371
```
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
// SPDX-License-Identifier: Apache-2.0
//

#pragma once
#ifdef SNIPPETS_DEBUG_CAPS

#include <fstream>
#pragma once

#include "snippets/itt.hpp"
#include "snippets/lowered/loop_manager.hpp"
Expand Down Expand Up @@ -51,78 +51,90 @@ class BrgemmDebugParams : public snippets::lowered::pass::RangedPass, public Brg
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
static size_t seq_number = 0;
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);
}
if (!brgemm)
continue;
// Collect brgemm parameters
auto params = collect_params(brgemm_expr, linear_ir);
const auto& perf_count_begin = std::make_shared<snippets::op::PerfCountBegin>();
perf_count_begin->set_friendly_name(std::string("PerfCount_Begin_") + std::to_string(seq_number) +
"_DebugParams");
const auto empty_inputs = std::vector<PortConnectorPtr>{};
linear_ir.insert_node(perf_count_begin, empty_inputs, expr_it->get()->get_loop_ids(), false, expr_it);

const auto& perf_count_end = std::make_shared<snippets::op::PerfCountEnd>(perf_count_begin->output(0));
perf_count_end->set_friendly_name(std::string("PerfCount_End_") + std::to_string(seq_number) +
"_DebugParams");
// Attach brgemm parameters to PerfCountEnd node
auto csv_path = linear_ir.get_config().debug_config.dumpParams.csv_path;
perf_count_end->get_rt_info()["brgemm_params"] = params;
perf_count_end->get_rt_info()["brgemm_params_csv_path"] = csv_path;
linear_ir.insert_node(perf_count_end, empty_inputs, expr_it->get()->get_loop_ids(), false, next(expr_it));
seq_number++;
}
#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) {
private:
std::string collect_params(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() << ",";
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 << ';';
}
}
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 << ';';
}
}
ss << ",";
ss << ',';
for (const auto& input : brgemm->inputs()) {
const auto& shape = input.get_partial_shape();
for (const auto& dim : shape) {
ss << dim << " ";
ss << dim << ' ';
}
ss.seekp(-1, ss.cur);
ss << ";";
ss << ';';
}
ss.seekp(-1, ss.cur);
ss << ",";
ss << ',';
for (const auto& output : brgemm->outputs()) {
const auto& shape = output.get_partial_shape();
for (const auto& dim : shape) {
ss << dim << " ";
ss << dim << ' ';
}
ss.seekp(-1, ss.cur);
ss << ";";
ss << ';';
}
ss.seekp(-1, ss.cur);
ss << ",";
ss << ',';
int64_t M, N, K;
std::tie(M, N, K) = get_brgemm_dimensions(brgemm_expr);
ss << M << "," << N << "," << K << ",";
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();
ss << m_block << ',' << n_block << ',' << k_block;
ss << ',';
return ss.str();
}
#endif // SNIPPETS_DEBUG_CAPS
};

} // namespace pass
} // namespace lowered
} // namespace snippets
} // namespace ov
} // namespace ov

#endif // SNIPPETS_DEBUG_CAPS
13 changes: 9 additions & 4 deletions src/common/snippets/include/snippets/op/perf_count.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,20 +74,25 @@ class PerfCountEnd : public PerfCountEndBase {
public:
OPENVINO_OP("PerfCountEnd", "SnippetsOpset", PerfCountEndBase);
PerfCountEnd(const Output<Node>& pc_begin);
PerfCountEnd() = default;
~PerfCountEnd() {
output_perf_count();
}
PerfCountEnd();
~PerfCountEnd();

void output_perf_count();
std::shared_ptr<Node> clone_with_new_inputs(const OutputVector& inputs) const override;

void init_pc_begin();
void set_accumulated_time();

void dump_brgemm_params_csv_file();

private:
ov::threading::ThreadLocal<uint64_t> accumulation;
ov::threading::ThreadLocal<uint32_t> iteration;
std::shared_ptr<PerfCountBegin> m_pc_begin = nullptr;

static std::string brgemm_csv_path;
static std::map<std::string, std::string> m_debug_params_map;
static size_t nodes_count;
};

} // namespace op
Expand Down
6 changes: 5 additions & 1 deletion src/common/snippets/src/lowered/pass/brgemm_debug_params.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// SPDX-License-Identifier: Apache-2.0
//

#ifdef SNIPPETS_DEBUG_CAPS

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

#include "snippets/itt.hpp"
Expand Down Expand Up @@ -39,4 +41,6 @@ std::tuple<size_t, size_t, size_t> BrgemmDebugParamsBase::get_brgemm_dimensions(
} // namespace pass
} // namespace lowered
} // namespace snippets
} // namespace ov
} // namespace ov

#endif // SNIPPETS_DEBUG_CAPS
53 changes: 52 additions & 1 deletion src/common/snippets/src/op/perf_count.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
//
#ifdef SNIPPETS_DEBUG_CAPS

#include <fstream>

#include "snippets/op/perf_count.hpp"

namespace ov {
Expand Down Expand Up @@ -62,9 +64,30 @@ void PerfCountBegin::set_start_time() {
}

//////////////////PerfCountEnd///////////////
PerfCountEnd::PerfCountEnd(const Output<Node>& pc_begin) : PerfCountEndBase({pc_begin}), accumulation(0ul), iteration(0u) {

size_t PerfCountEnd::nodes_count = 0;
std::map<std::string, std::string> PerfCountEnd::m_debug_params_map;
std::string PerfCountEnd::brgemm_csv_path; // NOLINT

PerfCountEnd::PerfCountEnd() : PerfCountEndBase() {
++nodes_count;
}

PerfCountEnd::PerfCountEnd(const Output<Node>& pc_begin)
: PerfCountEndBase({pc_begin}),
accumulation(0ul),
iteration(0u) {
constructor_validate_and_infer_types();
init_pc_begin();
++nodes_count;
}

PerfCountEnd::~PerfCountEnd() {
output_perf_count();
--nodes_count;
if (nodes_count == 0) {
dump_brgemm_params_csv_file();
}
}

std::shared_ptr<Node> PerfCountEnd::clone_with_new_inputs(const OutputVector& inputs) const {
Expand Down Expand Up @@ -109,6 +132,34 @@ void PerfCountEnd::output_perf_count() {
std::cout << "max accumulated time:" << acc_max << "ns" << std::endl;
// max avg
std::cout << "max avg time:" << avg_max << "ns" << std::endl;

// Dump brgemm debug parameters to csv file
if (acc_max != 0 && avg_max != 0 && get_friendly_name().find("_DebugParams") != std::string::npos) {
const auto& rt_info = get_rt_info();
auto brgemm_params_it = rt_info.find("brgemm_params");
if (brgemm_params_it == rt_info.end()) {
return;
}
if (brgemm_csv_path.empty()) {
auto brgemm_csv_path_it = rt_info.find("brgemm_params_csv_path");
brgemm_csv_path = brgemm_csv_path_it->second.as<std::string>();
}
m_debug_params_map[get_friendly_name()] =
brgemm_params_it->second.as<std::string>() + std::to_string(acc_max) + ',' + std::to_string(avg_max);
}
}

void PerfCountEnd::dump_brgemm_params_csv_file() {
if (m_debug_params_map.empty()) {
return;
}
std::ofstream csv_file(brgemm_csv_path);
OPENVINO_ASSERT(csv_file.is_open(), "Failed to open csv file for brgemm debug parameters.");
csv_file << "name,in_type,out_type,in_shapes,out_shapes,M,N,K,m_block,n_block,k_block,acc_max_time,avg_max_time\n";
for (const auto& [_, params] : m_debug_params_map) {
csv_file << params << '\n';
}
csv_file.close();
}

} // namespace op
Expand Down
2 changes: 2 additions & 0 deletions src/plugins/intel_cpu/src/nodes/subgraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -521,9 +521,11 @@ Subgraph::ControlFlowPasses Subgraph::getControlFlowPasses() const {
ov::snippets::lowered::pass::MarkLoops,
ov::intel_cpu::pass::BrgemmCPUBlocking);

#ifdef SNIPPETS_DEBUG_CAPS
SNIPPETS_REGISTER_PASS_RELATIVE(Place::After,
ov::intel_cpu::pass::BrgemmCPUBlocking,
ov::snippets::lowered::pass::BrgemmDebugParams<BrgemmCPU>);
#endif // SNIPPETS_DEBUG_CAPS

SNIPPETS_REGISTER_PASS_RELATIVE(Place::After,
ov::snippets::lowered::pass::InitLoops,
Expand Down

0 comments on commit 1fe9e26

Please sign in to comment.