block_desc.h 3.4 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
F
fengjiayi 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

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

F
fengjiayi 已提交
17
#include <deque>
Y
Fix CI  
Yu Yang 已提交
18
#include <memory>
Y
Yu Yang 已提交
19
#include <set>
L
Luo Tao 已提交
20
#include <string>
F
fengjiayi 已提交
21 22
#include <unordered_map>
#include <vector>
Y
Yu Yang 已提交
23

Y
Yi Wang 已提交
24 25 26 27
#include "paddle/fluid/framework/op_desc.h"
#include "paddle/fluid/framework/proto_desc.h"
#include "paddle/fluid/framework/var_desc.h"
#include "paddle/fluid/platform/macros.h"
F
fengjiayi 已提交
28 29 30 31

namespace paddle {
namespace framework {

W
wanghuancoder 已提交
32
class OpDesc;
33
class ProgramDesc;
W
wanghuancoder 已提交
34
class VarDesc;
F
fengjiayi 已提交
35 36 37 38 39

// Each Protobuf Message, we provide a XXXBind class. In that class, we optimize
// read/write speed. Only when we want the protobuf message, the local changes
// will be synchronized (by `Sync` method).

Y
Yu Yang 已提交
40
class BlockDesc {
F
fengjiayi 已提交
41
 public:
Y
Yu Yang 已提交
42
  BlockDesc(ProgramDesc *prog, proto::BlockDesc *desc);
F
fengjiayi 已提交
43

Y
Yu Yang 已提交
44
  BlockDesc(const BlockDesc &other, proto::BlockDesc *desc, ProgramDesc *prog);
Y
Yu Yang 已提交
45

F
fengjiayi 已提交
46 47 48 49
  int32_t ID() const { return desc_->idx(); }

  int32_t Parent() const { return desc_->parent_idx(); }

Y
Yu Yang 已提交
50 51
  int32_t ForwardBlockID() const { return desc_->forward_block_idx(); }

Y
Yu Yang 已提交
52
  VarDesc *Var(const std::string &name_bytes);
F
fengjiayi 已提交
53

Y
Yu Yang 已提交
54
  VarDesc *FindVar(const std::string &name_bytes) const;
F
fengjiayi 已提交
55

Q
qiaolongfei 已提交
56 57
  bool HasVar(const std::string &var_name) const;

T
wip  
typhoonzero 已提交
58
  VarDesc *RenameVar(const std::string &old_name, const std::string &new_name);
T
typhoonzero 已提交
59

Y
Yu Yang 已提交
60
  VarDesc *FindVarRecursive(const std::string &name_bytes) const;
61

Y
Yang Yu 已提交
62
  VarDesc &FindRecursiveOrCreateVar(const std::string &name_bytes);
Y
Yang Yang(Tony) 已提交
63

64 65
  bool HasVarRecursive(const std::string &var_name) const;

Y
Yu Yang 已提交
66 67 68 69 70 71 72 73
  std::set<std::string> LocalVarNames() const {
    std::set<std::string> var_names;
    for (auto &var : vars_) {
      var_names.insert(var.first);
    }
    return var_names;
  }

Y
Yu Yang 已提交
74
  std::vector<VarDesc *> AllVars() const;
F
fengjiayi 已提交
75

Y
Yu Yang 已提交
76
  BlockDesc *ParentBlock() const;
F
fengjiayi 已提交
77

Y
Yu Yang 已提交
78 79 80 81
  BlockDesc *ForwardBlock() const;

  void SetForwardBlockID(int32_t forward_block_id);

Y
Yu Yang 已提交
82
  OpDesc *AppendOp();
F
fengjiayi 已提交
83

Y
Yu Yang 已提交
84
  void AppendAllocatedOp(std::unique_ptr<OpDesc> &&op_desc);
85

Y
Yu Yang 已提交
86
  OpDesc *PrependOp();
F
fengjiayi 已提交
87

Y
Yao Cheng 已提交
88
  void PrependAllocatedOp(std::unique_ptr<OpDesc> &&op_desc);
Y
Yao Cheng 已提交
89

90 91
  OpDesc *InsertOp(size_t index);

L
Luo Tao 已提交
92
  /*
F
fengjiayi 已提交
93 94
   * Only remove op itself,
   * do nothing to its input and output variables
L
Luo Tao 已提交
95
   */
T
typhoonzero 已提交
96 97
  void RemoveOp(size_t s, size_t e);

98 99
  void RemoveOpInternal(const OpDesc *op_desc);

L
Luo Tao 已提交
100 101
  void RemoveVar(const std::string &name) { vars_.erase(name); }

Y
Yu Yang 已提交
102
  std::vector<OpDesc *> AllOps() const;
F
fengjiayi 已提交
103

104 105
  size_t OpSize() const { return ops_.size(); }

F
fengjiayi 已提交
106
  OpDesc *Op(int idx) const { return ops_.at(idx).get(); }
107

108
  void Flush();
F
fengjiayi 已提交
109

110
  proto::BlockDesc *Proto();
F
fengjiayi 已提交
111

Y
Yu Yang 已提交
112
  ProgramDesc *Program() const { return this->prog_; }
Y
Yu Yang 已提交
113

114 115
  void MoveFrom(BlockDesc *block);

L
Leo Chen 已提交
116 117
  bool NeedUpdate(bool include_subs = true);

118
 private:
Y
Yu Yang 已提交
119
  ProgramDesc *prog_;       // not_own
120
  proto::BlockDesc *desc_;  // not_own
L
Leo Chen 已提交
121 122
  bool need_update_;  // block itself need_update, not aware of its ops_ and
                      // vars_
F
fengjiayi 已提交
123

Y
Yu Yang 已提交
124
  std::deque<std::unique_ptr<OpDesc>> ops_;
L
Leo Chen 已提交
125
  std::map<std::string, std::unique_ptr<VarDesc>> vars_;
D
format  
dongzhihong 已提交
126

Y
Yu Yang 已提交
127
  DISABLE_COPY_AND_ASSIGN(BlockDesc);
F
fengjiayi 已提交
128
};
F
fengjiayi 已提交
129 130
}  // namespace framework
}  // namespace paddle