block_desc.cc 2.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 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36

namespace paddle {
namespace framework {

VarDescBind *BlockDescBind::NewVar(const std::string &name) {
  need_update_ = true;
  auto it = vars_.find(name);
  PADDLE_ENFORCE(it == vars_.end(), "Duplicated variable %s", name);
  auto var = new VarDescBind(name);
  vars_[name].reset(var);
  return var;
}

VarDescBind *BlockDescBind::Var(const std::string &name) const {
  auto it = vars_.find(name);
  PADDLE_ENFORCE(it != vars_.end(),
                 "Can not find variable %s in current block.", name);
  return it->second.get();
}

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

F
fengjiayi 已提交
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
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();
}

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;
}

void BlockDescBind::Sync() {
  if (need_update_) {
    auto &op_field = *this->desc_->mutable_ops();
    op_field.Clear();
    op_field.Reserve(static_cast<int>(ops_.size()));
    for (auto &op_desc : ops_) {
      op_field.AddAllocated(op_desc->Proto());
    }
    need_update_ = false;
  }
}

BlockDescBind *BlockDescBind::ParentBlock() const {
  if (this->desc_->parent_idx() == -1) {
    return nullptr;
  }
  return prog_->Block(static_cast<size_t>(this->desc_->parent_idx()));
}

void OpDescBind::SetBlockAttr(const std::string &name, BlockDescBind &block) {
  BlockDesc *desc = block.RawPtr();
  this->attrs_[name] = desc;
}
F
fengjiayi 已提交
92 93
}  // namespace framework
}  // namespace paddle