reid from https://github.com/michuanhaohao/reid-strong-baseline
zhangmeng
2020-01-10 c3765bd24fe73747688a0ec2a550f219c9acb384
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
#ifndef PROF_DAG_COUNTERS_H
#define PROF_DAG_COUNTERS_H
 
#include "caffe2/core/common.h"
#include "caffe2/core/logging.h"
#include "caffe2/core/timer.h"
#include "caffe2/proto/caffe2_pb.h"
#include "caffe2/proto/prof_dag.pb.h"
 
#include <unordered_map>
 
namespace caffe2 {
 
class ProfDAGStats {
 public:
  ProfDAGStats() : sum_(0.0), sqrsum_(0.0), cnt_(0) {}
  explicit ProfDAGStats(float time_ms)
      : sum_(time_ms), sqrsum_(time_ms * time_ms), cnt_(1) {}
 
  ProfDAGStats& operator+=(const ProfDAGStats& rhs) {
    sum_ += rhs.sum_;
    sqrsum_ += rhs.sqrsum_;
    cnt_ += rhs.cnt_;
    return *this;
  }
 
  std::pair<float, float> computeMoments() const {
    CAFFE_ENFORCE_GT(cnt_, 0);
    float mean = sum_ / cnt_;
    float stddev = std::sqrt(std::abs(sqrsum_ / cnt_ - mean * mean));
    return {mean, stddev};
  }
 
  float sum() const {
    return sum_;
  }
 
  float sqrsum() const {
    return sqrsum_;
  }
 
  size_t cnt() const {
    return cnt_;
  }
 
 private:
  float sum_;
  float sqrsum_;
  size_t cnt_;
};
 
class ProfDAGReport {
 public:
  friend class ProfDAGCounters;
  // Collects the execution time per each operator type
  ProfDAGProtos GetOperatorStats() const;
 
  // Collects the execution time of each operator, the output is
  // formatted as a map: (netName__opIndex__opType, cost)
  ProfDAGProtos GetPerOperatorCost() const;
 
  ProfDAGReport& operator+=(const ProfDAGReport& rhs);
 
  void PrintStats();
 
 private:
  ProfDAGProto statsProto(
      const std::string& name,
      const ProfDAGStats& stats,
      const std::vector<std::string>& op_extra_info) const;
 
  bool hasStats() const;
 
  std::vector<std::string> op_types_;
  std::vector<std::vector<std::string>> op_extra_info_;
 
  std::string net_name_;
 
  int num_runs_;
  // Cumulative stats per operator instance of the net
  std::vector<ProfDAGStats> time_per_op_total_;
 
  // Cumulative stats per unique operator type
  CaffeMap<std::string, ProfDAGStats> time_per_op_type_total_;
 
  CaffeMap<std::string, ProfDAGStats> times_per_run_per_type_total_;
 
  ProfDAGStats runtime_stats_;
};
 
/**
 * A simple wrapper around prof_dag's counters
 */
class ProfDAGCounters {
 public:
  explicit ProfDAGCounters(const std::shared_ptr<const NetDef>& net_def);
 
  // ReportRunStart/End are called at the beginning and at the end of
  // each net's run
  void ReportRunStart();
  void ReportRunEnd();
 
  void AddPerOpStartTime(size_t op_id);
  void AddPerOpEndTime(size_t op_id);
  void AddPerOpAsyncEndTime(size_t op_id);
  ProfDAGReport GetReport() const;
 
 private:
  Timer timer_;
 
  std::vector<float> op_start_times_run_;
  std::vector<float> op_end_times_run_;
  std::vector<float> op_async_end_times_run_;
  ProfDAGReport report_;
};
 
} // namespace caffe2
 
#endif