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 <memory>
19
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
ConditionalThunk(ThunkInfo thunk_info,ConditionalThunkConfig config,const BufferAllocation::Slice & branch_index_buffer_index)27 ConditionalThunk::ConditionalThunk(
28 ThunkInfo thunk_info, ConditionalThunkConfig config,
29 const BufferAllocation::Slice& branch_index_buffer_index)
30 : Thunk(Kind::kConditional, thunk_info),
31 config_(std::move(config)),
32 branch_index_buffer_index_(branch_index_buffer_index) {}
33
Initialize(const GpuExecutable & executable,se::StreamExecutor * executor)34 Status ConditionalThunk::Initialize(const GpuExecutable& executable,
35 se::StreamExecutor* executor) {
36 if (config_.branch_index_is_bool) {
37 TF_RET_CHECK(config_.branch_thunks.size() == 2);
38 } else {
39 TF_RET_CHECK(!config_.branch_thunks.empty());
40 }
41 for (auto& branch_thunk : config_.branch_thunks) {
42 TF_RETURN_IF_ERROR(branch_thunk->Initialize(executable, executor));
43 }
44 return OkStatus();
45 }
46
ExecuteOnStream(const ExecuteParams & params)47 Status ConditionalThunk::ExecuteOnStream(const ExecuteParams& params) {
48 auto& stream = *params.stream;
49
50 // Copy the predicate value from device.
51 int32_t branch_index = -1;
52 bool pred = false;
53 se::DeviceMemoryBase branch_index_address =
54 params.buffer_allocations->GetDeviceAddress(branch_index_buffer_index_);
55 if (config_.branch_index_is_bool) {
56 stream.ThenMemcpy(&pred, branch_index_address, sizeof(bool));
57 } else {
58 stream.ThenMemcpy(&branch_index, branch_index_address, sizeof(int32_t));
59 }
60
61 Status block_status = stream.BlockHostUntilDone();
62 if (!block_status.ok()) {
63 return InternalError(
64 "Failed to retrieve branch_index value on stream %p: %s.", &stream,
65 block_status.error_message());
66 }
67 if (config_.branch_index_is_bool) {
68 branch_index = pred ? 0 : 1;
69 } else {
70 // Handle default scenario for branch_index not in [0, num_branches).
71 if (branch_index < 0 || branch_index >= config_.branch_count) {
72 branch_index = config_.branch_count - 1;
73 }
74 }
75
76 // Execute the branch computation corresponding to the value of branch_index.
77 TF_RETURN_IF_ERROR(
78 config_.branch_thunks[branch_index]->ExecuteOnStream(params));
79
80 return OkStatus();
81 }
82
83 } // namespace gpu
84 } // namespace xla
85