1 #include "observers/perf_observer.h" 2 #include "observers/observer_config.h" 4 #include "caffe2/core/flags.h" 5 #include "observers/net_observer_reporter_print.h" 9 #include "caffe2/core/common.h" 10 #include "caffe2/core/init.h" 11 #include "caffe2/core/operator.h" 15 aiBench_netInitSampleRate,
17 "One in N sampling rate for net delay");
20 aiBench_netFollowupSampleRate,
22 "One in N sampling rate for net delay");
25 aiBench_netFollowupSampleCount,
27 "control the following c logs");
30 aiBench_operatorNetSampleRatio,
32 "One in N sampling rate for operator delay");
37 "skip the first N iterations of the net run");
43 bool registerGlobalPerfNetObserverCreator(
int* ,
char*** ) {
44 AddGlobalNetObserverCreator([](NetBase* subject) {
45 return caffe2::make_unique<PerfNetObserver>(subject);
48 #if !defined(C10_MOBILE) 50 caffe2::ObserverConfig::setReporter(
51 caffe2::make_unique<caffe2::NetObserverReporterPrint>());
53 caffe2::ObserverConfig::initSampleRate(
54 FLAGS_aiBench_netInitSampleRate,
55 FLAGS_aiBench_netFollowupSampleRate,
56 FLAGS_aiBench_netFollowupSampleCount,
57 FLAGS_aiBench_operatorNetSampleRatio,
58 FLAGS_aiBench_skipIters);
65 REGISTER_CAFFE2_EARLY_INIT_FUNCTION(
66 registerGlobalPerfNetObserverCreator,
67 ®isterGlobalPerfNetObserverCreator,
68 "Caffe2 net global observer creator");
70 PerfNetObserver::PerfNetObserver(NetBase* subject_)
71 : NetObserver(subject_), numRuns_(0) {}
73 PerfNetObserver::~PerfNetObserver() {}
75 void PerfNetObserver::Start() {
76 static int visitCount = 0;
79 int netInitSampleRate = ObserverConfig::getNetInitSampleRate();
80 int netFollowupSampleRate = ObserverConfig::getNetFollowupSampleRate();
81 int netFollowupSampleCount = ObserverConfig::getNetFollowupSampleCount();
82 int operatorNetSampleRatio = ObserverConfig::getOpoeratorNetSampleRatio();
83 int skipIters = ObserverConfig::getSkipIters();
84 int sampleRate = visitCount > 0 ? netFollowupSampleRate : netInitSampleRate;
85 if (skipIters <= numRuns_ && sampleRate > 0 && rand() % sampleRate == 0) {
87 if (visitCount == netFollowupSampleCount) {
90 if (operatorNetSampleRatio > 0 && rand() % operatorNetSampleRatio == 0) {
91 logType_ = PerfNetObserver::OPERATOR_DELAY;
93 logType_ = PerfNetObserver::NET_DELAY;
96 logType_ = PerfNetObserver::NONE;
100 if (logType_ == PerfNetObserver::OPERATOR_DELAY) {
103 const auto& operators = subject_->GetOperators();
104 for (
auto* op : operators) {
105 observerMap_[op] = op->AttachObserver(
106 caffe2::make_unique<PerfOperatorObserver>(op,
this));
110 if (logType_ != PerfNetObserver::NONE) {
116 void PerfNetObserver::Stop() {
117 if (logType_ == PerfNetObserver::NONE) {
120 auto currentRunTime = timer_.MilliSeconds();
121 std::map<std::string, PerformanceInformation> info;
122 PerformanceInformation net_perf;
123 net_perf.latency = currentRunTime;
124 if (logType_ == PerfNetObserver::OPERATOR_DELAY) {
125 const auto& operators = subject_->GetOperators();
126 for (
int idx = 0; idx < operators.size(); ++idx) {
127 const auto* op = operators[idx];
128 auto name = getObserverName(op, idx);
129 PerformanceInformation p;
131 p.latency =
static_cast<const PerfOperatorObserver*
>(observerMap_[op])
134 p.engine = op->engine();
137 static_cast<const PerfOperatorObserver*
>(observerMap_[op])
140 if (op->has_debug_def()) {
141 for (
auto arg : op->debug_def().arg()) {
142 p.args.emplace_back(arg);
146 info.insert({name, p});
151 for (
auto* op : operators) {
152 op->DetachObserver(observerMap_[op]);
154 observerMap_.clear();
156 info.insert({
"NET_DELAY", net_perf});
157 ObserverConfig::getReporter()->report(subject_, info);
160 caffe2::string PerfNetObserver::getObserverName(
const OperatorBase* op,
int idx)
162 string opType = op->has_debug_def() ? op->debug_def().type() :
"NO_TYPE";
164 (op->has_debug_def() ? op->debug_def().name().size()
165 ? op->debug_def().name()
166 : (op->debug_def().output_size() ? op->debug_def().output(0)
169 caffe2::string name =
170 "ID_" + c10::to_string(idx) +
"_" + opType +
"_" + displayName;
174 PerfOperatorObserver::PerfOperatorObserver(
176 PerfNetObserver* netObserver)
177 : ObserverBase<OperatorBase>(op),
178 netObserver_(netObserver),
180 CAFFE_ENFORCE(netObserver_,
"Observers can't operate outside of the net");
183 PerfOperatorObserver::~PerfOperatorObserver() {}
185 void PerfOperatorObserver::Start() {
191 milliseconds_ = netObserver_->getTimer().MilliSeconds() - milliseconds_;
194 void PerfOperatorObserver::Stop() {
197 milliseconds_ = netObserver_->getTimer().MilliSeconds() - milliseconds_;
198 tensor_shapes_ = subject_->InputTensorShapes();
201 double PerfOperatorObserver::getMilliseconds()
const {
202 return milliseconds_;
205 std::vector<TensorShape> PerfOperatorObserver::getTensorShapes()
const {
206 return tensor_shapes_;
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...