未验证 提交 f5a7df25 编写于 作者: H huzhiqiang 提交者: GitHub

[BUGFIX][Hash] Improve the implementation of hash_combine #3573

上级 2a344823
...@@ -24,9 +24,9 @@ namespace lite_api { ...@@ -24,9 +24,9 @@ namespace lite_api {
size_t Place::hash() const { size_t Place::hash() const {
std::hash<int> h; std::hash<int> h;
size_t hash = h(static_cast<int>(target)); size_t hash = h(static_cast<int>(target));
hash = lite::hash_combine(hash, static_cast<int>(precision)); lite::CombineHash(static_cast<int64_t>(precision), &hash);
hash = lite::hash_combine(hash, static_cast<int>(layout)); lite::CombineHash(static_cast<int64_t>(layout), &hash);
hash = lite::hash_combine(hash, static_cast<int>(device)); lite::CombineHash(static_cast<int64_t>(device), &hash);
return hash; return hash;
} }
......
...@@ -42,16 +42,14 @@ bool OpLite::InferShapeWithCache() { ...@@ -42,16 +42,14 @@ bool OpLite::InferShapeWithCache() {
// combined dims value into new_hash value. // combined dims value into new_hash value.
auto &element_dims = (*iter)->dims(); auto &element_dims = (*iter)->dims();
for (size_t i = 0; i < element_dims.size(); i++) { for (size_t i = 0; i < element_dims.size(); i++) {
new_hash = lite::CombineHash(static_cast<int64_t>(element_dims[i]), &new_hash);
lite::hash_combine(new_hash, static_cast<int>(element_dims[i]));
} }
// combine lod value into new_hash valud. // combine lod value into new_hash valud.
auto &emement_lods = (*iter)->lod(); auto &emement_lods = (*iter)->lod();
for (auto lod_iter = emement_lods.begin(); lod_iter != emement_lods.end(); for (auto lod_iter = emement_lods.begin(); lod_iter != emement_lods.end();
lod_iter++) { lod_iter++) {
for (size_t i = 0; i < lod_iter->size(); i++) { for (size_t i = 0; i < lod_iter->size(); i++) {
new_hash = lite::CombineHash(static_cast<int64_t>(lod_iter->at(i)), &new_hash);
lite::hash_combine(new_hash, static_cast<int>(lod_iter->at(i)));
} }
} }
} }
......
...@@ -21,9 +21,9 @@ namespace lite { ...@@ -21,9 +21,9 @@ namespace lite {
size_t ParamTypeRegistry::KernelIdTy::hash() const { size_t ParamTypeRegistry::KernelIdTy::hash() const {
std::hash<std::string> h; std::hash<std::string> h;
size_t hash = h(kernel_type); size_t hash = h(kernel_type);
hash = hash_combine(hash, place.hash()); lite::CombineHash(place.hash(), &hash);
hash = hash_combine(hash, std::hash<int>()(static_cast<int>(io))); lite::CombineHash(std::hash<int>()(static_cast<int>(io)), &hash);
hash = hash_combine(hash, std::hash<std::string>()(arg_name)); lite::CombineHash(std::hash<std::string>()(arg_name), &hash);
return hash; return hash;
} }
...@@ -48,8 +48,7 @@ const Type *Type::GetTensorTy(TargetType target, ...@@ -48,8 +48,7 @@ const Type *Type::GetTensorTy(TargetType target,
// NOTE quite naive implementation here, but not performance sensitive. // NOTE quite naive implementation here, but not performance sensitive.
DataType::ID type_id = DataType::ID::Tensor; DataType::ID type_id = DataType::ID::Tensor;
#define HASH_ONE(x) v = hash_combine(v, hasher(static_cast<int>(x))) #define HASH_ONE(x) CombineHash(hasher(static_cast<int>(x)), &v);
std::hash<int> hasher; std::hash<int> hasher;
size_t v = hasher(static_cast<int>(type_id)); size_t v = hasher(static_cast<int>(type_id));
HASH_ONE(target); HASH_ONE(target);
...@@ -80,8 +79,7 @@ const Type *Type::GetTensorListTy(TargetType target, ...@@ -80,8 +79,7 @@ const Type *Type::GetTensorListTy(TargetType target,
static std::map<size_t, const Type *> type_repo; static std::map<size_t, const Type *> type_repo;
DataType::ID type_id = DataType::ID::TensorList; DataType::ID type_id = DataType::ID::TensorList;
#define HASH_ONE(x) v = hash_combine(v, hasher(static_cast<int>(x))) #define HASH_ONE(x) CombineHash(hasher(static_cast<int>(x)), &v);
std::hash<int> hasher; std::hash<int> hasher;
size_t v = hasher(static_cast<int>(type_id)); size_t v = hasher(static_cast<int>(type_id));
HASH_ONE(target); HASH_ONE(target);
......
...@@ -18,10 +18,11 @@ ...@@ -18,10 +18,11 @@
namespace paddle { namespace paddle {
namespace lite { namespace lite {
// A simplified implementation of boost::hash_combine.
template <typename T> template <typename T>
inline size_t hash_combine(size_t s, const T& v) { inline void CombineHash(const T& from, size_t* to) {
std::hash<T> h; std::hash<T> h;
return (s ^ h(v)) + 0x9e3779b9 + (s << 6) + (s >> 2); *to ^= h(from) + 0x9e3779b9 + (*to << 6) + (*to >> 2);
} }
} // namespace lite } // namespace lite
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册