Skip to content

Commit da4e602

Browse files
mrshenlifacebook-github-bot
authored andcommitted
Keep Reducer hooks in a vector instead of an unordered_map (#21783)
Summary: kuttas pointed out that the DDP Reducer only needs to remember `uintptr, Function` pairs, and hence does not need a nunordered map as added by #21591. Using a vector should speed it up a bit. Pull Request resolved: #21783 Differential Revision: D15854312 Pulled By: mrshenli fbshipit-source-id: 153ba035b8d658c7878a613f16a42de977d89c43
1 parent 76713fb commit da4e602

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

torch/csrc/distributed/c10d/reducer.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -105,14 +105,17 @@ Reducer::Reducer(
105105
auto grad_accumulator = variable.grad_accumulator();
106106

107107
// Hook to execute after the gradient accumulator has executed.
108-
hooks_[grad_accumulator->add_post_hook(
109-
torch::make_unique<LambdaPostHook>([=] {
110-
std::lock_guard<std::mutex> lock(this->mutex_);
111-
this->mark_variable_ready(
112-
replica_index,
113-
variable_index,
114-
/* called_from_autograd= */ true);
115-
}))] = grad_accumulator;
108+
hooks_.emplace_back(
109+
grad_accumulator->add_post_hook(
110+
torch::make_unique<LambdaPostHook>([=] {
111+
std::lock_guard<std::mutex> lock(this->mutex_);
112+
this->mark_variable_ready(
113+
replica_index,
114+
variable_index,
115+
/* called_from_autograd= */ true);
116+
})),
117+
grad_accumulator
118+
);
116119

117120
// Map raw function pointer to replica index and parameter index.
118121
// This is used later on when the autograd graph is traversed

torch/csrc/distributed/c10d/reducer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class Reducer {
5454
std::vector<std::vector<std::shared_ptr<torch::autograd::Function>>>
5555
grad_accumulators_;
5656
std::unordered_map<torch::autograd::Function*, std::tuple<int, int>> func_;
57-
std::unordered_map<uintptr_t, std::shared_ptr<torch::autograd::Function>>
57+
std::vector<std::pair<uintptr_t, std::shared_ptr<torch::autograd::Function>>>
5858
hooks_;
5959

6060
bool expect_autograd_hooks_;

0 commit comments

Comments
 (0)