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