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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
/**
* 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 "caffe2/core/logging.h"
#include "profile_observer.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_->InputIsType<TensorCPU>(i)) {
const auto& tensor = subject_->Input<TensorCPU>(i);
const auto& name = subject_->debug_def().input(i);
TensorPrinter printer(name);
LOG(INFO) << "Input " << i << ": " << printer.MetaStr(tensor);
} else if (subject_->InputIsType<TensorCUDA>(i)) {
const auto& tensor = subject_->Input<TensorCUDA>(i);
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_->OutputIsType<TensorCPU>(o)) {
auto* tensor = subject_->Output<TensorCPU>(o);
const auto& name = subject_->debug_def().output(o);
TensorPrinter printer(name);
LOG(INFO) << "Output " << o << ": " << printer.MetaStr(*tensor);
} else if (subject_->OutputIsType<TensorCUDA>(o)) {
auto* tensor = subject_->Output<TensorCUDA>(o);
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() {
auto cudaOp = dynamic_cast_if_rtti<const Operator<CUDAContext>*>(subject_);
if (cudaOp) {
auto context = cudaOp->getContext();
int device;
cudaGetDevice(&device);
cudaSetDevice(context->cuda_gpu_id());
cudaEventCreate(&start_);
cudaEventRecord(start_, context->cuda_stream());
cudaSetDevice(device);
cudaError_t error = cudaGetLastError();
if (error != cudaSuccess) {
CAFFE_THROW("Encountered CUDA error Start: ", cudaGetErrorString(error));
}
} else {
start_time_ = timer_.MilliSeconds();
}
}
void ProfileOperatorObserver::Stop() {
auto cudaOp = dynamic_cast_if_rtti<const Operator<CUDAContext>*>(subject_);
if (cudaOp) {
auto context = cudaOp->getContext();
int device;
cudaGetDevice(&device);
cudaSetDevice(context->cuda_gpu_id());
cudaEventCreate(&stop_);
cudaEventRecord(stop_, context->cuda_stream());
cudaEventSynchronize(stop_);
cudaEventElapsedTime(&run_time_, start_, stop_);
cudaEventDestroy(start_);
cudaEventDestroy(stop_);
cudaSetDevice(device);
cudaError_t error = cudaGetLastError();
if (error != cudaSuccess) {
CAFFE_THROW("Encountered CUDA error Stop: ", cudaGetErrorString(error));
}
} else {
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
|