Skip to content

Commit

Permalink
Replace poor hash function with hash_combine
Browse files Browse the repository at this point in the history
These hashes use hash<int> which is the identity function. So no
hashing occurs at all!
  • Loading branch information
suranap committed Dec 14, 2024
1 parent 899ddea commit 97ad611
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 31 deletions.
27 changes: 14 additions & 13 deletions src/runtime/machine_view.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "flexflow/machine_view.h"
#include "flexflow/utils/hash_utils.h"

namespace FlexFlow {

Expand Down Expand Up @@ -47,13 +48,13 @@ size_t MachineView::num_parts() const {
}

size_t MachineView::hash() const {
size_t ret = 17;
ret = ret * 31 + std::hash<int>()(device_type);
ret = ret * 31 + std::hash<int>()(ndims);
ret = ret * 31 + std::hash<int>()(start_device_id);
size_t ret = 0;
hash_combine(ret, device_type);
hash_combine(ret, ndims);
hash_combine(ret, start_device_id);
for (int i = 0; i < ndims; i++) {
ret = ret * 31 + std::hash<int>()(dim[i]);
ret = ret * 31 + std::hash<int>()(stride[i]);
hash_combine(ret, dim[i]);
hash_combine(ret, stride[i]);
}
return ret;
}
Expand Down Expand Up @@ -116,12 +117,12 @@ MachineResource::MachineResource(FFConfig const &config)
available_gpus_per_node(config.workersPerNode) {}

size_t MachineResource::hash() const {
size_t ret = 17;
ret = ret * 31 + std::hash<int>()(num_nodes);
ret = ret * 31 + std::hash<int>()(available_gpus_per_node);
ret = ret * 31 + std::hash<int>()(available_cpus_per_node);
ret = ret * 31 + std::hash<int>()(start_gpu_id);
ret = ret * 31 + std::hash<int>()(start_cpu_id);
size_t ret = 0;
hash_combine(ret, num_nodes);
hash_combine(ret, available_gpus_per_node);
hash_combine(ret, available_cpus_per_node);
hash_combine(ret, start_gpu_id);
hash_combine(ret, start_cpu_id);
return ret;
}

Expand All @@ -132,4 +133,4 @@ size_t hash<FlexFlow::MachineView>::operator()(
FlexFlow::MachineView const &mv) const {
return mv.hash();
}
}; // namespace std
}; // namespace std
13 changes: 7 additions & 6 deletions src/runtime/parallel_tensor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -449,13 +449,14 @@ bool ParallelTensorBase::get_output_sub_tensor(ParallelConfig const &pc,
}

size_t ParallelTensorBase::get_owner_independent_hash() const {
size_t hash = 17 * 31 + std::hash<int>()((int)data_type);
hash = hash * 31 + std::hash<int>()((int)sync_type);
hash = hash * 31 + std::hash<int>()(num_dims);
size_t hash = 0;
hash_combine(hash, static_cast<int>(data_type));
hash_combine(hash, static_cast<int>(sync_type));
hash_combine(hash, num_dims);
for (int i = 0; i < num_dims; i++) {
hash = hash * 31 + std::hash<int>()(dims[i].size);
hash = hash * 31 + std::hash<int>()(dims[i].degree);
hash = hash * 31 + std::hash<int>()(dims[i].parallel_idx);
hash_combine(hash, dims[i].size);
hash_combine(hash, dims[i].degree);
hash_combine(hash, dims[i].parallel_idx);
}
return hash;
}
Expand Down
29 changes: 17 additions & 12 deletions src/runtime/simulator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -315,8 +315,9 @@ SimTask *TaskManager::new_comm_task(std::string const &name,
SimTask *TaskManager::new_forward_task(Op const *op, int idx) {
SimTask *task = new_task();
task->type = SimTask::TASK_FORWARD;
size_t hash = 17 * 31 + (size_t)(op);
hash = hash * 31 + std::hash<int>()(idx);
size_t hash = 0;
hash_combine(hash, (size_t)op);
hash_combine(hash, idx);
hash_to_forward_task[hash] = task;
task->name = op->name;
return task;
Expand All @@ -325,23 +326,26 @@ SimTask *TaskManager::new_forward_task(Op const *op, int idx) {
SimTask *TaskManager::new_backward_task(Op const *op, int idx) {
SimTask *task = new_task();
task->type = SimTask::TASK_BACKWARD;
size_t hash = 17 * 31 + (size_t)(op);
hash = hash * 31 + std::hash<int>()(idx);
size_t hash = 0;
hash_combine(hash, (size_t)op);
hash_combine(hash, idx);
hash_to_backward_task[hash] = task;
task->name = op->name;
return task;
}

SimTask *TaskManager::get_forward_task(Op const *op, int idx) {
size_t hash = 17 * 31 + (size_t)(op);
hash = hash * 31 + std::hash<int>()(idx);
size_t hash = 0;
hash_combine(hash, (size_t)op);
hash_combine(hash, idx);
assert(hash_to_forward_task.find(hash) != hash_to_forward_task.end());
return hash_to_forward_task[hash];
}

SimTask *TaskManager::get_backward_task(Op const *op, int idx) {
size_t hash = 17 * 31 + (size_t)(op);
hash = hash * 31 + std::hash<int>()(idx);
size_t hash = 0;
hash_combine(hash, (size_t)op);
hash_combine(hash, idx);
assert(hash_to_backward_task.find(hash) != hash_to_backward_task.end());
return hash_to_backward_task[hash];
}
Expand Down Expand Up @@ -535,11 +539,12 @@ CostMetrics Simulator::measure_operator_cost(Op const *op,
return this->strict_hash_to_operator_cost.at(key);
}

size_t hash = 17 * 31 + op->get_untyped_params_hash();
hash = hash * 31 + std::hash<int>()(mv.device_type);
hash = hash * 31 + std::hash<int>()(mv.ndims);
size_t hash = 0;
hash_combine(hash, op->get_untyped_params_hash());
hash_combine(hash, mv.device_type);
hash_combine(hash, mv.ndims);
for (int i = 0; i < mv.ndims; i++) {
hash = hash * 31 + std::hash<int>()(mv.dim[i]);
hash_combine(hash, mv.dim[i]);
}
std::unordered_map<size_t, CostMetrics>::const_iterator iter =
hash_to_operator_cost.find(hash);
Expand Down

0 comments on commit 97ad611

Please sign in to comment.