tvm
attr_registry_map.h
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
23 #ifndef TVM_NODE_ATTR_REGISTRY_MAP_H_
24 #define TVM_NODE_ATTR_REGISTRY_MAP_H_
25 
26 #include <tvm/ffi/string.h>
27 
28 #include <utility>
29 #include <vector>
30 
31 namespace tvm {
32 
37 template <typename KeyType>
39  public:
45  int count(const KeyType& key) const {
46  if (key.defined()) {
47  const uint32_t idx = key->AttrRegistryIndex();
48  return idx < data_.size() ? (data_[idx].second != 0) : 0;
49  } else {
50  return 0;
51  }
52  }
58  const ffi::Any& operator[](const KeyType& key) const {
59  ICHECK(key.defined());
60  const uint32_t idx = key->AttrRegistryIndex();
61  ICHECK(idx < data_.size() && data_[idx].second != 0)
62  << "Attribute " << attr_name_ << " has not been registered for " << key->name;
63  return data_[idx].first;
64  }
72  template <typename ValueType>
73  ValueType get(const KeyType& key, ValueType def_value) const {
74  ICHECK(key.defined());
75  const uint32_t idx = key->AttrRegistryIndex();
76  if (idx < data_.size() && data_[idx].second != 0) {
77  if constexpr (std::is_same_v<ValueType, ffi::Any>) {
78  return data_[idx].first;
79  } else {
80  return data_[idx].first.template cast<ValueType>();
81  }
82  } else {
83  return def_value;
84  }
85  }
86 
87  private:
89  String attr_name_;
91  std::vector<std::pair<ffi::Any, int>> data_;
93  AttrRegistryMapContainerMap() = default;
94  template <typename, typename>
95  friend class AttrRegistry;
96  friend class OpRegEntry;
97 };
98 
104 template <typename KeyType, typename ValueType>
106  public:
117  int count(const KeyType& key) const { return map_.count(key); }
123  ValueType operator[](const KeyType& key) const {
124  if constexpr (std::is_same_v<ValueType, ffi::Any>) {
125  return map_[key];
126  } else {
127  return map_[key].template cast<ValueType>();
128  }
129  }
136  ValueType get(const KeyType& key, ValueType def_value) const { return map_.get(key, def_value); }
137 
138  protected:
141 };
142 
143 } // namespace tvm
144 #endif // TVM_NODE_ATTR_REGISTRY_MAP_H_
Generic attribute map.
Definition: attr_registry_map.h:38
const ffi::Any & operator[](const KeyType &key) const
get the corresponding value element at key.
Definition: attr_registry_map.h:58
int count(const KeyType &key) const
Check if the map has key.
Definition: attr_registry_map.h:45
ValueType get(const KeyType &key, ValueType def_value) const
get the corresponding value element at key with default value.
Definition: attr_registry_map.h:73
Map<Key, ValueType> used to store meta-data.
Definition: attr_registry_map.h:105
const AttrRegistryMapContainerMap< KeyType > & map_
The internal map field.
Definition: attr_registry_map.h:140
ValueType get(const KeyType &key, ValueType def_value) const
get the corresponding value element at key with default value.
Definition: attr_registry_map.h:136
int count(const KeyType &key) const
Check if the map has op as key.
Definition: attr_registry_map.h:117
ValueType operator[](const KeyType &key) const
get the corresponding value element at key.
Definition: attr_registry_map.h:123
AttrRegistryMap(const AttrRegistryMapContainerMap< KeyType > &map)
constructor
Definition: attr_registry_map.h:111
Definition: instruction.h:30
Helper structure to register operators.
Definition: op.h:172
Performance counters for profiling via the PAPI library.
Definition: analyzer.h:37