-
Notifications
You must be signed in to change notification settings - Fork 657
/
pipeline_buffer_pass.cpp
329 lines (290 loc) · 14.9 KB
/
pipeline_buffer_pass.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
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/*
Copyright 2020 The OneFlow Authors. All rights reserved.
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 "oneflow/core/common/maybe.h"
#include "oneflow/core/job_rewriter/job_pass.h"
#include "oneflow/core/job/job.pb.h"
#include "oneflow/core/job/scope.h"
#include "oneflow/core/job/scope.cfg.h"
#include "oneflow/core/job_rewriter/calculation_pass.h"
#include "oneflow/core/vm/symbol_storage.h"
#include "oneflow/core/framework/framework.h"
#include "oneflow/core/operator/operator.h"
namespace oneflow {
namespace {
class PipelineBufferPass final : public JobPass {
public:
OF_DISALLOW_COPY_AND_MOVE(PipelineBufferPass);
PipelineBufferPass() = default;
~PipelineBufferPass() = default;
Maybe<void> Apply(Job* job, JobPassCtx* ctx) const override {
if (!IsEnabled(*ctx)) { return Maybe<void>::Ok(); }
const OpGraph op_graph(*job);
JobBuilder job_builder(job);
return Apply(op_graph, &job_builder);
}
bool IsEnabled(const JobPassCtx& ctx) const { return ctx.job_desc().IsTrain(); }
Maybe<void> Apply(const OpGraph& op_graph, JobBuilder* job_builder) const;
};
const std::string kBufferOpNamePrefix = "System-Pipeline-Buffer-Op_";
const Scope& Scope4ScopeSymbolId(int64_t scope_symbol_id) {
CHECK(Global<symbol::Storage<Scope>>::Get()->Has(scope_symbol_id));
return Global<symbol::Storage<Scope>>::Get()->Get(scope_symbol_id);
}
const Scope& Scope4OpNode(const OpNode* op_node) {
const OperatorConf& op_conf = op_node->op().op_conf();
CHECK(op_conf.has_scope_symbol_id());
return Scope4ScopeSymbolId(op_conf.scope_symbol_id());
}
bool IsForwardPass(const OpNode* node) {
return Scope4OpNode(node).scope_proto().calculation_pass_name() == kForwardPass;
}
bool IsBackwardPass(const OpNode* node) {
return Scope4OpNode(node).scope_proto().calculation_pass_name() == kBackwardPass;
}
bool OpNodeHasScope(const OpNode* node) { return node->op().op_conf().has_scope_symbol_id(); }
bool IsIdentityBufferOrRepeatOpNode(const OpNode* node) {
const OperatorConf& op_conf = node->op().op_conf();
if (op_conf.has_user_conf()) {
const std::string& op_type_name = op_conf.user_conf().op_type_name();
if (op_type_name == "identity_buffer" || op_type_name == "repeat") { return true; }
}
return false;
}
int64_t GetStageIdHint(const OpNode* node) {
return Scope4OpNode(node).Int64("pipeline_stage_id_hint");
}
void TryInsertOrUseBufferOpToDstNode(
const OpEdge* op_edge, const int64_t buffer_size,
HashMap<std::string, OperatorConf>* buffer_op_name2op_conf,
HashMap<std::string, ParallelConf>* buffer_op_name2parallel_conf,
HashMap<std::string, OperatorConf>* mut_op_name2conf) {
const OpNode* src_node = op_edge->src_node();
const OpNode* dst_node = op_edge->dst_node();
const int64_t src_stage_id = GetStageIdHint(src_node);
const int64_t dst_stage_id = GetStageIdHint(dst_node);
const std::string& dst_op_name = dst_node->op().op_name();
const int64_t stage_id = GetStageIdHint(dst_node);
for (const LogicalBlobId& lbi : op_edge->lbis()) {
std::string lbn = GenLogicalBlobName(lbi);
std::string buffer_op_name = kBufferOpNamePrefix + "-" + lbi.op_name() + "-" + lbi.blob_name()
+ "-stage_id_" + std::to_string(stage_id);
auto it = buffer_op_name2op_conf->find(buffer_op_name);
if (it == buffer_op_name2op_conf->end()) {
it = buffer_op_name2op_conf
->emplace(buffer_op_name,
user_op::UserOpConfWrapperBuilder(buffer_op_name)
.Op("identity_buffer")
.Input("in", lbn)
.Output("out")
.Attr<int64_t>("buffer_size", buffer_size)
.ScopeSymbolId(dst_node->op().op_conf().scope_symbol_id())
.Build()
.op_conf())
.first;
CHECK(buffer_op_name2parallel_conf
->emplace(buffer_op_name, dst_node->parallel_desc().parallel_conf())
.second);
LOG(INFO) << "\n Insert buffer op : [" << buffer_op_name << "](buffer_size:" << buffer_size
<< ") \n from [" << src_node->op().op_name()
<< "] (stage_id:" << std::to_string(src_stage_id) << ") -> ["
<< dst_node->op().op_name() << "] (stage_id:" << std::to_string(dst_stage_id)
<< ") \n";
}
auto mut_op_it = mut_op_name2conf->find(dst_op_name);
if (mut_op_it == mut_op_name2conf->end()) {
mut_op_it = mut_op_name2conf->emplace(dst_op_name, dst_node->op().op_conf()).first;
}
const std::string buffer_out = user_op::UserOpConfWrapper(it->second).output("out", 0);
for (const std::string& ibn : op_edge->lbi2ibns().at(lbi)) {
std::string old_lbn =
ReplaceInputLbnInOpCustomizedConf(&(mut_op_it->second), ibn, buffer_out);
CHECK_EQ(old_lbn, lbn);
}
}
}
void TryInsertOrUseBufferOpBothSrcDst(
const OpEdge* op_edge, const int64_t src_buffer_size, const int64_t dst_buffer_size,
HashMap<std::string, OperatorConf>* buffer_op_name2op_conf,
HashMap<std::string, ParallelConf>* buffer_op_name2parallel_conf,
HashMap<std::string, OperatorConf>* mut_op_name2conf) {
const OpNode* src_node = op_edge->src_node();
const OpNode* dst_node = op_edge->dst_node();
const ParallelDesc& src_parallel_desc = src_node->parallel_desc();
const ParallelDesc& dst_parallel_desc = dst_node->parallel_desc();
CHECK(!src_parallel_desc.EqualsIgnoringHierarchy(dst_parallel_desc));
const std::string& dst_op_name = dst_node->op().op_name();
const int64_t src_stage_id = GetStageIdHint(src_node);
const int64_t dst_stage_id = GetStageIdHint(dst_node);
CHECK_NE(src_stage_id, dst_stage_id);
CHECK_GE(src_buffer_size, 1);
CHECK_GE(dst_buffer_size, 1);
for (const LogicalBlobId& lbi : op_edge->lbis()) {
std::string lbn = GenLogicalBlobName(lbi);
std::string src_buffer_op_name =
kBufferOpNamePrefix + "-" + lbi.op_name() + "-" + lbi.blob_name();
std::string dst_buffer_op_name = kBufferOpNamePrefix + "-" + lbi.op_name() + "-"
+ lbi.blob_name() + "-stage_id_"
+ std::to_string(dst_stage_id);
auto src_buffer_it = buffer_op_name2op_conf->find(src_buffer_op_name);
if (src_buffer_it == buffer_op_name2op_conf->end()) {
src_buffer_it = buffer_op_name2op_conf
->emplace(src_buffer_op_name,
user_op::UserOpConfWrapperBuilder(src_buffer_op_name)
.Op("identity_buffer")
.Input("in", lbn)
.Output("out")
.Attr<int64_t>("buffer_size", src_buffer_size)
.ScopeSymbolId(src_node->op().op_conf().scope_symbol_id())
.Build()
.op_conf())
.first;
CHECK(buffer_op_name2parallel_conf
->emplace(src_buffer_op_name, src_parallel_desc.parallel_conf())
.second);
}
const OperatorConf& src_conf = src_buffer_it->second;
const std::string src_buffer_out = user_op::UserOpConfWrapper(src_conf).output("out", 0);
auto dst_buffer_it = buffer_op_name2op_conf->find(dst_buffer_op_name);
if (dst_buffer_it == buffer_op_name2op_conf->end()) {
dst_buffer_it = buffer_op_name2op_conf
->emplace(dst_buffer_op_name,
user_op::UserOpConfWrapperBuilder(dst_buffer_op_name)
.Op("identity_buffer")
.Input("in", src_buffer_out)
.Output("out")
.Attr<int64_t>("buffer_size", dst_buffer_size)
.ScopeSymbolId(dst_node->op().op_conf().scope_symbol_id())
.Build()
.op_conf())
.first;
CHECK(buffer_op_name2parallel_conf
->emplace(dst_buffer_op_name, dst_parallel_desc.parallel_conf())
.second);
}
const OperatorConf& dst_conf = dst_buffer_it->second;
auto mut_op_it = mut_op_name2conf->find(dst_op_name);
if (mut_op_it == mut_op_name2conf->end()) {
mut_op_it = mut_op_name2conf->emplace(dst_op_name, dst_node->op().op_conf()).first;
}
LOG(INFO) << "\n Insert buffer op pair : src_buffer = <" << src_buffer_op_name
<< ">(buffer_size:" << src_buffer_size << ") , dst_buffer = <" << dst_buffer_op_name
<< ">(buffer_size:" << dst_buffer_size << ") \n from [" << src_node->op().op_name()
<< "] (stage_id:" << std::to_string(src_stage_id) << ") -> ["
<< dst_node->op().op_name() << "] (stage_id:" << std::to_string(dst_stage_id)
<< ") \n";
const std::string dst_buffer_out = user_op::UserOpConfWrapper(dst_conf).output("out", 0);
for (const std::string& ibn : op_edge->lbi2ibns().at(lbi)) {
std::string old_lbn =
ReplaceInputLbnInOpCustomizedConf(&(mut_op_it->second), ibn, dst_buffer_out);
CHECK_EQ(old_lbn, lbn);
}
}
}
Maybe<void> PipelineBufferPass::Apply(const OpGraph& op_graph, JobBuilder* job_builder) const {
if (GlobalJobDesc().job_conf().num_gradient_accumulation_steps() <= 1) {
return Maybe<void>::Ok();
}
int64_t max_stage_id = 0;
op_graph.ForEachNode([&](const OpNode* this_node) {
if (!OpNodeHasScope(this_node)) {
LOG(WARNING) << " op : " << this_node->op().op_conf().DebugString() << " has NOT scope!";
return;
}
max_stage_id = std::max(max_stage_id, GetStageIdHint(this_node));
});
if (max_stage_id == 0) { return Maybe<void>::Ok(); }
const int64_t total_stage_num = max_stage_id + 1;
LOG(INFO) << "total stage num = " << total_stage_num;
HashMap<std::string, OperatorConf> buffer_op_name2op_conf;
HashMap<std::string, ParallelConf> buffer_op_name2parallel_conf;
HashMap<std::string, OperatorConf> mut_op_name2conf;
op_graph.ForEachNode([&](const OpNode* this_node) {
if (!OpNodeHasScope(this_node)) { return; /* ignore op without scope */ }
if (!IsBackwardPass(this_node)) { return; /* ignore fw dst op */ }
for (const OpEdge* in_edge : this_node->in_edges()) {
const OpNode* src_node = in_edge->src_node();
if (!OpNodeHasScope(src_node)) { continue; /* ignore op without scope */ }
const int64_t src_stage_id = GetStageIdHint(src_node);
const int64_t dst_stage_id = GetStageIdHint(this_node);
if (IsForwardPass(src_node) && (!IsIdentityBufferOrRepeatOpNode(src_node))) {
if (dst_stage_id == max_stage_id) {
continue; /* last stage(loss) does NOT need to insert buffer */
}
if (src_stage_id != dst_stage_id) {
LOG(WARNING) << " Cross diff stage link From: [" << src_node->op().op_conf().DebugString()
<< "](stage_id:" << std::to_string(src_stage_id) << ") -> ["
<< this_node->op().op_conf().DebugString()
<< "](stage_id:" << std::to_string(dst_stage_id) << ")\n";
}
const int64_t buffer_size = total_stage_num * 2; /* NOTE(chengcheng): max buffer size */
TryInsertOrUseBufferOpToDstNode(in_edge, buffer_size, &buffer_op_name2op_conf,
&buffer_op_name2parallel_conf, &mut_op_name2conf);
}
}
for (const std::string& ctrl_in_op_name : this_node->op().op_conf().ctrl_in_op_name()) {
const OpNode* src_node = op_graph.OpNode4OpName(ctrl_in_op_name);
if (!OpNodeHasScope(src_node)) { continue; /* ignore op without scope */ }
if (IsForwardPass(src_node)) {
LOG(WARNING) << "CtrlEdge: src_op[FwPass]: " << src_node->op().op_conf().DebugString()
<< " dst_op[BwPass]: " << this_node->op().op_conf().DebugString()
<< " connected.";
}
}
});
op_graph.ForEachEdge([&](const OpEdge* edge) {
const OpNode* src_node = edge->src_node();
const OpNode* dst_node = edge->dst_node();
if (OpNodeHasScope(src_node) && OpNodeHasScope(dst_node) && IsForwardPass(src_node)
&& IsForwardPass(dst_node)) {
const int64_t src_stage_id = GetStageIdHint(src_node);
const int64_t dst_stage_id = GetStageIdHint(dst_node);
if (src_node->parallel_desc().device_type() == DeviceType::kCPU
&& dst_node->parallel_desc().device_type() == DeviceType::kGPU) {
if (src_stage_id == 0 && (dst_stage_id == max_stage_id || dst_stage_id == 0)) {
TryInsertOrUseBufferOpToDstNode(edge, total_stage_num * 2, &buffer_op_name2op_conf,
&buffer_op_name2parallel_conf, &mut_op_name2conf);
return;
}
}
if (src_stage_id < dst_stage_id) {
/* NOTE(chengcheng): We insert double buffer between src / dst node.
* src_buffer_size = 1 because we need free memory as early as possible so we can overlap
* CopyD2H with Compute.
* dst_buffer_size = dst_stage_id - src_stage_id for pipeline.
*/
const int64_t dst_buffer_size = dst_stage_id - src_stage_id;
TryInsertOrUseBufferOpBothSrcDst(edge, 1, dst_buffer_size, &buffer_op_name2op_conf,
&buffer_op_name2parallel_conf, &mut_op_name2conf);
}
}
if (OpNodeHasScope(src_node) && OpNodeHasScope(dst_node) && IsBackwardPass(src_node)
&& IsBackwardPass(dst_node)) {
const int64_t src_stage_id = GetStageIdHint(src_node);
const int64_t dst_stage_id = GetStageIdHint(dst_node);
// NOTE(chengcheng): Backward ONLY need buffer size 1.
if (src_stage_id > dst_stage_id) {
TryInsertOrUseBufferOpBothSrcDst(edge, 1, 1, &buffer_op_name2op_conf,
&buffer_op_name2parallel_conf, &mut_op_name2conf);
}
}
});
for (auto& pair : buffer_op_name2op_conf) {
CHECK(buffer_op_name2parallel_conf.find(pair.first) != buffer_op_name2parallel_conf.end());
JUST(job_builder->AddOp(buffer_op_name2parallel_conf.at(pair.first), pair.second));
}
for (auto& pair : mut_op_name2conf) { JUST(job_builder->MutOpOnlyOnce(pair.second)); }
return Maybe<void>::Ok();
}
} // namespace
REGISTER_JOB_PASS("PipelineBufferPass", PipelineBufferPass);
} // namespace oneflow