op_desc.cpp 2.7 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
  for (int k = 0; k < desc->n_attrs; ++k) {
    PaddleMobile__Framework__Proto__OpDesc__Attr *attr = desc->attrs[k];
    std::string attr_name(attr->name);
44
    attrs_[attr_name] = Attribute::GetAttrValue(attr);
45
  }
朔-望's avatar
朔-望 已提交
46
}
47

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

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

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

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

L
liuruilong 已提交
65
Print &operator<<(Print &printer, const OpDesc &op_desc) {
L
liuruilong 已提交
66
  OpDesc &no_const_op_desc = const_cast<OpDesc &>(op_desc);
L
liuruilong 已提交
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
  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
朔-望 已提交
84 85
}  // namespace framework
}  // namespace paddle_mobile