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 #include <tvm/runtime/ndarray.h>
29 
30 #include <functional>
31 #include <string>
32 
33 namespace tvm {
34 
39  public:
40  size_t operator()(const double& key) const { return std::hash<double>()(key); }
41 
42  size_t operator()(const int64_t& key) const { return std::hash<int64_t>()(key); }
43 
44  size_t operator()(const uint64_t& key) const { return std::hash<uint64_t>()(key); }
45 
46  size_t operator()(const int& key) const { return std::hash<int>()(key); }
47 
48  size_t operator()(const bool& key) const { return std::hash<bool>()(key); }
49 
50  size_t operator()(const std::string& key) const { return std::hash<std::string>()(key); }
51 
52  size_t operator()(const runtime::DataType& key) const {
53  return std::hash<int32_t>()(static_cast<int32_t>(key.code()) |
54  (static_cast<int32_t>(key.bits()) << 8) |
55  (static_cast<int32_t>(key.lanes()) << 16));
56  }
57 
58  template <typename ENum, typename = typename std::enable_if<std::is_enum<ENum>::value>::type>
59  bool operator()(const ENum& key) const {
60  return std::hash<size_t>()(static_cast<size_t>(key));
61  }
62 };
63 
75 class StructuralHash : public BaseValueHash {
76  public:
77  // inheritate operator()
78  using BaseValueHash::operator();
84  TVM_DLL size_t operator()(const ObjectRef& key) const;
85 };
86 
103  public:
105  class Handler {
106  public:
112  virtual void SHashReduceHashedValue(size_t hashed_value) = 0;
119  virtual void SHashReduce(const ObjectRef& key, bool map_free_vars) = 0;
133  virtual void SHashReduceFreeVar(const runtime::Object* var, bool map_free_vars) = 0;
142  virtual bool LookupHashedValue(const ObjectRef& key, size_t* hashed_value) = 0;
147  virtual void MarkGraphNode() = 0;
148  };
149 
151  SHashReducer() = default;
157  explicit SHashReducer(Handler* handler, bool map_free_vars)
158  : handler_(handler), map_free_vars_(map_free_vars) {}
163  template <typename T,
164  typename = typename std::enable_if<!std::is_base_of<ObjectRef, T>::value>::type>
165  void operator()(const T& key) const {
166  // handle normal values.
167  handler_->SHashReduceHashedValue(BaseValueHash()(key));
168  }
173  void operator()(const ObjectRef& key) const { return handler_->SHashReduce(key, map_free_vars_); }
179  void DefHash(const ObjectRef& key) const { return handler_->SHashReduce(key, true); }
185  void FreeVarHashImpl(const runtime::Object* var) const {
186  handler_->SHashReduceFreeVar(var, map_free_vars_);
187  }
188 
190  Handler* operator->() const { return handler_; }
191 
192  private:
194  Handler* handler_;
200  bool map_free_vars_;
201 };
202 
203 class SEqualReducer;
205  static constexpr const std::nullptr_t VisitAttrs = nullptr;
206  static void SHashReduce(const runtime::NDArray::Container* key, SHashReducer hash_reduce);
207  static bool SEqualReduce(const runtime::NDArray::Container* lhs,
209 };
210 
211 } // namespace tvm
212 #endif // TVM_NODE_STRUCTURAL_HASH_H_
void FreeVarHashImpl(const runtime::Object *var) const
Implementation for hash for a free var.
Definition: structural_hash.h:185
bool operator()(const ENum &key) const
Definition: structural_hash.h:59
Internal handler that defines custom behaviors.
Definition: structural_hash.h:105
A Reducer class to reduce the structural equality result of two objects.
Definition: structural_equal.h:102
size_t operator()(const double &key) const
Definition: structural_hash.h:40
runtime implementation for LibTorch/TorchScript.
Definition: analyzer.h:36
A Reducer class to reduce the structural hash value.
Definition: structural_hash.h:102
PrimExpr equal(PrimExpr a, PrimExpr b, Span span=Span())
equal
int code() const
Definition: data_type.h:81
size_t operator()(const bool &key) const
Definition: structural_hash.h:48
size_t operator()(const int64_t &key) const
Definition: structural_hash.h:42
base class of all object containers.
Definition: object.h:167
A device-independent managed NDArray abstraction.
Definition: structural_hash.h:204
Handler * operator->() const
Definition: structural_hash.h:190
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:38
Object container class that backs NDArray.
Definition: ndarray.h:294
size_t operator()(const int &key) const
Definition: structural_hash.h:46
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:511
SHashReducer(Handler *handler, bool map_free_vars)
Constructor with a specific handler.
Definition: structural_hash.h:157
size_t operator()(const runtime::DataType &key) const
Definition: structural_hash.h:52
size_t operator()(const std::string &key) const
Definition: structural_hash.h:50
void operator()(const T &key) const
Push hash of key to the current sequence of hash values.
Definition: structural_hash.h:165
Content-aware structural hasing.
Definition: structural_hash.h:75
size_t operator()(const uint64_t &key) const
Definition: structural_hash.h:44
void operator()(const ObjectRef &key) const
Push hash of key to the current sequence of hash values.
Definition: structural_hash.h:173
void DefHash(const ObjectRef &key) const
Push hash of key to the current sequence of hash values.
Definition: structural_hash.h:179