program_desc.cc 3.5 KB
Newer Older
F
fengjiayi 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* 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. */

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

namespace paddle {
namespace framework {

Y
Yu Yang 已提交
22
BlockDesc *ProgramDesc::AppendBlock(const BlockDesc &parent) {
23
  auto *b = desc_.add_blocks();
F
fengjiayi 已提交
24
  b->set_parent_idx(parent.ID());
25
  b->set_idx(desc_.blocks_size() - 1);
Y
Yu Yang 已提交
26
  blocks_.emplace_back(new BlockDesc(this, b));
F
fengjiayi 已提交
27 28 29
  return blocks_.back().get();
}

Y
Yu Yang 已提交
30
proto::ProgramDesc *ProgramDesc::Proto() {
F
fengjiayi 已提交
31
  for (auto &block : blocks_) {
32
    block->Flush();
F
fengjiayi 已提交
33
  }
34
  return &desc_;
F
fengjiayi 已提交
35 36
}

Y
Yu Yang 已提交
37
ProgramDesc::ProgramDesc() {
38
  auto *block = desc_.mutable_blocks()->Add();
39 40
  block->set_idx(kRootBlockIndex);
  block->set_parent_idx(kNoneBlockIndex);
Y
Yu Yang 已提交
41
  blocks_.emplace_back(new BlockDesc(this, block));
F
fengjiayi 已提交
42
}
Y
Yu Yang 已提交
43

Y
Yu Yang 已提交
44
ProgramDesc::ProgramDesc(const ProgramDesc &o) {
45
  desc_ = o.desc_;
Y
Yu Yang 已提交
46

47 48
  for (int i = 0; i < desc_.blocks_size(); ++i) {
    auto *block = desc_.mutable_blocks(i);
Y
Yu Yang 已提交
49
    blocks_.emplace_back(new BlockDesc(*o.blocks_[i], block, this));
Y
Yu Yang 已提交
50 51
  }
}
52

Y
Yu Yang 已提交
53
ProgramDesc::ProgramDesc(const proto::ProgramDesc &desc) {
54
  desc_ = desc;
55
  std::cout << std::endl << "starting in ProgDesc constructor" << std::endl;
56
  for (auto &block_desc : *desc_.mutable_blocks()) {
Y
Yu Yang 已提交
57
    blocks_.emplace_back(new BlockDesc(this, &block_desc));
58 59
    std::cout << "Done constructing block idx " << block_desc.idx()
              << " parent idx " << block_desc.parent_idx() << std::endl;
60
  }
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
  for (auto &block : blocks_) {
    for (auto *op : block->AllOps()) {
      for (auto &name : op->AttrNames()) {
        if (op->GetAttrType(name) == proto::AttrType::BLOCK) {
          auto attr = op->GetAttr(name);
          size_t blk_idx =
              reinterpret_cast<size_t>(boost::get<BlockDesc *>(attr));
          op->SetBlockAttr(name, *this->MutableBlock(blk_idx));
          std::cout << "Update attr name " << name << " for block idx "
                    << blk_idx << std::endl;
        }
      }
    }
  }
  std::cout << "Done ProgDesc construction" << std::endl << std::endl;
76 77
}

Y
Yu Yang 已提交
78
ProgramDesc::ProgramDesc(const std::string &binary_str) {
79 80 81
  PADDLE_ENFORCE(desc_.ParseFromString(binary_str),
                 "Fail to parse program_desc from binary string.");
  for (auto &block_desc : *desc_.mutable_blocks()) {
Y
Yu Yang 已提交
82
    blocks_.emplace_back(new BlockDesc(this, &block_desc));
83 84 85
  }
}

86
const std::vector<std::string> ProgramDesc::GetFeedTargetNames() {
87
  BlockDesc *global_block = blocks_[0].get();
88
  std::vector<std::string> feed_target_names;
89
  for (auto *op : global_block->AllOps()) {
90 91
    if (op->Type() == kFeedOpType) {
      feed_target_names.insert(feed_target_names.begin(), op->Output("Out")[0]);
92 93
    }
  }
94
  return feed_target_names;
95 96
}

97
const std::vector<std::string> ProgramDesc::GetFetchTargetNames() {
98
  BlockDesc *global_block = blocks_[0].get();
99
  std::vector<std::string> fetch_target_names;
100
  for (auto *op : global_block->AllOps()) {
101 102
    if (op->Type() == kFetchOpType) {
      fetch_target_names.push_back(op->Input("X")[0]);
103 104
    }
  }
105
  return fetch_target_names;
106 107
}

F
fengjiayi 已提交
108 109
}  // namespace framework
}  // namespace paddle