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/while_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
WhileThunk(const BufferAllocation::Slice & condition_result_buffer_index,std::unique_ptr<ThunkSequence> condition_thunk_sequence,std::unique_ptr<ThunkSequence> body_thunk_sequence,const HloInstruction * hlo)26 WhileThunk::WhileThunk(
27 const BufferAllocation::Slice& condition_result_buffer_index,
28 std::unique_ptr<ThunkSequence> condition_thunk_sequence,
29 std::unique_ptr<ThunkSequence> body_thunk_sequence,
30 const HloInstruction* hlo)
31 : Thunk(Kind::kWhile, hlo),
32 condition_result_buffer_index_(condition_result_buffer_index),
33 // Pass nullptr as the HloInstruction* to the condition_thunk_sequence_
34 // and body_thunk_sequence_ constructors because these SequentialThunks
35 // are logically "part of" this WhileThunk, and shouldn't be profiled
36 // separately from it.
37 condition_thunk_sequence_(absl::make_unique<SequentialThunk>(
38 std::move(*condition_thunk_sequence), nullptr)),
39 body_thunk_sequence_(absl::make_unique<SequentialThunk>(
40 std::move(*body_thunk_sequence), nullptr)) {}
41
Initialize(const GpuExecutable & executable,se::StreamExecutor * executor)42 Status WhileThunk::Initialize(const GpuExecutable& executable,
43 se::StreamExecutor* executor) {
44 TF_RETURN_IF_ERROR(
45 condition_thunk_sequence_->Initialize(executable, executor));
46 TF_RETURN_IF_ERROR(body_thunk_sequence_->Initialize(executable, executor));
47 return Status::OK();
48 }
49
ExecuteOnStream(const BufferAllocations & buffer_allocations,se::Stream * stream,HloExecutionProfiler * profiler)50 Status WhileThunk::ExecuteOnStream(const BufferAllocations& buffer_allocations,
51 se::Stream* stream,
52 HloExecutionProfiler* profiler) {
53 se::DeviceMemoryBase condition_result_data =
54 buffer_allocations.GetDeviceAddress(condition_result_buffer_index_);
55
56 auto op_profiler = profiler->MakeScopedInstructionProfiler(hlo_instruction());
57 while (true) {
58 // Invoke thunk sequence for while 'condition' computation.
59 profiler->StartHloComputation();
60 VLOG(3) << "Executing condition computation";
61 TF_RETURN_IF_ERROR(condition_thunk_sequence_->ExecuteOnStream(
62 buffer_allocations, stream, profiler));
63 profiler->FinishHloComputation(hlo_instruction()->while_condition());
64
65 // Copy the result of condition computation and break the loop if 'false'.
66 bool condition_result;
67 stream->ThenMemcpy(&condition_result, condition_result_data, sizeof(bool));
68 VLOG(3) << "condition_result = " << condition_result;
69 Status block_status = stream->BlockHostUntilDone();
70 if (!block_status.ok()) {
71 return InternalError(
72 "Failed to complete all kernels launched on stream %p: %s", stream,
73 block_status.error_message());
74 }
75
76 if (!condition_result) {
77 break;
78 }
79
80 // We measure the time of one execution of the while body computation. The
81 // while body may be executed more than once, the last measurement "wins".
82 profiler->StartHloComputation();
83 VLOG(3) << "Executing body computation";
84 // Invoke thunk sequence for while 'body' computation, and pass on
85 // 'profiler' to measure the timing of the thunks in 'body_thunk_sequence_'.
86 TF_RETURN_IF_ERROR(body_thunk_sequence_->ExecuteOnStream(buffer_allocations,
87 stream, profiler));
88 profiler->FinishHloComputation(hlo_instruction()->while_body());
89 }
90 return Status::OK();
91 }
92
93 } // namespace gpu
94 } // namespace xla
95