block_desc.cc 6.2 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

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. */

Y
Yi Wang 已提交
15
#include "paddle/fluid/framework/block_desc.h"
M
minqiyang 已提交
16

17
#include <queue>
M
minqiyang 已提交
18 19 20
#include <unordered_set>
#include <utility>

Y
Yi Wang 已提交
21 22
#include "paddle/fluid/framework/operator.h"
#include "paddle/fluid/framework/program_desc.h"
F
fengjiayi 已提交
23 24 25 26

namespace paddle {
namespace framework {

Y
Yu Yang 已提交
27
VarDesc *BlockDesc::Var(const std::string &name) {
F
fengjiayi 已提交
28
  auto it = vars_.find(name);
D
Dong Zhihong 已提交
29
  if (it != vars_.end()) {
D
Dong Zhihong 已提交
30
    return it->second.get();
D
Dong Zhihong 已提交
31
  }
32
  need_update_ = true;
Y
Yu Yang 已提交
33
  auto *var = new VarDesc(name);
F
fengjiayi 已提交
34 35 36 37
  vars_[name].reset(var);
  return var;
}

Y
Yu Yang 已提交
38
VarDesc *BlockDesc::FindVar(const std::string &name) const {
F
fengjiayi 已提交
39
  auto it = vars_.find(name);
D
Dong Zhihong 已提交
40 41 42
  if (it == vars_.end()) {
    return nullptr;
  }
F
fengjiayi 已提交
43 44 45
  return it->second.get();
}

Y
Yu Yang 已提交
46
bool BlockDesc::HasVar(const std::string &name) const {
Q
qiaolongfei 已提交
47
  return vars_.find(name) != vars_.end();
Q
qiaolongfei 已提交
48 49
}

T
wip  
typhoonzero 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62 63
VarDesc *BlockDesc::RenameVar(const std::string &old_name,
                              const std::string &new_name) {
  if (!this->HasVar(old_name)) {
    return nullptr;
  }
  need_update_ = true;
  auto *var = this->Var(old_name);
  VarDesc *new_var = new VarDesc(*(var->Proto()));
  new_var->SetName(new_name);
  vars_[new_name].reset(new_var);
  // rename inputs and outputs
  for (const auto &op : ops_) {
    auto *it = op.get();
    it->Rename(old_name, new_name);
T
typhoonzero 已提交
64
  }
T
wip  
typhoonzero 已提交
65 66
  vars_.erase(old_name);
  return new_var;
T
typhoonzero 已提交
67 68
}

Y
Yu Yang 已提交
69
VarDesc *BlockDesc::FindVarRecursive(const std::string &name) const {
70 71
  if (name == kEmptyVarName) return nullptr;

Y
Yu Yang 已提交
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
  std::queue<const BlockDesc *> frontier;
  std::unordered_set<const BlockDesc *> visited;

  frontier.push(this);

  while (!frontier.empty()) {  // BFS
    auto cur = frontier.front();
    frontier.pop();
    if (visited.count(cur) != 0) {
      continue;
    }
    auto var = cur->FindVar(name);
    if (var != nullptr) {
      return var;
    }

    auto fwd = cur->ForwardBlock();
    auto parent = cur->ParentBlock();

    if (fwd != nullptr) {
      frontier.push(fwd);
    }
    if (parent != nullptr) {
      frontier.push(parent);
    }

    visited.insert(cur);
99
  }
Y
Yu Yang 已提交
100 101

  return nullptr;
102 103
}

Y
Yang Yu 已提交
104
VarDesc &BlockDesc::FindRecursiveOrCreateVar(const std::string &name_bytes) {
Y
Yu Yang 已提交
105
  VarDesc *res = FindVarRecursive(name_bytes);
Y
Yang Yang(Tony) 已提交
106 107 108
  if (res == nullptr) {
    res = Var(name_bytes);
  }
Y
Yang Yu 已提交
109
  return *res;
Y
Yang Yang(Tony) 已提交
110 111
}

Y
Yu Yang 已提交
112
bool BlockDesc::HasVarRecursive(const std::string &name) const {
113 114 115
  return FindVarRecursive(name) != nullptr;
}

Y
Yu Yang 已提交
116 117
std::vector<VarDesc *> BlockDesc::AllVars() const {
  std::vector<VarDesc *> res;
F
fengjiayi 已提交
118 119 120 121 122 123
  for (const auto &p : vars_) {
    res.push_back(p.second.get());
  }
  return res;
}

Y
Yu Yang 已提交
124
OpDesc *BlockDesc::AppendOp() {
F
fengjiayi 已提交
125
  need_update_ = true;
126
  ops_.emplace_back(new OpDesc(this));
F
fengjiayi 已提交
127 128 129
  return ops_.back().get();
}

Y
Yu Yang 已提交
130
void BlockDesc::AppendAllocatedOp(std::unique_ptr<OpDesc> &&op_desc) {
131 132 133 134
  need_update_ = true;
  ops_.emplace_back(std::move(op_desc));
}

Y
Yu Yang 已提交
135
OpDesc *BlockDesc::PrependOp() {
F
fengjiayi 已提交
136
  need_update_ = true;
137
  ops_.emplace_front(new OpDesc(this));
F
fengjiayi 已提交
138 139 140
  return ops_.front().get();
}

Y
Yao Cheng 已提交
141 142 143 144 145
void BlockDesc::PrependAllocatedOp(std::unique_ptr<OpDesc> &&op_desc) {
  need_update_ = true;
  ops_.emplace_front(std::move(op_desc));
}

146 147 148 149 150 151 152 153
OpDesc *BlockDesc::InsertOp(size_t index) {
  need_update_ = true;
  auto it = ops_.begin() + index;
  std::unique_ptr<OpDesc> new_op(new OpDesc(this));
  it = ops_.insert(it, std::move(new_op));
  return (*it).get();
}

T
typhoonzero 已提交
154
void BlockDesc::RemoveOp(size_t s, size_t e) {
Y
Yancey 已提交
155
  if (ops_.begin() + s >= ops_.end() || ops_.begin() + e > ops_.end()) {
T
typhoonzero 已提交
156 157 158 159 160 161
    return;
  }
  need_update_ = true;
  ops_.erase(ops_.begin() + s, ops_.begin() + e);
}

162
void BlockDesc::RemoveOpInternal(const OpDesc *op_desc) {
M
minqiyang 已提交
163
  // TODO(minqiyang): make this faster
164 165 166
  for (auto it = ops_.begin(); it != ops_.end(); ++it) {
    if (it->get() == op_desc) {
      ops_.erase(it);
167
      break;
168 169 170 171
    }
  }
}

Y
Yu Yang 已提交
172 173
std::vector<OpDesc *> BlockDesc::AllOps() const {
  std::vector<OpDesc *> res;
F
fengjiayi 已提交
174 175 176 177 178 179
  for (const auto &op : ops_) {
    res.push_back(op.get());
  }
  return res;
}

Y
Yu Yang 已提交
180
void BlockDesc::Flush() {
181 182 183 184
  for (auto &op_desc : ops_) {
    op_desc->Flush();
  }

F
fengjiayi 已提交
185
  if (need_update_) {
Q
Qiao Longfei 已提交
186
    this->desc_->mutable_ops()->Clear();
F
fengjiayi 已提交
187
    for (auto &op_desc : ops_) {
Q
Qiao Longfei 已提交
188
      this->desc_->mutable_ops()->Add()->CopyFrom(*op_desc->Proto());
F
fengjiayi 已提交
189
    }
Q
Qiao Longfei 已提交
190
    this->desc_->mutable_vars()->Clear();
F
Fix bug  
fengjiayi 已提交
191
    for (auto &var_desc : vars_) {
Q
Qiao Longfei 已提交
192
      this->desc_->mutable_vars()->Add()->CopyFrom(*var_desc.second->Proto());
F
Fix bug  
fengjiayi 已提交
193
    }
F
fengjiayi 已提交
194 195 196 197
    need_update_ = false;
  }
}

Y
Yu Yang 已提交
198
BlockDesc *BlockDesc::ParentBlock() const {
Y
Yu Yang 已提交
199
  return prog_->MutableBlock(static_cast<size_t>(desc_->parent_idx()));
F
fengjiayi 已提交
200 201
}

Y
Yu Yang 已提交
202
proto::BlockDesc *BlockDesc::Proto() {
203 204 205
  Flush();
  return desc_;
}
206

Y
Yu Yang 已提交
207
BlockDesc::BlockDesc(ProgramDesc *prog, proto::BlockDesc *desc)
208
    : prog_(prog), desc_(desc), need_update_(false) {
209
  for (const proto::VarDesc &var_desc : desc_->vars()) {
Y
Yu Yang 已提交
210
    vars_[var_desc.name()].reset(new VarDesc(var_desc));
211
  }
212
  for (const proto::OpDesc &op_desc : desc_->ops()) {
F
fengjiayi 已提交
213
    ops_.emplace_back(new OpDesc(op_desc, this));
214 215 216
  }
}

Y
Yu Yang 已提交
217 218
BlockDesc::BlockDesc(const BlockDesc &other, proto::BlockDesc *desc,
                     ProgramDesc *prog)
Y
Yu Yang 已提交
219 220 221
    : prog_(prog), desc_(desc) {
  need_update_ = true;
  for (auto &op : other.ops_) {
F
fengjiayi 已提交
222
    ops_.emplace_back(new OpDesc(*op, this));
Y
Yu Yang 已提交
223 224
  }
  for (auto &it : other.vars_) {
Y
Yu Yang 已提交
225
    auto *var = new VarDesc(*it.second);
Y
Yu Yang 已提交
226 227 228
    vars_[it.first].reset(var);
  }
}
229

Y
Yu Yang 已提交
230
void BlockDesc::SetForwardBlockID(int32_t forward_block_id) {
231 232 233 234 235
  PADDLE_ENFORCE_EQ(
      desc_->has_forward_block_idx(), false,
      platform::errors::PreconditionNotMet(
          "Block %d's parent block ID has been set to %d, cannot be set to %d.",
          desc_->idx(), desc_->forward_block_idx(), forward_block_id));
Y
Yu Yang 已提交
236 237 238 239 240 241 242
  desc_->set_forward_block_idx(forward_block_id);
}

BlockDesc *BlockDesc::ForwardBlock() const {
  return prog_->MutableBlock(static_cast<size_t>(desc_->forward_block_idx()));
}

F
fengjiayi 已提交
243 244
}  // namespace framework
}  // namespace paddle