forked from tensorflow/tensorflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpu_utils.cc
284 lines (260 loc) · 12 KB
/
gpu_utils.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
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
/* Copyright 2019 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.
==============================================================================*/
#include "tensorflow/core/kernels/gpu_utils.h"
#if GOOGLE_CUDA
#include <iterator>
#include "google/protobuf/any.pb.h"
#include "absl/algorithm/container.h"
#include "tensorflow/core/platform/logger.h"
#include "tensorflow/core/protobuf/autotuning.pb.h"
#include "tensorflow/core/protobuf/conv_autotuning.pb.h"
#include "tensorflow/core/util/env_var.h"
#include "tensorflow/core/util/proto/proto_utils.h"
#include "tensorflow/stream_executor/gpu/asm_compiler.h"
#include "tensorflow/stream_executor/gpu/redzone_allocator.h"
namespace tensorflow {
bool RedzoneCheckDisabled() {
const char* disable_rz_str = std::getenv("TF_DISABLE_RZ_CHECK");
return disable_rz_str != nullptr && std::strcmp(disable_rz_str, "1") == 0;
}
se::DeviceMemoryBase WrapRedzoneBestEffort(se::RedzoneAllocator* rz_allocator,
se::DeviceMemoryBase buffer) {
if (RedzoneCheckDisabled()) {
return buffer;
}
se::DeviceMemoryBase output_tensor;
auto output_rz_or = rz_allocator->AllocateBytes(buffer.size());
if (!output_rz_or.ok()) {
static std::once_flag rz_allocation_failure_logged;
std::call_once(rz_allocation_failure_logged, []() {
LOG(WARNING) << "Failed to allocate memory for convolution redzone "
<< "checking; skipping this check. This is benign and only "
<< "means that we won't check cudnn for out-of-bounds reads "
<< "and writes. This message will only be printed once.";
});
return buffer;
}
return se::DeviceMemoryBase(output_rz_or.ValueOrDie());
}
void CheckRedzones(const se::RedzoneAllocator& rz_allocator,
tensorflow::AutotuneResult* autotune_result) {
if (RedzoneCheckDisabled()) {
return;
}
se::port::StatusOr<se::RedzoneAllocator::RedzoneCheckStatus> rz_status =
rz_allocator.CheckRedzones();
if (!rz_status.ok()) {
static std::once_flag failure_logged;
std::call_once(failure_logged, [&]() {
LOG(WARNING) << "Failed to check cudnn convolutions for out-of-bounds "
<< "reads and writes with an error message: '"
<< rz_status.status().error_message()
<< "'; skipping this check. This only means that we won't "
<< "check cudnn for out-of-bounds reads and writes. This "
<< "message will only be printed once.";
});
return;
}
auto rz_check_status = rz_status.ValueOrDie();
if (!rz_check_status.ok()) {
auto* fail = autotune_result->mutable_failure();
fail->set_msg(rz_check_status.RedzoneFailureMsg());
fail->set_kind(AutotuneResult::REDZONE_MODIFIED);
fail->set_buffer_address(
reinterpret_cast<uint64>(rz_check_status.user_buffer_address));
LOG(ERROR)
<< "Detected cudnn out-of-bounds write in convolution buffer! This is "
"likely a cudnn bug. We will skip this algorithm in the future, but "
"your GPU state may already be corrupted, leading to incorrect "
"results. Within Google, no action is needed on your part. Outside "
"of Google, please ensure you're running the latest version of "
"cudnn. If that doesn't fix the problem, please file a bug with "
"this full error message and we'll contact nvidia.";
LOG(ERROR) << rz_check_status.RedzoneFailureMsg();
}
}
namespace {
tensorflow::CudnnVersion GetCudnnVersion(se::StreamExecutor* stream_executor) {
tensorflow::CudnnVersion cudnn_version;
if (auto* dnn = stream_executor->AsDnn()) {
se::port::StatusOr<se::dnn::VersionInfo> version_or = dnn->GetVersion();
if (version_or.ok()) {
const auto& version = version_or.ValueOrDie();
cudnn_version.set_major(version.major_version());
cudnn_version.set_minor(version.minor_version());
cudnn_version.set_patch(version.patch());
}
}
return cudnn_version;
}
tensorflow::ComputeCapability GetComputeCapability(
se::StreamExecutor* stream_executor) {
tensorflow::ComputeCapability cc;
int cc_major, cc_minor;
stream_executor->GetDeviceDescription().cuda_compute_capability(&cc_major,
&cc_minor);
cc.set_major(cc_major);
cc.set_minor(cc_minor);
return cc;
}
} // namespace
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) {
AutotuningLog log;
{
ConvolutionProto instr;
instr.set_kind(kind);
*instr.mutable_input() = input_desc.ToProto(element_type);
*instr.mutable_filter() = filter_desc.ToProto(element_type);
*instr.mutable_output() = output_desc.ToProto(element_type);
*instr.mutable_conv_desc() = conv_desc.ToProto();
instr.set_conv_scale(1);
instr.set_side_value_scale(0);
instr.set_input_address(reinterpret_cast<uint64>(input_buffer.opaque()));
instr.set_filter_address(reinterpret_cast<uint64>(filter_buffer.opaque()));
instr.set_output_address(reinterpret_cast<uint64>(output_buffer.opaque()));
log.mutable_instr()->PackFrom(std::move(instr));
}
*log.mutable_cudnn_version() = GetCudnnVersion(stream_exec);
*log.mutable_compute_capability() = GetComputeCapability(stream_exec);
log.set_device_pci_bus_id(stream_exec->GetDeviceDescription().pci_bus_id());
{
string blas_version;
if (auto* blas = stream_exec->AsBlas()) {
if (blas->GetVersion(&blas_version).ok()) {
log.set_blas_version(blas_version);
}
}
}
for (const auto& result : results) {
*log.add_results() = result;
}
Logger::GetSingleton()->LogProto(log);
}
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) {
AutotuningLog log;
{
ConvolutionProto instr;
instr.set_kind(se::dnn::ConvolutionKind::FORWARD_BIAS_ACTIVATION);
*instr.mutable_input() = input_desc.ToProto(element_type);
*instr.mutable_filter() = filter_desc.ToProto(element_type);
*instr.mutable_output() = output_desc.ToProto(element_type);
*instr.mutable_conv_desc() = conv_desc.ToProto();
instr.set_conv_scale(conv_scale);
instr.set_side_value_scale(side_value_scale);
instr.set_activation(activation_mode);
instr.set_input_address(reinterpret_cast<uint64>(input_buffer.opaque()));
instr.set_filter_address(reinterpret_cast<uint64>(filter_buffer.opaque()));
instr.set_output_address(reinterpret_cast<uint64>(output_buffer.opaque()));
instr.set_bias_address(reinterpret_cast<uint64>(bias_buffer.opaque()));
instr.set_side_input_address(
reinterpret_cast<uint64>(side_input_buffer.opaque()));
log.mutable_instr()->PackFrom(std::move(instr));
}
*log.mutable_cudnn_version() = GetCudnnVersion(stream_exec);
*log.mutable_compute_capability() = GetComputeCapability(stream_exec);
log.set_device_pci_bus_id(stream_exec->GetDeviceDescription().pci_bus_id());
{
string blas_version;
if (auto* blas = stream_exec->AsBlas()) {
if (blas->GetVersion(&blas_version).ok()) {
log.set_blas_version(blas_version);
}
}
}
for (const auto& result : results) {
*log.add_results() = result;
}
Logger::GetSingleton()->LogProto(log);
}
// The following function allows deterministic ops to be implemented relatively
// quickly using environment variables. It is intended to be temporary. The
// longer-term intention is to enable deterministic ops via tf.config and
// appropriate plumbing. See the discussion on PR 34951 for more information:
// https://github.com/tensorflow/tensorflow/pull/34951#discussion_r355682316
// This function and associated comment are replicated in the following three
// places:
// 1. tensorflow/compiler/xla/service/gpu/gpu_conv_algorithm_picker.cc
// 2. tensorflow/core/kernels/gpu_utils.cc
// 3. tensorflow/stream_executor/cuda/cuda_dnn.cc
// When implementing the plumbing, you should also search for the use of
// TF_DETERMINISTIC_OPS on its own.
// TODO(duncanriach): move to an API that uses tf.config and implement the first
// phase of plumbing.
bool RequireCudnnDeterminism() {
static bool require_cudnn_determinism = [] {
bool deterministic_ops = false;
TF_CHECK_OK(tensorflow::ReadBoolFromEnvVar("TF_DETERMINISTIC_OPS",
/*default_val=*/false,
&deterministic_ops));
bool cudnn_deterministic = false;
TF_CHECK_OK(tensorflow::ReadBoolFromEnvVar("TF_CUDNN_DETERMINISTIC",
/*default_val=*/false,
&cudnn_deterministic));
return deterministic_ops || cudnn_deterministic;
}();
return require_cudnn_determinism;
}
Status BestCudnnConvAlgorithm(absl::Span<const AutotuneResult> results,
se::dnn::AlgorithmConfig* algo) {
std::vector<AutotuneResult> filtered_results;
absl::c_copy_if(
results, std::back_inserter(filtered_results),
[](const AutotuneResult& result) { return !result.has_failure(); });
if (filtered_results.empty()) {
return errors::NotFound("No algorithm worked!");
}
std::vector<AutotuneResult> filtered_results_no_scratch;
absl::c_copy_if(
filtered_results, std::back_inserter(filtered_results_no_scratch),
[](const AutotuneResult& result) { return result.scratch_bytes() == 0; });
auto selected_result = filtered_results.begin();
auto selected_result_no_scratch = filtered_results_no_scratch.begin();
if (!RequireCudnnDeterminism()) {
auto compare_run_times = [](const AutotuneResult& lhs,
const AutotuneResult& rhs) {
return proto_utils::FromDurationProto(lhs.run_time()) <
proto_utils::FromDurationProto(rhs.run_time());
};
selected_result = absl::c_min_element(filtered_results, compare_run_times);
selected_result_no_scratch = absl::c_min_element(
filtered_results_no_scratch, compare_run_times);
}
algo->set_algorithm({selected_result->conv().algorithm(),
selected_result->conv().tensor_ops_enabled()});
if (selected_result_no_scratch != filtered_results_no_scratch.end()) {
algo->set_algorithm_no_scratch(
{selected_result_no_scratch->conv().algorithm(),
selected_result_no_scratch->conv().tensor_ops_enabled()});
}
return Status::OK();
}
} // namespace tensorflow
#endif // GOOGLE_CUDA