1 #include "observers/net_observer_reporter_print.h" 4 #include "caffe2/core/init.h" 5 #include "observers/observer_config.h" 9 const std::string NetObserverReporterPrint::IDENTIFIER =
"Caffe2Observer ";
10 static std::string get_op_args(PerformanceInformation p);
11 static std::string get_tensor_shapes(PerformanceInformation p);
13 void NetObserverReporterPrint::report(
15 std::map<std::string, PerformanceInformation>& info) {
17 std::vector<std::map<std::string, std::string>> caffe2_perf;
19 for (
auto& p : info) {
20 if ((p.first ==
"NET_DELAY") && (info.size() == 1)) {
22 caffe2_perf.push_back({{
"type",
"NET"},
23 {
"value", c10::to_string(p.second.latency * 1000)},
25 {
"metric",
"latency"}});
26 }
else if (p.first !=
"NET_DELAY") {
28 std::string shape_str = get_tensor_shapes(p.second);
29 std::string args_str = get_op_args(p.second);
31 caffe2_perf.push_back({{
"type", p.first},
32 {
"value", c10::to_string(p.second.latency * 1000)},
34 {
"metric",
"latency"}});
35 if (p.second.flops > 0) {
36 caffe2_perf.push_back({{
"type", p.first},
37 {
"value", c10::to_string(p.second.flops)},
39 {
"metric",
"flops"}});
41 if (shape_str !=
"") {
42 caffe2_perf.push_back({{
"type", p.first},
43 {
"info_string", shape_str},
45 {
"metric",
"tensor_shapes"}});
48 caffe2_perf.push_back({{
"type", p.first},
49 {
"info_string", args_str},
51 {
"metric",
"op_args"}});
56 for (
auto it = caffe2_perf.begin(); it != caffe2_perf.end(); it++) {
57 std::stringstream buffer;
59 buffer << IDENTIFIER <<
"{";
60 buffer <<
"\"type\": \"" << entry[
"type"] <<
"\"," 61 <<
"\"unit\": \"" << entry[
"unit"] <<
"\"," 62 <<
"\"metric\": \"" << entry[
"metric"] <<
"\",";
63 if (entry.find(
"value") != entry.end()) {
64 buffer <<
"\"value\": \"" << entry[
"value"] <<
"\"";
65 }
else if (entry.find(
"info_string") != entry.end()) {
66 buffer <<
"\"info_string\": \"" << entry[
"info_string"] <<
"\"";
69 LOG(INFO) << buffer.str();
73 static std::string get_tensor_shapes(PerformanceInformation p) {
74 std::string shape_str;
75 std::stringstream shape_stream;
76 if (!p.tensor_shapes.empty()) {
78 for (
int i = 0; i < p.tensor_shapes.size(); i++) {
80 for (
int j = 0; j < p.tensor_shapes[i].dims_size(); j++) {
81 shape_stream << p.tensor_shapes[i].dims(j) <<
", ";
83 shape_stream <<
"], ";
86 shape_str = shape_stream.str();
93 static std::string get_op_args(PerformanceInformation p) {
95 if (!p.args.empty()) {
96 std::stringstream args;
98 for (
int i = 0; i < p.args.size(); i++) {
99 args <<
"{" << p.args[i].name() <<
": ";
100 if (p.args[i].has_i()) {
101 args << p.args[i].i();
102 }
else if (p.args[i].has_s()) {
103 args << p.args[i].s();
104 }
else if (p.args[i].has_n()) {
105 args << &p.args[i].n();
106 }
else if (p.args[i].has_f()) {
107 args << p.args[i].f();
114 args_str = args.str();
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...