forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprofile_observer.cc
82 lines (72 loc) · 2.88 KB
/
profile_observer.cc
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
/**
* Copyright (c) 2016-present, Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "profile_observer.h"
#include "caffe2/core/logging.h"
namespace caffe2 {
void ProfileOperatorObserver::Dump() const {
static std::mutex loggingMutex;
std::lock_guard<std::mutex> lock(loggingMutex);
LOG(INFO) << "--------- Starting operator " << subject_->debug_def().type()
<< " op#" << getId() << " ---------";
for (int i = 0; i < subject_->InputSize(); ++i) {
if (subject_->InputIsTensorType(i, CPU)) {
const auto& tensor = subject_->Input<Tensor>(i, CPU);
const auto& name = subject_->debug_def().input(i);
TensorPrinter printer(name);
LOG(INFO) << "Input " << i << ": " << printer.MetaStr(tensor);
} else if (subject_->InputIsTensorType(i, CUDA)) {
const auto& tensor = subject_->Input<Tensor>(i, CUDA);
const auto& name = subject_->debug_def().input(i);
TensorPrinter printer(name);
LOG(INFO) << "Input " << i << ": " << printer.MetaStr(tensor);
}
}
int a = 0;
for (const auto& arg : subject_->debug_def().arg()) {
LOG(INFO) << "Argument " << a << ": " << arg.ShortDebugString();
++a;
}
for (int o = 0; o < subject_->OutputSize(); ++o) {
if (subject_->OutputIsTensorType(o, CPU)) {
auto* tensor = subject_->Output<Tensor>(o, CPU);
const auto& name = subject_->debug_def().output(o);
TensorPrinter printer(name);
LOG(INFO) << "Output " << o << ": " << printer.MetaStr(*tensor);
} else if (subject_->OutputIsTensorType(o, CUDA)) {
auto* tensor = subject_->Output<Tensor>(o, CUDA);
const auto& name = subject_->debug_def().output(o);
TensorPrinter printer(name);
LOG(INFO) << "Output " << o << ": " << printer.MetaStr(*tensor);
}
}
LOG(INFO) << "--------- Finished operator " << subject_->debug_def().type()
<< " in " << run_time_ << " ms ---------";
}
void ProfileOperatorObserver::Start() {
start_time_ = timer_.MilliSeconds();
}
void ProfileOperatorObserver::Stop() {
run_time_ = timer_.MilliSeconds() - start_time_;
Dump();
}
std::unique_ptr<ObserverBase<OperatorBase>> ProfileOperatorObserver::rnnCopy(
OperatorBase* subject,
int rnn_order) const {
return std::unique_ptr<ObserverBase<OperatorBase>>(
new ProfileOperatorObserver(
subject, netObserver_, net_position_, rnn_order));
}
} // namespace caffe2