attribute.h 6.1 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 <cstdlib>
W
wangliu 已提交
18
#include <string>
D
dolphin8 已提交
19
#include <typeinfo>
L
liuruilong 已提交
20
#include <unordered_map>
W
wangliu 已提交
21
#include <vector>
L
liuruilong 已提交
22

L
liuruilong 已提交
23
#include "common/enforce.h"
L
liuruilong 已提交
24
#include "common/log.h"
朔-望's avatar
朔-望 已提交
25
#include "common/variant.h"
L
liuruilong 已提交
26
#include "framework/framework.pb-c.h"
朔-望's avatar
朔-望 已提交
27 28

namespace paddle_mobile {
朔-望's avatar
朔-望 已提交
29
namespace framework {
W
wangliu 已提交
30 31
using std::string;
using std::vector;
朔-望's avatar
朔-望 已提交
32

朔-望's avatar
朔-望 已提交
33
class BlockDesc;
朔-望's avatar
朔-望 已提交
34

朔-望's avatar
朔-望 已提交
35
class Attribute {
朔-望's avatar
朔-望 已提交
36
 public:
L
liuruilong 已提交
37 38
  static Attribute GetAttrValue(
      PaddleMobile__Framework__Proto__OpDesc__Attr *attr_desc) {
L
liuruilong 已提交
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
    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: {
54
        attr.SetString(std::string(attr_desc->s));
L
liuruilong 已提交
55 56 57
        break;
      }
      case PADDLE_MOBILE__FRAMEWORK__PROTO__ATTR_TYPE__BOOLEANS: {
W
wangliu 已提交
58
        vector<bool> val(attr_desc->n_bools);
L
liuruilong 已提交
59 60 61
        for (int i = 0; i < attr_desc->n_bools; ++i) {
          val[i] = attr_desc->bools[i];
        }
W
wangliu 已提交
62
        attr.Set<vector<bool>>(val);
L
liuruilong 已提交
63 64 65
        break;
      }
      case PADDLE_MOBILE__FRAMEWORK__PROTO__ATTR_TYPE__INTS: {
W
wangliu 已提交
66
        vector<int> val(attr_desc->n_ints);
L
liuruilong 已提交
67 68 69
        for (int i = 0; i < attr_desc->n_ints; ++i) {
          val[i] = attr_desc->ints[i];
        }
W
wangliu 已提交
70
        attr.Set<vector<int>>(val);
L
liuruilong 已提交
71 72 73
        break;
      }
      case PADDLE_MOBILE__FRAMEWORK__PROTO__ATTR_TYPE__FLOATS: {
W
wangliu 已提交
74
        vector<float> val(attr_desc->n_floats);
L
liuruilong 已提交
75 76 77
        for (int i = 0; i < attr_desc->n_floats; ++i) {
          val[i] = attr_desc->floats[i];
        }
W
wangliu 已提交
78
        attr.Set<vector<float>>(val);
L
liuruilong 已提交
79 80 81
        break;
      }
      case PADDLE_MOBILE__FRAMEWORK__PROTO__ATTR_TYPE__STRINGS: {
W
wangliu 已提交
82
        vector<string> val(attr_desc->n_strings);
L
liuruilong 已提交
83 84 85
        for (int i = 0; i < attr_desc->n_strings; ++i) {
          val[i] = attr_desc->strings[i];
        }
W
wangliu 已提交
86
        attr.Set<vector<string>>(val);
L
liuruilong 已提交
87 88 89 90 91 92
        break;
      }
      case PADDLE_MOBILE__FRAMEWORK__PROTO__ATTR_TYPE__LONG: {
        attr.Set<int64_t>(attr_desc->l);
        break;
      }
93 94 95
      case PADDLE_MOBILE__FRAMEWORK__PROTO__ATTR_TYPE__BLOCK: {
        break;
      }
96 97 98 99 100 101 102 103
      case PADDLE_MOBILE__FRAMEWORK__PROTO__ATTR_TYPE__LONGS: {
        vector<int> val(attr_desc->n_longs);
        for (int i = 0; i < attr_desc->n_longs; ++i) {
          val[i] = attr_desc->longs[i];
        }
        attr.Set<vector<int>>(val);
        break;
      }
L
liuruilong 已提交
104 105 106 107 108 109
      default:
        PADDLE_MOBILE_THROW_EXCEPTION("attr type not support");
    }
    return attr;
  }

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

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

122 123 124 125 126 127 128
  Attribute &SetString(std::string string) {
    variant_.SetString(string);
    return *this;
  }

