forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecord_function.cpp
202 lines (167 loc) · 4.47 KB
/
record_function.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
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
#include <torch/csrc/autograd/record_function.h>
#include <torch/csrc/autograd/function.h>
#include <torch/csrc/utils/memory.h>
#include <cstdlib>
#include <random>
namespace torch { namespace autograd { namespace profiler {
namespace {
class CallbackManager {
public:
void setSamplingProbability(double prob) {
if (prob == 1.0) {
sampling_prop_set = false;
} else {
TORCH_CHECK(prob >= 0.0 && prob < 1.0);
sampling_prop_set = true;
}
sampling_prob = prob;
}
double getSamplingProbability() {
return sampling_prob;
}
bool shouldRunSampledCallbacks() {
return (num_sampled_callbacks > 0) &&
(!sampling_prop_set ||
(sample_zero_one() < sampling_prob));
}
void pushCallback(
RecordFunctionCallback start,
RecordFunctionCallback end,
bool needs_inputs,
bool sampled) {
start_callbacks.push_back(std::move(start));
end_callbacks.push_back(std::move(end));
if (callback_needs_inputs > 0 || needs_inputs) {
++callback_needs_inputs;
}
is_callback_sampled.push_back(sampled);
if (sampled) {
++num_sampled_callbacks;
}
}
void popCallback() {
if (start_callbacks.empty()) {
throw std::runtime_error("Empty callbacks stack");
}
start_callbacks.pop_back();
end_callbacks.pop_back();
if (callback_needs_inputs > 0) {
--callback_needs_inputs;
}
if (is_callback_sampled.back()) {
--num_sampled_callbacks;
}
is_callback_sampled.pop_back();
}
bool hasCallbacks() {
return !start_callbacks.empty();
}
bool needsInputs() {
return callback_needs_inputs > 0;
}
bool hasNonSampledCallbacks() {
return num_sampled_callbacks < start_callbacks.size();
}
std::vector<RecordFunctionCallback> start_callbacks;
std::vector<RecordFunctionCallback> end_callbacks;
std::vector<bool> is_callback_sampled;
size_t num_sampled_callbacks = 0;
size_t callback_needs_inputs = 0;
bool sampling_prop_set = false;
double sampling_prob = 1.0;
static double sample_zero_one() {
static thread_local auto gen =
torch::make_unique<std::mt19937>(std::random_device()());
std::uniform_real_distribution<double> dist(0.0, 1.0);
return dist(*gen);
}
};
thread_local RecordFunction* thread_local_func_ = nullptr;
CallbackManager& manager() {
static CallbackManager instance;
return instance;
}
} // namespace
void setSamplingProbability(double prob) {
manager().setSamplingProbability(prob);
}
double getSamplingProbability() {
return manager().getSamplingProbability();
}
bool shouldRunSampledCallbacks() {
return manager().shouldRunSampledCallbacks();
}
void pushCallback(
RecordFunctionCallback start,
RecordFunctionCallback end,
bool needs_inputs,
bool sampled) {
manager().pushCallback(
std::move(start),
std::move(end),
needs_inputs,
sampled);
}
void popCallback() {
manager().popCallback();
}
bool hasCallbacks() {
return manager().hasCallbacks();
}
bool needsInputs() {
return manager().needsInputs();
}
bool hasNonSampledCallbacks() {
return manager().hasNonSampledCallbacks();
}
void RecordFunction::before(const char* name, int64_t sequence_nr) {
if (!hasCallbacks()) {
return;
}
AT_ASSERT(!initialized_);
name_ = StringView(name);
sequence_nr_ = sequence_nr;
initialized_ = true;
processCallbacks();
}
void RecordFunction::before(std::string name, int64_t sequence_nr) {
if (!hasCallbacks()) {
return;
}
AT_ASSERT(!initialized_);
name_ = StringView(std::move(name));
sequence_nr_ = sequence_nr;
initialized_ = true;
processCallbacks();
}
void RecordFunction::before(Node* fn, int64_t sequence_nr) {
if (!hasCallbacks()) {
return;
}
AT_ASSERT(!initialized_);
fn_ = fn;
name_ = StringView(fn->name());
sequence_nr_ = (sequence_nr >= 0) ? sequence_nr : fn->sequence_nr();
initialized_ = true;
processCallbacks();
}
void RecordFunction::processCallbacks() {
parent_ = thread_local_func_;
thread_local_func_ = this;
for (size_t idx = 0; idx < manager().start_callbacks.size(); ++idx) {
if (!manager().is_callback_sampled[idx] || run_sampled_) {
manager().start_callbacks[idx](*this);
}
}
}
RecordFunction::~RecordFunction() {
if (initialized_) {
for (size_t idx = 0; idx < manager().end_callbacks.size(); ++idx) {
if (!manager().is_callback_sampled[idx] || run_sampled_) {
manager().end_callbacks[idx](*this);
}
}
thread_local_func_ = parent_;
}
}
}}}