layer.h 13.1 KB
Newer Older
J
Jiabin Yang 已提交
1
// Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
2 3 4 5 6 7 8 9 10 11 12 13 14 15
//
// 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
J
Jiabin Yang 已提交
16 17
#include <algorithm>
#include <atomic>
Z
Zeng Jinle 已提交
18
#include <cstdint>
J
Jiabin Yang 已提交
19
#include <list>
Z
Zeng Jinle 已提交
20 21 22 23
#include <map>     // NOLINT
#include <memory>  // NOLINT
#include <mutex>   // NOLINT
#include <set>
24
#include <string>         // NOLINT
M
minqiyang 已提交
25
#include <unordered_map>  // NOLINT
26
#include <utility>
J
Jiabin Yang 已提交
27
#include <vector>
28 29
#include "paddle/fluid/framework/op_desc.h"
#include "paddle/fluid/framework/operator.h"
M
minqiyang 已提交
30
#include "paddle/fluid/framework/var_type_inference.h"
J
Jiabin Yang 已提交
31
#include "paddle/fluid/framework/variable.h"
Z
Zeng Jinle 已提交
32
#include "paddle/fluid/imperative/flags.h"
J
Jiabin Yang 已提交
33 34 35
#include "paddle/fluid/imperative/type_defs.h"
#include "paddle/fluid/platform/enforce.h"
#include "paddle/fluid/platform/macros.h"
M
minqiyang 已提交
36

37 38 39 40 41
namespace paddle {
namespace imperative {

class OpBase;

Z
Zeng Jinle 已提交
42 43 44 45 46 47 48 49 50 51 52 53 54
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_;
};

55
class VarBase {
J
Jiabin Yang 已提交
56 57
  DISABLE_COPY_AND_ASSIGN(VarBase);

58
 public:
Z
Zeng Jinle 已提交
59
  static std::vector<std::string> AliveVarNames();
J
Jiabin Yang 已提交
60
  explicit VarBase(bool has_grad, const std::string& name)
61
      : name_(name),
J
Jiabin Yang 已提交
62
        grad_var_(has_grad ? new VarBase(false, GradVarName()) : nullptr) {
Z
Zeng Jinle 已提交
63
    if (IsDebugEnabled()) {
J
Jiabin Yang 已提交
64
      VLOG(10) << "Construct VarBase: " << name;
Z
Zeng Jinle 已提交
65 66
      name_set_.Insert(name_);
    }
67
  }
68

J
Jiabin Yang 已提交
69 70 71 72
  explicit VarBase(const std::string& name) : VarBase(true, name) {}

  ~VarBase() {
    VLOG(10) << "Destruct VarBase: " << name_;
Z
Zeng Jinle 已提交
73 74 75
    if (IsDebugEnabled()) {
      name_set_.Remove(name_);
    }
M
minqiyang 已提交
76
  }
77

J
Jiabin Yang 已提交
78
  const framework::Variable& Var() const { return var_; }
79

J
Jiabin Yang 已提交
80
  framework::Variable* MutableVar() { return &var_; }
M
minqiyang 已提交
81

J
Jiabin Yang 已提交
82 83 84 85 86 87 88
  bool HasGradVar() const { return grad_var_ != nullptr; }

  const std::shared_ptr<VarBase>& GradVarBase() const { return grad_var_; }

  const framework::Variable& GradVar() const {
    PADDLE_ENFORCE_NOT_NULL(grad_var_, "Gradient of %s does not exist", name_);
    return grad_var_->var_;
M
minqiyang 已提交
89
  }
M
minqiyang 已提交
90

J
Jiabin Yang 已提交
91 92 93 94
  framework::Variable* MutableGradVar() {
    PADDLE_ENFORCE_NOT_NULL(grad_var_, "Gradient of %s does not exist", name_);
    return &(grad_var_->var_);
  }
X
Xin Pan 已提交
95

96 97 98 99 100 101 102
  // This is used for python api
  void SetOverridedStopGradient(bool stop_gradient) {
    if (stop_gradient) {
      overrided_stop_gradient_ = 1;
    } else {
      overrided_stop_gradient_ = 0;
    }
J
Jiabin Yang 已提交
103
    if (grad_var_) {
104 105 106 107 108 109 110 111 112
      grad_var_->SetOverridedStopGradient(stop_gradient);
    }
  }
  // This is used for python api
  bool OverridedStopGradient() const {
    if (overrided_stop_gradient_ == 0) {
      return false;
    } else {
      return true;
113
    }
M
minqiyang 已提交
114
  }
X
Xin Pan 已提交
115

116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
  // This is used inside C++
  int InnerOverridedStopGradient() const { return overrided_stop_gradient_; }

  bool GradGenerated() const { return grad_generated_; }

