tvm
structural_hash.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_STRUCTURAL_HASH_H_
24 #define TVM_NODE_STRUCTURAL_HASH_H_
25 
26 #include <tvm/node/functor.h>
27 #include <tvm/runtime/data_type.h>
28 
29 #include <functional>
30 #include <string>
31 
32 namespace tvm {
33 
38  public:
39  size_t operator()(const double& key) const { return std::hash<double>()(key); }
40 
41  size_t operator()(const int64_t& key) const { return std::hash<int64_t>()(key); }
42 
43  size_t operator()(const uint64_t& key) const { return std::hash<uint64_t>()(key); }
44 
45  size_t operator()(const int& key) const { return std::hash<int>()(key); }
46 
47  size_t operator()(const bool& key) const { return std::hash<bool>()(key); }
48 
49  size_t operator()(const std::string& key) const { return std::hash<std::string>()(key); }
50 
51  size_t operator()(const runtime::DataType& key) const {
52  return std::hash<int32_t>()(static_cast<int32_t>(key.code()) |
53  (static_cast<int32_t>(key.bits()) << 8) |
54  (static_cast<int32_t>(key.lanes()) << 16));
55  }
56 
57  template <typename ENum, typename = typename std::enable_if<std::is_enum<ENum>::value>::type>
58  bool operator()(const ENum& key) const {
59  return std::hash<size_t>()(static_cast<size_t>(key));
60  }
61 };
62 
74 class StructuralHash : public BaseValueHash {
75  public:
76  // inheritate operator()
77  using BaseValueHash::operator();
83  TVM_DLL size_t operator()(const ObjectRef& key) const;
84 };
85 
102  public:
104  class Handler {
105  public:
111  virtual void SHashReduceHashedValue(size_t hashed_value) = 0;
118  virtual void SHashReduce(const ObjectRef& key, bool map_free_vars) = 0;
132  virtual void SHashReduceFreeVar(const runtime::Object* var, bool map_free_vars) = 0;
141  virtual bool LookupHashedValue(const ObjectRef& key, size_t* hashed_value) = 0;
146  virtual void MarkGraphNode() = 0;
147  };
148 
150  SHashReducer() = default;
156  explicit SHashReducer(Handler* handler, bool map_free_vars)
157  : handler_(handler), map_free_vars_(map_free_vars) {}
162  template <typename T,
163  typename = typename std::enable_if<!std::is_base_of<ObjectRef, T>::value>::type>
164  void operator()(const T& key) const {
165  // handle normal values.
166  handler_->SHashReduceHashedValue(BaseValueHash()(key));
167  }
172  void operator()(const ObjectRef& key) const { return handler_->SHashReduce(key, map_free_vars_); }
178  void DefHash(const ObjectRef& key) const { return handler_->SHashReduce(key, true); }
184  void FreeVarHashImpl(const runtime::Object* var) const {
185  handler_->SHashReduceFreeVar(var, map_free_vars_);
186  }
187 
189  Handler* operator->() const { return handler_; }
190 
191  private:
193  Handler* handler_;
199  bool map_free_vars_;
200 };
201 
202 } // namespace tvm
203 #endif // TVM_NODE_STRUCTURAL_HASH_H_
void FreeVarHashImpl(const runtime::Object *var) const
Implementation for hash for a free var.
Definition: structural_hash.h:184
bool operator()(const ENum &key) const
Definition: structural_hash.h:58
Internal handler that defines custom behaviors.
Definition: structural_hash.h:104
size_t operator()(const double &key) const
Definition: structural_hash.h:39
Performance counters for profiling via the PAPI library.
Definition: analyzer.h:36
A Reducer class to reduce the structural hash value.
Definition: structural_hash.h:101
int code() const
Definition: data_type.h:81
size_t operator()(const bool &key) const
Definition: structural_hash.h:47
size_t operator()(const int64_t &key) const
Definition: structural_hash.h:41
base class of all object containers.
Definition: object.h:165
Handler * operator->() const
Definition: structural_hash.h:189
Runtime primitive data type.
Definition: data_type.h:41
int bits() const
Definition: data_type.h:83
Hash definition of base value classes.
Definition: structural_hash.h:37
size_t operator()(const int &key) const
Definition: structural_hash.h:45
int lanes() const
Definition: data_type.h:87
Var var(std::string name_hint, DataType t=DataType::Int(32))
Construct a new Var expression.
Defines the Functor data structures.
Base class of all object reference.
Definition: object.h:504
SHashReducer(Handler *handler, bool map_free_vars)
Constructor with a specific handler.
Definition: structural_hash.h:156
size_t operator()(const runtime::DataType &key) const
Definition: structural_hash.h:51
size_t operator()(const std::string &key) const
Definition: structural_hash.h:49
void operator()(const T &key) const
Push hash of key to the current sequence of hash values.
Definition: structural_hash.h:164
Content-aware structural hasing.
Definition: structural_hash.h:74
size_t operator()(const uint64_t &key) const
Definition: structural_hash.h:43
void operator()(const ObjectRef &key) const
Push hash of key to the current sequence of hash values.
Definition: structural_hash.h:172
void DefHash(const ObjectRef &key) const
Push hash of key to the current sequence of hash values.
Definition: structural_hash.h:178