Caffe2 - C++ API
A deep learning, cross platform ML framework
net_observer_reporter_print.cc
1 #include "observers/net_observer_reporter_print.h"
2 
3 #include <sstream>
4 #include "caffe2/core/init.h"
5 #include "observers/observer_config.h"
6 
7 namespace caffe2 {
8 
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);
12 
13 void NetObserverReporterPrint::report(
14  NetBase* net,
15  std::map<std::string, PerformanceInformation>& info) {
16  // Not allowed to use json library
17  std::vector<std::map<std::string, std::string>> caffe2_perf;
18 
19  for (auto& p : info) {
20  if ((p.first == "NET_DELAY") && (info.size() == 1)) {
21  // for Net_delay perf
22  caffe2_perf.push_back({{"type", "NET"},
23  {"value", c10::to_string(p.second.latency * 1000)},
24  {"unit", "us"},
25  {"metric", "latency"}});
26  } else if (p.first != "NET_DELAY") {
27  // for operator perf
28  std::string shape_str = get_tensor_shapes(p.second);
29  std::string args_str = get_op_args(p.second);
30 
31  caffe2_perf.push_back({{"type", p.first},
32  {"value", c10::to_string(p.second.latency * 1000)},
33  {"unit", "us"},
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)},
38  {"unit", "flop"},
39  {"metric", "flops"}});
40  }
41  if (shape_str != "") {
42  caffe2_perf.push_back({{"type", p.first},
43  {"info_string", shape_str},
44  {"unit", ""},
45  {"metric", "tensor_shapes"}});
46  }
47  if (args_str != "") {
48  caffe2_perf.push_back({{"type", p.first},
49  {"info_string", args_str},
50  {"unit", ""},
51  {"metric", "op_args"}});
52  }
53  }
54  }
55 
56  for (auto it = caffe2_perf.begin(); it != caffe2_perf.end(); it++) {
57  std::stringstream buffer;
58  auto entry = *it;
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"] << "\"";
67  }
68  buffer << "}";
69  LOG(INFO) << buffer.str();
70  }
71 }
72 
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()) {
77  shape_stream << "[";
78  for (int i = 0; i < p.tensor_shapes.size(); i++) {
79  shape_stream << "[";
80  for (int j = 0; j < p.tensor_shapes[i].dims_size(); j++) {
81  shape_stream << p.tensor_shapes[i].dims(j) << ", ";
82  }
83  shape_stream << "], ";
84  }
85  shape_stream << "]";
86  shape_str = shape_stream.str();
87  } else {
88  shape_str = "";
89  }
90  return shape_str;
91 }
92 
93 static std::string get_op_args(PerformanceInformation p) {
94  std::string args_str;
95  if (!p.args.empty()) {
96  std::stringstream args;
97  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();
108  } else {
109  args << "None";
110  }
111  args << "}, ";
112  }
113  args << "]";
114  args_str = args.str();
115  } else {
116  args_str = "";
117  }
118  return args_str;
119 }
120 }
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
Definition: blob.h:13