-
Notifications
You must be signed in to change notification settings - Fork 5.7k
/
Copy pathexecutor_gc_helper.cc
352 lines (314 loc) · 12.5 KB
/
executor_gc_helper.cc
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
// Copyright (c) 2019 PaddlePaddle 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 "paddle/fluid/framework/executor_gc_helper.h"
#include <string>
#include "glog/logging.h"
#include "paddle/fluid/framework/block_desc.h"
#include "paddle/fluid/framework/no_need_buffer_vars_inference.h"
#include "paddle/fluid/framework/op_info.h"
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/framework/operator.h"
#include "paddle/fluid/framework/var_desc.h"
#include "paddle/fluid/operators/controlflow/conditional_block_op_helper.h"
#include "paddle/fluid/operators/controlflow/pylayer_op_helper.h"
#include "paddle/fluid/operators/controlflow/while_op_helper.h"
#include "paddle/fluid/platform/enforce.h"
namespace paddle::framework {
void OpInOutInfo::Build(const OperatorBase *op) {
is_built_ = true;
auto &inferer = op->Info().NoNeedBufferVarsInferer();
if (inferer) {
no_need_buffer_ins_ = inferer(op->Inputs(), op->Outputs(), op->Attrs());
if (no_need_buffer_ins_.empty()) return;
for (auto &in_name_pair : op->Inputs()) {
if (no_need_buffer_ins_.count(in_name_pair.first) != 0) {
continue;
}
for (auto &in_arg_name : in_name_pair.second) {
other_args_set_.insert(in_arg_name);
}
}
for (auto &out_name_pair : op->Outputs()) {
for (auto &out_arg_name : out_name_pair.second) {
other_args_set_.insert(out_arg_name);
}
}
}
}
bool OpInOutInfo::IsInArgBufferNeeded(const std::string &in_arg_name) const {
return no_need_buffer_ins_.empty() || other_args_set_.count(in_arg_name) != 0;
}
static bool VarCanBeDeleted(const std::string &name,
const BlockDesc &block,
const std::unordered_set<std::string> &skip_vars,
const std::multiset<std::string> *unpersist_vars) {
if (skip_vars.count(name) != 0) {
return false;
}
auto *var_desc = block.FindVar(name);
if (var_desc == nullptr || var_desc->Persistable()) {
if (unpersist_vars != nullptr) {
// unpersist vars
if (unpersist_vars->find(name) == unpersist_vars->end()) {
return false;
}
} else {
return false;
}
}
auto type = var_desc->Proto()->type().type();
return type == proto::VarType::DENSE_TENSOR ||
type == proto::VarType::SELECTED_ROWS ||
type == proto::VarType::DENSE_TENSOR_ARRAY;
}
std::unordered_map<const OperatorBase *, std::vector<std::string>>
GetUnusedVars(const BlockDesc &block,
const std::vector<std::unique_ptr<OperatorBase>> &ops,
const std::vector<std::string> &skip_var_list,
const std::multiset<std::string> *unpersist_vars,
bool is_shard_for_thread_mode) {
std::unordered_set<std::string> skip_vars(skip_var_list.begin(),
skip_var_list.end());
std::unordered_map<std::string, size_t> var_op_idx_map;
std::unordered_map<std::string, std::string> old_to_new;
std::unordered_map<std::string, std::string> new_to_old;
for (size_t i = 0; i < ops.size(); ++i) {
auto *op = ops[i].get();
OpInOutInfo info;
for (auto &name_pair : op->Inputs()) {
for (auto &name : name_pair.second) {
if (!VarCanBeDeleted(name, block, skip_vars, unpersist_vars)) {
continue;
}
bool is_unpersist_var = false;
if (is_shard_for_thread_mode) {
if (unpersist_vars->find(name) != unpersist_vars->end()) {
is_unpersist_var = true;
if (op->Type() == std::string("c_broadcast")) {
auto it = old_to_new.find(name);
if (it == old_to_new.end()) {
old_to_new[name] = name;
new_to_old[name] = name;
} else {
std::string new_name = it->second + std::string("_");
old_to_new[name] = new_name;
new_to_old[new_name] = name;
}
}
}
}
// var can be gc-ed
if (!info.IsBuilt()) {
info.Build(op);
}
if (info.IsInArgBufferNeeded(name)) {
// Update the last living op of variable to current op
if (is_unpersist_var && old_to_new.count(name) > 0) {
var_op_idx_map[old_to_new[name]] = i;
} else {
var_op_idx_map[name] = i;
}
} else {
VLOG(10) << "Skip reference count computing of variable "
<< name_pair.first << "(" << name << ") in Operator "
<< op->Type();
}
}
}
for (auto &name_pair : op->Outputs()) {
for (auto &name : name_pair.second) {
if (VarCanBeDeleted(name, block, skip_vars, unpersist_vars)) {
// Update the last living op of variable to current op
if (is_shard_for_thread_mode && old_to_new.count(name) > 0) {
var_op_idx_map[old_to_new[name]] = i;
} else {
var_op_idx_map[name] = i;
}
}
}
}
}
std::unordered_map<const OperatorBase *, std::vector<std::string>> result;
for (auto &name_op_idx_pair : var_op_idx_map) {
auto &name = name_op_idx_pair.first;
size_t op_idx = name_op_idx_pair.second;
if (is_shard_for_thread_mode && new_to_old.count(name) > 0) {
result[ops[op_idx].get()].emplace_back(new_to_old[name]);
} else {
result[ops[op_idx].get()].emplace_back(name);
}
}
return result;
}
void DeleteUnusedTensors(const Scope &scope,
const std::vector<std::string> &delete_vars,
GarbageCollector *gc) {
std::deque<std::shared_ptr<memory::Allocation>> garbages;
for (auto &var_name : delete_vars) {
auto *var = scope.FindVar(var_name);
if (var == nullptr) {
continue;
}
VLOG(2) << "Erase variable " << var_name;
if (var->IsType<phi::DenseTensor>()) {
garbages.emplace_back(
var->GetMutable<phi::DenseTensor>()->MoveMemoryHolder());
} else if (var->IsType<phi::SelectedRows>()) {
garbages.emplace_back(var->GetMutable<phi::SelectedRows>()
->mutable_value()
->MoveMemoryHolder());
} else if (var->IsType<phi::TensorArray>()) {
auto *dense_tensor_arr = var->GetMutable<phi::TensorArray>();
for (auto &t : *dense_tensor_arr) {
garbages.emplace_back(t.MoveMemoryHolder());
}
// NOTE(wangxi): need clear the vector, otherwise dense_tensor_arr.size()
// is wrong, if size() decrease in next step, an error maybe occur.
dense_tensor_arr->clear();
} else if (var->IsType<Strings>()) {
} else {
PADDLE_THROW(common::errors::Unimplemented(
"Type %s of variable %s is not supported eager deletion.",
framework::ToTypeName(var->Type()),
var_name));
}
}
if (!garbages.empty()) {
gc->Add(std::move(garbages));
}
}
void DeleteUnusedTensors(
const Scope &scope,
const OperatorBase *op,
const std::unordered_map<const OperatorBase *, std::vector<std::string>>
&delete_vars_map,
GarbageCollector *gc) {
auto iter = delete_vars_map.find(op);
if (iter == delete_vars_map.end()) {
return;
}
auto &delete_vars = iter->second;
DeleteUnusedTensors(scope, delete_vars, gc);
}
static std::vector<std::unique_ptr<OperatorBase>> CreateOpsFromBlock(
const BlockDesc &block) {
std::vector<std::unique_ptr<OperatorBase>> ops;
size_t op_num = block.OpSize();
ops.reserve(op_num);
for (size_t i = 0; i < op_num; ++i) {
auto *op_desc = block.Op(static_cast<int>(i));
ops.push_back(OpRegistry::CreateOp(*op_desc));
}
return ops;
}
std::vector<std::vector<std::vector<std::string>>> GetEagerDeletionCleanVars(
const ProgramDesc &program, const std::vector<std::string> &skip_vars) {
return GetEagerDeletionCleanVarsForPartial(program, skip_vars, false);
}
std::vector<std::vector<std::vector<std::string>>>
GetEagerDeletionCleanVarsForPartial(const ProgramDesc &origin_program,
const std::vector<std::string> &skip_vars,
const bool &for_partial_block) {
ProgramDesc program{origin_program};
size_t block_num = program.Size();
PADDLE_ENFORCE_GE(block_num,
1,
common::errors::PermissionDenied(
"Program should have at least one block"));
// Note(zhangbo): For dygraph2static inplace policy, origin_program is a
// partial program(only include forward or backward), and control flow op's
// attr skip_eager_deletion_vars has been updated at graph->program before
// calling this function.
if (!for_partial_block) {
// prepare safe GCs on sub block ops
auto global_block_ops = CreateOpsFromBlock(program.Block(0));
operators::PrepareSafeEagerDeletionOnConditionalOpAndConditionalGradOp(
program, 0, global_block_ops);
operators::PrepareSafeEagerDeletionOnPyLayerOpAndPyLayerGradOp(
program, 0, global_block_ops);
operators::PrepareSafeEagerDeletionOnWhileOpAndWhileGradOp(
program, 0, global_block_ops);
}
// find the skip vars on each block
std::vector<std::vector<std::string>> skip_vars_on_each_block(block_num);
skip_vars_on_each_block[0] = skip_vars;
std::vector<bool> found_skip_vars(block_num, false);
found_skip_vars[0] = true;
const char *kSubBlock = "sub_block";
const char *kSkipEagerDeletionVars = "skip_eager_deletion_vars";
// NOTE: pylayer op contains may contain two blocks: forward block and
// backward block
const char *kBlocks = "blocks";
for (size_t i = 0; i < block_num; ++i) {
const auto &block = program.Block(i);
size_t op_num = block.OpSize();
for (size_t j = 0; j < op_num; ++j) {
auto *op = block.Op(static_cast<int>(j));
if ((!op->HasAttr(kSubBlock) && !op->HasAttr(kBlocks)) ||
!op->HasAttr(kSkipEagerDeletionVars)) {
continue;
}
std::vector<int32_t> sub_block_ids;
if (op->HasAttr(kSubBlock)) {
sub_block_ids.push_back(
op->GetAttrIfExists<BlockDesc *>(kSubBlock)->ID());
} else if (op->HasAttr(kBlocks)) {
const auto &blocks =
op->GetAttrIfExists<std::vector<BlockDesc *>>(kBlocks);
for (const auto &block : blocks) {
sub_block_ids.push_back(block->ID());
}
}
for (auto sub_block_id : sub_block_ids) {
PADDLE_ENFORCE_GE(sub_block_id,
0,
common::errors::PermissionDenied(
"sub_block id must be non-negative number"));
PADDLE_ENFORCE_LT(sub_block_id,
block_num,
common::errors::PermissionDenied(
"sub_block id exceeds max block num"));
PADDLE_ENFORCE_EQ(
found_skip_vars[sub_block_id],
false,
common::errors::PermissionDenied(
"there are 2 ops which refer to the same sub_block %d",
sub_block_id));
found_skip_vars[sub_block_id] = true;
auto sub_block_skip_vars =
op->GetAttrIfExists<std::vector<std::string>>(
kSkipEagerDeletionVars);
skip_vars_on_each_block[sub_block_id] = std::move(sub_block_skip_vars);
}
}
}
std::vector<std::vector<std::vector<std::string>>> result;
result.reserve(block_num);
for (size_t i = 0; i < block_num; ++i) {
const auto &block = program.Block(i);
const auto block_ops = CreateOpsFromBlock(block);
const auto &block_skip_vars = skip_vars_on_each_block[i];
auto delete_var_map = GetUnusedVars(block, block_ops, block_skip_vars);
std::vector<std::vector<std::string>> block_result;
block_result.reserve(block_ops.size());
for (const auto &op : block_ops) {
auto &delete_vars = delete_var_map[op.get()];
std::sort(delete_vars.begin(), delete_vars.end()); // for stable result
block_result.emplace_back(delete_vars);
}
result.emplace_back(std::move(block_result));
}
return result;
}
} // namespace paddle::framework