Caffe2 - C++ API
A deep learning, cross platform ML framework
profile_observer.cc
1 
17 #include "profile_observer.h"
18 #include "caffe2/core/logging.h"
19 
20 namespace caffe2 {
21 
22 void ProfileOperatorObserver::Dump() const {
23  static std::mutex loggingMutex;
24  std::lock_guard<std::mutex> lock(loggingMutex);
25 
26  LOG(INFO) << "--------- Starting operator " << subject_->debug_def().type()
27  << " op#" << getId() << " ---------";
28  for (int i = 0; i < subject_->InputSize(); ++i) {
29  if (subject_->InputIsTensorType(i, CPU)) {
30  const auto& tensor = subject_->Input<Tensor>(i, CPU);
31  const auto& name = subject_->debug_def().input(i);
32  TensorPrinter printer(name);
33  LOG(INFO) << "Input " << i << ": " << printer.MetaStr(tensor);
34  } else if (subject_->InputIsTensorType(i, CUDA)) {
35  const auto& tensor = subject_->Input<Tensor>(i, CUDA);
36  const auto& name = subject_->debug_def().input(i);
37  TensorPrinter printer(name);
38  LOG(INFO) << "Input " << i << ": " << printer.MetaStr(tensor);
39  }
40  }
41 
42  int a = 0;
43  for (const auto& arg : subject_->debug_def().arg()) {
44  LOG(INFO) << "Argument " << a << ": " << arg.ShortDebugString();
45  ++a;
46  }
47 
48  for (int o = 0; o < subject_->OutputSize(); ++o) {
49  if (subject_->OutputIsTensorType(o, CPU)) {
50  auto* tensor = subject_->Output<Tensor>(o, CPU);
51  const auto& name = subject_->debug_def().output(o);
52  TensorPrinter printer(name);
53  LOG(INFO) << "Output " << o << ": " << printer.MetaStr(*tensor);
54  } else if (subject_->OutputIsTensorType(o, CUDA)) {
55  auto* tensor = subject_->Output<Tensor>(o, CUDA);
56  const auto& name = subject_->debug_def().output(o);
57  TensorPrinter printer(name);
58  LOG(INFO) << "Output " << o << ": " << printer.MetaStr(*tensor);
59  }
60  }
61 
62  LOG(INFO) << "--------- Finished operator " << subject_->debug_def().type()
63  << " in " << run_time_ << " ms ---------";
64 }
65 
66 void ProfileOperatorObserver::Start() {
67  start_time_ = timer_.MilliSeconds();
68 }
69 
70 void ProfileOperatorObserver::Stop() {
71  run_time_ = timer_.MilliSeconds() - start_time_;
72  Dump();
73 }
74 
75 std::unique_ptr<ObserverBase<OperatorBase>> ProfileOperatorObserver::rnnCopy(
76  OperatorBase* subject,
77  int rnn_order) const {
78  return std::unique_ptr<ObserverBase<OperatorBase>>(
79  new ProfileOperatorObserver(
80  subject, netObserver_, net_position_, rnn_order));
81 }
82 } // namespace caffe2
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
Definition: blob.h:13
float MilliSeconds()
Returns the elapsed time in milliseconds.
Definition: timer.h:32