  std::string GetString() const { return variant_.GetString(); }

L
liuruilong 已提交
129 130
  template <typename Vistor>
  static typename Vistor::type_t ApplyVistor(Vistor vistor, Attribute attr) {
131
    if (attr.variant_.TypeId() == type_id<int>()) {  // NOLINT
L
liuruilong 已提交
132
      return vistor(attr.variant_.Get<int>());
133
    } else if (attr.variant_.TypeId() == type_id<float>()) {  // NOLINT
L
liuruilong 已提交
134
      return vistor(attr.variant_.Get<float>());
135
    } else if (attr.variant_.TypeId() == type_id<string>()) {
136
      return vistor(attr.variant_.GetString());
137
    } else if (attr.variant_.TypeId() == type_id<vector<int>>()) {
W
wangliu 已提交
138
      return vistor(attr.variant_.Get<vector<int>>());
139
    } else if (attr.variant_.TypeId() == type_id<vector<float>>()) {
W
wangliu 已提交
140
      return vistor(attr.variant_.Get<vector<float>>());
141
    } else if (attr.variant_.TypeId() == type_id<vector<string>>()) {
W
wangliu 已提交
142
      return vistor(attr.variant_.Get<vector<string>>());
143
    } else if (attr.variant_.TypeId() == type_id<bool>()) {  // NOLINT
L
liuruilong 已提交
144
      return vistor(attr.variant_.Get<bool>());
145
    } else if (attr.variant_.TypeId() == type_id<vector<bool>>()) {
W
wangliu 已提交
146
      return vistor(attr.variant_.Get<vector<bool>>());
147
    } else if (attr.variant_.TypeId() == type_id<int64_t>()) {
L
liuruilong 已提交
148
      return vistor(attr.variant_.Get<int64_t>());
149
    } else if (attr.variant_.TypeId() == type_id<framework::BlockDesc *>()) {
150 151
      return vistor(attr.variant_.Get<framework::BlockDesc *>());
    } else if (attr.variant_.TypeId() ==
152
               type_id<vector<framework::BlockDesc *>>()) {
153
      return vistor(attr.variant_.Get<vector<framework::BlockDesc *>>());
154
    } else if (attr.variant_.TypeId() == type_id<vector<int64_t>>()) {
155
      return vistor(attr.variant_.Get<vector<int64_t>>());
L
liuruilong 已提交
156
    } else {
W
wangliu 已提交
157
      PADDLE_MOBILE_THROW_EXCEPTION("type not support");
L
liuruilong 已提交
158 159 160
    }
  }

朔-望's avatar
朔-望 已提交
161
 private:
W
wangliu 已提交
162
  Variant<int, float, string, vector<int>, vector<float>, vector<string>, bool,
163 164
          vector<bool>, BlockDesc *, vector<BlockDesc *>, int64_t,
          vector<int64_t>>
165
      variant_;
朔-望's avatar
朔-望 已提交
166
};
朔-望's avatar
朔-望 已提交
167

W
wangliu 已提交
168
using AttributeMap = std::unordered_map<string, Attribute>;
朔-望's avatar
朔-望 已提交
169

朔-望's avatar
朔-望 已提交
170
class AttrReader {
朔-望's avatar
朔-望 已提交
171
 public:
172
  explicit AttrReader(const AttributeMap &attrs) : attrs_(attrs) {}
朔-望's avatar
朔-望 已提交
173

朔-望's avatar
朔-望 已提交
174
  template <typename T>
W
wangliu 已提交
175 176
  inline T Get(const string &name) const {
    PADDLE_MOBILE_ENFORCE(attrs_.count(name) != 0,
H
hjchen2 已提交
177
                          "%s should  be in AttributeMap", name.c_str());
178 179
    return ((Attribute)attrs_.at(name)).Get<T>();
  }
朔-望's avatar
朔-望 已提交
180

朔-望's avatar
朔-望 已提交
181
 private:
182
  const AttributeMap &attrs_;
朔-望's avatar
朔-望 已提交
183
};
朔-望's avatar
朔-望 已提交
184

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

朔-望's avatar
朔-望 已提交
187 188
}  // namespace framework
}  // namespace paddle_mobile