-
Notifications
You must be signed in to change notification settings - Fork 74k
/
gpu_utils.h
443 lines (384 loc) · 17 KB
/
gpu_utils.h
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
/* Copyright 2015 The TensorFlow Authors. 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.
==============================================================================*/
#ifndef TENSORFLOW_CORE_KERNELS_GPU_UTILS_H_
#define TENSORFLOW_CORE_KERNELS_GPU_UTILS_H_
#if GOOGLE_CUDA || TENSORFLOW_USE_ROCM
#include <unordered_map>
#include "absl/strings/str_cat.h"
#include "absl/types/span.h"
#include "tensorflow/compiler/xla/stream_executor/dnn.h"
#include "tensorflow/compiler/xla/stream_executor/lazy_op_runner.h"
#include "tensorflow/core/framework/tensor.h"
#include "tensorflow/core/lib/core/status.h"
#include "tensorflow/core/lib/strings/str_util.h"
#include "tensorflow/core/lib/strings/strcat.h"
#include "tensorflow/core/lib/strings/stringprintf.h"
#include "tensorflow/core/platform/logging.h"
#include "tensorflow/core/platform/stream_executor.h"
namespace stream_executor {
class RedzoneAllocator;
} // namespace stream_executor
namespace xla {
class AutotuneResult;
} // namespace xla
namespace tensorflow {
class NodeDef;
using xla::AutotuneResult;
template <typename T>
se::DeviceMemory<T> AsDeviceMemory(const T* gpu_memory) {
se::DeviceMemoryBase wrapped(const_cast<T*>(gpu_memory));
se::DeviceMemory<T> typed(wrapped);
return typed;
}
// Return whether the redzone check is disabled.
//
// Controlled by the TF_DISABLE_RZ_CHECK environment variable.
bool RedzoneCheckDisabled();
// Return an allocated buffer with redzones the size of `buffer`. Does
// *not* copy the contents of the `buffer` into the newly allocated buffer:
// assumes that buffer is a pure out-parameter.
//
// Returns `buffer` if RedzoneCheckDisabled() is true.
//
// On error, return `buffer`, and log an error message (once).
se::DeviceMemoryBase WrapRedzoneBestEffort(se::RedzoneAllocator* rz_allocator,
se::DeviceMemoryBase buffer);
// Check the passed allocator for redzone violations.
// If violations have occurred, mark the corresponding autotune result
// as a failure.
void CheckRedzones(const se::RedzoneAllocator& rz_allocator,
AutotuneResult* autotune_result);
template <typename T>
inline se::DeviceMemory<T> AsDeviceMemory(const T* cuda_memory, uint64 size) {
se::DeviceMemoryBase wrapped(const_cast<T*>(cuda_memory), size * sizeof(T));
se::DeviceMemory<T> typed(wrapped);
return typed;
}
// Returns whether cuBLASLt is enabled.
//
// Controlled by the TF_USE_CUBLASLT environment variable.
bool EnableCublasLtGemm();
namespace internal {
template <typename Parameters>
struct AutotuneMapHasher {
std::size_t operator()(const Parameters& parameter) const {
return parameter.hash();
}
};
} // namespace internal
// A helper class that looks up the best autotuned config from parameters.
// Due to the noisy nature of autotune, especially with multiple devices, it
// only accepts a config if its margin exceeds a threshold.
// For the same shape configs, if a new best config matches the previous best,
// they get promoted; otherwise, the winner gets demoted. This process stops
// when the winner's score exceeds the threshold.
// In a bad case when two configs are very close to each other and flips
// back and forth randomly, the expected number of experiments before autotune
// settles is O(threshold ^ 2). So we recommend that number of warmup runs
// for any benchmarks.
template <typename Parameters, typename Config,
typename Hasher = internal::AutotuneMapHasher<Parameters>>
class AutotuneMap {
public:
bool Find(const Parameters& params, Config* config) const {
mutex_lock lock(mu_);
auto iter = params_config_map_.find(params);
if (iter == params_config_map_.end() ||
(iter->second.score < min_score_threshold_ &&
iter->second.count <= max_autotune_count_)) {
return false;
}
*config = iter->second.config;
return true;
}
void Insert(const Parameters& params, const Config& config) {
mutex_lock lock(mu_);
auto iter = params_config_map_.find(params);
int new_score = 0;
if (iter == params_config_map_.end()) {
// Create a new entry if params is new.
VLOG(1) << GetActionSummary("creates", params, config);
params_config_map_.insert(
std::make_pair(params, ValueType{config, 1, 1}));
new_score = 1;
} else if (iter->second.score < min_score_threshold_ &&
iter->second.count <= max_autotune_count_) {
DCHECK_GT(iter->second.score, 0);
if (iter->second.config != config) {
// If it is different from the current winner, demotes the winner.
VLOG(1) << GetActionSummary("demotes", params, config);
new_score = --iter->second.score;
++iter->second.count;
if (new_score <= 0) {
VLOG(1) << GetActionSummary("erases", params, config);
params_config_map_.erase(iter);
}
} else {
// If it is the same as the current winner, promotes the winner.
VLOG(1) << GetActionSummary("promotes", params, config);
new_score = ++iter->second.score;
++iter->second.count;
}
}
if (new_score >= min_score_threshold_) {
VLOG(1) << GetActionSummary("accepts", params, config);
} else if (autotune_global_count_ >= max_autotune_global_count_) {
// The autotuning exceeds the max iteration threshold and we accept the
// the winner if it exists in the map, otherwise we accept the current
// winner.
auto winner = params_config_map_.find(params);
if (winner == params_config_map_.end()) {
VLOG(1) << GetActionSummary("creates", params, config);
for (int i = 0; i < min_score_threshold_; ++i) {
VLOG(1) << GetActionSummary("promotes", params, config);
}
params_config_map_.insert(
std::make_pair(params, ValueType{config, min_score_threshold_, 1}));
} else {
int promotes_times = min_score_threshold_ - winner->second.score;
for (int i = 0; i < promotes_times; ++i) {
VLOG(1) << GetActionSummary("promotes", params, config);
}
winner->second.score = min_score_threshold_;
}
VLOG(1) << GetActionSummary("accepts", params, config);
}
autotune_global_count_++;
}
std::unordered_map<Parameters, Config, Hasher> GetMap() const {
mutex_lock lock(mu_);
std::unordered_map<Parameters, Config, Hasher> map;
for (const auto& entry : params_config_map_) {
map.insert(std::make_pair(entry.first, entry.second.config));
}
return map;
}
// Only for testing
void ClearMap() {
mutex_lock lock(mu_);
params_config_map_.clear();
}
private:
// Underlying data structure of values in the map.
struct ValueType {
Config config;
int32 score;
int32 count;
};
AutotuneMap(const std::string& name) : name_(name) {
min_score_threshold_ = 1;
int min_warmup_iterations = 10;
const char* threshold_str = getenv("TF_AUTOTUNE_THRESHOLD");
if (threshold_str != nullptr) {
VLOG(1) << "TF_AUTOTUNE_THRESHOLD = " << threshold_str;
strings::safe_strto32(threshold_str, &min_score_threshold_);
}
const char* min_warmup_iteration_str =
getenv("TF_AUTOTUNE_MIN_WARMUP_ITERATIONS");
if (min_warmup_iteration_str != nullptr) {
strings::safe_strto32(min_warmup_iteration_str, &min_warmup_iterations);
}
min_score_threshold_ = std::max(min_score_threshold_, 1);
max_autotune_count_ = std::max(
5 * min_score_threshold_ * min_score_threshold_, min_warmup_iterations);
max_autotune_global_count_ = 2 * max_autotune_count_;
autotune_global_count_ = 0;
}
template <class Group, class Params, class Cfg, class Hash>
friend class AutotuneSingleton;
std::string GetActionSummary(StringPiece action, const Parameters& params,
const Config& config) {
return strings::Printf("autotune_map %s %s: %s -> (%s)", name_.c_str(),
string(action).c_str(), params.ToString().c_str(),
config.ToString().c_str());
}
mutable mutex mu_;
std::unordered_map<Parameters, ValueType, Hasher> params_config_map_
TF_GUARDED_BY(mu_);
std::string name_;
int32 min_score_threshold_;
int32 max_autotune_count_;
int32 max_autotune_global_count_;
int32 autotune_global_count_;
TF_DISALLOW_COPY_AND_ASSIGN(AutotuneMap);
};
// A Singleton helper that manages the global autotune results by groups.
// The caller specified arbitrary Group type that can distinguish between
// different autotune results, even if their Parameters and Configs are the
// same.
template <class Group, typename Parameters, typename Config,
typename Hasher = internal::AutotuneMapHasher<Parameters>>
class AutotuneSingleton {
public:
typedef AutotuneMap<Parameters, Config, Hasher> AutotuneType;
static AutotuneType* GetInstance() {
static AutotuneType* instance = new AutotuneType(Group::name());
return instance;
}
};
// Logs convolution results to customized back-storage.
void LogConvAutotuneResults(se::dnn::ConvolutionKind kind,
se::dnn::DataType element_type,
se::DeviceMemoryBase input_buffer,
se::DeviceMemoryBase filter_buffer,
se::DeviceMemoryBase output_buffer,
const se::dnn::BatchDescriptor& input_desc,
const se::dnn::FilterDescriptor& filter_desc,
const se::dnn::BatchDescriptor& output_desc,
const se::dnn::ConvolutionDescriptor& conv_desc,
se::StreamExecutor* stream_exec,
absl::Span<const AutotuneResult> results);
// Logs fused convolution results to customized back-storage.
void LogFusedConvForwardAutotuneResults(
se::dnn::DataType element_type, se::DeviceMemoryBase input_buffer,
se::DeviceMemoryBase filter_buffer, se::DeviceMemoryBase output_buffer,
se::DeviceMemoryBase bias_buffer, se::DeviceMemoryBase side_input_buffer,
const se::dnn::BatchDescriptor& input_desc,
const se::dnn::FilterDescriptor& filter_desc,
const se::dnn::BatchDescriptor& output_desc,
const se::dnn::ConvolutionDescriptor& conv_desc, double conv_scale,
double side_value_scale, se::dnn::ActivationMode activation_mode,
se::StreamExecutor* stream_exec, absl::Span<const AutotuneResult> results);
// Logs fused matmul results to customized back-storage.
void LogFusedMatmulAutotuneResults(
se::dnn::DataType ab_dtype, se::dnn::DataType c_dtype,
se::DeviceMemoryBase a_buffer, se::DeviceMemoryBase b_buffer,
se::DeviceMemoryBase c_buffer, se::DeviceMemoryBase bias_buffer,
bool trans_a, bool trans_b, uint32_t m, uint32_t n, uint32_t k, int32_t lda,
int32_t ldb, int32_t ldc, se::dnn::ActivationMode activation_mode,
se::StreamExecutor* stream_exec, absl::Span<const AutotuneResult> results);
// Autotuning map entry for cuDNN-frontend-capable APIs.
//
// The longer-term intent is to remove the AlgorithmConfig variant and make this
// contain only the two LazyOpRunners, but for the time being ROCm is stuck on
// the legacy API and requires an AlgorithmConfig.
template <typename Op>
class AutotuneEntry {
public:
AutotuneEntry() : is_algorithm_config_(true) {}
// Initialize with legacy-API AlgorithmConfig; used for the ROCm backend only.
explicit AutotuneEntry(se::dnn::AlgorithmConfig config)
: is_algorithm_config_(true), algorithm_config_(std::move(config)) {}
AutotuneEntry(std::shared_ptr<se::dnn::LazyOpRunner<Op>> primary,
std::shared_ptr<se::dnn::LazyOpRunner<Op>> no_scratch_fallback)
: is_algorithm_config_(false),
op_runners_{std::move(primary), std::move(no_scratch_fallback)} {}
// Initialize from config data, without pre-cached runners, such as when
// loading AoT autotuning maps.
AutotuneEntry(se::dnn::AlgorithmDesc primary,
absl::optional<se::dnn::AlgorithmDesc> no_scratch_fallback)
: AutotuneEntry(std::make_shared<se::dnn::LazyOpRunner<Op>>(primary),
no_scratch_fallback
? std::make_shared<se::dnn::LazyOpRunner<Op>>(
*no_scratch_fallback)
: nullptr) {}
// Initialize with pre-cached OpRunners, such as during autotuning.
static StatusOr<AutotuneEntry> FromOpRunners(
std::unique_ptr<const se::dnn::OpRunner<typename Op::Signature>> primary,
std::unique_ptr<const se::dnn::OpRunner<typename Op::Signature>>
no_cache_fallback) {
TF_ASSIGN_OR_RETURN(
auto primary_cache,
se::dnn::LazyOpRunner<Op>::FromOpRunner(std::move(primary)));
if (no_cache_fallback) {
TF_ASSIGN_OR_RETURN(auto fallback_cache,
se::dnn::LazyOpRunner<Op>::FromOpRunner(
std::move(no_cache_fallback)));
return AutotuneEntry(std::move(primary_cache), std::move(fallback_cache));
} else {
return AutotuneEntry(std::move(primary_cache), nullptr);
}
}
struct OpRunners {
OpRunners() = default;
OpRunners(std::shared_ptr<se::dnn::LazyOpRunner<Op>> primary_,
std::shared_ptr<se::dnn::LazyOpRunner<Op>> no_scratch_fallback_)
: primary(std::move(primary_)),
no_scratch_fallback(std::move(no_scratch_fallback_)) {}
// Null iff this 'OpRunners' is default-constructed as part of the
// fake-variant in AutotuneEntry; users outside gpu_utils.h itself should
// never see primary = nullptr.
std::shared_ptr<se::dnn::LazyOpRunner<Op>> primary;
std::shared_ptr<se::dnn::LazyOpRunner<Op>> no_scratch_fallback; // Nullable
bool operator==(const OpRunners& other) const {
return *primary == *other.primary &&
((!no_scratch_fallback && !other.no_scratch_fallback) ||
(no_scratch_fallback && other.no_scratch_fallback &&
*no_scratch_fallback == *other.no_scratch_fallback));
}
};
bool is_algorithm_config() const { return is_algorithm_config_; }
const se::dnn::AlgorithmConfig& GetAlgorithmConfig() const {
DCHECK(is_algorithm_config_);
return algorithm_config_;
}
const OpRunners& GetOpRunners() const {
DCHECK(!is_algorithm_config_);
return op_runners_;
}
// AutotuneMap needs to test equality to keep track of the number of times an
// algorithm has won autotuning; for this purpose, we can use ToString to
// determine whether runners are equal.
bool operator==(const AutotuneEntry<Op>& other) const {
if (is_algorithm_config_) {
return other.is_algorithm_config_ &&
algorithm_config_ == other.algorithm_config_;
}
return !other.is_algorithm_config_ && op_runners_ == other.op_runners_;
}
bool operator!=(const AutotuneEntry<Op>& other) const {
return !(*this == other);
}
std::string ToString() const {
if (is_algorithm_config_) {
return algorithm_config_.ToString();
}
return absl::StrCat("{", op_runners_.primary->ToString(), ", ",
(op_runners_.no_scratch_fallback
? op_runners_.no_scratch_fallback->ToString()
: "(op_runners have no fallback)"),
"}");
}
private:
// NVCC is broken, so we can't use absl::variant here. Just fake it with a
// bool and both fields.
bool is_algorithm_config_;
se::dnn::AlgorithmConfig algorithm_config_;
OpRunners op_runners_;
};
namespace internal {
StatusOr<std::tuple<int, int>> BestCudnnConvAlgorithmIndices(
absl::Span<const AutotuneResult> results);
} // namespace internal
// Returns the best algorithms for the config, one is the fastest, the other is
// other is fastest with 0 scratch space. Unsuccessful autotuning results are
// allowed and ignored.
StatusOr<se::dnn::AlgorithmConfig> BestCudnnConvAlgorithm(
absl::Span<const AutotuneResult> results);
// Explicitly-instantiated with ConvOp and FusedConvOp.
//
// The definition can't be in the header because including .pb.h files in
// headers is forbidden.
template <typename Op>
StatusOr<AutotuneEntry<Op>> BestCudnnConvAlgorithm(
absl::Span<const AutotuneResult> results,
std::vector<
std::unique_ptr<const se::dnn::OpRunner<typename Op::Signature>>>
runners);
// Get the Dnn workspace limit from the environment variable, which is in MB.
// Return the workspace memory limit in bytes. If no value is set, return the
// default value.
int64_t GetDnnWorkspaceLimit(const string& envvar_in_mb,
int64_t default_value_in_bytes);
} // namespace tensorflow
#endif // GOOGLE_CUDA || TENSORFLOW_USE_ROCM
#endif // TENSORFLOW_CORE_KERNELS_GPU_UTILS_H_