xref: /aosp_15_r20/external/tensorflow/tensorflow/compiler/xla/service/gpu/while_thunk.cc (revision b6fb3261f9314811a0f4371741dbb8839866f948)
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 <memory>
19 
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)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     : Thunk(Kind::kWhile, thunk_info),
32       condition_result_buffer_index_(condition_result_buffer_index),
33       condition_thunk_sequence_(std::make_unique<SequentialThunk>(
34           ThunkInfo(), std::move(*condition_thunk_sequence))),
35       body_thunk_sequence_(std::make_unique<SequentialThunk>(
36           ThunkInfo(), std::move(*body_thunk_sequence))) {}
37 
Initialize(const GpuExecutable & executable,se::StreamExecutor * executor)38 Status WhileThunk::Initialize(const GpuExecutable& executable,
39                               se::StreamExecutor* executor) {
40   TF_RETURN_IF_ERROR(
41       condition_thunk_sequence_->Initialize(executable, executor));
42   TF_RETURN_IF_ERROR(body_thunk_sequence_->Initialize(executable, executor));
43   return OkStatus();
44 }
45 
ExecuteOnStream(const ExecuteParams & params)46 Status WhileThunk::ExecuteOnStream(const ExecuteParams& params) {
47   auto& stream = *params.stream;
48 
49   se::DeviceMemoryBase condition_result_data =
50       params.buffer_allocations->GetDeviceAddress(
51           condition_result_buffer_index_);
52 
53   while (true) {
54     // Invoke thunk sequence for while 'condition' computation.
55     VLOG(3) << "Executing condition computation";
56     TF_RETURN_IF_ERROR(condition_thunk_sequence_->ExecuteOnStream(params));
57 
58     // Copy the result of condition computation and break the loop if 'false'.
59     bool condition_result;
60     stream.ThenMemcpy(&condition_result, condition_result_data, sizeof(bool));
61     VLOG(3) << "condition_result = " << condition_result;
62     Status block_status = stream.BlockHostUntilDone();
63     if (!block_status.ok()) {
64       return InternalError(
65           "Failed to complete all kernels launched on stream %p: %s", &stream,
66           block_status.error_message());
67     }
68 
69     if (!condition_result) {
70       break;
71     }
72 
73     VLOG(3) << "Executing body computation";
74     // Invoke thunk sequence for while 'body' computation.
75     TF_RETURN_IF_ERROR(body_thunk_sequence_->ExecuteOnStream(params));
76   }
77   return OkStatus();
78 }
79 
80 }  // namespace gpu
81 }  // namespace xla
82