1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
|
#include "observers/net_observer_reporter_print.h"
#include <algorithm>
#include <sstream>
#include "caffe2/core/init.h"
#include "observers/observer_config.h"
#include <c10/util/irange.h>
namespace caffe2 {
const std::string NetObserverReporterPrint::IDENTIFIER = "Caffe2Observer ";
static std::string get_op_args(PerformanceInformation p);
static std::string get_tensor_shapes(PerformanceInformation p);
static std::string sanatize(std::string json_s);
void NetObserverReporterPrint::report(
NetBase* net,
std::map<std::string, PerformanceInformation>& info) {
// Not allowed to use json library
std::vector<std::map<std::string, std::string>> caffe2_perf;
for (auto& p : info) {
if ((p.first == "NET_DELAY") && (info.size() == 1)) {
// for Net_delay perf
caffe2_perf.push_back({{"type", "NET"},
{"value", c10::to_string(p.second.latency * 1000)},
{"unit", "us"},
{"metric", "latency"}});
caffe2_perf.push_back({{"type", "NET_"},
{
"value",
c10::to_string(
p.second.cpuMilliseconds /
p.second.latency *
100),
},
{"unit", "percent"},
{"metric", "cpu_percent"}});
} else if (p.first != "NET_DELAY") {
// for operator perf
std::string shape_str = get_tensor_shapes(p.second);
std::string args_str = get_op_args(p.second);
std::string type = p.first;
caffe2_perf.push_back({{"type", type},
{"value", c10::to_string(p.second.latency * 1000)},
{"unit", "us"},
{"metric", "latency"}});
caffe2_perf.push_back({{"type", type},
{
"value",
c10::to_string(
p.second.cpuMilliseconds /
p.second.latency *
100),
},
{"unit", "percent"},
{"metric", "cpu_percent"}});
if (p.second.flops > 0) {
caffe2_perf.push_back({{"type", type},
{"value", c10::to_string(p.second.flops)},
{"unit", "flop"},
{"metric", "flops"}});
}
if (shape_str != "") {
caffe2_perf.push_back({{"type", type},
{"info_string", shape_str},
{"unit", ""},
{"metric", "tensor_shapes"}});
}
if (args_str != "") {
caffe2_perf.push_back({{"type", type},
{"info_string", args_str},
{"unit", ""},
{"metric", "op_args"}});
}
}
}
// NOLINTNEXTLINE(modernize-loop-convert)
for (auto it = caffe2_perf.begin(); it != caffe2_perf.end(); it++) {
std::stringstream buffer;
auto entry = *it;
buffer << IDENTIFIER << "{";
// NOLINTNEXTLINE(modernize-raw-string-literal)
buffer << "\"type\": \"" << sanatize(entry["type"]) << "\","
// NOLINTNEXTLINE(modernize-raw-string-literal)
<< "\"unit\": \"" << sanatize(entry["unit"]) << "\","
// NOLINTNEXTLINE(modernize-raw-string-literal)
<< "\"metric\": \"" << sanatize(entry["metric"]) << "\",";
if (entry.find("value") != entry.end()) {
// NOLINTNEXTLINE(modernize-raw-string-literal)
buffer << "\"value\": \"" << sanatize(entry["value"]) << "\"";
} else if (entry.find("info_string") != entry.end()) {
// NOLINTNEXTLINE(modernize-raw-string-literal)
buffer << "\"info_string\": \"" << sanatize(entry["info_string"]) << "\"";
}
buffer << "}";
LOG(INFO) << buffer.str();
}
}
static std::string get_tensor_shapes(PerformanceInformation p) {
std::string shape_str;
std::stringstream shape_stream;
if (!p.tensor_shapes.empty()) {
shape_stream << "[";
for (const auto i : c10::irange(p.tensor_shapes.size())) {
shape_stream << "[";
for (int j = 0; j < p.tensor_shapes[i].dims_size(); j++) {
shape_stream << p.tensor_shapes[i].dims(j) << ", ";
}
shape_stream << "], ";
}
shape_stream << "]";
shape_str = shape_stream.str();
} else {
shape_str = "";
}
return shape_str;
}
static std::string get_op_args(PerformanceInformation p) {
std::string args_str;
if (!p.args.empty()) {
std::stringstream args;
args << "[";
for (const auto i : c10::irange(p.args.size())) {
args << "{" << p.args[i].name() << ": ";
if (p.args[i].has_i()) {
args << p.args[i].i();
} else if (p.args[i].has_s()) {
args << p.args[i].s();
} else if (p.args[i].has_n()) {
args << &p.args[i].n();
} else if (p.args[i].has_f()) {
args << p.args[i].f();
} else {
args << "None";
}
args << "}, ";
}
args << "]";
args_str = args.str();
} else {
args_str = "";
}
return args_str;
}
static std::string sanatize(std::string json_s) {
// Remove illegal characters from the name that would cause json string to
// become invalid
json_s.erase(std::remove(json_s.begin(), json_s.end(), '"'), json_s.end());
json_s.erase(std::remove(json_s.begin(), json_s.end(), '\\'), json_s.end());
return json_s;
}
}
|