attribute.h 10.0 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
Y
Yi Wang 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14

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. */

15 16
#pragma once

17
#include <stdint.h>
18

19
#include <functional>
20
#include <iosfwd>
21 22
#include <string>
#include <unordered_map>
Y
Yu Yang 已提交
23
#include <unordered_set>
24
#include <vector>
Y
Yi Wang 已提交
25

Y
Yi Wang 已提交
26 27 28
#include "paddle/fluid/framework/framework.pb.h"
#include "paddle/fluid/framework/type_defs.h"
#include "paddle/fluid/platform/enforce.h"
29
#include "paddle/fluid/platform/errors.h"
30
#include "paddle/utils/any.h"
R
Ruibiao Chen 已提交
31
#include "paddle/utils/variant.h"
32 33 34

namespace paddle {
namespace framework {
T
tangwei12 已提交
35

36 37 38 39
paddle::any GetAttrValue(const Attribute& attr);

Attribute GetAttrValue(const proto::OpDesc::Attr& attr_desc);

T
tangwei12 已提交
40 41 42 43 44 45 46 47
template <typename T>
struct ExtractAttribute {
  explicit ExtractAttribute(const std::string& attr_name)
      : attr_name_(attr_name) {}

  T* operator()(Attribute& attr) const {
    T* attr_value = nullptr;
    try {
R
Ruibiao Chen 已提交
48 49
      attr_value = &paddle::get<T>(attr);
    } catch (paddle::bad_variant_access const& bad_get) {
50
      PADDLE_THROW(platform::errors::InvalidArgument(
51 52
          "Cannot get attribute (%s) by type %s, its type is %s.",
          attr_name_,
53 54
          paddle::platform::demangle(typeid(T).name()),
          paddle::platform::demangle(attr.type().name())));
T
tangwei12 已提交
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
    }
    return attr_value;
  }

  const std::string& attr_name_;
};

// special handle bool
// FIXME(yuyang18): Currently we cast bool into int in python binding. It is
// hard to change the logic there. In another way, we should correct handle
// if the user set `some_flag=1`.
//
// FIX ME anytime if there is a better solution.
template <>
struct ExtractAttribute<bool> {
  explicit ExtractAttribute(const std::string& attr_name)
      : attr_name_(attr_name) {}

  bool* operator()(Attribute& attr) const {
    if (attr.type() == typeid(int)) {  // NOLINT
R
Ruibiao Chen 已提交
75
      int val = PADDLE_GET_CONST(int, attr);
T
tangwei12 已提交
76 77
      attr = static_cast<bool>(val);
    } else if (attr.type() == typeid(float)) {  // NOLINT
R
Ruibiao Chen 已提交
78
      float val = PADDLE_GET_CONST(float, attr);
T
tangwei12 已提交
79 80 81 82
      attr = static_cast<bool>(val);
    }
    bool* attr_value = nullptr;
    try {
R
Ruibiao Chen 已提交
83 84
      attr_value = &paddle::get<bool>(attr);
    } catch (paddle::bad_variant_access const& bad_get) {
85
      PADDLE_THROW(platform::errors::InvalidArgument(
86 87
          "Cannot get attribute (%s) by type bool, its type is %s.",
          attr_name_,
88
          paddle::platform::demangle(attr.type().name())));
T
tangwei12 已提交
89 90 91 92 93 94 95 96 97 98 99 100 101 102
    }
    return attr_value;
  }

  const std::string& attr_name_;
};

template <>
struct ExtractAttribute<int64_t> {
  explicit ExtractAttribute(const std::string& attr_name)
      : attr_name_(attr_name) {}

  int64_t* operator()(Attribute& attr) const {
    if (attr.type() == typeid(int)) {  // NOLINT
R
Ruibiao Chen 已提交
103
      int val = PADDLE_GET_CONST(int, attr);
T
tangwei12 已提交
104 105
      attr = static_cast<int64_t>(val);
    } else if (attr.type() == typeid(float)) {  // NOLINT
R
Ruibiao Chen 已提交
106
      int val = PADDLE_GET_CONST(float, attr);
T
tangwei12 已提交
107 108 109 110
      attr = static_cast<int64_t>(val);
    }
    int64_t* attr_value = nullptr;
    try {
R
Ruibiao Chen 已提交
111 112
      attr_value = &paddle::get<int64_t>(attr);
    } catch (paddle::bad_variant_access const& bad_get) {
113 114
      PADDLE_THROW(platform::errors::InvalidArgument(
          "Cannot get attribute (%s) by type int64_t, its type is %s.",
115 116
          attr_name_,
          paddle::platform::demangle(attr.type().name())));
T
tangwei12 已提交
117 118 119 120 121 122 123 124 125 126 127 128 129 130
    }
    return attr_value;
  }

