forked from horovod/horovod
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stall_inspector.cc
183 lines (157 loc) · 6.06 KB
/
stall_inspector.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Copyright 2019 Uber Technologies, Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// =============================================================================
#include "stall_inspector.h"
#include <map>
#include <unordered_set>
#include "logging.h"
namespace horovod {
namespace common {
bool StallInspector::CheckForStalledTensors(int global_size) {
bool should_shut_down = false;
auto now = std::chrono::steady_clock::now();
std::map<int32_t, std::set<std::string>> missing_ranks;
std::unordered_set<int32_t> shutdown_ranks;
std::chrono::seconds stall_warning_time(stall_warning_time_seconds);
std::chrono::seconds stall_shutdown_time(stall_shutdown_time_seconds);
if (stall_shutdown_time > std::chrono::seconds(0) &&
stall_shutdown_time < stall_warning_time) {
LOG(WARNING) << "HOROVOD_STALL_SHUTDOWN_TIME_SECONDS is less than "
"HOROVOD_STALL_CHECK_TIME_SECONDS, will not shutdown.";
stall_shutdown_time = std::chrono::seconds(0);
}
for (auto& m : uncached_tensor_table) {
auto tensor_name = m.first;
std::vector<int>& ranks = std::get<0>(m.second);
std::chrono::steady_clock::time_point start_at = std::get<1>(m.second);
auto lag = now - start_at;
if (lag > stall_warning_time) {
std::unordered_set<int32_t> ready_ranks;
for (auto rank : ranks) {
ready_ranks.insert(rank);
}
for (int32_t rank = 0; rank < global_size; ++rank) {
if (ready_ranks.find(rank) == ready_ranks.end()) {
missing_ranks[rank].insert(tensor_name);
if (stall_shutdown_time > std::chrono::seconds(0) &&
lag > stall_shutdown_time) {
shutdown_ranks.insert(rank);
should_shut_down = true;
}
}
}
}
}
if (!missing_ranks.empty()) {
std::stringstream message;
message << "One or more tensors were submitted to be "
"reduced, gathered or broadcasted by subset of ranks and "
"are waiting for remainder of ranks for more than "
<< stall_warning_time.count() << " seconds. "
<< "This may indicate that different ranks are trying to "
"submit different tensors or that only subset of ranks is "
"submitting tensors, which will cause deadlock. "
<< std::endl
<< "Stalled ranks:";
for (auto& kv : missing_ranks) {
message << std::endl << kv.first;
if (shutdown_ranks.find(kv.first) != shutdown_ranks.end()) {
message << "!";
}
message << ": [";
auto it = kv.second.begin();
message << *it;
int count = 0;
while (++it != kv.second.end()) {
message << ", " << *it;
if (++count == 5) {
message << " ...";
break;
}
}
message << "]";
}
if (should_shut_down) {
message
<< std::endl
<< "One or more rank (marked by \"!\") is stalled for longer than "
<< stall_shutdown_time.count() << " seconds. Will shutdown.";
LOG(ERROR) << message.str();
} else {
LOG(WARNING) << message.str();
}
}
return should_shut_down;
}
void StallInspector::InvalidateStalledCachedTensors(
CacheCoordinator& cache_coordinator) {
auto now = std::chrono::steady_clock::now();
std::chrono::seconds stall_warning_time(stall_warning_time_seconds);
for (auto& entry : cached_tensor_table) {
// If pending time for cached tensor exceeds stall_warning_time, mark entry
// for global removal from cache to trigger stall messaging.
if (now - entry.second > stall_warning_time) {
uint32_t cache_bit = response_cache_.peek_cache_bit(entry.first);
cache_coordinator.record_invalid_bit(cache_bit);
cache_coordinator.set_uncached_in_queue(true);
}
}
}
void StallInspector::RecordUncachedTensorStart(const std::string& tensor_name,
int rank, int global_size) {
auto table_iter = uncached_tensor_table.find(tensor_name);
if (table_iter == uncached_tensor_table.end()) {
std::vector<int> ranks = {rank};
ranks.reserve(static_cast<unsigned long>(global_size));
auto now = std::chrono::steady_clock::now();
uncached_tensor_table.emplace(tensor_name,
std::make_tuple(std::move(ranks), now));
} else {
std::vector<int>& ranks = std::get<0>(table_iter->second);
ranks.push_back(rank);
}
}
void StallInspector::RecordCachedTensorStart(const std::string& tensor_name) {
if (perform_stall_check &&
cached_tensor_table.find(tensor_name) == cached_tensor_table.end()) {
cached_tensor_table[tensor_name] = std::chrono::steady_clock::now();
}
}
void StallInspector::RemoveCachedTensor(const std::string& tensor_name) {
if (perform_stall_check) {
cached_tensor_table.erase(tensor_name);
}
}
void StallInspector::RemoveUncachedTensor(const std::string& tensor_name) {
uncached_tensor_table.erase(tensor_name);
}
bool StallInspector::ShouldPerformCheck() {
return perform_stall_check &&
std::chrono::steady_clock::now() - last_stall_check >
std::chrono::seconds(stall_warning_time_seconds);
}
void StallInspector::UpdateCheckTime() {
last_stall_check = std::chrono::steady_clock::now();
}
void StallInspector::SetPerformStallCheck(bool value) {
perform_stall_check = value;
}
void StallInspector::SetStallWarningTimeSeconds(int value) {
stall_warning_time_seconds = value;
}
void StallInspector::SetStallShutdownTimeSeconds(int value) {
stall_shutdown_time_seconds = value;
}
} // namespace common
} // namespace horovod