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
// 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.
#pragma once
#include <algorithm>
#include <cstdint>
#include <list>
#include <map>
#include <memory>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#include "paddle/fluid/framework/operator.h"
#include "paddle/fluid/framework/type_defs.h"
#include "paddle/fluid/framework/var_type.h"
#include "paddle/fluid/framework/variable.h"
#include "paddle/fluid/imperative/flags.h"
#include "paddle/fluid/imperative/hooks.h"
#include "paddle/fluid/imperative/saved_variable_wrapper_list.h"
#include "paddle/fluid/imperative/type_defs.h"
#include "paddle/fluid/imperative/variable_wrapper.h"
#include "paddle/fluid/platform/enforce.h"
#include "paddle/fluid/platform/macros.h"
#include "paddle/pten/include/core.h"
namespace paddle {
namespace framework {
class Variable;
} // namespace framework
} // namespace paddle
namespace paddle {
namespace imperative {
class GradOpNode;
class OpBase;
class VariableWrapper;
class ThreadSafeNameSet {
public:
void Insert(const std::string& name);
void Remove(const std::string& name);
std::vector<std::string> Names() const;
private:
std::multiset<std::string> set_;
mutable std::mutex mtx_;
};
class VarBase {
DISABLE_COPY_AND_ASSIGN(VarBase);
public:
static std::vector<std::string> AliveVarNames();
public:
explicit VarBase(bool has_grad, const std::string& name)
: var_(std::make_shared<VariableWrapper>(name)),
grad_var_(has_grad ? new VarBase(false, GradVarName()) : nullptr) {
if (has_grad) {
var_->SetGradVar(grad_var_->var_);
}
if (IsDebugEnabled()) {
VLOG(10) << "Construct VarBase: " << Name();
name_set_.Insert(Name());
}
}
explicit VarBase(const std::string& name) : VarBase(true, name) {}
// NOTE(zengjinle): be careful when you use this constructor!!!
// Unpack VarBase from VariableWrapper.
explicit VarBase(const std::shared_ptr<VariableWrapper>& var);
~VarBase() {
VLOG(10) << "Destruct VarBase: " << Name();
if (IsDebugEnabled()) {
name_set_.Remove(Name());
}
}
const std::shared_ptr<VariableWrapper>& SharedVar() const { return var_; }
const framework::Variable& Var() const { return var_->Var(); }
framework::Variable* MutableVar() { return var_->MutableVar(); }
bool HasGradVar() const { return grad_var_ != nullptr; }
const std::shared_ptr<VarBase>& GradVarBase() const { return grad_var_; }
void ClearGradVarBase() { grad_var_ = nullptr; }
void SetGradVarBase(const VarBase& grad_var) {
MutableGradVarBase()->CopyFrom(grad_var, true);
MutableGradVarBase()->SharedVar()->SetIsEmpty(false);
}
const std::shared_ptr<VarBase>& MutableGradVarBase() {
if (grad_var_ == nullptr) {
if (auto grad_var_wrapper = var_->GetGradVar()) {
grad_var_ = std::make_shared<VarBase>(grad_var_wrapper);
} else {
grad_var_ = std::make_shared<VarBase>(false, GradVarName());
var_->SetGradVar(grad_var_->var_);
grad_var_->var_->SetGradNode(grad_var_->grad_node_);
}
// NOTE(zhiqiu): we should keep grad_var_'s stop_gradient property
// same as fwd varbase
grad_var_->SetOverridedStopGradient(var_->InnerOverridedStopGradient());
}
return grad_var_;
}
const framework::Variable& GradVar() const {
PADDLE_ENFORCE_NOT_NULL(
grad_var_,
platform::errors::NotFound("Gradient of %s does not exist", Name()));
return grad_var_->Var();
}
framework::Variable* MutableGradVar() {
PADDLE_ENFORCE_NOT_NULL(
grad_var_,
platform::errors::NotFound("Gradient of %s does not exist", Name()));
return grad_var_->MutableVar();
}
bool IsLeaf() const { return var_->IsLeaf(); }
void SetOverridedStopGradient(bool stop_gradient) {
var_->SetOverridedStopGradient(stop_gradient);
if (grad_var_) {
grad_var_->SetOverridedStopGradient(stop_gradient);
}
}
bool OverridedStopGradient() const { return var_->OverridedStopGradient(); }
void InnerSetOverridedStopGradient(bool stop_gradient) {
if (InnerOverridedStopGradient() == -1) {
var_->InnerSetOverridedStopGradient(stop_gradient);
if (grad_var_) {
grad_var_->InnerSetOverridedStopGradient(stop_gradient);
}
}
}
int InnerOverridedStopGradient() const {
return var_->InnerOverridedStopGradient();
}
void SetPersistable(bool persistable) { var_->SetPersistable(persistable); }
bool Persistable() const { return var_->Persistable(); }
// Only grad var is allowed to call these 2 methods
void SetGradNode(const std::shared_ptr<GradOpNode>& node) {
grad_node_ = node;
var_->SetGradNode(node);
}
size_t GradOpNum() const;
const std::shared_ptr<GradOpNode>& GradNode() const { return grad_node_; }
void ClearGradNode() { SetGradNode(nullptr); }
const std::string& Name() const { return var_->Name(); }
void SetName(const std::string& name) {
var_->SetName(name);
if (grad_var_) {
grad_var_->SetName(GradVarName());
}
}
std::string GradVarName() { return framework::GradVarName(Name()); }
void SetGraphIsFreed(bool free) { graph_is_free_ = free; }
const bool& GraphIsFreed() const { return graph_is_free_; }
void SetType(framework::proto::VarType::Type type) { var_->SetType(type); }
framework::proto::VarType::Type Type() const { return var_->Type(); }
void SetDataType(framework::proto::VarType::Type data_type) {
var_->SetDataType(data_type);
if (grad_var_) {
grad_var_->SetDataType(data_type);
}
}
framework::proto::VarType::Type DataType() const { return var_->DataType(); }
void SetForwardDataType(framework::proto::VarType::Type data_type) {
var_->SetForwardDataType(data_type);
}
framework::proto::VarType::Type ForwardDataType() const {
return var_->ForwardDataType();
}
const platform::Place Place() const { return var_->Place(); }
void ClearGradient();
std::shared_ptr<VarBase> NewVarBase(const platform::Place& dst_place,
const bool blocking) const;
void CopyFrom(const imperative::VarBase& src, bool blocking);
void BumpInplaceVersion();
void _CopyGradientFrom(const imperative::VarBase& src);
/* Hook related method: now only used for GradVarBase */
bool HasVariableWrapperHook() const { return var_->HasVariableWrapperHook(); }
int64_t AddVariableWrapperHook(std::shared_ptr<VariableWrapperHook>&& hook) {
return var_->AddVariableWrapperHook(
std::forward<std::shared_ptr<VariableWrapperHook>>(hook));
}
bool RemoveVariableWrapperHook(const int64_t& hook_id) {
return var_->RemoveVariableWrapperHook(hook_id);
}
const std::map<int64_t, std::shared_ptr<VariableWrapperHook>>&
GetVariableWrapperHooks() const {
return var_->GetVariableWrapperHooks();
}
void AddVoidHook(std::shared_ptr<std::function<void()>>&& hook) {
var_->AddVoidHook(
std::forward<std::shared_ptr<std::function<void()>>>(hook));
}
private:
/**
* NOTE(zengjinle): never remove the const qualifier of `var_` if you are
* not very familiar with the autograd idea (including the higher order
* derivative).
*/
const std::shared_ptr<VariableWrapper> var_;
std::shared_ptr<VarBase> grad_var_;
/**
* NOTE(zengjinle): should consider whether to implement an inlined vector
* or other things like that.
*/
std::shared_ptr<GradOpNode> grad_node_;
bool graph_is_free_ = false;
mutable size_t copied_counter_ = 0;
static ThreadSafeNameSet name_set_;
};
class Layer {
public:
virtual ~Layer() {}
virtual std::vector<std::shared_ptr<VarBase>> Forward(
const std::vector<std::shared_ptr<VarBase>>& inputs) {
return {};
}
};
std::shared_ptr<GradOpNode> CreateGradOpNode(
const framework::OperatorBase& op, const NameVarBaseMap& ins,
const NameVarBaseMap& outs, const framework::AttributeMap& attrs,
const framework::AttributeMap& default_attrs, const platform::Place& place,
const std::map<std::string, std::string>& inplace_map);
void ClearNoNeedBufferInputs(OpBase* op);
} // namespace imperative
} // namespace paddle