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