FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

[XLA:GPU] Specify cudnn conv algorithm via backend_config. · codeplaysoftware/tensorflow@05c0502 · GitHub

Repository navigation

Commit 05c0502

Browse files
authored andcommitted
[XLA:GPU] Specify cudnn conv algorithm via backend_config.
Gets rid of the tricky algorithm/use-tensor-cores operands to cudnn convolution customcalls, using instead a backend_config. PiperOrigin-RevId: 198794988
1 parent 9225636 commit 05c0502

6 files changed

Lines changed: 55 additions & 38 deletions

File tree

‎tensorflow/compiler/xla/service/gpu/BUILD‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Description:
22
# GPU-specific components in XLA service implementation.
33

4+
load("//tensorflow/compiler/xla:xla.bzl", "xla_proto_library")
5+
46
licenses(["notice"]) # Apache 2.0
57

68
package(default_visibility = [":friends"])
@@ -23,6 +25,11 @@ filegroup(
2325

2426
load("//tensorflow:tensorflow.bzl", "tf_cc_test")
2527

28+
xla_proto_library(
29+
name = "backend_configs",
30+
srcs = ["backend_configs.proto"],
31+
)
32+
2633
cc_library(
2734
name = "gpu_constants",
2835
srcs = ["gpu_constants.cc"],
@@ -133,6 +140,7 @@ cc_library(
133140
"ir_emitter_unnested.h",
134141
],
135142
deps = [
143+
":backend_configs",
136144
":cudnn_convolution_runner",
137145
":elemental_ir_emitter",
138146
":gpu_constants",
@@ -266,6 +274,7 @@ cc_library(
266274
"while_thunk.h",
267275
],
268276
deps = [
277+
":backend_configs",
269278
":buffer_allocations",
270279
":cudnn_convolution_runner",
271280
":infeed_manager",
@@ -322,6 +331,7 @@ cc_library(
322331
srcs = ["cudnn_convolution_algorithm_picker.cc"],
323332
hdrs = ["cudnn_convolution_algorithm_picker.h"],
324333
deps = [
334+
":backend_configs",
325335
":cudnn_convolution_runner",
326336
":gpu_executable",
327337
":ir_emission_utils",
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
syntax = "proto3";
2+
3+
package xla.gpu;
4+
5+
// Backend configs for XLA:GPU.
6+
//
7+
// These are metadata that the GPU backend attaches to HloInstrucitons and later
8+
// uses during e.g. codegen.
9+
//
10+
// Remember that proto3 doesn't give clients a way to tell the difference
11+
// between a field not being present and a field having the default value.
12+
// Choose your defaults carefully.
13+
//
14+
// No guarantee is made about the stability of these protos.
15+
//
16+
// See HloInstruction::backend_config() for more info.
17+
18+
// Backend config for a convolution that runs through cudnn.
19+
message CudnnConvBackendConfig {
20+
// Opaque algorithm number of cudnn algorithm chosen for this conv.
21+
int64 algorithm = 1;
22+
23+
// Whether we may use tensor cores when running this conv. Even if this is
24+
// true, cudnn may choose not to use tensor cores, e.g. because the GPU or
25+
// selected algorithm doesn't support it.
26+
bool tensor_ops_enabled = 2;
27+
}

‎tensorflow/compiler/xla/service/gpu/cudnn_convolution_algorithm_picker.cc‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ limitations under the License.
1414
==============================================================================*/
1515

1616
#include "tensorflow/compiler/xla/service/gpu/cudnn_convolution_algorithm_picker.h"
17+
#include "tensorflow/compiler/xla/service/gpu/backend_configs.pb.h"
1718
#include "tensorflow/compiler/xla/service/gpu/convolution_thunk.h"
1819
#include "tensorflow/compiler/xla/service/gpu/ir_emission_utils.h"
1920
#include "tensorflow/core/lib/gtl/optional.h"
@@ -316,21 +317,20 @@ StatusOr<bool> CudnnConvolutionAlgorithmPicker::RunOnInstruction(
316317
Shape new_call_shape =
317318
ShapeUtil::MakeTupleShape({instr->shape().tuple_shapes(0),
318319
ShapeUtil::MakeShape(U8, {scratch_bytes})});
319-
HloInstruction* algorithm_hlo = computation->AddInstruction(
320-
HloInstruction::CreateConstant(Literal::CreateR0<int64>(algorithm)));
321-
HloInstruction* tensor_ops_enabled_hlo =
322-
computation->AddInstruction(HloInstruction::CreateConstant(
323-
Literal::CreateR0<bool>(tensor_ops_enabled)));
320+
321+
CudnnConvBackendConfig backend_config;
322+
backend_config.set_algorithm(algorithm);
323+
backend_config.set_tensor_ops_enabled(tensor_ops_enabled);
324324

325325
HloInstruction* new_call =
326326
computation->AddInstruction(HloInstruction::CreateCustomCall(
327327
new_call_shape,
328-
{instr->mutable_operand(0), instr->mutable_operand(1), algorithm_hlo,
329-
tensor_ops_enabled_hlo},
328+
{instr->mutable_operand(0), instr->mutable_operand(1)},
330329
instr->custom_call_target()));
331330
new_call->set_window(instr->window());
332331
new_call->set_convolution_dimension_numbers(
333332
instr->convolution_dimension_numbers());
333+
TF_RETURN_IF_ERROR(new_call->set_backend_config(backend_config));
334334

335335
// Repackage new_call so it has the same shape as the original call, namely
336336
// (conv_result, u8[0]).

‎tensorflow/compiler/xla/service/gpu/gpu_copy_insertion.cc‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,6 @@ StatusOr<bool> GpuCopyInsertion::Run(HloModule* module) {
7878
for (int64 i = 0; i < hlo->operand_count() - 2; ++i) {
7979
TF_RETURN_IF_ERROR(copy_operand_if_constant(i));
8080
}
81-
} else if (IsCustomCallToDnnConvolution(*hlo)) {
82-
// The last two arguments to a CUDNN convolution are two HLO constants for
83-
// cudnn algorithm and tensor_ops_enabled flag, which shouldn't be copied.
84-
for (int64 i = 0; i < hlo->operand_count() - 2; ++i) {
85-
TF_RETURN_IF_ERROR(copy_operand_if_constant(i));
86-
}
8781
} else if (ImplementedAsLibraryCall(*hlo) ||
8882
hlo->opcode() == HloOpcode::kCrossReplicaSum) {
8983
// For all other library calls and cross-replica-sum, materialize all the

‎tensorflow/compiler/xla/service/gpu/ir_emission_utils.cc‎

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -162,19 +162,8 @@ static HloInstruction* CreateCudnnConv(
162162
Shape call_shape =
163163
ShapeUtil::MakeTupleShape({shape, ShapeUtil::MakeShape(U8, {0})});
164164

165-
// Our CustomCall takes four arguments: The conv lhs and rhs, the cudnn
166-
// algorithm to use, and a boolean indicating whether to use tensor cores.
167-
//
168-
// It's up to a later pass to choose the algorithm and decide whether to use
169-
// tensor cores, so to indicate that we haven't yet made a choice, we speicfy
170-
// -1 and false for those args.
171-
HloInstruction* negative_one = computation->AddInstruction(
172-
HloInstruction::CreateConstant(Literal::CreateR0<int64>(-1)));
173-
HloInstruction* false_constant = computation->AddInstruction(
174-
HloInstruction::CreateConstant(Literal::CreateR0<bool>(false)));
175-
HloInstruction* custom_call =
176-
computation->AddInstruction(HloInstruction::CreateCustomCall(
177-
call_shape, {lhs, rhs, negative_one, false_constant}, call_target));
165+
HloInstruction* custom_call = computation->AddInstruction(
166+
HloInstruction::CreateCustomCall(call_shape, {lhs, rhs}, call_target));
178167
custom_call->set_window(window);
179168
custom_call->set_convolution_dimension_numbers(dnums);
180169
return custom_call;

‎tensorflow/compiler/xla/service/gpu/ir_emitter_unnested.cc‎

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ limitations under the License.
3232
#include "tensorflow/compiler/xla/ptr_util.h"
3333
#include "tensorflow/compiler/xla/service/buffer_assignment.h"
3434
#include "tensorflow/compiler/xla/service/dfs_hlo_visitor.h"
35+
#include "tensorflow/compiler/xla/service/gpu/backend_configs.pb.h"
3536
#include "tensorflow/compiler/xla/service/gpu/conditional_thunk.h"
3637
#include "tensorflow/compiler/xla/service/gpu/convolution_thunk.h"
3738
#include "tensorflow/compiler/xla/service/gpu/copy_thunk.h"
@@ -423,15 +424,8 @@ Status IrEmitterUnnested::HandleCustomCall(HloInstruction* custom_call) {
423424
auto conv_result_slice = assn.GetUniqueSlice(custom_call, {0}).ValueOrDie();
424425
auto scratch_slice = assn.GetUniqueSlice(custom_call, {1}).ValueOrDie();
425426

426-
const HloInstruction* algorithm_inst = custom_call->operand(2);
427-
CHECK(algorithm_inst->IsConstant()) << algorithm_inst->ToString();
428-
int64 algorithm = algorithm_inst->literal().Get<int64>({});
429-
430-
const HloInstruction* tensor_ops_enabled_inst = custom_call->operand(3);
431-
CHECK(tensor_ops_enabled_inst->IsConstant())
432-
<< tensor_ops_enabled_inst->ToString();
433-
bool tensor_ops_enabled = tensor_ops_enabled_inst->literal().Get<bool>({});
434-
427+
TF_ASSIGN_OR_RETURN(CudnnConvBackendConfig backend_config,
428+
custom_call->backend_config<CudnnConvBackendConfig>());
435429
const auto& target = custom_call->custom_call_target();
436430
std::unique_ptr<ConvolutionThunk> thunk;
437431
if (target == kCudnnConvForwardCallTarget) {
@@ -446,7 +440,8 @@ Status IrEmitterUnnested::HandleCustomCall(HloInstruction* custom_call) {
446440
/*filter_shape=*/rhs_shape,
447441
/*output_shape=*/conv_result_shape, //
448442
custom_call->window(), custom_call->convolution_dimension_numbers(),
449-
algorithm, tensor_ops_enabled, custom_call);
443+
backend_config.algorithm(), backend_config.tensor_ops_enabled(),
444+
custom_call);
450445
} else if (target == kCudnnConvBackwardInputCallTarget) {
451446
thunk = MakeUnique<ConvolutionThunk>(
452447
CudnnConvKind::kBackwardInput,
@@ -459,7 +454,8 @@ Status IrEmitterUnnested::HandleCustomCall(HloInstruction* custom_call) {
459454
/*filter_shape=*/rhs_shape,
460455
/*output_shape=*/lhs_shape, //
461456
custom_call->window(), custom_call->convolution_dimension_numbers(),
462-
algorithm, tensor_ops_enabled, custom_call);
457+
backend_config.algorithm(), backend_config.tensor_ops_enabled(),
458+
custom_call);
463459
} else if (target == kCudnnConvBackwardFilterCallTarget) {
464460
thunk = MakeUnique<ConvolutionThunk>(
465461
CudnnConvKind::kBackwardFilter,
@@ -472,7 +468,8 @@ Status IrEmitterUnnested::HandleCustomCall(HloInstruction* custom_call) {
472468
/*filter_shape=*/conv_result_shape,
473469
/*output_shape=*/rhs_shape, //
474470
custom_call->window(), custom_call->convolution_dimension_numbers(),
475-
algorithm, tensor_ops_enabled, custom_call);
471+
backend_config.algorithm(), backend_config.tensor_ops_enabled(),
472+
custom_call);
476473
} else {
477474
LOG(FATAL) << "Unexpected custom call target: "
478475
<< custom_call->custom_call_target();

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL