xref: /aosp_15_r20/external/tensorflow/tensorflow/compiler/xla/service/gpu/for_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/for_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 
ForThunk(ThunkInfo thunk_info,const int64_t loop_limit,std::unique_ptr<ThunkSequence> body_thunk_sequence)26 ForThunk::ForThunk(ThunkInfo thunk_info, const int64_t loop_limit,
27                    std::unique_ptr<ThunkSequence> body_thunk_sequence)
28     : Thunk(Kind::kWhile, thunk_info),
29       loop_limit_(loop_limit),
30       body_thunk_sequence_(std::make_unique<SequentialThunk>(
31           // Pass nullptr as the HloInstruction* to the body_thunk_sequence_
32           // constructor because this SequentialThunk is logically "part of"
33           // this ForThunk, and shouldn't be profiled separately from it.
34           ThunkInfo(), std::move(*body_thunk_sequence))) {}
35 
Initialize(const GpuExecutable & executable,se::StreamExecutor * executor)36 Status ForThunk::Initialize(const GpuExecutable& executable,
37                             se::StreamExecutor* executor) {
38   TF_RETURN_IF_ERROR(body_thunk_sequence_->Initialize(executable, executor));
39   return OkStatus();
40 }
41 
ExecuteOnStream(const ExecuteParams & params)42 Status ForThunk::ExecuteOnStream(const ExecuteParams& params) {
43   VLOG(2) << "Executing ForThunk with " << loop_limit_ << " iters";
44   for (int64_t i = 0; i < loop_limit_; ++i) {
45     VLOG(3) << "Executing iteration # " << i;
46     // Invoke loop body thunk sequence.
47     TF_RETURN_IF_ERROR(body_thunk_sequence_->ExecuteOnStream(params));
48   }
49   return OkStatus();
50 }
51 
52 }  // namespace gpu
53 }  // namespace xla
54