  const std::string& attr_name_;
};

template <>
struct ExtractAttribute<std::vector<int64_t>> {
  explicit ExtractAttribute(const std::string& attr_name)
      : attr_name_(attr_name) {}

  std::vector<int64_t>* operator()(Attribute& attr) const {
    if (attr.type() == typeid(std::vector<int>)) {  // NOLINT
R
Ruibiao Chen 已提交
131
      std::vector<int> val = PADDLE_GET_CONST(std::vector<int>, attr);
T
tangwei12 已提交
132 133 134
      std::vector<int64_t> vec(val.begin(), val.end());
      attr = vec;
    } else if (attr.type() == typeid(std::vector<float>)) {  // NOLINT
R
Ruibiao Chen 已提交
135
      std::vector<float> val = PADDLE_GET_CONST(std::vector<float>, attr);
T
tangwei12 已提交
136 137 138 139 140
      std::vector<int64_t> vec(val.begin(), val.end());
      attr = vec;
    }
    std::vector<int64_t>* attr_value = nullptr;
    try {
R
Ruibiao Chen 已提交
141 142
      attr_value = &paddle::get<std::vector<int64_t>>(attr);
    } catch (paddle::bad_variant_access const& bad_get) {
143 144 145
      PADDLE_THROW(platform::errors::InvalidArgument(
          "Cannot get attribute (%s) by type std::vector<int64_t>, its type is "
          "%s.",
146 147
          attr_name_,
          paddle::platform::demangle(attr.type().name())));
T
tangwei12 已提交
148 149 150 151 152 153
    }
    return attr_value;
  }

  const std::string& attr_name_;
};
154 155 156 157 158 159 160 161

template <>
struct ExtractAttribute<float> {
  explicit ExtractAttribute(const std::string& attr_name)
      : attr_name_(attr_name) {}

  float* operator()(Attribute& attr) const {
    if (attr.type() == typeid(int)) {  // NOLINT
R
Ruibiao Chen 已提交
162
      int val = PADDLE_GET_CONST(int, attr);
163 164
      attr = static_cast<float>(val);
    } else if (attr.type() == typeid(int64_t)) {  // NOLINT
R
Ruibiao Chen 已提交
165
      int64_t val = PADDLE_GET_CONST(int64_t, attr);
166 167 168 169
      attr = static_cast<float>(val);
    }
    float* attr_value = nullptr;
    try {
R
Ruibiao Chen 已提交
170 171
      attr_value = &paddle::get<float>(attr);
    } catch (paddle::bad_variant_access const& bad_get) {
172 173
      PADDLE_THROW(platform::errors::InvalidArgument(
          "Cannot get attribute (%s) by type float, its type is %s.",
174 175
          attr_name_,
          paddle::platform::demangle(attr.type().name())));
176 177 178 179 180 181
    }
    return attr_value;
  }

  const std::string& attr_name_;
};
T
tangwei12 已提交
182

183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
template <>
struct ExtractAttribute<double> {
  explicit ExtractAttribute(const std::string& attr_name)
      : attr_name_(attr_name) {}

  double* operator()(Attribute& attr) const {
    if (attr.type() == typeid(int)) {  // NOLINT
      int val = PADDLE_GET_CONST(int, attr);
      attr = static_cast<double>(val);
    } else if (attr.type() == typeid(int64_t)) {  // NOLINT
      int64_t val = PADDLE_GET_CONST(int64_t, attr);
      attr = static_cast<double>(val);
    } else if (attr.type() == typeid(float)) {  // NOLINT
      int64_t val = PADDLE_GET_CONST(float, attr);
      attr = static_cast<double>(val);
    }
    double* attr_value = nullptr;
    try {
      attr_value = &paddle::get<double>(attr);
    } catch (paddle::bad_variant_access const& bad_get) {
      PADDLE_THROW(platform::errors::InvalidArgument(
          "Cannot get attribute (%s) by type double, its type is %s.",
          attr_name_,
          paddle::platform::demangle(attr.type().name())));
    }
    return attr_value;
  }

  const std::string& attr_name_;
};

214 215 216 217 218 219 220
template <>
struct ExtractAttribute<std::vector<double>> {
  explicit ExtractAttribute(const std::string& attr_name)
      : attr_name_(attr_name) {}

