attribute.h 10.1 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);

40 41
Attribute GetAttrValue(const proto::VarDesc::Attr& attr_desc);

T
tangwei12 已提交
42 43 44 45 46 47 48 49
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 已提交
50 51
      attr_value = &paddle::get<T>(attr);
    } catch (paddle::bad_variant_access const& bad_get) {
52
      PADDLE_THROW(platform::errors::InvalidArgument(
53 54
          "Cannot get attribute (%s) by type %s, its type is %s.",
          attr_name_,
55 56
          paddle::platform::demangle(typeid(T).name()),
          paddle::platform::demangle(attr.type().name())));
T
tangwei12 已提交
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
    }
    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 已提交
77
      int val = PADDLE_GET_CONST(int, attr);
T
tangwei12 已提交
78 79
      attr = static_cast<bool>(val);
    } else if (attr.type() == typeid(float)) {  // NOLINT
R
Ruibiao Chen 已提交
80
      float val = PADDLE_GET_CONST(float, attr);
T
tangwei12 已提交
81 82 83 84
      attr = static_cast<bool>(val);
    }
    bool* attr_value = nullptr;
    try {
R
Ruibiao Chen 已提交
85 86
      attr_value = &paddle::get<bool>(attr);
    } catch (paddle::bad_variant_access const& bad_get) {
87
      PADDLE_THROW(platform::errors::InvalidArgument(
88 89
          "Cannot get attribute (%s) by type bool, its type is %s.",
          attr_name_,
90
          paddle::platform::demangle(attr.type().name())));
T
tangwei12 已提交
91 92 93 94 95 96 97 98 99 100 101 102 103 104
    }
    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 已提交
105
      int val = PADDLE_GET_CONST(int, attr);
T
tangwei12 已提交
106 107
      attr = static_cast<int64_t>(val);
    } else if (attr.type() == typeid(float)) {  // NOLINT
R
Ruibiao Chen 已提交
108
      int val = PADDLE_GET_CONST(float, attr);
T
tangwei12 已提交
109 110 111 112
      attr = static_cast<int64_t>(val);
    }
    int64_t* attr_value = nullptr;
    try {
R
Ruibiao Chen 已提交
113 114
      attr_value = &paddle::get<int64_t>(attr);
    } catch (paddle::bad_variant_access const& bad_get) {
115 116
      PADDLE_THROW(platform::errors::InvalidArgument(
          "Cannot get attribute (%s) by type int64_t, its type is %s.",
117 118
          attr_name_,
          paddle::platform::demangle(attr.type().name())));
T
tangwei12 已提交
119 120 121 122 123 124 125 126 127 128 129 130 131 132
    }
    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 已提交
133
      std::vector<int> val = PADDLE_GET_CONST(std::vector<int>, attr);
T
tangwei12 已提交
134 135 136
      std::vector<int64_t> vec(val.begin(), val.end());
      attr = vec;
    } else if (attr.type() == typeid(std::vector<float>)) {  // NOLINT
R
Ruibiao Chen 已提交
137
      std::vector<float> val = PADDLE_GET_CONST(std::vector<float>, attr);
T
tangwei12 已提交
138 139 140 141 142
      std::vector<int64_t> vec(val.begin(), val.end());
      attr = vec;
    }
    std::vector<int64_t>* attr_value = nullptr;
    try {
R
Ruibiao Chen 已提交
143 144
      attr_value = &paddle::get<std::vector<int64_t>>(attr);
    } catch (paddle::bad_variant_access const& bad_get) {
145 146 147
      PADDLE_THROW(platform::errors::InvalidArgument(
          "Cannot get attribute (%s) by type std::vector<int64_t>, its type is "
          "%s.",
148 149
          attr_name_,
          paddle::platform::demangle(attr.type().name())));
T
tangwei12 已提交
150 151 152 153 154 155
    }
    return attr_value;
  }

  const std::string& attr_name_;
};
156 157 158 159 160 161 162 163

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

  const std::string& attr_name_;
};
T
tangwei12 已提交
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 214 215
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_;
};

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

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

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

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

257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
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 已提交
279 280
class AttrReader {
 public:
281 282 283 284 285
  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 已提交
286 287 288

  template <typename T>
  inline const T& Get(const std::string& name) const {
289 290 291 292 293 294 295 296
    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();
      }
    }
297 298
    PADDLE_ENFORCE_EQ(found,
                      true,
299 300
                      platform::errors::NotFound(
                          "Attribute (%s) should be in AttributeMap.", name));
T
tangwei12 已提交
301

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

308
  const Attribute* GetAttr(const std::string& name) const {
309 310 311 312 313 314 315 316
    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();
      }
    }
317 318 319 320
    if (found) {
      return &it->second;
    }
    return nullptr;
321 322
  }

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

328 329
}  // namespace framework
}  // namespace paddle