Lock optimizations for DistAutogradContainer.#36529
Lock optimizations for DistAutogradContainer.#36529pritamdamania87 wants to merge 2 commits intogh/pritamdamania87/117/basefrom
Conversation
DistAutogradContainer is a singleton for the entire process and has a single lock that protects access to map keyed by context_id. Performance profiling showed that this lock is a potential bottleneck for training. As a result, in this PR, we have the following optimizations: 1) Shard the map into 256 buckets with each bucket having its own lock. This would ensure we hold much finer grained locks. 2) sendReleaseContextRpc was being called under a lock, moved this to be outside the lock. Differential Revision: [D21003934](https://our.internmc.facebook.com/intern/diff/D21003934/) [ghstack-poisoned]
DistAutogradContainer is a singleton for the entire process and has a single lock that protects access to map keyed by context_id. Performance profiling showed that this lock is a potential bottleneck for training. As a result, in this PR, we have the following optimizations: 1) Shard the map into 256 buckets with each bucket having its own lock. This would ensure we hold much finer grained locks. 2) sendReleaseContextRpc was being called under a lock, moved this to be outside the lock. Differential Revision: [D21003934](https://our.internmc.facebook.com/intern/diff/D21003934/) ghstack-source-id: 102066593 Pull Request resolved: #36529
💊 Build failures summary and remediationsAs of commit 9cd489f (more details on the Dr. CI page):
XLA failureJob pytorch_xla_linux_xenial_py3_6_clang7_build is failing. Please create an issue with title prefixed by This comment was automatically generated by Dr. CI (expand for details).Follow this link to opt-out of these comments for your Pull Requests.Please report bugs/suggestions on the GitHub issue tracker. This comment has been revised 5 times. |
| inline DistAutogradContainer::ContextsShard& DistAutogradContainer::getShard( | ||
| int64_t context_id) { | ||
| // kNumShards has to be a power of 2 for this to work. | ||
| DCHECK((kNumShards & (kNumShards - 1)) == 0); |
There was a problem hiding this comment.
minor: maybe static_assert() instead?
There was a problem hiding this comment.
Moved the check to init instead.
| }; | ||
|
|
||
| // Number of shards for the map storing autograd contexts. | ||
| static constexpr int16_t kNumShards = 256; |
There was a problem hiding this comment.
This value of 256 is fine.
fwiw, my feeling is that it might be slightly on the high side (e.g. my initial guess would be circa 64??) - when I've done lock sharding in the past, I tended to see diminishing returns much above num_cpus (or num_cpus*2) shards.
There was a problem hiding this comment.
should we use some heuristic formula (sth like @jjlilley mentioned above) for the default numShards?
There was a problem hiding this comment.
btw, I wasn't necessarily advocating setting size based on num_cpus... (I kind of like the power-of-2 mode here)
Just saying (without data), that my guess is that the perf gain from 128 to 256 might be low
| std::unordered_map<int64_t, ContextPtr> contexts; | ||
|
|
||
| // Lock for this shard. | ||
| mutable std::mutex lock; |
There was a problem hiding this comment.
-
consider reordering so that lock is first (acquiring the lock will fetch the beginning part of the unordered_map<> into cache in exclusive mode automatically)
-
consider padding slightly, to avoid interference.
in fbcode, sizeof() this struct should be 96 bytes. If the hardware cache line is 128, two adjacent entries will contend with each other.
std::hardware_destructive_interference_size is c++17-only, but could always just add something like the following to the bottom, and call it a day:
int64_t unusedCachePadding_[8]; // prevent adjacent shards from sharing a cache line
There was a problem hiding this comment.
oh, so I'm learning that the more modern way is something like
struct alignas(128) ContextsShard {
|
|
||
| std::lock_guard<std::mutex> guard(autograd_context_lock_); | ||
| auto context_id = next_context_id_++; | ||
| current_context_id_ = context_id; |
There was a problem hiding this comment.
what's the difference between context_id and current_context_id?
There was a problem hiding this comment.
current_context_id_ is the thread local context id variable. This needs to be set for RPC operations to pick this up.
| }; | ||
|
|
||
| // Number of shards for the map storing autograd contexts. | ||
| static constexpr int16_t kNumShards = 256; |
There was a problem hiding this comment.
should we use some heuristic formula (sth like @jjlilley mentioned above) for the default numShards?
DistAutogradContainer is a singleton for the entire process and has a single lock that protects access to a map keyed by `context_id`. Performance profiling showed that this lock is a potential bottleneck for training. As a result, in this PR we have the following optimizations: 1) Shard the map into 256 buckets with each bucket having its own lock. This would ensure we hold much finer grained locks. 2) sendReleaseContextRpc was being called under a lock, moved this to be outside the lock. Differential Revision: [D21003934](https://our.internmc.facebook.com/intern/diff/D21003934/) [ghstack-poisoned]
Pull Request resolved: #36529 DistAutogradContainer is a singleton for the entire process and has a single lock that protects access to map keyed by context_id. Performance profiling showed that this lock is a potential bottleneck for training. As a result, in this PR, we have the following optimizations: 1) Shard the map into 256 buckets with each bucket having its own lock. This would ensure we hold much finer grained locks. 2) sendReleaseContextRpc was being called under a lock, moved this to be outside the lock. ghstack-source-id: 102085139 Differential Revision: [D21003934](https://our.internmc.facebook.com/intern/diff/D21003934/)
| num_shards <<= 1; | ||
| } | ||
| } | ||
| LOG(INFO) << "Number of shards for DistAutogradContainer: " << num_shards; |
There was a problem hiding this comment.
This log is fine for now, though eventually we might want to demote to VLOG(1)
|
This pull request has been merged in 6e7eaab. |
Summary: Pull Request resolved: pytorch#36529 DistAutogradContainer is a singleton for the entire process and has a single lock that protects access to map keyed by context_id. Performance profiling showed that this lock is a potential bottleneck for training. As a result, in this PR, we have the following optimizations: 1) Shard the map into 256 buckets with each bucket having its own lock. This would ensure we hold much finer grained locks. 2) sendReleaseContextRpc was being called under a lock, moved this to be outside the lock. ghstack-source-id: 102085139 Test Plan: waitforbuildbot Differential Revision: D21003934 fbshipit-source-id: 55f80dd317311bce0efd3ca8ca617d071297b5dc
Stack from ghstack:
DistAutogradContainer is a singleton for the entire process and has a
single lock that protects access to a map keyed by
context_id. Performanceprofiling showed that this lock is a potential bottleneck for training. As a
result, in this PR we have the following optimizations:
would ensure we hold much finer grained locks.
outside the lock.
Differential Revision: D21003934