提交 cd1192aa 编写于 作者: H huzhiqiang 提交者: GitHub

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

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