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

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

#include "paddle/framework/block_desc.h"
F
fengjiayi 已提交
16
#include "paddle/framework/program_desc.h"
F
fengjiayi 已提交
17 18 19 20

namespace paddle {
namespace framework {

D
dongzhihong 已提交
21
VarDescBind *BlockDescBind::Var(const std::string &name) {
F
fengjiayi 已提交
22
  auto it = vars_.find(name);
D
Dong Zhihong 已提交
23
  if (it != vars_.end()) {
D
Dong Zhihong 已提交
24
    return it->second.get();
D
Dong Zhihong 已提交
25
  }
26
  need_update_ = true;
D
Dong Zhihong 已提交
27
  auto *var = new VarDescBind(name);
F
fengjiayi 已提交
28 29 30 31
  vars_[name].reset(var);
  return var;
}

D
Dong Zhihong 已提交
32
VarDescBind *BlockDescBind::FindVar(const std::string &name) const {
F
fengjiayi 已提交
33
  auto it = vars_.find(name);
D
Dong Zhihong 已提交
34 35 36
  if (it == vars_.end()) {
    return nullptr;
  }
F
fengjiayi 已提交
37 38 39
  return it->second.get();
}

Q
qiaolongfei 已提交
40
bool BlockDescBind::HasVar(const std::string &name) const {
Q
qiaolongfei 已提交
41
  return vars_.find(name) != vars_.end();
Q
qiaolongfei 已提交
42 43
}

44 45 46 47 48 49 50 51 52
VarDescBind *BlockDescBind::FindVarRecursive(const std::string &name) const {
  auto it = vars_.find(name);
  if (it == vars_.end()) {
    return Parent() == kNoneBlockIndex ? nullptr
                                       : ParentBlock()->FindVarRecursive(name);
  }
  return it->second.get();
}

Y
Yang Yang(Tony) 已提交
53 54 55 56 57 58 59 60 61
VarDescBind *BlockDescBind::FindRecursiveOrCreateVar(
    const std::string &name_bytes) {
  VarDescBind *res = FindVarRecursive(name_bytes);
  if (res == nullptr) {
    res = Var(name_bytes);
  }
  return res;
}

62 63 64 65
bool BlockDescBind::HasVarRecursive(const std::string &name) const {
  return FindVarRecursive(name) != nullptr;
}

F
fengjiayi 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78 79
std::vector<VarDescBind *> BlockDescBind::AllVars() const {
  std::vector<VarDescBind *> res;
  for (const auto &p : vars_) {
    res.push_back(p.second.get());
  }
  return res;
}

OpDescBind *BlockDescBind::AppendOp() {
  need_update_ = true;
  ops_.emplace_back(new OpDescBind());
  return ops_.back().get();
}

80 81 82 83 84
void BlockDescBind::AppendAllocatedOp(std::unique_ptr<OpDescBind> &&op_desc) {
  need_update_ = true;
  ops_.emplace_back(std::move(op_desc));
}

F
fengjiayi 已提交
85 86 87 88 89 90 91 92 93 94 95 96 97 98
OpDescBind *BlockDescBind::PrependOp() {
  need_update_ = true;
  ops_.emplace_front(new OpDescBind());
  return ops_.front().get();
}

std::vector<OpDescBind *> BlockDescBind::AllOps() const {
  std::vector<OpDescBind *> res;
  for (const auto &op : ops_) {
    res.push_back(op.get());
  }
  return res;
}

99
void BlockDescBind::Flush() {
100 101 102 103
  for (auto &op_desc : ops_) {
    op_desc->Flush();
  }

F
fengjiayi 已提交
104 105
  if (need_update_) {
    auto &op_field = *this->desc_->mutable_ops();
106
    this->ClearPBOps();
F
fengjiayi 已提交
107 108 109 110
    op_field.Reserve(static_cast<int>(ops_.size()));
    for (auto &op_desc : ops_) {
      op_field.AddAllocated(op_desc->Proto());
    }
F
Fix bug  
fengjiayi 已提交
111
    auto &var_field = *this->desc_->mutable_vars();
112
    this->ClearPBVars();
F
Fix bug  
fengjiayi 已提交
113 114 115 116
    var_field.Reserve(static_cast<int>(vars_.size()));
    for (auto &var_desc : vars_) {
      var_field.AddAllocated(var_desc.second->Proto());
    }
F
fengjiayi 已提交
117 118 119 120 121
    need_update_ = false;
  }
}

BlockDescBind *BlockDescBind::ParentBlock() const {
122
  if (this->desc_->parent_idx() == kNoneBlockIndex) {
F
fengjiayi 已提交
123 124
    return nullptr;
  }
125
  return prog_->MutableBlock(static_cast<size_t>(this->desc_->parent_idx()));
F
fengjiayi 已提交
126 127
}

128 129 130 131
BlockDesc *BlockDescBind::Proto() {
  Flush();
  return desc_;
}
132 133 134 135 136 137 138 139 140 141 142

BlockDescBind::BlockDescBind(ProgramDescBind *prog, BlockDesc *desc)
    : prog_(prog), desc_(desc), need_update_(false) {
  for (const VarDesc &var_desc : desc_->vars()) {
    vars_[var_desc.name()].reset(new VarDescBind(var_desc));
  }
  for (const OpDesc &op_desc : desc_->ops()) {
    ops_.emplace_back(new OpDescBind(op_desc, prog));
  }
}

Y
Yu Yang 已提交
143 144 145 146 147 148 149 150 151 152 153 154 155
BlockDescBind::BlockDescBind(const BlockDescBind &other, BlockDesc *desc,
                             ProgramDescBind *prog)
    : prog_(prog), desc_(desc) {
  need_update_ = true;
  for (auto &op : other.ops_) {
    ops_.emplace_back(new OpDescBind(*op));
  }

  for (auto &it : other.vars_) {
    auto *var = new VarDescBind(*it.second);
    vars_[it.first].reset(var);
  }
}
156

157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
void BlockDescBind::ClearPBOps() {
  auto ops = this->desc_->mutable_ops();
  while (!ops->empty()) {
    // we do not own the OpDesc, so release the ownership.
    ops->ReleaseLast();
  }
}

void BlockDescBind::ClearPBVars() {
  auto vars = this->desc_->mutable_vars();
  while (!vars->empty()) {
    // we do not own the VarDesc, so release the ownership.
    vars->ReleaseLast();
  }
}

F
fengjiayi 已提交
173 174
}  // namespace framework
}  // namespace paddle