• 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/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(ThunkInfo thunk_info,const int64 loop_limit,std::unique_ptr<ThunkSequence> body_thunk_sequence,absl::optional<size_t> body_profile_index)26 ForThunk::ForThunk(ThunkInfo thunk_info, const int64 loop_limit,
27                    std::unique_ptr<ThunkSequence> body_thunk_sequence,
28                    absl::optional<size_t> body_profile_index)
29     : Thunk(Kind::kWhile, thunk_info),
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           ThunkInfo(), std::move(*body_thunk_sequence))),
36       body_profile_index_(body_profile_index) {}
37 
Initialize(const GpuExecutable & executable,se::StreamExecutor * executor)38 Status ForThunk::Initialize(const GpuExecutable& executable,
39                             se::StreamExecutor* executor) {
40   TF_RETURN_IF_ERROR(body_thunk_sequence_->Initialize(executable, executor));
41   return Status::OK();
42 }
43 
ExecuteOnStream(const ExecuteParams & params)44 Status ForThunk::ExecuteOnStream(const ExecuteParams& params) {
45   VLOG(2) << "Executing ForThunk with " << loop_limit_ << " iters";
46   auto op_profiler =
47       params.profiler->MakeScopedInstructionProfiler(profile_index());
48   for (int64 i = 0; i < loop_limit_; ++i) {
49     params.profiler->StartHloComputation();
50     // Invoke loop body thunk sequence.
51     TF_RETURN_IF_ERROR(body_thunk_sequence_->ExecuteOnStream(params));
52     params.profiler->FinishHloComputation(body_profile_index_);
53   }
54   return Status::OK();
55 }
56 
57 }  // namespace gpu
58 }  // namespace xla
59