attribute.h 5.9 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. */

朔-望's avatar
朔-望 已提交
15 16
#pragma once

L
liuruilong 已提交
17
#include <unordered_map>
L
liuruilong 已提交
18
#include "common/log.h"
L
liuruilong 已提交
19
#include "common/enforce.h"
朔-望's avatar
朔-望 已提交
20
#include "common/variant.h"
L
liuruilong 已提交
21
#include "framework/framework.pb-c.h"
朔-望's avatar
朔-望 已提交
22 23

namespace paddle_mobile {
朔-望's avatar
朔-望 已提交
24
namespace framework {
朔-望's avatar
朔-望 已提交
25

朔-望's avatar
朔-望 已提交
26
class BlockDesc;
朔-望's avatar
朔-望 已提交
27

朔-望's avatar
朔-望 已提交
28
class Attribute {
朔-望's avatar
朔-望 已提交
29
 public:
朔-望's avatar
朔-望 已提交
30

L
liuruilong 已提交
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
  /*
   *  PADDLE_MOBILE__FRAMEWORK__PROTO__ATTR_TYPE__INT = 0,
  PADDLE_MOBILE__FRAMEWORK__PROTO__ATTR_TYPE__FLOAT = 1,
  PADDLE_MOBILE__FRAMEWORK__PROTO__ATTR_TYPE__STRING = 2,
  PADDLE_MOBILE__FRAMEWORK__PROTO__ATTR_TYPE__INTS = 3,
  PADDLE_MOBILE__FRAMEWORK__PROTO__ATTR_TYPE__FLOATS = 4,
  PADDLE_MOBILE__FRAMEWORK__PROTO__ATTR_TYPE__STRINGS = 5,
  PADDLE_MOBILE__FRAMEWORK__PROTO__ATTR_TYPE__BOOLEAN = 6,
  PADDLE_MOBILE__FRAMEWORK__PROTO__ATTR_TYPE__BOOLEANS = 7,
  PADDLE_MOBILE__FRAMEWORK__PROTO__ATTR_TYPE__BLOCK = 8,
  PADDLE_MOBILE__FRAMEWORK__PROTO__ATTR_TYPE__LONG = 9
    PROTOBUF_C__FORCE_ENUM_TO_BE_INT_SIZE(PADDLE_MOBILE__FRAMEWORK__PROTO__ATTR_TYPE)
   *
   * */
  static Attribute GetAttrValue(PaddleMobile__Framework__Proto__OpDesc__Attr *attr_desc) {
    //    std::cout << "begin get attr value" << std::endl;
    Attribute attr;
    switch (attr_desc->type) {
      case PADDLE_MOBILE__FRAMEWORK__PROTO__ATTR_TYPE__BOOLEAN: {
        attr.Set<bool>(attr_desc->b);
        break;
      }
      case PADDLE_MOBILE__FRAMEWORK__PROTO__ATTR_TYPE__INT: {
        attr.Set<int>(attr_desc->i);
        break;
      }
      case PADDLE_MOBILE__FRAMEWORK__PROTO__ATTR_TYPE__FLOAT: {
        attr.Set<float>(attr_desc->f);
        break;
      }
      case PADDLE_MOBILE__FRAMEWORK__PROTO__ATTR_TYPE__STRING: {
        attr.Set<std::string>(std::string(attr_desc->s));
        break;
      }
      case PADDLE_MOBILE__FRAMEWORK__PROTO__ATTR_TYPE__BOOLEANS: {
        std::vector<bool> val(attr_desc->n_bools);
        for (int i = 0; i < attr_desc->n_bools; ++i) {
          val[i] = attr_desc->bools[i];
        }
        attr.Set<std::vector<bool>>(val);
        break;
      }
      case PADDLE_MOBILE__FRAMEWORK__PROTO__ATTR_TYPE__INTS: {
        std::vector<int> val(attr_desc->n_ints);
        for (int i = 0; i < attr_desc->n_ints; ++i) {
          val[i] = attr_desc->ints[i];
        }
        attr.Set<std::vector<int>>(val);
        break;
      }
      case PADDLE_MOBILE__FRAMEWORK__PROTO__ATTR_TYPE__FLOATS: {
        std::vector<float> val(attr_desc->n_floats);
        for (int i = 0; i < attr_desc->n_floats; ++i) {
          val[i] = attr_desc->floats[i];
        }
        attr.Set<std::vector<float>>(val);
        break;
      }
      case PADDLE_MOBILE__FRAMEWORK__PROTO__ATTR_TYPE__STRINGS: {
        std::vector<std::string> val(attr_desc->n_strings);
        for (int i = 0; i < attr_desc->n_strings; ++i) {
          val[i] = attr_desc->strings[i];
        }
        attr.Set<std::vector<std::string>>(val);
        break;
      }
      case PADDLE_MOBILE__FRAMEWORK__PROTO__ATTR_TYPE__LONG: {
        attr.Set<int64_t>(attr_desc->l);
        break;
      }
      default:
        PADDLE_MOBILE_THROW_EXCEPTION("attr type not support");
    }
    return attr;
  }

107
  Attribute() {}
朔-望's avatar
朔-望 已提交
108 109
  template <typename T, typename... Args>
  Attribute &Set(Args &&... args) {
110 111 112
    variant_.Set<T>(args...);
    return *this;
  }
朔-望's avatar
朔-望 已提交
113

朔-望's avatar
朔-望 已提交
114 115 116 117
  template <typename T>
  T &Get() const {
    return variant_.Get<T>();
  }
118

L
liuruilong 已提交
119 120 121 122 123 124 125 126 127 128
  template <typename Vistor>
  static typename Vistor::type_t ApplyVistor(Vistor vistor, Attribute attr) {
    if (attr.variant_.TypeId() == typeid(int).hash_code()) {
      return vistor(attr.variant_.Get<int>());
    } else if (attr.variant_.TypeId() == typeid(float).hash_code()) {
      return vistor(attr.variant_.Get<float>());
    } else if (attr.variant_.TypeId() == typeid(std::string).hash_code()) {
      return vistor(attr.variant_.Get<std::string>());
    } else if (attr.variant_.TypeId() == typeid(std::vector<int>).hash_code()) {
      return vistor(attr.variant_.Get<std::vector<int>>());
L
liuruilong 已提交
129 130
    } else if (attr.variant_.TypeId() ==
               typeid(std::vector<float>).hash_code()) {
L
liuruilong 已提交
131
      return vistor(attr.variant_.Get<std::vector<float>>());
L
liuruilong 已提交
132 133
    } else if (attr.variant_.TypeId() ==
               typeid(std::vector<std::string>).hash_code()) {
L
liuruilong 已提交
134 135 136
      return vistor(attr.variant_.Get<std::vector<std::string>>());
    } else if (attr.variant_.TypeId() == typeid(bool).hash_code()) {
      return vistor(attr.variant_.Get<bool>());
L
liuruilong 已提交
137 138
    } else if (attr.variant_.TypeId() ==
               typeid(std::vector<bool>).hash_code()) {
L
liuruilong 已提交
139
      return vistor(attr.variant_.Get<std::vector<bool>>());
L
liuruilong 已提交
140
    } else if (attr.variant_.TypeId() == typeid(int64_t).hash_code()) {
L
liuruilong 已提交
141 142 143 144 145 146
      return vistor(attr.variant_.Get<int64_t>());
    } else {
      throw std::bad_exception();
    }
  }

朔-望's avatar
朔-望 已提交
147
 private:
148 149 150 151
  Variant<int, float, std::string, std::vector<int>, std::vector<float>,
          std::vector<std::string>, bool, std::vector<bool>, BlockDesc *,
          int64_t>
      variant_;
朔-望's avatar
朔-望 已提交
152
};
朔-望's avatar
朔-望 已提交
153

朔-望's avatar
朔-望 已提交
154
using AttributeMap = std::unordered_map<std::string, Attribute>;
朔-望's avatar
朔-望 已提交
155

朔-望's avatar
朔-望 已提交
156
class AttrReader {
朔-望's avatar
朔-望 已提交
157
 public:
158
  explicit AttrReader(const AttributeMap &attrs) : attrs_(attrs) {}
朔-望's avatar
朔-望 已提交
159

朔-望's avatar
朔-望 已提交
160 161
  template <typename T>
  inline T Get(const std::string &name) const {
162 163 164 165 166 167
    //          PADDLE_ENFORCE(attrs_.count(name) != 0, "%s should
    //          be in
    //          AttributeMap",
    //                         name);
    return ((Attribute)attrs_.at(name)).Get<T>();
  }
朔-望's avatar
朔-望 已提交
168

朔-望's avatar
朔-望 已提交
169
 private:
170
  const AttributeMap &attrs_;
朔-望's avatar
朔-望 已提交
171
};
朔-望's avatar
朔-望 已提交
172

L
liuruilong 已提交
173 174
Print &operator<<(Print &printer, const Attribute &op_desc);

朔-望's avatar
朔-望 已提交
175 176
}  // namespace framework
}  // namespace paddle_mobile