Skip to content

Commit eec798a

Browse files
authored
Merge branch 'master' into feature/optimize_yolo
2 parents 515a1a1 + 22c922e commit eec798a

File tree

3 files changed

+42
-20
lines changed

3 files changed

+42
-20
lines changed

src/Native/include/nncase/runtime/stackvm/op_profile.h

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,38 @@
1414
*/
1515
#pragma once
1616
#include "opcode.h"
17-
#include <chrono>
1817
#include <iomanip>
19-
#include <iostream>
20-
#include <tuple>
2118
#include <vector>
2219

20+
#if defined(LINUX_RUNTIME)
21+
#include <chrono>
22+
#endif
23+
2324
using namespace nncase::runtime::stackvm;
2425

26+
#if defined(NNCASE_BAREMETAL)
2527
extern "C" {
2628
double get_ms_time();
2729
}
30+
#endif
31+
32+
struct OpInfo {
33+
uint8_t type;
34+
const char *name;
35+
double begin;
36+
double end;
37+
OpInfo(uint8_t t, const char *n, double s, double e)
38+
: type(t), name(n), begin(s), end(e) {}
39+
};
2840

2941
class op_profile {
3042
public:
3143
op_profile(opcode_t opcode, uint8_t enable_profiling = 1)
3244
: active_(enable_profiling) {
3345
if (active_) {
46+
if (op_timing_.empty()) {
47+
op_timing_.reserve(256);
48+
}
3449
op_type_ = (uint8_t)opcode;
3550
op_name_ = to_string(opcode);
3651
begin_ = get_time();
@@ -41,6 +56,9 @@ class op_profile {
4156
uint8_t enable_profiling = 1)
4257
: active_(enable_profiling) {
4358
if (active_) {
59+
if (op_timing_.empty()) {
60+
op_timing_.reserve(256);
61+
}
4462
op_type_ = (uint8_t)opcode;
4563
op_name_ = to_string(tensor_funct);
4664
begin_ = get_time();
@@ -50,16 +68,14 @@ class op_profile {
5068
~op_profile() {
5169
if (active_) {
5270
end_ = get_time();
53-
op_timing_.push_back(
54-
std::make_tuple(op_name_, op_type_, begin_, end_));
71+
op_timing_.emplace_back(op_type_, op_name_, begin_, end_);
5572
}
5673
}
5774

5875
static void print();
5976

6077
public:
61-
static std::vector<std::tuple<std::string, uint8_t, double, double>>
62-
op_timing_;
78+
static std::vector<OpInfo> op_timing_;
6379

6480
private:
6581
double get_time() {
@@ -78,7 +94,7 @@ class op_profile {
7894
private:
7995
double begin_;
8096
double end_;
81-
std::string op_name_;
97+
const char *op_name_;
8298
uint8_t op_type_;
8399
uint8_t active_;
84100
};

src/Native/include/nncase/runtime/stackvm/opcode.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -843,7 +843,7 @@ struct tensor_where_op_t {
843843
bool is_tf_where;
844844
};
845845

846-
inline std::string to_string(tensor_function_t tensor_funct) {
846+
inline const char *to_string(tensor_function_t tensor_funct) {
847847
switch (tensor_funct) {
848848
case tensor_function_t::batch_normalization:
849849
return "batch_normalization";
@@ -1039,7 +1039,7 @@ inline std::string to_string(tensor_function_t tensor_funct) {
10391039
return "unknown tensor_function_t";
10401040
}
10411041

1042-
inline std::string to_string(opcode_t code) {
1042+
inline const char *to_string(opcode_t code) {
10431043
switch (code) {
10441044
case opcode_t::NOP:
10451045
return "NOP";

src/Native/src/runtime/stackvm/op_profile.cpp

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,17 @@
1414
*/
1515

1616
#include <algorithm>
17+
#include <iostream>
1718
#include <map>
1819
#include <nncase/runtime/stackvm/op_profile.h>
1920
#include <unordered_map>
2021
#include <vector>
2122

22-
std::vector<std::tuple<std::string, uint8_t, double, double>>
23-
op_profile::op_timing_;
23+
std::vector<OpInfo> op_profile::op_timing_;
2424

2525
void op_profile::print() {
26-
std::map<std::string, double> op_timing;
27-
std::unordered_map<std::string, size_t> op_count;
26+
std::map<const char *, double> op_timing;
27+
std::unordered_map<const char *, size_t> op_count;
2828

2929
std::cout << "stack OPs timeline" << std::endl;
3030
std::cout << "|" << std::setw(24) << std::left << "stackvm tensor op"
@@ -39,7 +39,11 @@ void op_profile::print() {
3939
<< "|" << std::setw(24) << std::left << "---"
4040
<< "|" << std::endl;
4141
double init_timing = -1;
42-
for (auto &&[op_name, op_type, begin, end] : op_timing_) {
42+
for (auto op_info : op_timing_) {
43+
uint8_t op_type = op_info.type;
44+
const char *op_name = op_info.name;
45+
double begin = op_info.begin;
46+
double end = op_info.end;
4347
if (init_timing == -1) {
4448
init_timing = begin;
4549
}
@@ -61,17 +65,19 @@ void op_profile::print() {
6165
}
6266

6367
double total = 0.f;
64-
std::vector<std::pair<std::string, double>> v;
68+
std::vector<std::pair<const char *, double>> v;
69+
v.reserve(op_timing.size());
6570
for (auto e : op_timing) {
6671
total += e.second;
6772
v.push_back(e);
6873
}
6974
std::cout << std::endl;
7075

71-
std::sort(
72-
v.begin(), v.end(),
73-
[=](std::pair<std::string, double> &a,
74-
std::pair<std::string, double> &b) { return a.second > b.second; });
76+
std::sort(v.begin(), v.end(),
77+
[=](std::pair<const char *, double> &a,
78+
std::pair<const char *, double> &b) {
79+
return a.second > b.second;
80+
});
7581

7682
std::cout << "stackvm OPs profile" << std::endl;
7783
std::cout << "|" << std::setw(24) << std::left << "stackvm tensor op"

0 commit comments

Comments
 (0)