Caffe2 - C++ API
A deep learning, cross platform ML framework
prof_dag_counters.h
1 #ifndef PROF_DAG_COUNTERS_H
2 #define PROF_DAG_COUNTERS_H
3 
4 #include "caffe2/core/common.h"
5 #include "caffe2/core/logging.h"
6 #include "caffe2/core/timer.h"
7 #include "caffe2/proto/caffe2_pb.h"
8 #include "caffe2/proto/prof_dag.pb.h"
9 
10 #include <unordered_map>
11 
12 namespace caffe2 {
13 
14 class ProfDAGStats {
15  public:
16  ProfDAGStats() : sum_(0.0), sqrsum_(0.0), cnt_(0) {}
17  explicit ProfDAGStats(float time_ms)
18  : sum_(time_ms), sqrsum_(time_ms * time_ms), cnt_(1) {}
19 
20  ProfDAGStats& operator+=(const ProfDAGStats& rhs) {
21  sum_ += rhs.sum_;
22  sqrsum_ += rhs.sqrsum_;
23  cnt_ += rhs.cnt_;
24  return *this;
25  }
26 
27  std::pair<float, float> computeMoments() const {
28  CAFFE_ENFORCE_GT(cnt_, 0);
29  float mean = sum_ / cnt_;
30  float stddev = std::sqrt(std::abs(sqrsum_ / cnt_ - mean * mean));
31  return {mean, stddev};
32  }
33 
34  float sum() const {
35  return sum_;
36  }
37 
38  float sqrsum() const {
39  return sqrsum_;
40  }
41 
42  size_t cnt() const {
43  return cnt_;
44  }
45 
46  private:
47  float sum_;
48  float sqrsum_;
49  size_t cnt_;
50 };
51 
53  public:
54  friend class ProfDAGCounters;
55  // Collects the execution time per each operator type
56  ProfDAGProtos GetOperatorStats() const;
57 
58  // Collects the execution time of each operator, the output is
59  // formatted as a map: (netName__opIndex__opType, cost)
60  ProfDAGProtos GetPerOperatorCost() const;
61 
62  ProfDAGReport& operator+=(const ProfDAGReport& rhs);
63 
64  void PrintStats();
65 
66  private:
67  ProfDAGProto statsProto(
68  const std::string& name,
69  const ProfDAGStats& stats,
70  const std::vector<std::string>& op_extra_info) const;
71 
72  bool hasStats() const;
73 
74  std::vector<std::string> op_types_;
75  std::vector<std::vector<std::string>> op_extra_info_;
76 
77  std::string net_name_;
78 
79  int num_runs_;
80  // Cumulative stats per operator instance of the net
81  std::vector<ProfDAGStats> time_per_op_total_;
82 
83  // Cumulative stats per unique operator type
84  CaffeMap<std::string, ProfDAGStats> time_per_op_type_total_;
85 
86  CaffeMap<std::string, ProfDAGStats> times_per_run_per_type_total_;
87 
88  ProfDAGStats runtime_stats_;
89 };
90 
95  public:
96  explicit ProfDAGCounters(const std::shared_ptr<const NetDef>& net_def);
97 
98  // ReportRunStart/End are called at the beginning and at the end of
99  // each net's run
100  void ReportRunStart();
101  void ReportRunEnd();
102 
103  void AddPerOpStartTime(size_t op_id);
104  void AddPerOpEndTime(size_t op_id);
105  void AddPerOpAsyncEndTime(size_t op_id);
106  ProfDAGReport GetReport() const;
107 
108  private:
109  Timer timer_;
110 
111  std::vector<float> op_start_times_run_;
112  std::vector<float> op_end_times_run_;
113  std::vector<float> op_async_end_times_run_;
114  ProfDAGReport report_;
115 };
116 
117 } // namespace caffe2
118 
119 #endif
A simple wrapper around prof_dag&#39;s counters.
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
Definition: blob.h:13
A simple timer object for measuring time.
Definition: timer.h:16