1 /* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
2
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15
16 #include "tensorflow/compiler/xla/service/gpu/conditional_thunk.h"
17
18 #include "absl/memory/memory.h"
19 #include "tensorflow/compiler/xla/service/gpu/hlo_execution_profiler.h"
20 #include "tensorflow/compiler/xla/service/hlo_instruction.h"
21 #include "tensorflow/compiler/xla/util.h"
22 #include "tensorflow/core/lib/core/errors.h"
23
24 namespace xla {
25 namespace gpu {
26
GetConditionalThunkConfig(const HloInstruction * instr,std::vector<ThunkSequence> branch_thunk_sequences,std::vector<absl::optional<size_t>> branch_profile_indices)27 ConditionalThunkConfig GetConditionalThunkConfig(
28 const HloInstruction* instr,
29 std::vector<ThunkSequence> branch_thunk_sequences,
30 std::vector<absl::optional<size_t>> branch_profile_indices) {
31 ConditionalThunkConfig config;
32 config.branch_index_is_bool =
33 instr->operand(0)->shape().element_type() == PRED;
34 config.branch_count = instr->branch_count();
35 // Pass nullptr as the HloInstruction* to the branch_thunks
36 // constructors because these SequentialThunks are logically "part of"
37 // this ConditionalThunk, and shouldn't be profiled separately from it.
38 config.branch_thunks.reserve(branch_thunk_sequences.size());
39 for (auto& branch_thunk_sequence : branch_thunk_sequences) {
40 config.branch_thunks.emplace_back(new SequentialThunk(
41 Thunk::ThunkInfo(), std::move(branch_thunk_sequence)));
42 }
43 config.branch_profile_indices = std::move(branch_profile_indices);
44 return config;
45 }
46
ConditionalThunk(ThunkInfo thunk_info,ConditionalThunkConfig config,const BufferAllocation::Slice & branch_index_buffer_index,absl::Span<const BufferAllocation::Slice> branch_operand_buffer_indexes)47 ConditionalThunk::ConditionalThunk(
48 ThunkInfo thunk_info, ConditionalThunkConfig config,
49 const BufferAllocation::Slice& branch_index_buffer_index,
50 absl::Span<const BufferAllocation::Slice> branch_operand_buffer_indexes)
51 : Thunk(Kind::kConditional, thunk_info),
52 config_(std::move(config)),
53 branch_index_buffer_index_(branch_index_buffer_index),
54 branch_operand_buffer_indexes_(branch_operand_buffer_indexes.begin(),
55 branch_operand_buffer_indexes.end()) {}
56
Initialize(const GpuExecutable & executable,se::StreamExecutor * executor)57 Status ConditionalThunk::Initialize(const GpuExecutable& executable,
58 se::StreamExecutor* executor) {
59 if (config_.branch_index_is_bool) {
60 TF_RET_CHECK(config_.branch_thunks.size() == 2);
61 } else {
62 TF_RET_CHECK(!config_.branch_thunks.empty());
63 }
64 for (auto& branch_thunk : config_.branch_thunks) {
65 TF_RETURN_IF_ERROR(branch_thunk->Initialize(executable, executor));
66 }
67 return Status::OK();
68 }
69
ExecuteOnStream(const ExecuteParams & params)70 Status ConditionalThunk::ExecuteOnStream(const ExecuteParams& params) {
71 auto& profiler = *params.profiler;
72 auto& stream = *params.stream;
73
74 auto op_profiler = profiler.MakeScopedInstructionProfiler(profile_index());
75 // Copy the predicate value from device.
76 int32 branch_index = -1;
77 bool pred = false;
78 se::DeviceMemoryBase branch_index_address =
79 params.buffer_allocations->GetDeviceAddress(branch_index_buffer_index_);
80 if (config_.branch_index_is_bool) {
81 stream.ThenMemcpy(&pred, branch_index_address, sizeof(bool));
82 } else {
83 stream.ThenMemcpy(&branch_index, branch_index_address, sizeof(int32));
84 }
85
86 Status block_status = stream.BlockHostUntilDone();
87 if (!block_status.ok()) {
88 return InternalError(
89 "Failed to retrieve branch_index value on stream %p: %s.", &stream,
90 block_status.error_message());
91 }
92 if (config_.branch_index_is_bool) {
93 branch_index = pred ? 0 : 1;
94 } else {
95 // Handle default scenario for branch_index not in [0, num_branches).
96 if (branch_index < 0 || branch_index >= config_.branch_count) {
97 branch_index = config_.branch_count - 1;
98 }
99 }
100
101 // Execute the branch computation corresponding to the value of branch_index.
102 profiler.StartHloComputation();
103 TF_RETURN_IF_ERROR(
104 config_.branch_thunks[branch_index]->ExecuteOnStream(params));
105 profiler.FinishHloComputation(config_.branch_profile_indices[branch_index]);
106
107 return Status::OK();
108 }
109
110 } // namespace gpu
111 } // namespace xla
112