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 <cmath>
31 #include <functional>
32 #include <limits>
33 #include <string>
34 
35 namespace tvm {
36 
41  protected:
42  template <typename T, typename U>
43  uint64_t Reinterpret(T value) const {
44  union Union {
45  T a;
46  U b;
47  } u;
48  static_assert(sizeof(Union) == sizeof(T), "sizeof(Union) != sizeof(T)");
49  static_assert(sizeof(Union) == sizeof(U), "sizeof(Union) != sizeof(U)");
50  u.b = 0;
51  u.a = value;
52  return u.b;
53  }
54 
55  public:
56  uint64_t operator()(const float& key) const { return Reinterpret<float, uint32_t>(key); }
57  uint64_t operator()(const double& key) const {
58  if (std::isnan(key)) {
59  // The IEEE format defines more than one bit-pattern that
60  // represents NaN. For the purpose of comparing IR
61  // representations, all NaN values are considered equivalent.
62  return Reinterpret<double, uint64_t>(std::numeric_limits<double>::quiet_NaN());
63  } else {
64  return Reinterpret<double, uint64_t>(key);
65  }
66  }
67  uint64_t operator()(const int64_t& key) const { return Reinterpret<int64_t, uint64_t>(key); }
68  uint64_t operator()(const uint64_t& key) const { return key; }
69  uint64_t operator()(const int& key) const { return Reinterpret<int, uint32_t>(key); }
70  uint64_t operator()(const bool& key) const { return key; }
71  uint64_t operator()(const runtime::DataType& key) const {
72  return Reinterpret<DLDataType, uint32_t>(key);
73  }
74  template <typename ENum, typename = typename std::enable_if<std::is_enum<ENum>::value>::type>
75  uint64_t operator()(const ENum& key) const {
76  return Reinterpret<int64_t, uint64_t>(static_cast<int64_t>(key));
77  }
78  uint64_t operator()(const std::string& key) const {
79  return runtime::String::StableHashBytes(key.data(), key.length());
80  }
81 };
82 
94 class StructuralHash : public BaseValueHash {
95  public:
96  // inherit operator()
97  using BaseValueHash::operator();
103  TVM_DLL uint64_t operator()(const ObjectRef& key) const;
104 };
105 
122  public:
124  class Handler {
125  public:
131  virtual void SHashReduceHashedValue(uint64_t hashed_value) = 0;
138  virtual void SHashReduce(const ObjectRef& key, bool map_free_vars) = 0;
152  virtual void SHashReduceFreeVar(const runtime::Object* var, bool map_free_vars) = 0;
161  virtual bool LookupHashedValue(const ObjectRef& key, uint64_t* hashed_value) = 0;
166  virtual void MarkGraphNode() = 0;
167  };
168 
170  SHashReducer() = default;
176  explicit SHashReducer(Handler* handler, bool map_free_vars)
177  : handler_(handler), map_free_vars_(map_free_vars) {}
182  template <typename T,
183  typename = typename std::enable_if<!std::is_base_of<ObjectRef, T>::value>::type>
184  void operator()(const T& key) const {
185  // handle normal values.
186  handler_->SHashReduceHashedValue(BaseValueHash()(key));
187  }
192  void operator()(const ObjectRef& key) const { return handler_->SHashReduce(key, map_free_vars_); }
198  void DefHash(const ObjectRef& key) const { return handler_->SHashReduce(key, true); }
203  void FreeVarHashImpl(const runtime::Object* var) const {
204  handler_->SHashReduceFreeVar(var, map_free_vars_);
205  }
206 
208  Handler* operator->() const { return handler_; }
209 
210  private:
212  Handler* handler_;
218  bool map_free_vars_;
219 };
220 
227  public:
230 
231  void SHashReduceHashedValue(uint64_t hashed_value) override;
232  void SHashReduce(const ObjectRef& key, bool map_free_vars) override;
233  void SHashReduceFreeVar(const runtime::Object* var, bool map_free_vars) override;
234  bool LookupHashedValue(const ObjectRef& key, uint64_t* hashed_value) override;
235  void MarkGraphNode() override;
236 
243  virtual uint64_t Hash(const ObjectRef& object, bool map_free_vars);
244 
245  protected:
251  virtual void DispatchSHash(const ObjectRef& object, bool map_free_vars);
252 
253  private:
254  class Impl;
255  Impl* impl;
256 };
257 
258 class SEqualReducer;
260  static constexpr const std::nullptr_t VisitAttrs = nullptr;
261  static void SHashReduce(const runtime::NDArray::Container* key, SHashReducer hash_reduce);
264 };
265 
266 } // namespace tvm
267 #endif // TVM_NODE_STRUCTURAL_HASH_H_
Hash definition of base value classes.
Definition: structural_hash.h:40
uint64_t operator()(const int &key) const
Definition: structural_hash.h:69
uint64_t operator()(const bool &key) const
Definition: structural_hash.h:70
uint64_t operator()(const double &key) const
Definition: structural_hash.h:57
uint64_t operator()(const runtime::DataType &key) const
Definition: structural_hash.h:71
uint64_t operator()(const std::string &key) const
Definition: structural_hash.h:78
uint64_t Reinterpret(T value) const
Definition: structural_hash.h:43
uint64_t operator()(const ENum &key) const
Definition: structural_hash.h:75
uint64_t operator()(const uint64_t &key) const
Definition: structural_hash.h:68
uint64_t operator()(const float &key) const
Definition: structural_hash.h:56
uint64_t operator()(const int64_t &key) const
Definition: structural_hash.h:67
A Reducer class to reduce the structural equality result of two objects.
Definition: structural_equal.h:137
The default handler for hash key computation.
Definition: structural_hash.h:226
virtual uint64_t Hash(const ObjectRef &object, bool map_free_vars)
The entry point for hashing.
virtual void DispatchSHash(const ObjectRef &object, bool map_free_vars)
The dispatcher for hashing of intermediate objects.
bool LookupHashedValue(const ObjectRef &key, uint64_t *hashed_value) override
Lookup a hash value for key.
void MarkGraphNode() override
Mark current comparison as graph node in hashing. Graph node hash will depends on the graph structure...
void SHashReduceHashedValue(uint64_t hashed_value) override
Append hashed_value to the current sequence of hashes.
void SHashReduce(const ObjectRef &key, bool map_free_vars) override
Append hash value of key to the current sequence of hashes.
void SHashReduceFreeVar(const runtime::Object *var, bool map_free_vars) override
Append a hash value of free variable to the current sequence of hashes.
Internal handler that defines custom behaviors.
Definition: structural_hash.h:124
virtual bool LookupHashedValue(const ObjectRef &key, uint64_t *hashed_value)=0
Lookup a hash value for key.
virtual void SHashReduce(const ObjectRef &key, bool map_free_vars)=0
Append hash value of key to the current sequence of hashes.
virtual void SHashReduceHashedValue(uint64_t hashed_value)=0
Append hashed_value to the current sequence of hashes.
virtual void MarkGraphNode()=0
Mark current comparison as graph node in hashing. Graph node hash will depends on the graph structure...
virtual void SHashReduceFreeVar(const runtime::Object *var, bool map_free_vars)=0
Append a hash value of free variable to the current sequence of hashes.
A Reducer class to reduce the structural hash value.
Definition: structural_hash.h:121
void operator()(const T &key) const
Push hash of key to the current sequence of hash values.
Definition: structural_hash.h:184
void operator()(const ObjectRef &key) const
Push hash of key to the current sequence of hash values.
Definition: structural_hash.h:192
Handler * operator->() const
Definition: structural_hash.h:208
void FreeVarHashImpl(const runtime::Object *var) const
Implementation for hash for a free var.
Definition: structural_hash.h:203
void DefHash(const ObjectRef &key) const
Push hash of key to the current sequence of hash values.
Definition: structural_hash.h:198
SHashReducer(Handler *handler, bool map_free_vars)
Constructor with a specific handler.
Definition: structural_hash.h:176
SHashReducer()=default
default constructor
Content-aware structural hashing.
Definition: structural_hash.h:94
uint64_t operator()(const ObjectRef &key) const
Compute structural hashing value for an object.
Runtime primitive data type.
Definition: data_type.h:43
Object container class that backs NDArray.
Definition: ndarray.h:306
Base class of all object reference.
Definition: object.h:519
base class of all object containers.
Definition: object.h:171
static uint64_t StableHashBytes(const char *data, size_t size)
Hash the binary bytes.
Definition: string.h:251
Defines the Functor data structures.
IntSet Union(const Array< IntSet > &sets)
Create a union set of all sets, possibly relaxed.
Var var(std::string name_hint, DataType t=DataType::Int(32))
Construct a new Var expression.
const Op & isnan()
Check if value is nan.
runtime implementation for LibTorch/TorchScript.
Definition: analyzer.h:36
PrimExpr equal(PrimExpr a, PrimExpr b, Span span=Span())
equal
A device-independent managed NDArray abstraction.
Definition: structural_hash.h:259
static constexpr const std::nullptr_t VisitAttrs
Definition: structural_hash.h:260
static void SHashReduce(const runtime::NDArray::Container *key, SHashReducer hash_reduce)
static bool SEqualReduce(const runtime::NDArray::Container *lhs, const runtime::NDArray::Container *rhs, SEqualReducer equal)