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 "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
ForThunk(const int64 loop_limit,std::unique_ptr<ThunkSequence> body_thunk_sequence,const HloInstruction * hlo)26 ForThunk::ForThunk(const int64 loop_limit,
27 std::unique_ptr<ThunkSequence> body_thunk_sequence,
28 const HloInstruction* hlo)
29 : Thunk(Kind::kWhile, hlo),
30 loop_limit_(loop_limit),
31 body_thunk_sequence_(absl::make_unique<SequentialThunk>(
32 // Pass nullptr as the HloInstruction* to the body_thunk_sequence_
33 // constructor because this SequentialThunk is logically "part of"
34 // this ForThunk, and shouldn't be profiled separately from it.
35 std::move(*body_thunk_sequence), nullptr)) {}
36
Initialize(const GpuExecutable & executable,se::StreamExecutor * executor)37 Status ForThunk::Initialize(const GpuExecutable& executable,
38 se::StreamExecutor* executor) {
39 TF_RETURN_IF_ERROR(body_thunk_sequence_->Initialize(executable, executor));
40 return Status::OK();
41 }
42
ExecuteOnStream(const BufferAllocations & buffer_allocations,se::Stream * stream,HloExecutionProfiler * profiler)43 Status ForThunk::ExecuteOnStream(const BufferAllocations& buffer_allocations,
44 se::Stream* stream,
45 HloExecutionProfiler* profiler) {
46 VLOG(2) << "Executing ForThunk with " << loop_limit_ << " iters for "
47 << (hlo_instruction() ? hlo_instruction()->ToString() : "<null>");
48 auto op_profiler = profiler->MakeScopedInstructionProfiler(hlo_instruction());
49 for (int64 i = 0; i < loop_limit_; ++i) {
50 profiler->StartHloComputation();
51 // Invoke loop body thunk sequence.
52 TF_RETURN_IF_ERROR(body_thunk_sequence_->ExecuteOnStream(buffer_allocations,
53 stream, profiler));
54 profiler->FinishHloComputation(hlo_instruction()->while_body());
55 }
56 return Status::OK();
57 }
58
59 } // namespace gpu
60 } // namespace xla
61