@@ -36,52 +36,60 @@ namespace tvm {
3636 * \brief Hash definition of base value classes.
3737 */
3838class BaseValueHash {
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 ));
39+ protected:
40+ template <typename T, typename U>
41+ uint64_t Reinterpret (T value) const {
42+ union Union {
43+ T a;
44+ U b;
45+ } u;
46+ static_assert (sizeof (Union) == sizeof (T), " sizeof(Union) != sizeof(T)" );
47+ static_assert (sizeof (Union) == sizeof (U), " sizeof(Union) != sizeof(U)" );
48+ u.b = 0 ;
49+ u.a = value;
50+ return u.b ;
5651 }
5752
53+ public:
54+ uint64_t operator ()(const float & key) const { return Reinterpret<float , uint32_t >(key); }
55+ uint64_t operator ()(const double & key) const { return Reinterpret<double , uint64_t >(key); }
56+ uint64_t operator ()(const int64_t & key) const { return Reinterpret<int64_t , uint64_t >(key); }
57+ uint64_t operator ()(const uint64_t & key) const { return key; }
58+ uint64_t operator ()(const int & key) const { return Reinterpret<int , uint32_t >(key); }
59+ uint64_t operator ()(const bool & key) const { return key; }
60+ uint64_t operator ()(const runtime::DataType& key) const {
61+ return Reinterpret<DLDataType, uint32_t >(key);
62+ }
5863 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));
64+ uint64_t operator ()(const ENum& key) const {
65+ return Reinterpret<int64_t , uint64_t >(static_cast <int64_t >(key));
66+ }
67+ uint64_t operator ()(const std::string& key) const {
68+ return runtime::String::StableHashBytes (key.data (), key.length ());
6169 }
6270};
6371
6472/* !
65- * \brief Content-aware structural hasing .
73+ * \brief Content-aware structural hashing .
6674 *
6775 * The structural hash value is recursively defined in the DAG of IRNodes.
6876 * There are two kinds of nodes:
6977 *
7078 * - Normal node: the hash value is defined by its content and type only.
7179 * - Graph node: each graph node will be assigned a unique index ordered by the
72- * first occurence during the visit. The hash value of a graph node is
80+ * first occurrence during the visit. The hash value of a graph node is
7381 * combined from the hash values of its contents and the index.
7482 */
7583class StructuralHash : public BaseValueHash {
7684 public:
77- // inheritate operator()
85+ // inherit operator()
7886 using BaseValueHash::operator ();
7987 /* !
8088 * \brief Compute structural hashing value for an object.
8189 * \param key The left operand.
8290 * \return The hash value.
8391 */
84- TVM_DLL size_t operator ()(const ObjectRef& key) const ;
92+ TVM_DLL uint64_t operator ()(const ObjectRef& key) const ;
8593};
8694
8795/* !
@@ -109,23 +117,23 @@ class SHashReducer {
109117 *
110118 * \param hashed_value The hashed value
111119 */
112- virtual void SHashReduceHashedValue (size_t hashed_value) = 0;
120+ virtual void SHashReduceHashedValue (uint64_t hashed_value) = 0;
113121 /* !
114122 * \brief Append hash value of key to the current sequence of hashes.
115123 *
116124 * \param key The object to compute hash from.
117- * \param map_free_vars Whether to map free variables by their occurence number.
125+ * \param map_free_vars Whether to map free variables by their occurrence number.
118126 */
119127 virtual void SHashReduce (const ObjectRef& key, bool map_free_vars) = 0;
120128 /* !
121- * \brief Apppend a hash value of free variable to the current sequence of hashes.
129+ * \brief Append a hash value of free variable to the current sequence of hashes.
122130 *
123131 * \param var The var of interest.
124- * \param map_free_vars Whether to map free variables by their occurence number.
132+ * \param map_free_vars Whether to map free variables by their occurrence number.
125133 *
126134 * \note If map_free_vars is set to be true,
127135 * internally the handler can maintain a counter to encode free variables
128- * by their order of occurence . This helps to resolve variable
136+ * by their order of occurrence . This helps to resolve variable
129137 * mapping of function parameters and let binding variables.
130138 *
131139 * If map_free_vars is set to be false, the address of the variable will be used.
@@ -139,7 +147,7 @@ class SHashReducer {
139147 *
140148 * \return Whether there is already a pre-computed hash value.
141149 */
142- virtual bool LookupHashedValue (const ObjectRef& key, size_t * hashed_value) = 0;
150+ virtual bool LookupHashedValue (const ObjectRef& key, uint64_t * hashed_value) = 0;
143151 /* !
144152 * \brief Mark current comparison as graph node in hashing.
145153 * Graph node hash will depends on the graph structure.
@@ -193,7 +201,7 @@ class SHashReducer {
193201 /* ! \brief Internal class pointer. */
194202 Handler* handler_;
195203 /* !
196- * \brief Whether or not to map free variables by their occurence
204+ * \brief Whether or not to map free variables by their occurrence
197205 * If the flag is false, then free variables will be mapped
198206 * by their in-memory address.
199207 */
@@ -210,10 +218,10 @@ class SHashHandlerDefault : public SHashReducer::Handler {
210218 SHashHandlerDefault ();
211219 virtual ~SHashHandlerDefault ();
212220
213- void SHashReduceHashedValue (size_t hashed_value) override ;
221+ void SHashReduceHashedValue (uint64_t hashed_value) override ;
214222 void SHashReduce (const ObjectRef& key, bool map_free_vars) override ;
215223 void SHashReduceFreeVar (const runtime::Object* var, bool map_free_vars) override ;
216- bool LookupHashedValue (const ObjectRef& key, size_t * hashed_value) override ;
224+ bool LookupHashedValue (const ObjectRef& key, uint64_t * hashed_value) override ;
217225 void MarkGraphNode () override ;
218226
219227 /* !
@@ -222,7 +230,7 @@ class SHashHandlerDefault : public SHashReducer::Handler {
222230 * \param map_free_vars Whether or not to remap variables if possible.
223231 * \return The hash result.
224232 */
225- virtual size_t Hash (const ObjectRef& object, bool map_free_vars);
233+ virtual uint64_t Hash (const ObjectRef& object, bool map_free_vars);
226234
227235 protected:
228236 /* !
0 commit comments