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 
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 runtime::TVMRetValue& 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  return data_[idx].first;
78  } else {
79  return def_value;
80  }
81  }
82 
83  private:
85  String attr_name_;
87  std::vector<std::pair<runtime::TVMRetValue, int>> data_;
89  AttrRegistryMapContainerMap() = default;
90  template <typename, typename>
91  friend class AttrRegistry;
92  friend class OpRegEntry;
93 };
94 
100 template <typename KeyType, typename ValueType>
102  public:
113  int count(const KeyType& key) const { return map_.count(key); }
119  ValueType operator[](const KeyType& key) const { return map_[key]; }
126  ValueType get(const KeyType& key, ValueType def_value) const { return map_.get(key, def_value); }
127 
128  protected:
131 };
132 
133 } // namespace tvm
134 #endif // TVM_NODE_ATTR_REGISTRY_MAP_H_
Generic attribute map.
Definition: attr_registry_map.h:38
const runtime::TVMRetValue & 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:101
const AttrRegistryMapContainerMap< KeyType > & map_
The internal map field.
Definition: attr_registry_map.h:130
ValueType get(const KeyType &key, ValueType def_value) const
get the corresponding value element at key with default value.
Definition: attr_registry_map.h:126
int count(const KeyType &key) const
Check if the map has op as key.
Definition: attr_registry_map.h:113
ValueType operator[](const KeyType &key) const
get the corresponding value element at key.
Definition: attr_registry_map.h:119
AttrRegistryMap(const AttrRegistryMapContainerMap< KeyType > &map)
constructor
Definition: attr_registry_map.h:107
Definition: executor.h:43
Helper structure to register operators.
Definition: op.h:205
Reference to string objects.
Definition: string.h:98
Return Value container, Unlike TVMArgValue, which only holds reference and do not delete the underlyi...
Definition: packed_func.h:946
runtime implementation for LibTorch/TorchScript.
Definition: analyzer.h:36
Runtime String container types.