  std::vector<double>* operator()(Attribute& attr) const {
    if (attr.type() == typeid(std::vector<int>)) {  // NOLINT
R
Ruibiao Chen 已提交
221
      std::vector<int> val = PADDLE_GET_CONST(std::vector<int>, attr);
222 223 224
      std::vector<double> vec(val.begin(), val.end());
      attr = vec;
    } else if (attr.type() == typeid(std::vector<float>)) {  // NOLINT
R
Ruibiao Chen 已提交
225
      std::vector<float> val = PADDLE_GET_CONST(std::vector<float>, attr);
226 227 228 229 230
      std::vector<double> vec(val.begin(), val.end());
      attr = vec;
    }
    std::vector<double>* attr_value = nullptr;
    try {
R
Ruibiao Chen 已提交
231 232
      attr_value = &paddle::get<std::vector<double>>(attr);
    } catch (paddle::bad_variant_access const& bad_get) {
233 234 235
      PADDLE_THROW(platform::errors::InvalidArgument(
          "Cannot get attribute (%s) by type std::vector<double>, its type is "
          "%s.",
236 237
          attr_name_,
          paddle::platform::demangle(attr.type().name())));
238 239 240 241 242 243
    }
    return attr_value;
  }

  const std::string& attr_name_;
};
C
Chen Weihang 已提交
244

Y
Yi Wang 已提交
245
template <typename T>
246
inline proto::AttrType AttrTypeID() {
Y
Yu Yang 已提交
247
  Attribute tmp = T();
R
Ruibiao Chen 已提交
248
  return static_cast<proto::AttrType>(tmp.index() - 1);
Y
Yu Yang 已提交
249
}
Y
Yi Wang 已提交
250

C
Chen Weihang 已提交
251
inline proto::AttrType AttrTypeID(const Attribute& attr) {
R
Ruibiao Chen 已提交
252
  return static_cast<proto::AttrType>(attr.index() - 1);
C
Chen Weihang 已提交
253 254
}

255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
inline bool IsAttrVar(const Attribute& attr) {
  return AttrTypeID(attr) == proto::AttrType::VAR;
}

inline bool IsAttrVars(const Attribute& attr) {
  return AttrTypeID(attr) == proto::AttrType::VARS;
}

inline bool HasAttrVar(const Attribute& attr) {
  return IsAttrVar(attr) || IsAttrVars(attr);
}

inline AttributeMap FilterAttrVar(const AttributeMap& attrs) {
  AttributeMap attrs_var;
  for (auto& attr : attrs) {
    if (HasAttrVar(attr.second)) {
      attrs_var.emplace(attr);
    }
  }
  return attrs_var;
}

Q
Qiao Longfei 已提交
277 278
class AttrReader {
 public:
279 280 281 282 283
  explicit AttrReader(const AttributeMap& attrs)
      : attrs_(attrs), default_attrs_(nullptr) {}

  AttrReader(const AttributeMap& attrs, const AttributeMap& default_attrs)
      : attrs_(attrs), default_attrs_(&default_attrs) {}
Q
Qiao Longfei 已提交
284 285 286

  template <typename T>
  inline const T& Get(const std::string& name) const {
287 288 289 290 291 292 293 294
    auto it = attrs_.find(name);
    bool found = it != attrs_.end();
    if (!found) {
      if (default_attrs_ != nullptr) {
        it = default_attrs_->find(name);
        found = it != default_attrs_->end();
      }
    }
295 296
    PADDLE_ENFORCE_EQ(found,
                      true,
297 298
                      platform::errors::NotFound(
                          "Attribute (%s) should be in AttributeMap.", name));
T
tangwei12 已提交
299

300
    Attribute& attr = const_cast<Attribute&>(it->second);
T
tangwei12 已提交
301 302 303
    ExtractAttribute<T> extract_attr(name);
    T* attr_value = extract_attr(attr);
    return *attr_value;
Q
Qiao Longfei 已提交
304 305
  }

306
  const Attribute* GetAttr(const std::string& name) const {
307 308 309 310 311 312 313 314
    auto it = attrs_.find(name);
    bool found = it != attrs_.end();
    if (!found) {
      if (default_attrs_ != nullptr) {
        it = default_attrs_->find(name);
        found = it != default_attrs_->end();
      }
    }
315 316 317 318
    if (found) {
      return &it->second;
    }
    return nullptr;
319 320
  }

Q
Qiao Longfei 已提交
321 322
 private:
  const AttributeMap& attrs_;
323
  const AttributeMap* default_attrs_;
Q
Qiao Longfei 已提交
324 325
};

326 327
}  // namespace framework
}  // namespace paddle