  void SetGradGenerated(bool generated) { grad_generated_ = generated; }
  // This is used inside C++
  void InnerSetOverridedStopGradient(bool stop_gradient) {
    if (overrided_stop_gradient_ == -1) {
      overrided_stop_gradient_ = static_cast<int>(stop_gradient);
      if (grad_var_) {
        grad_var_->InnerSetOverridedStopGradient(stop_gradient);
      }
    } else {
      VLOG(6) << "Ignore Stop gradient conversion for Var: " << Name()
              << "Set value is: " << overrided_stop_gradient_;
    }
  }
134

J
Jiabin Yang 已提交
135
  void SetPersistable(bool persistable) { persistable_ = persistable; }
136

J
Jiabin Yang 已提交
137
  bool Persistable() const { return persistable_; }
138

J
Jiabin Yang 已提交
139
  void AddGradOps(const std::weak_ptr<OpBase>& op);
X
Xin Pan 已提交
140

J
Jiabin Yang 已提交
141 142 143 144 145 146
  std::vector<OpBase*> GradOps() {
    std::vector<OpBase*> rlt;
    // TODO(jiabin): use better data structure to remove nullptr when we find it
    for (const auto& wk_ptr : grad_ops_) {
      OpBase* tmp_op = wk_ptr.lock().get();
      if (tmp_op) rlt.emplace_back(tmp_op);
M
minqiyang 已提交
147
    }
J
Jiabin Yang 已提交
148
    return rlt;
X
Xin Pan 已提交
149
  }
J
Jiabin Yang 已提交
150
  void ClearGradOps() { grad_ops_.clear(); }
X
Xin Pan 已提交
151

J
Jiabin Yang 已提交
152
  const std::string& Name() const { return name_; }
M
minqiyang 已提交
153

J
Jiabin Yang 已提交
154 155 156 157 158
  void SetName(const std::string& name) {
    name_ = name;
    if (grad_var_) {
      grad_var_->SetName(GradVarName());
    }
M
minqiyang 已提交
159 160
  }

J
Jiabin Yang 已提交
161
  std::string GradVarName() { return framework::GradVarName(name_); }
162

J
Jiabin Yang 已提交
163
  void SetType(framework::proto::VarType::Type type) { type_ = type; }
164

J
Jiabin Yang 已提交
165
  framework::proto::VarType::Type Type() const { return type_; }
166

J
Jiabin Yang 已提交
167 168 169 170
  void SetDataType(framework::proto::VarType::Type data_type) {
    data_type_ = data_type;
    if (grad_var_) {
      grad_var_->SetDataType(data_type_);
171 172 173
    }
  }

J
Jiabin Yang 已提交
174
  framework::proto::VarType::Type DataType() const { return data_type_; }
X
polish  
Xin Pan 已提交
175

J
Jiabin Yang 已提交
176
  void ClearGradient();
X
Xin Pan 已提交
177

J
Jiabin Yang 已提交
178 179
  std::shared_ptr<VarBase> NewVarBase(const platform::Place& dst_place,
                                      const bool blocking) const;
M
minqiyang 已提交
180

J
Jiabin Yang 已提交
181 182 183 184 185
 private:
  framework::Variable var_;
  std::string name_;
  std::shared_ptr<VarBase> grad_var_;
  mutable size_t copied_counter_ = 0;
186

J
Jiabin Yang 已提交
187 188
  // grad_op indicates which grad_op will this var be used as input
  std::vector<std::weak_ptr<OpBase>> grad_ops_;
189 190 191 192 193
  // add this property for users may set stop_gradient themselves and this
  // should override the
  // frameworks setting (-1) unset, (1) true, (0) false
  int overrided_stop_gradient_{-1};
  bool grad_generated_{false};
J
Jiabin Yang 已提交
194
  bool persistable_{false};
M
minqiyang 已提交
195

J
Jiabin Yang 已提交
196 197 198
  framework::proto::VarType::Type type_{framework::proto::VarType::LOD_TENSOR};
  framework::proto::VarType::Type data_type_{framework::proto::VarType::FP32};
  static ThreadSafeNameSet name_set_;
199 200 201 202 203 204
};

class Layer {
 public:
  virtual ~Layer() {}

205 206
  virtual std::vector<std::shared_ptr<VarBase>> Forward(
      const std::vector<std::shared_ptr<VarBase>>& inputs) {
J
Jiabin Yang 已提交
207
    return {};
208
  }
X
Xin Pan 已提交
209
};
210

M
minqiyang 已提交
211
// infer var type context for imperative mode
J
Jiabin Yang 已提交
212
class RuntimeInferVarTypeContext : public framework::InferVarTypeContext {
M
minqiyang 已提交
213
 public:
J
Jiabin Yang 已提交
214 215 216
  RuntimeInferVarTypeContext(const NameVarBaseMap& inputs,
                             const NameVarBaseMap* outputs,
                             const framework::AttributeMap& attrs_map)
M
minqiyang 已提交
217 218 219 220 221 222 223
      : InferVarTypeContext(nullptr, nullptr),
        inputs_(inputs),
        outputs_(outputs),
        attrs_(attrs_map),
        input_names_(),
        output_names_(),
        var_set_() {
J
Jiabin Yang 已提交
224 225 226
    input_names_.reserve(inputs_.size());
    for (auto& it : inputs_) {
      for (auto& var : it.second) {
M
minqiyang 已提交
227
        input_names_[it.first].emplace_back(var->Name());
J
Jiabin Yang 已提交
228
        var_set_[var->Name()] = var.get();
M
minqiyang 已提交
229 230 231 232 233
      }
    }

    output_names_.reserve(outputs_->size());
    for (auto& it : *outputs_) {
J
Jiabin Yang 已提交
234
      for (auto& var : it.second) {
M
minqiyang 已提交
235
        output_names_[it.first].emplace_back(var->Name());
J
Jiabin Yang 已提交
236
        var_set_[var->Name()] = var.get();
M
minqiyang 已提交
237 238 239 240
      }
    }
  }

M
minqiyang 已提交
241 242 243
  virtual ~RuntimeInferVarTypeContext() {}

