block_desc.cc 7.8 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 16 17
#include "paddle/fluid/framework/block_desc.h"
#include "paddle/fluid/framework/operator.h"
#include "paddle/fluid/framework/program_desc.h"
F
fengjiayi 已提交
18

Y
Yu Yang 已提交
19 20
#include <queue>

F
fengjiayi 已提交
21 22 23
namespace paddle {
namespace framework {

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

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

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

T
wip  
typhoonzero 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59 60
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 已提交
61
  }
T
wip  
typhoonzero 已提交
62 63
  vars_.erase(old_name);
  return new_var;
T
typhoonzero 已提交
64 65
}

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

Y
Yu Yang 已提交
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
  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);
96
  }
Y
Yu Yang 已提交
97 98

  return nullptr;
99 100
}

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

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

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

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

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

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

138 139 140 141 142 143 144 145
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 已提交
146
void BlockDesc::RemoveOp(size_t s, size_t e) {
T
typhoonzero 已提交
147 148 149
  if (ops_.begin() + s == ops_.end() || ops_.begin() + e == ops_.end()) {
    return;
  }
L
Luo Tao 已提交
150 151 152 153 154 155 156 157 158 159
  auto get_vars = [](std::deque<std::unique_ptr<OpDesc>>::iterator &op,
                     std::vector<std::string> &v) {
    auto in_names = (*op)->InputArgumentNames();
    v.insert(v.end(), in_names.begin(), in_names.end());
    auto out_names = (*op)->OutputArgumentNames();
    v.insert(v.end(), out_names.begin(), out_names.end());
    std::sort(v.begin(), v.end());
    auto last = std::unique(v.begin(), v.end());
    v.erase(last, v.end());
  };
T
typhoonzero 已提交
160
  need_update_ = true;
L
Luo Tao 已提交
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176

  for (size_t i = s; i < e; i++) {
    // since remove op one by one, every time remove the first op.
    auto op = ops_.begin() + s;

    // collect input and output variables from current delete op
    std::vector<std::string> cur_vars;
    get_vars(op, cur_vars);

    // remove current op
    ops_.erase(ops_.begin() + s);

    // collect input and output variables from other ops
    std::vector<std::string> other_vars;
    for (auto it = ops_.begin(); it != ops_.end(); it++) {
      get_vars(it, other_vars);
T
typhoonzero 已提交
177
    }
L
Luo Tao 已提交
178

L
Luo Tao 已提交
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
    // variables should be deleted
    std::vector<std::string> delete_vars;
    // delete_vars = cur_vars -  cur_vars ^ other_input_vars
    std::set_difference(cur_vars.begin(), cur_vars.end(), other_vars.begin(),
                        other_vars.end(),
                        std::inserter(delete_vars, delete_vars.end()));
    // remove variables
    for (size_t i = 0; i < delete_vars.size(); i++) {
      auto name = delete_vars[i];
      auto it = vars_.find(name);
      PADDLE_ENFORCE(it != vars_.end(),
                     "%s is not in variable list, it should not be deleted",
                     name);
      vars_.erase(it);
      VLOG(3) << "deleting variable " << name;
    }
L
Luo Tao 已提交
195
  }
T
typhoonzero 已提交
196 197
}

Y
Yu Yang 已提交
198 199
std::vector<OpDesc *> BlockDesc::AllOps() const {
  std::vector<OpDesc *> res;
F
fengjiayi 已提交
200 201 202 203 204 205
  for (const auto &op : ops_) {
    res.push_back(op.get());
  }
  return res;
}

Y
Yu Yang 已提交
206
void BlockDesc::Flush() {
207 208 209 210
  for (auto &op_desc : ops_) {
    op_desc->Flush();
  }

F
fengjiayi 已提交
211 212
  if (need_update_) {
    auto &op_field = *this->desc_->mutable_ops();
213
    this->ClearPBOps();
F
fengjiayi 已提交
214 215 216 217
    op_field.Reserve(static_cast<int>(ops_.size()));
    for (auto &op_desc : ops_) {
      op_field.AddAllocated(op_desc->Proto());
    }
F
Fix bug  
fengjiayi 已提交
218
    auto &var_field = *this->desc_->mutable_vars();
219
    this->ClearPBVars();
F
Fix bug  
fengjiayi 已提交
220 221 222 223
    var_field.Reserve(static_cast<int>(vars_.size()));
    for (auto &var_desc : vars_) {
      var_field.AddAllocated(var_desc.second->Proto());
    }
F
fengjiayi 已提交
224 225 226 227
    need_update_ = false;
  }
}

Y
Yu Yang 已提交
228
BlockDesc *BlockDesc::ParentBlock() const {
Y
Yu Yang 已提交
229
  return prog_->MutableBlock(static_cast<size_t>(desc_->parent_idx()));
F
fengjiayi 已提交
230 231
}

Y
Yu Yang 已提交
232
proto::BlockDesc *BlockDesc::Proto() {
233 234 235
  Flush();
  return desc_;
}
236

Y
Yu Yang 已提交
237
BlockDesc::BlockDesc(ProgramDesc *prog, proto::BlockDesc *desc)
238
    : prog_(prog), desc_(desc), need_update_(false) {
239
  for (const proto::VarDesc &var_desc : desc_->vars()) {
Y
Yu Yang 已提交
240
    vars_[var_desc.name()].reset(new VarDesc(var_desc));
241
  }
242
  for (const proto::OpDesc &op_desc : desc_->ops()) {
243
    ops_.emplace_back(new OpDesc(op_desc, prog, this));
244 245 246
  }
}

Y
Yu Yang 已提交
247 248
BlockDesc::BlockDesc(const BlockDesc &other, proto::BlockDesc *desc,
                     ProgramDesc *prog)
Y
Yu Yang 已提交
249 250 251
    : prog_(prog), desc_(desc) {
  need_update_ = true;
  for (auto &op : other.ops_) {
K
Kexin Zhao 已提交
252
    ops_.emplace_back(new OpDesc(*op->Proto(), prog, this));
Y
Yu Yang 已提交
253 254
  }
  for (auto &it : other.vars_) {
Y
Yu Yang 已提交
255
    auto *var = new VarDesc(*it.second);
Y
Yu Yang 已提交
256 257 258
    vars_[it.first].reset(var);
  }
}
259

Y
Yu Yang 已提交
260
void BlockDesc::ClearPBOps() {
261 262 263 264 265 266 267
  auto ops = this->desc_->mutable_ops();
  while (!ops->empty()) {
    // we do not own the OpDesc, so release the ownership.
    ops->ReleaseLast();
  }
}

Y
Yu Yang 已提交
268
void BlockDesc::ClearPBVars() {
269 270 271 272 273 274 275
  auto vars = this->desc_->mutable_vars();
  while (!vars->empty()) {
    // we do not own the VarDesc, so release the ownership.
    vars->ReleaseLast();
  }
}

Y
Yu Yang 已提交
276 277 278 279 280 281 282 283 284 285 286
void BlockDesc::SetForwardBlockID(int32_t forward_block_id) {
  PADDLE_ENFORCE(!desc_->has_forward_block_idx(),
                 "Parent block ID has been set to %d. Cannot set to %d",
                 desc_->forward_block_idx(), forward_block_id);
  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 已提交
287 288
}  // namespace framework
}  // namespace paddle