forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
profiler_edge.cpp
126 lines (117 loc) · 4.12 KB
/
profiler_edge.cpp
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
124
125
126
#include <c10/util/Exception.h>
#include <torch/csrc/jit/mobile/profiler_edge.h>
#include <string>
#include <vector>
namespace torch {
namespace jit {
namespace mobile {
thread_local KinetoEdgeCPUProfiler* tls_edge_profiler{nullptr};
KinetoEdgeCPUProfiler::KinetoEdgeCPUProfiler(
const torch::jit::mobile::Module& m,
const std::string& fname,
const bool report_input_shapes,
const bool profile_memory,
const bool with_stack,
const bool with_flops,
const bool with_modules)
: m_(m), trace_file_name_(fname) {
torch::profiler::impl::ProfilerConfig config(
torch::profiler::impl::ProfilerState::KINETO,
report_input_shapes,
profile_memory,
with_stack,
with_flops,
with_modules);
torch::autograd::profiler::prepareProfiler(
config, {torch::autograd::profiler::ActivityType::CPU});
if (with_modules || with_stack) {
auto post_processing =
[this, with_stack, with_modules](
std::vector<torch::autograd::profiler::KinetoEvent>& events) {
std::string no_debug_info(
"Model was not saved with debug information");
for (auto& e : events) {
if (with_modules) {
// Since KinetoEvents's module hierarchy takes vector of strings
// we just construct a temporary vector using one string element
if (this->m_.hasDebugHandles()) {
e.moduleHierarchy(std::vector<std::string>(
{this->m_.getModuleHierarchy(e.debugHandle())}));
} else {
e.moduleHierarchy(std::vector<std::string>({no_debug_info}));
}
} else if (with_stack) {
// Since KinetoEvents's stack trace takes vector of strings we
// just construct a temporary vector using one string element
if (this->m_.hasDebugHandles()) {
e.stack(std::vector<std::string>(
{this->m_.getCallStack(e.debugHandle())}));
} else {
e.stack(std::vector<std::string>({no_debug_info}));
}
}
}
};
torch::autograd::profiler::enableProfilerWithEventPostProcess(
config,
{torch::autograd::profiler::ActivityType::CPU},
post_processing,
{at::RecordScope::LITE_INTERPRETER});
} else {
torch::autograd::profiler::enableProfiler(
config,
{torch::autograd::profiler::ActivityType::CPU},
{at::RecordScope::LITE_INTERPRETER});
}
trace_file_name_ = fname;
TORCH_CHECK(
tls_edge_profiler == nullptr, "Edge profiler is already profiling.")
tls_edge_profiler = this;
}
void KinetoEdgeCPUProfiler::recordBackendEvent(
const int64_t start_time_us,
const int64_t end_time_us,
const int64_t debug_handle,
const std::string& event_name,
const std::string& backend_name) {
torch::autograd::profiler::reportBackendEventToActiveKinetoProfiler(
start_time_us,
end_time_us,
debug_handle,
at::RecordScope::LITE_INTERPRETER,
event_name,
backend_name);
}
const std::unique_ptr<torch::autograd::profiler::ProfilerResult>&
KinetoEdgeCPUProfiler::disableProfiler() {
TORCH_CHECK(
!profiler_result_,
"KinetoEdgeCPUProfiler already disabled. "
"To get list of events use getProfilerResults()");
profiler_result_ = torch::autograd::profiler::disableProfiler();
return profiler_result_;
}
const std::unique_ptr<torch::autograd::profiler::ProfilerResult>&
KinetoEdgeCPUProfiler::getProfilerResult() {
TORCH_CHECK(
profiler_result_,
"KinetoEdgeCPUProfiler has not been disabled. "
"use disableProfiler() API first, which returns the ProfilerResult.");
return profiler_result_;
}
KinetoEdgeCPUProfiler::~KinetoEdgeCPUProfiler() {
if (!trace_file_name_.empty()) {
if (profiler_result_) {
profiler_result_->save(trace_file_name_);
} else {
torch::autograd::profiler::disableProfiler()->save(trace_file_name_);
}
}
tls_edge_profiler = nullptr;
}
KinetoEdgeCPUProfiler* getCurrentEdgeProfiler() {
return tls_edge_profiler;
}
} // namespace mobile
} // namespace jit
} // namespace torch