  framework::Attribute GetAttr(const std::string& name) const override {
J
Jiabin Yang 已提交
244 245 246 247
    auto iter = attrs_.find(name);
    PADDLE_ENFORCE_EQ(iter != attrs_.end(), true, "Cannot find attribute %s",
                      name);
    return iter->second;
M
minqiyang 已提交
248 249
  }

M
minqiyang 已提交
250
  bool HasVar(const std::string& name) const override {
M
minqiyang 已提交
251 252 253
    return var_set_.count(name) > 0;
  }

M
minqiyang 已提交
254
  bool HasInput(const std::string& name) const override {
J
Jiabin Yang 已提交
255
    return inputs_.count(name) > 0;
M
minqiyang 已提交
256 257
  }

M
minqiyang 已提交
258
  bool HasOutput(const std::string& name) const override {
M
minqiyang 已提交
259 260 261 262
    PADDLE_ENFORCE_NOT_NULL(outputs_);
    return outputs_->count(name) > 0;
  }

M
minqiyang 已提交
263 264
  const std::vector<std::string>& Input(
      const std::string& name) const override {
J
Jiabin Yang 已提交
265 266 267 268
    auto iter = input_names_.find(name);
    PADDLE_ENFORCE_EQ(iter != input_names_.end(), true, "Cannot find input %s",
                      name);
    return iter->second;
M
minqiyang 已提交
269 270
  }

M
minqiyang 已提交
271 272
  const std::vector<std::string>& Output(
      const std::string& name) const override {
J
Jiabin Yang 已提交
273 274 275 276
    auto iter = output_names_.find(name);
    PADDLE_ENFORCE_EQ(iter != output_names_.end(), true,
                      "Cannot find output %s", name);
    return iter->second;
M
minqiyang 已提交
277 278
  }

M
minqiyang 已提交
279 280
  framework::proto::VarType::Type GetType(
      const std::string& name) const override {
J
Jiabin Yang 已提交
281 282 283 284
    auto iter = var_set_.find(name);
    PADDLE_ENFORCE_EQ(iter != var_set_.end(), true,
                      "Cannot find var %s in GetType", name);
    return iter->second->Type();
M
minqiyang 已提交
285 286
  }

M
minqiyang 已提交
287 288
  void SetType(const std::string& name,
               framework::proto::VarType::Type type) override {
289 290 291 292 293
    if (name == "kLookupTablePath") {
      VLOG(2) << "SUPER UGLY FIX, remove this when move imperative mode in C++";
    } else {
      var_set_[name]->SetType(type);
    }
M
minqiyang 已提交
294 295
  }

M
minqiyang 已提交
296 297
  framework::proto::VarType::Type GetDataType(
      const std::string& name) const override {
J
Jiabin Yang 已提交
298 299 300 301
    auto iter = var_set_.find(name);
    PADDLE_ENFORCE_EQ(iter != var_set_.end(), true,
                      "Cannot find var %s in GetDataType", name);
    return iter->second->DataType();
M
minqiyang 已提交
302 303
  }

M
minqiyang 已提交
304 305
  void SetDataType(const std::string& name,
                   framework::proto::VarType::Type type) override {
M
minqiyang 已提交
306
    var_set_[name]->SetDataType(type);
M
minqiyang 已提交
307 308
  }

M
minqiyang 已提交
309 310
  std::vector<framework::proto::VarType::Type> GetDataTypes(
      const std::string& name) const override {
M
minqiyang 已提交
311 312 313
    PADDLE_THROW("GetDataTypes is not supported in runtime InferVarType");
  }

M
minqiyang 已提交
314 315 316
  void SetDataTypes(const std::string& name,
                    const std::vector<framework::proto::VarType::Type>&
                        multiple_data_type) override {
M
minqiyang 已提交
317 318 319
    PADDLE_THROW("SetDataTypes is not supported in runtime InferVarType");
  }

M
minqiyang 已提交
320
  std::vector<int64_t> GetShape(const std::string& name) const override {
M
minqiyang 已提交
321 322 323
    PADDLE_THROW("Do not handle Shape in runtime InferVarType");
  }

M
minqiyang 已提交
324 325
  void SetShape(const std::string& name,
                const std::vector<int64_t>& dims) override {
M
minqiyang 已提交
326 327 328
    PADDLE_THROW("Do not handle Shape in runtime InferVarType");
  }

M
minqiyang 已提交
329
  int32_t GetLoDLevel(const std::string& name) const override {
M
minqiyang 已提交
330 331 332
    PADDLE_THROW("Do not handle LoDLevel in runtime InferVarType");
  }

M
minqiyang 已提交
333
  void SetLoDLevel(const std::string& name, int32_t lod_level) override {
M
minqiyang 已提交
334 335 336 337
    PADDLE_THROW("Do not handle LoDLevel in runtime InferVarType");
  }

