op_desc.cpp 2.8 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 26 27 28 29
OpDesc::OpDesc(PaddleMobile__Framework__Proto__OpDesc *desc) {
  this->type_ = std::string(desc->type);
  for (int i = 0; i < desc->n_inputs; ++i) {
    PaddleMobile__Framework__Proto__OpDesc__Var *var = desc->inputs[i];
    std::vector<std::string> &args = inputs_[std::string(var->parameter)];
    for (int j = 0; j < var->n_arguments; ++j) {
      args.emplace_back(std::string(var->arguments[j]));
朔-望's avatar
朔-望 已提交
30
    }
31 32
  }

L
liuruilong 已提交
33 34 35 36 37
  for (int i = 0; i < desc->n_outputs; ++i) {
    PaddleMobile__Framework__Proto__OpDesc__Var *var = desc->outputs[i];
    std::vector<std::string> &args = outputs_[std::string(var->parameter)];
    for (int j = 0; j < var->n_arguments; ++j) {
      args.emplace_back(std::string(var->arguments[j]));
朔-望's avatar
朔-望 已提交
38
    }
39 40
  }

L
liuruilong 已提交
41 42 43 44
  for (int k = 0; k < desc->n_attrs; ++k) {
    PaddleMobile__Framework__Proto__OpDesc__Attr *attr = desc->attrs[k];
    std::string attr_name(attr->name);
    if (attr->type != PADDLE_MOBILE__FRAMEWORK__PROTO__ATTR_TYPE__BLOCK) {
45
      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
Print &operator<<(Print &printer, const OpDesc &op_desc) {
L
liuruilong 已提交
68
  OpDesc &no_const_op_desc = const_cast<OpDesc &>(op_desc);
L
liuruilong 已提交
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
  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