builtin_attribute_storage.h 4.6 KB
Newer Older
Z
zhangbo9674 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
// Copyright (c) 2023 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.

#pragma once

#include <algorithm>
#include <map>
#include <type_traits>

#include "paddle/ir/attribute.h"
22
#include "paddle/ir/utils.h"
Z
zhangbo9674 已提交
23 24

namespace ir {
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47

#define DECLARE_BASE_TYPE_ATTRIBUTE_STORAGE(concrete_storage, base_type) \
  struct concrete_storage : public ir::AttributeStorage {                \
    using ParamKey = bool;                                               \
                                                                         \
    explicit concrete_storage(const ParamKey &key) { data_ = key; }      \
                                                                         \
    static concrete_storage *Construct(ParamKey key) {                   \
      return new concrete_storage(key);                                  \
    }                                                                    \
                                                                         \
    static std::size_t HashValue(const ParamKey &key) {                  \
      return std::hash<base_type>()(key);                                \
    }                                                                    \
                                                                         \
    bool operator==(const ParamKey &key) const { return data_ == key; }  \
                                                                         \
    ParamKey GetAsKey() const { return data_; }                          \
                                                                         \
   private:                                                              \
    ParamKey data_;                                                      \
  };

Z
zhangbo9674 已提交
48 49 50
///
/// \brief Define Parameteric AttributeStorage for StrAttribute.
///
51
struct StrAttributeStorage : public AttributeStorage {
Z
zhangbo9674 已提交
52 53 54 55
  using ParamKey = std::string;

  explicit StrAttributeStorage(const ParamKey &key) {
    data_ = reinterpret_cast<char *>(malloc(key.size()));
56
    memcpy(data_, key.c_str(), key.size());
Z
zhangbo9674 已提交
57 58 59 60 61 62 63 64 65 66 67 68 69 70
    size_ = key.size();
  }

  ~StrAttributeStorage() { free(data_); }

  static StrAttributeStorage *Construct(ParamKey key) {
    return new StrAttributeStorage(key);
  }

  static std::size_t HashValue(const ParamKey &key) {
    return std::hash<std::string>()(key);
  }

  bool operator==(const ParamKey &key) const {
71
    return std::equal(data_, data_ + size_, key.c_str());
Z
zhangbo9674 已提交
72 73 74 75 76 77 78 79 80
  }

  ParamKey GetAsKey() const { return ParamKey(data_, size_); }

 private:
  char *data_;
  uint32_t size_;
};

81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 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 129 130 131
DECLARE_BASE_TYPE_ATTRIBUTE_STORAGE(BoolAttributeStorage, bool);
DECLARE_BASE_TYPE_ATTRIBUTE_STORAGE(FloatAttributeStorage, float);
DECLARE_BASE_TYPE_ATTRIBUTE_STORAGE(DoubleAttributeStorage, double);
DECLARE_BASE_TYPE_ATTRIBUTE_STORAGE(Int32_tAttributeStorage, int32_t);
DECLARE_BASE_TYPE_ATTRIBUTE_STORAGE(Int64_tAttributeStorage, int64_t);

struct ArrayAttributeStorage : public AttributeStorage {
  using ParamKey = std::vector<Attribute>;

  explicit ArrayAttributeStorage(const ParamKey &key) {
    data_ =
        reinterpret_cast<Attribute *>(malloc(sizeof(Attribute) * key.size()));
    memcpy(reinterpret_cast<void *>(data_),
           reinterpret_cast<const void *>(key.data()),
           sizeof(Attribute) * key.size());
    length_ = key.size();
  }

  ~ArrayAttributeStorage() { free(reinterpret_cast<void *>(data_)); }

  static ArrayAttributeStorage *Construct(ParamKey key) {
    return new ArrayAttributeStorage(key);
  }

  static std::size_t HashValue(const ParamKey &key) {
    std::size_t hash_value = 0;
    for (size_t i = 0; i < key.size(); ++i) {
      hash_value = hash_combine(hash_value, std::hash<Attribute>()(key[i]));
    }
    return hash_value;
  }

  bool operator==(const ParamKey &key) const {
    if (key.size() != length_) {
      return false;
    }
    for (size_t i = 0; i < length_; ++i) {
      if (data_[i] != key[i]) {
        return false;
      }
    }
    return true;
  }

  ParamKey GetAsKey() const { return ParamKey(data_, data_ + length_); }

 private:
  Attribute *data_ = nullptr;
  size_t length_ = 0;
};

Z
zhangbo9674 已提交
132
}  // namespace ir