program_desc.cpp 3.6 KB
Newer Older
L
liuruilong 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.

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

L
liuruilong 已提交
15
#include <string>
L
liuruilong 已提交
16
#include <vector>
L
liuruilong 已提交
17

18
#include "framework/program/program_desc.h"
L
liuruilong 已提交
19
#include "framework/program/tensor_desc.h"
L
liuruilong 已提交
20 21 22 23

namespace paddle_mobile {
namespace framework {

L
liuruilong 已提交
24 25 26
ProgramDesc::ProgramDesc(PaddleMobile__Framework__Proto__ProgramDesc *desc) {
  for (int i = 0; i < desc->n_blocks; ++i) {
    blocks_.emplace_back(std::make_shared<BlockDesc>(desc->blocks[i]));
L
liuruilong 已提交
27
  }
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
  for (auto &block : blocks_) {
    for (auto op : block->Ops()) {
      for (const auto &attr : op->GetProtoAttr()) {
        if (attr.type == PADDLE_MOBILE__FRAMEWORK__PROTO__ATTR_TYPE__BLOCK) {
          size_t blk_idx = attr.block_idx;
          op->SetBlockAttr(attr.name, this->MutableBlock(blk_idx));
        } else if (attr.type ==
                   PADDLE_MOBILE__FRAMEWORK__PROTO__ATTR_TYPE__BLOCKS) {
          size_t n_blocks_idx = attr.n_blocks_idx;
          int32_t *blks_idx = attr.blocks_idx;
          std::vector<BlockDesc *> block_descs;
          for (size_t i = 0; i < n_blocks_idx; ++i) {
            block_descs.push_back(this->MutableBlock(blks_idx[i]));
          }
          op->SetBlocksAttr(attr.name, block_descs);
        }
      }
    }
  }
L
liuruilong 已提交
47 48
}

H
hjchen2 已提交
49
void ProgramDesc::Description(std::string header) const {
L
liuruilong 已提交
50
#ifdef PADDLE_MOBILE_DEBUG
L
liuruilong 已提交
51
  if (header.size()) {
L
liuruilong 已提交
52 53
    LOG(kLOG_INFO) << header;
  }
L
liuruilong 已提交
54 55 56

  for (int i = 0; i < this->blocks_.size(); ++i) {
    auto block = this->blocks_[i];
L
liuruilong 已提交
57 58 59
    LOG(kLOG_DEBUG) << "block: " << block->ID();
    LOG(kLOG_INFO) << "block ops size: " << block->Ops().size();
    for (int j = 0; j < block->Ops().size(); ++j) {
L
liuruilong 已提交
60
      auto op = block->Ops()[j];
L
liuruilong 已提交
61 62 63 64 65 66 67 68 69 70 71 72 73 74
      LOG(kLOG_DEBUG1) << "op: " << op->Type();
      for (auto &input : op->GetInputs()) {
        LOG(kLOG_DEBUG2) << "input parameter: " << input.first;
        for (auto &n : input.second) {
          LOG(kLOG_DEBUG3) << "argument - " << n;
        }
      }
      for (auto &output : op->GetOutputs()) {
        LOG(kLOG_DEBUG2) << "output parameter: " << output.first;
        for (auto &n : output.second) {
          LOG(kLOG_DEBUG3) << "argument - " << n;
        }
      }
      for (auto &attr : op->GetAttrMap()) {
75 76
        if (attr.first == "op_callstack") continue;
        LOG(kLOG_DEBUG2) << "attr name: " << attr.first;
L
liuruilong 已提交
77 78 79
        LOG(kLOG_DEBUG3) << "argument - " << attr.second;
      }
    }
L
liuruilong 已提交
80 81

    for (const auto &var_desc : block->Vars()) {
H
update  
hjchen2 已提交
82
      LOG(kLOG_DEBUG1) << "var name: " << var_desc->Name();
L
liuruilong 已提交
83 84 85 86 87 88 89 90 91 92 93
      if (var_desc->Type() == VARTYPE_TYPE_LOD_TENSOR) {
        const TensorDesc &tensor_desc = var_desc->Tensor_desc();

        LOG(kLOG_DEBUG2) << "in var tensor desc dims size: "
                         << tensor_desc.Dims().size();
        for (int l = 0; l < tensor_desc.Dims().size(); ++l) {
          LOG(kLOG_DEBUG3) << "var tensor desc dim " << l
                           << " value: " << tensor_desc.Dims()[l];
        }
      }
    }
L
liuruilong 已提交
94
  }
L
liuruilong 已提交
95 96 97

  for (const auto &block : this->blocks_) {
  }
L
liuruilong 已提交
98 99 100 101 102 103 104 105 106
#endif
}

std::shared_ptr<BlockDesc> ProgramDesc::Block(size_t idx) {
  return blocks_[idx];
}

}  // namespace framework
}  // namespace paddle_mobile