op_desc.cpp 2.6 KB
Newer Older
朔-望's avatar
朔-望 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* 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. */

#include <string>
#include <vector>
朔-望's avatar
朔-望 已提交
17

L
liuruilong 已提交
18 19
#include "framework/program/op_desc.h"

朔-望's avatar
朔-望 已提交
20
namespace paddle_mobile {
朔-望's avatar
朔-望 已提交
21 22
namespace framework {

L
liuruilong 已提交
23 24 25
OpDesc::OpDesc(const proto::OpDesc &desc):type_(desc.type()) {
  for (int i = 0; i < desc.inputs_size(); ++i) {
    const proto::OpDesc::Var &var = desc.inputs(i);
26 27 28 29
    std::vector<std::string> &args = inputs_[var.parameter()];
    int arg_size = var.arguments_size();
    for (int j = 0; j < arg_size; ++j) {
      args.push_back(var.arguments(j));
朔-望's avatar
朔-望 已提交
30
    }
31 32
  }

L
liuruilong 已提交
33 34
  for (int i = 0; i < desc.outputs_size(); ++i) {
    const proto::OpDesc::Var &var = desc.outputs(i);
35 36 37 38
    std::vector<std::string> &args = outputs_[var.parameter()];
    int arg_size = var.arguments_size();
    for (int j = 0; j < arg_size; ++j) {
      args.push_back(var.arguments(j));
朔-望's avatar
朔-望 已提交
39
    }
40 41
  }

L
liuruilong 已提交
42
  for (const proto::OpDesc::Attr &attr : desc.attrs()) {
43 44 45
    std::string attr_name = attr.name();
    if (attr.type() != proto::AttrType::BLOCK) {
      attrs_[attr_name] = Attribute::GetAttrValue(attr);
朔-望's avatar
朔-望 已提交
46
    }
47
  }
朔-望's avatar
朔-望 已提交
48
}
49

朔-望's avatar
朔-望 已提交
50
const std::vector<std::string> &OpDesc::Input(const std::string &name) const {
51
  return inputs_.find(name)->second;
朔-望's avatar
朔-望 已提交
52
}
53

朔-望's avatar
朔-望 已提交
54
const std::vector<std::string> &OpDesc::Output(const std::string &name) const {
55
  return outputs_.find(name)->second;
朔-望's avatar
朔-望 已提交
56 57 58
}

Attribute OpDesc::GetAttr(const std::string &name) const {
59 60
  auto it = attrs_.find(name);
  return it->second;
朔-望's avatar
朔-望 已提交
61 62
}

L
liuruilong 已提交
63
std::unordered_map<std::string, Attribute> &OpDesc::GetAttrMap() {
64
  return attrs_;
朔-望's avatar
朔-望 已提交
65
}
66

L
liuruilong 已提交
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
Print &operator<<(Print &printer, const OpDesc &op_desc) {
  OpDesc &no_const_op_desc =  const_cast<OpDesc &>(op_desc);
  printer << "inputs: \n";
  for (const auto &input : no_const_op_desc.GetInputs()) {
    printer << input.first << " : " << input.second << "\n";
  }

  printer << "outputs: \n";
  for (const auto &output : no_const_op_desc.GetOutputs()) {
    printer << output.first << " : " << output.second << "\n";
  }

  printer << "outputs: \n";
  for (const auto &attr : no_const_op_desc.GetAttrMap()) {
    printer << attr.first << " : " << attr.second << "\n";
  }
  return printer;
}

朔-望's avatar
朔-望 已提交
86 87
}  // namespace framework
}  // namespace paddle_mobile