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 tvm::ffi::details::StableHashBytes(key.data(), key.length());
80  }
81  uint64_t operator()(const Optional<int64_t>& key) const {
82  if (key.has_value()) {
83  return Reinterpret<int64_t, uint64_t>(*key);
84  } else {
85  return 0;
86  }
87  }
88  uint64_t operator()(const Optional<double>& key) const {
89  if (key.has_value()) {
90  return Reinterpret<double, uint64_t>(*key);
91  } else {
92  return 0;
93  }
94  }
100  TVM_FFI_INLINE uint64_t HashPODValueInAny(const ffi::Any& key) const {
101  return ffi::details::AnyUnsafe::TVMFFIAnyPtrFromAny(key)->v_uint64;
102  }
103 };
104 
117  public:
118  // inherit operator()
119  using BaseValueHash::operator();
125  TVM_DLL uint64_t operator()(const ffi::Any& key) const;
126 };
127 
128 } // namespace tvm
129 #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
TVM_FFI_INLINE uint64_t HashPODValueInAny(const ffi::Any &key) const
Compute structural hash value for a POD value in Any.
Definition: structural_hash.h:100
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 Optional< double > &key) const
Definition: structural_hash.h:88
uint64_t operator()(const Optional< int64_t > &key) const
Definition: structural_hash.h:81
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
Content-aware structural hashing.
Definition: structural_hash.h:116
uint64_t operator()(const ffi::Any &key) const
Compute structural hashing value for an object.
Runtime primitive data type.
Definition: data_type.h:47
Defines the Functor data structures.
IntSet Union(const Array< IntSet > &sets)
Create a union set of all sets, possibly relaxed.
const Op & isnan()
Check if value is nan.
Performance counters for profiling via the PAPI library.
Definition: analyzer.h:37
A device-independent managed NDArray abstraction.