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/util.h"
21 #include "tensorflow/core/lib/core/errors.h"
22 
23 namespace xla {
24 namespace gpu {
25 
ConditionalThunk(const BufferAllocation::Slice & branch_index_buffer_index,absl::Span<const BufferAllocation::Slice> branch_operand_buffer_indexes,std::vector<ThunkSequence> branch_thunk_sequences,const HloInstruction * hlo)26 ConditionalThunk::ConditionalThunk(
27     const BufferAllocation::Slice& branch_index_buffer_index,
28     absl::Span<const BufferAllocation::Slice> branch_operand_buffer_indexes,
29     std::vector<ThunkSequence> branch_thunk_sequences,
30     const HloInstruction* hlo)
31     : Thunk(Kind::kConditional, hlo),
32       branch_index_is_bool_(hlo->operand(0)->shape().element_type() == PRED),
33       branch_index_buffer_index_(branch_index_buffer_index),
34       branch_operand_buffer_indexes_(branch_operand_buffer_indexes.begin(),
35                                      branch_operand_buffer_indexes.end()) {
36   // Pass nullptr as the HloInstruction* to the branch_thunks_
37   // constructors because these SequentialThunks are logically "part of"
38   // this ConditionalThunk, and shouldn't be profiled separately from it.
39   branch_thunks_.reserve(branch_thunk_sequences.size());
40   for (auto& branch_thunk_sequence : branch_thunk_sequences) {
41     branch_thunks_.emplace_back(
42         new SequentialThunk(std::move(branch_thunk_sequence), nullptr));
43   }
44 }
45 
Initialize(const GpuExecutable & executable,se::StreamExecutor * executor)46 Status ConditionalThunk::Initialize(const GpuExecutable& executable,
47                                     se::StreamExecutor* executor) {
48   if (branch_index_is_bool_) {
49     TF_RET_CHECK(branch_thunks_.size() == 2);
50   } else {
51     TF_RET_CHECK(!branch_thunks_.empty());
52   }
53   for (auto& branch_thunk : branch_thunks_) {
54     TF_RETURN_IF_ERROR(branch_thunk->Initialize(executable, executor));
55   }
56   return Status::OK();
57 }
58 
ExecuteOnStream(const BufferAllocations & buffer_allocations,se::Stream * stream,HloExecutionProfiler * profiler)59 Status ConditionalThunk::ExecuteOnStream(
60     const BufferAllocations& buffer_allocations, se::Stream* stream,
61     HloExecutionProfiler* profiler) {
62   auto op_profiler = profiler->MakeScopedInstructionProfiler(hlo_instruction());
63   // Copy the predicate value from device.
64   int32 branch_index = -1;
65   bool pred = false;
66   se::DeviceMemoryBase branch_index_address =
67       buffer_allocations.GetDeviceAddress(branch_index_buffer_index_);
68   if (branch_index_is_bool_) {
69     stream->ThenMemcpy(&pred, branch_index_address, sizeof(bool));
70   } else {
71     stream->ThenMemcpy(&branch_index, branch_index_address, sizeof(int32));
72   }
73 
74   Status block_status = stream->BlockHostUntilDone();
75   if (!block_status.ok()) {
76     return InternalError(
77         "Failed to retrieve branch_index value on stream %p: %s.", stream,
78         block_status.error_message());
79   }
80   if (branch_index_is_bool_) {
81     branch_index = pred ? 0 : 1;
82   } else {
83     // Handle default scenario for branch_index not in [0, num_branches).
84     if (branch_index < 0 || branch_index >= hlo_instruction()->branch_count()) {
85       branch_index = hlo_instruction()->branch_count() - 1;
86     }
87   }
88 
89   // Execute the branch computation corresponding to the value of branch_index.
90   profiler->StartHloComputation();
91   TF_RETURN_IF_ERROR(branch_thunks_[branch_index]->ExecuteOnStream(
92       buffer_allocations, stream, profiler));
93   profiler->FinishHloComputation(
94       hlo_instruction()->branch_computation(branch_index));
95 
96   return Status::OK();
97 }
98 
99 }  // namespace gpu
100 }  // namespace xla
101