 private:
J
Jiabin Yang 已提交
338 339 340
  const NameVarBaseMap& inputs_;
  const NameVarBaseMap* outputs_;
  const framework::AttributeMap& attrs_;
M
minqiyang 已提交
341 342
  std::unordered_map<std::string, std::vector<std::string>> input_names_;
  std::unordered_map<std::string, std::vector<std::string>> output_names_;
J
Jiabin Yang 已提交
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
  std::unordered_map<std::string, VarBase*> var_set_;
};

// TODO(zjl): to support py_func layer
class OpBase : public std::enable_shared_from_this<OpBase> {
  DISABLE_COPY_AND_ASSIGN(OpBase);

 public:
  ~OpBase() { VLOG(3) << "Destruct Op: " << Type() << std::endl; }

  // Developer should not rely on this method to create OpBase.
  // OpBase should be created in Tracer and managed by Tracer totally.
  template <typename... Args>
  static std::shared_ptr<OpBase> Create(Args&&... args) {
    return std::shared_ptr<OpBase>(new OpBase(std::forward<Args>(args)...));
  }

  size_t id() const { return id_; }

  const std::string& Type() const { return op_->Type(); }

  void Run(const NameVarBaseMap& ins, const NameVarBaseMap& outs);

  const framework::VariableNameMap& InputNameMap() const {
    return op_->Inputs();
  }

  const framework::VariableNameMap& OutputNameMap() const {
    return op_->Outputs();
  }

  const framework::AttributeMap& Attrs() const { return op_->Attrs(); }
  const framework::OpInfo& Info() const { return op_->Info(); }

  void ClearBackwardTrace();

  const std::vector<OpBase*>& GradPendingOps() const {
    return grad_pending_ops_;
  }

  void InsertGradPendingOps(OpBase* op) { grad_pending_ops_.emplace_back(op); }

  void SortGradPendingOps() {
    std::sort(grad_pending_ops_.begin(), grad_pending_ops_.end(),
              [](OpBase* op1, OpBase* op2) { return op1->id() > op2->id(); });
  }
  NameVarBaseMap* GetMutableOutsMap() { return &outs_; }
  NameVarBaseMap* GetMutableInsMap() { return &ins_; }
  const NameVarBaseMap& GetInsMap() { return ins_; }
  const NameVarBaseMap& GetOutsMap() { return outs_; }
  const platform::Place& place() const { return place_; }

  // TODO(jiabin) prepare for backward hook
  void RegisterBackwardHooks(const std::function<void()>& func) {
    backward_hooks_.emplace_back(func);
  }

  void InvokeBackwardHooks() {
    for (const auto& func : backward_hooks_) {
      func();
      VLOG(5) << "Invoke Backward Hook for: " << Type() << std::endl;
    }
  }

 private:
  OpBase(size_t id, const std::string& type, const NameVarBaseMap& ins,
         const NameVarBaseMap& outs, framework::AttributeMap attrs,
         const platform::Place& place);

  OpBase(size_t id, const framework::OpDesc& op_desc,
         const platform::Place& place);

  size_t id_;

  std::unique_ptr<framework::OperatorBase> op_;

  std::vector<std::function<void()>> backward_hooks_;
  platform::Place place_;

  // Not need to be std::weak_ptr, because op is binded to a certain Tracer,
  // and would not be used by a Tracer that does not create itself.
  std::vector<OpBase*> grad_pending_ops_;

  // This part is only used for backward
  NameVarBaseMap ins_;
  NameVarBaseMap outs_;
429 430 431 432
};

}  // namespace imperative
}  // namespace paddle