Caffe2 - C++ API
A deep learning, cross platform ML framework
perf_observer.h
1 #pragma once
2 
3 #include "caffe2/core/common.h"
4 #include "caffe2/core/net.h"
5 #include "caffe2/core/observer.h"
6 #include "caffe2/core/timer.h"
7 #include "observers/macros.h"
8 
9 #include <unordered_map>
10 
11 namespace caffe2 {
12 
13 
14 class CAFFE2_OBSERVER_API PerfNetObserver : public NetObserver {
15  public:
16  explicit PerfNetObserver(NetBase* subject_);
17  virtual ~PerfNetObserver();
18 
19  caffe2::Timer& getTimer() {
20  return timer_;
21  }
22 
23  private:
24  void Start() override;
25  void Stop() override;
26 
27  caffe2::string getObserverName(const OperatorBase* op, int idx) const;
28 
29  private:
30  enum LogType {
31  NONE,
32  OPERATOR_DELAY,
33  NET_DELAY,
34  };
35  LogType logType_;
36  unsigned int numRuns_;
37  std::unordered_map<const OperatorBase*, const ObserverBase<OperatorBase>*>
38  observerMap_;
39 
40  caffe2::Timer timer_;
41 };
42 
43 class PerfOperatorObserver : public ObserverBase<OperatorBase> {
44  public:
46  virtual ~PerfOperatorObserver();
47 
48  double getMilliseconds() const;
49  std::vector<TensorShape> getTensorShapes() const;
50 
51  private:
52  void Start() override;
53  void Stop() override;
54 
55  private:
56  // Observer of a net that owns corresponding op. We make sure net is never
57  // destructed while operator observer is still alive. First operator observer
58  // gets destructed, then the op, then the net and its observer.
59  // We do this trick in order to get access to net's name and other fields
60  // without storing inside the operator observer. Each field is memory
61  // costly here and a raw pointer is a cheapest sholution
62  PerfNetObserver* netObserver_;
63  double milliseconds_;
64  std::vector<TensorShape> tensor_shapes_;
65 };
66 } // namespace caffe2
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