forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
profiler_kineto.h
264 lines (207 loc) · 5.59 KB
/
profiler_kineto.h
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#pragma once
#include <torch/csrc/autograd/profiler_legacy.h>
#ifdef USE_KINETO
// skip Kineto dependency on mobile
#ifdef C10_MOBILE
#undef USE_KINETO
#endif
#endif
#ifdef USE_KINETO
namespace libkineto {
struct TraceActivity;
class ActivityTraceInterface;
}
#endif
namespace torch {
namespace autograd {
namespace profiler {
enum class C10_API_ENUM ActivityType {
CPU = 0,
CUDA, // CUDA kernels, runtime
NUM_KINETO_ACTIVITIES, // must be the last one
};
#ifdef USE_KINETO
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init)
struct KinetoObserverContext : public at::ObserverContext {
int64_t startUs;
uint64_t correlationId;
uint64_t startThreadId;
uint64_t endThreadId;
c10::optional<std::vector<std::vector<int64_t>>> shapes;
c10::optional<std::vector<std::string>> dtypes;
int64_t sequenceNr;
uint64_t fwdThreadId;
uint8_t recFunScope;
c10::optional<std::vector<std::string>> stack;
// Extra arguments for computing op flops
c10::optional<std::unordered_map<std::string, c10::IValue>> extraArgs;
CUDAEventStub cuda_event_start_ = nullptr;
CUDAEventStub cuda_event_end_ = nullptr;
};
struct TORCH_API KinetoEvent {
uint64_t startThreadId() const {
return start_thread_id_;
}
uint64_t endThreadId() const {
return end_thread_id_;
}
uint8_t activityType() const {
return activity_type_;
}
uint64_t fwdThreadId() const {
return fwd_thread_id_;
}
bool hasShapes() const {
return shapes_ != c10::nullopt;
}
const std::vector<std::vector<int64_t>>& shapes() const {
return *shapes_;
}
bool hasTypes() const {
return dtypes_ != c10::nullopt;
}
const std::vector<std::string>& dtypes() const {
return *dtypes_;
}
uint64_t flops() const {
return flops_;
}
int64_t sequenceNr() const {
return sequence_nr_;
}
bool hasStack() const {
return stack_ != c10::nullopt;
}
const std::vector<std::string>& stack() const {
return *stack_;
}
uint8_t scope() const {
return scope_;
}
KinetoEvent& startThreadId(uint64_t start_thread_id) {
start_thread_id_ = start_thread_id;
return *this;
}
KinetoEvent& endThreadId(uint64_t end_thread_id) {
end_thread_id_ = end_thread_id;
return *this;
}
KinetoEvent& fwdThreadId(uint64_t fwd_thread_id) {
fwd_thread_id_ = fwd_thread_id;
return *this;
}
KinetoEvent& shapes(const std::vector<std::vector<int64_t>>& shapes) {
shapes_ = shapes;
return *this;
}
KinetoEvent& dtypes(const std::vector<std::string>& dtypes) {
dtypes_ = dtypes;
return *this;
}
KinetoEvent& flops(uint64_t flops) {
flops_ = flops;
return *this;
}
KinetoEvent& sequenceNr(int64_t sequence_nr) {
sequence_nr_ = sequence_nr;
return *this;
}
KinetoEvent& stack(const std::vector<std::string>& st) {
stack_ = st;
return *this;
}
KinetoEvent& scope(uint8_t scope) {
scope_ = scope;
return *this;
}
KinetoEvent& setAsync(bool is_async) {
is_async_ = is_async;
return *this;
}
// Kineto fields
KinetoEvent& activity(const libkineto::TraceActivity& activity);
std::string name() const {
return name_;
}
bool isAsync() const {
return is_async_;
}
uint64_t deviceIndex() const {
return device_index_;
}
uint64_t startUs() const {
return start_us_;
}
uint64_t durationUs() const {
return duration_us_;
}
uint64_t correlationId() const {
return correlation_id_;
}
KinetoEvent& correlationId(uint64_t correlation_id) {
correlation_id_ = correlation_id;
return *this;
}
uint64_t linkedCorrelationId() const {
return linked_correlation_id_;
}
int64_t deviceResourceId() const {
return device_resource_id_;
}
c10::DeviceType deviceType() const;
int64_t cudaElapsedUs() const;
uint64_t start_thread_id_ = 0;
uint64_t end_thread_id_ = 0;
uint64_t fwd_thread_id_ = 0;
int64_t sequence_nr_ = -1;
uint8_t scope_ = 0;
uint8_t activity_type_ = 0;
c10::optional<std::vector<std::vector<int64_t>>> shapes_;
c10::optional<std::vector<std::string>> stack_;
c10::optional<std::vector<std::string>> dtypes_;
uint64_t flops_ = 0;
std::string name_;
uint64_t device_index_ = 0;
uint64_t start_us_ = 0;
uint64_t duration_us_ = 0;
uint64_t correlation_id_ = 0;
uint64_t linked_correlation_id_ = 0;
int64_t device_resource_id_ = 0;
bool is_async_{false};
CUDAEventStub cuda_event_start_ = nullptr;
CUDAEventStub cuda_event_end_ = nullptr;
};
// Consolidating events returned directly from Kineto
// with events manually created by us (e.g. start/stop marks,
// memory allocation events)
struct TORCH_API ProfilerResult {
ProfilerResult(
std::vector<KinetoEvent> events,
thread_event_lists legacy_events,
std::unique_ptr<libkineto::ActivityTraceInterface> trace);
~ProfilerResult();
const std::vector<KinetoEvent>& events() const {
return events_;
}
const thread_event_lists& legacy_events() const {
return legacy_events_;
}
void save(const std::string& path);
private:
bool saved_ = false;
std::vector<KinetoEvent> events_;
thread_event_lists legacy_events_;
std::unique_ptr<libkineto::ActivityTraceInterface> trace_;
};
TORCH_API void enableProfiler(
const ProfilerConfig& config,
const std::set<ActivityType>& activities);
TORCH_API std::unique_ptr<ProfilerResult> disableProfiler();
TORCH_API void prepareProfiler(
const ProfilerConfig& config,
const std::set<ActivityType>& activities);
TORCH_API void addMetadataJson(
const std::string& key, const std::string& value);
#endif // USE_KINETO
} // namespace profiler
}} // namespace torch::autograd