attribute.h 5.0 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

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

namespace paddle_mobile {
朔-望's avatar
朔-望 已提交
26
namespace framework {
W
wangliu 已提交
27 28
using std::string;
using std::vector;
朔-望's avatar
朔-望 已提交
29

朔-望's avatar
朔-望 已提交
30
class BlockDesc;
朔-望's avatar
朔-望 已提交
31

朔-望's avatar
朔-望 已提交
32
class Attribute {
朔-望's avatar
朔-望 已提交
33
 public:
L
liuruilong 已提交
34 35
  static Attribute GetAttrValue(
      PaddleMobile__Framework__Proto__OpDesc__Attr *attr_desc) {
L
liuruilong 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
    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: {
W
wangliu 已提交
55
        vector<bool> val(attr_desc->n_bools);
L
liuruilong 已提交
56 57 58
        for (int i = 0; i < attr_desc->n_bools; ++i) {
          val[i] = attr_desc->bools[i];
        }
W
wangliu 已提交
59
        attr.Set<vector<bool>>(val);
L
liuruilong 已提交
60 61 62
        break;
      }
      case PADDLE_MOBILE__FRAMEWORK__PROTO__ATTR_TYPE__INTS: {
W
wangliu 已提交
63
        vector<int> val(attr_desc->n_ints);
L
liuruilong 已提交
64 65 66
        for (int i = 0; i < attr_desc->n_ints; ++i) {
          val[i] = attr_desc->ints[i];
        }
W
wangliu 已提交
67
        attr.Set<vector<int>>(val);
L
liuruilong 已提交
68 69 70
        break;
      }
      case PADDLE_MOBILE__FRAMEWORK__PROTO__ATTR_TYPE__FLOATS: {
W
wangliu 已提交
71
        vector<float> val(attr_desc->n_floats);
L
liuruilong 已提交
72 73 74
        for (int i = 0; i < attr_desc->n_floats; ++i) {
          val[i] = attr_desc->floats[i];
        }
W
wangliu 已提交
75
        attr.Set<vector<float>>(val);
L
liuruilong 已提交
76 77 78
        break;
      }
      case PADDLE_MOBILE__FRAMEWORK__PROTO__ATTR_TYPE__STRINGS: {
W
wangliu 已提交
79
        vector<string> val(attr_desc->n_strings);
L
liuruilong 已提交
80 81 82
        for (int i = 0; i < attr_desc->n_strings; ++i) {
          val[i] = attr_desc->strings[i];
        }
W
wangliu 已提交
83
        attr.Set<vector<string>>(val);
L
liuruilong 已提交
84 85 86 87 88 89 90 91 92 93 94 95
        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;
  }

96
  Attribute() {}
朔-望's avatar
朔-望 已提交
97 98
  template <typename T, typename... Args>
  Attribute &Set(Args &&... args) {
99 100 101
    variant_.Set<T>(args...);
    return *this;
  }
朔-望's avatar
朔-望 已提交
102

朔-望's avatar
朔-望 已提交
103 104 105 106
  template <typename T>
  T &Get() const {
    return variant_.Get<T>();
  }
107

L
liuruilong 已提交
108 109 110 111 112 113
  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>());
W
wangliu 已提交
114 115 116 117 118 119 120 121
    } else if (attr.variant_.TypeId() == typeid(string).hash_code()) {
      return vistor(attr.variant_.Get<string>());
    } else if (attr.variant_.TypeId() == typeid(vector<int>).hash_code()) {
      return vistor(attr.variant_.Get<vector<int>>());
    } else if (attr.variant_.TypeId() == typeid(vector<float>).hash_code()) {
      return vistor(attr.variant_.Get<vector<float>>());
    } else if (attr.variant_.TypeId() == typeid(vector<string>).hash_code()) {
      return vistor(attr.variant_.Get<vector<string>>());
L
liuruilong 已提交
122 123
    } else if (attr.variant_.TypeId() == typeid(bool).hash_code()) {
      return vistor(attr.variant_.Get<bool>());
W
wangliu 已提交
124 125
    } else if (attr.variant_.TypeId() == typeid(vector<bool>).hash_code()) {
      return vistor(attr.variant_.Get<vector<bool>>());
L
liuruilong 已提交
126
    } else if (attr.variant_.TypeId() == typeid(int64_t).hash_code()) {
L
liuruilong 已提交
127 128 129 130 131 132
      return vistor(attr.variant_.Get<int64_t>());
    } else {
      throw std::bad_exception();
    }
  }

朔-望's avatar
朔-望 已提交
133
 private:
W
wangliu 已提交
134 135
  Variant<int, float, string, vector<int>, vector<float>, vector<string>, bool,
          vector<bool>, BlockDesc *, int64_t>
136
      variant_;
朔-望's avatar
朔-望 已提交
137
};
朔-望's avatar
朔-望 已提交
138

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

朔-望's avatar
朔-望 已提交
141
class AttrReader {
朔-望's avatar
朔-望 已提交
142
 public:
143
  explicit AttrReader(const AttributeMap &attrs) : attrs_(attrs) {}
朔-望's avatar
朔-望 已提交
144

朔-望's avatar
朔-望 已提交
145
  template <typename T>
W
wangliu 已提交
146 147 148
  inline T Get(const string &name) const {
    PADDLE_MOBILE_ENFORCE(attrs_.count(name) != 0,
                          "%s should  be in AttributeMap", name);
149 150
    return ((Attribute)attrs_.at(name)).Get<T>();
  }
朔-望's avatar
朔-望 已提交
151

朔-望's avatar
朔-望 已提交
152
 private:
153
  const AttributeMap &attrs_;
朔-望's avatar
朔-望 已提交
154
};
朔-望's avatar
朔-望 已提交
155

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

朔-望's avatar
朔-望 已提交
158 159
}  // namespace framework
}  // namespace paddle_mobile