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

L
liuruilong 已提交
17
#include "common/log.h"
朔-望's avatar
朔-望 已提交
18
#include "common/variant.h"
L
liuruilong 已提交
19
#include "framework/framework.pb.h"
朔-望's avatar
朔-望 已提交
20 21

namespace paddle_mobile {
朔-望's avatar
朔-望 已提交
22
namespace framework {
朔-望's avatar
朔-望 已提交
23

朔-望's avatar
朔-望 已提交
24
class BlockDesc;
朔-望's avatar
朔-望 已提交
25

朔-望's avatar
朔-望 已提交
26
class Attribute {
朔-望's avatar
朔-望 已提交
27
 public:
28 29 30 31
  static Attribute GetAttrValue(const proto::OpDesc::Attr &attr_desc) {
    //    std::cout << "begin get attr value" << std::endl;
    Attribute attr;
    switch (attr_desc.type()) {
朔-望's avatar
朔-望 已提交
32 33 34
      case proto::AttrType::BOOLEAN: {
        attr.Set<bool>(attr_desc.b());
        break;
35
      }
朔-望's avatar
朔-望 已提交
36 37 38
      case proto::AttrType::INT: {
        attr.Set<int>(attr_desc.i());
        break;
39
      }
朔-望's avatar
朔-望 已提交
40 41 42
      case proto::AttrType::FLOAT: {
        attr.Set<float>(attr_desc.f());
        break;
43
      }
朔-望's avatar
朔-望 已提交
44 45 46
      case proto::AttrType::STRING: {
        attr.Set<std::string>(attr_desc.s());
        break;
47
      }
朔-望's avatar
朔-望 已提交
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
      case proto::AttrType::BOOLEANS: {
        std::vector<bool> val(attr_desc.bools_size());
        for (int i = 0; i < attr_desc.bools_size(); ++i) {
          val[i] = attr_desc.bools(i);
        }
        attr.Set<std::vector<bool>>(val);
        break;
      }
      case proto::AttrType::INTS: {
        std::vector<int> val(attr_desc.ints_size());
        for (int i = 0; i < attr_desc.ints_size(); ++i) {
          val[i] = attr_desc.ints(i);
        }
        attr.Set<std::vector<int>>(val);
        break;
      }
      case proto::AttrType::FLOATS: {
        std::vector<float> val(attr_desc.floats_size());
        for (int i = 0; i < attr_desc.floats_size(); ++i) {
          val[i] = attr_desc.floats(i);
        }
        attr.Set<std::vector<float>>(val);
        break;
      }
      case proto::AttrType::STRINGS: {
        std::vector<std::string> val(attr_desc.strings_size());
        for (int i = 0; i < attr_desc.strings_size(); ++i) {
          val[i] = attr_desc.strings(i);
        }
        attr.Set<std::vector<std::string>>(val);
        break;
      }
      case proto::AttrType::LONG: {
        attr.Set<int64_t>(attr_desc.l());
        break;
      }
      default:
        //        std::cout << " not support " << std::endl;
        break;
87 88 89 90
    }
    //    std::cout << "end get attr value" << std::endl;
    return attr;
  }
朔-望's avatar
朔-望 已提交
91

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

朔-望's avatar
朔-望 已提交
99 100 101 102
  template <typename T>
  T &Get() const {
    return variant_.Get<T>();
  }
103

L
liuruilong 已提交
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 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>>());
    } else if (attr.variant_.TypeId() == typeid(std::vector<float>).hash_code()) {
      return vistor(attr.variant_.Get<std::vector<float>>());
    } else if (attr.variant_.TypeId() == typeid(std::vector<std::string>).hash_code()) {
      return vistor(attr.variant_.Get<std::vector<std::string>>());
    } else if (attr.variant_.TypeId() == typeid(bool).hash_code()) {
      return vistor(attr.variant_.Get<bool>());
    } else if (attr.variant_.TypeId() == typeid(std::vector<bool>).hash_code()) {
      return vistor(attr.variant_.Get<std::vector<bool>>());
    }  else if (attr.variant_.TypeId() == typeid(int64_t).hash_code()) {
      return vistor(attr.variant_.Get<int64_t>());
    } else {
      throw std::bad_exception();
    }
  }

朔-望's avatar
朔-望 已提交
129
 private:
130 131 132 133
  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
朔-望 已提交
134
};
朔-望's avatar
朔-望 已提交
135

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

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

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

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

L
liuruilong 已提交
155 156 157 158 159



Print &operator<<(Print &printer, const Attribute &op_desc);

朔-望's avatar
朔-望 已提交
160 161
}  // namespace framework
}  // namespace paddle_mobile