• 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/kernel_thunk.h"
17 
18 #include "absl/memory/memory.h"
19 #include "absl/strings/str_cat.h"
20 #include "absl/strings/string_view.h"
21 #include "tensorflow/compiler/xla/service/gpu/gpu_executable.h"
22 #include "tensorflow/compiler/xla/service/gpu/hlo_execution_profiler.h"
23 #include "tensorflow/compiler/xla/service/gpu/stream_executor_util.h"
24 #include "tensorflow/compiler/xla/status_macros.h"
25 #include "tensorflow/compiler/xla/types.h"
26 #include "tensorflow/compiler/xla/util.h"
27 #include "tensorflow/core/lib/core/errors.h"
28 #include "tensorflow/core/platform/logging.h"
29 #include "tensorflow/core/platform/stream_executor_no_cuda.h"
30 #include "tensorflow/stream_executor/device_memory.h"
31 #include "tensorflow/stream_executor/kernel.h"
32 
33 namespace xla {
34 namespace gpu {
35 
KernelThunk(ThunkInfo thunk_info,absl::Span<const BufferAllocation * const> args,const string & kernel_name)36 KernelThunk::KernelThunk(ThunkInfo thunk_info,
37                          absl::Span<const BufferAllocation* const> args,
38                          const string& kernel_name)
39     : Thunk(Kind::kKernel, thunk_info),
40       args_(args.begin(), args.end()),
41       kernel_name_(kernel_name) {}
42 
Initialize(const GpuExecutable & executable,se::StreamExecutor * executor)43 Status KernelThunk::Initialize(const GpuExecutable& executable,
44                                se::StreamExecutor* executor) {
45   tensorflow::mutex_lock lock(mutex_);
46 
47   // Load the kernel into the device if necessary.
48   //
49   // We could alternatively do this within ExecuteOnStream, but doing it here
50   // lets the time spent loading the kernel not count towards our execution
51   // profiles.
52   auto it = kernel_cache_.find(executor);
53   if (kernel_cache_.end() == it) {
54     TF_ASSIGN_OR_RETURN(
55         std::unique_ptr<se::KernelBase> kernel,
56         CreateKernel(kernel_name_, args_.size(), executable.text(),
57                      executable.binary(), executor));
58 
59     kernel_cache_.emplace(executor, std::move(kernel));
60   }
61 
62   return Status::OK();
63 }
64 
SetLaunchDimensions(const LaunchDimensions & launch_dims)65 void KernelThunk::SetLaunchDimensions(const LaunchDimensions& launch_dims) {
66   tensorflow::mutex_lock lock(mutex_);
67   launch_dimensions_ = launch_dims;
68 }
69 
PrintBufferContents(se::Stream * stream,absl::Span<const se::DeviceMemoryBase> buffer_args)70 static void PrintBufferContents(
71     se::Stream* stream, absl::Span<const se::DeviceMemoryBase> buffer_args) {
72   int input_idx = 0;
73   for (const se::DeviceMemoryBase& buf : buffer_args) {
74     auto host_buffer = absl::make_unique<char[]>(buf.size());
75     CHECK(stream->ThenMemcpy(host_buffer.get(), buf, buf.size()).ok());
76     CHECK(stream->BlockHostUntilDone().ok());
77 
78     std::string buffer_contents;
79     for (int i = 0; i < buf.size(); i++) {
80       absl::StrAppendFormat(&buffer_contents, "%x ",
81                             static_cast<unsigned>(host_buffer[i]));
82     }
83     VLOG(100) << "BUF(" << input_idx++ << ") = " << buffer_contents;
84   }
85 }
86 
ExecuteOnStream(const ExecuteParams & params)87 Status KernelThunk::ExecuteOnStream(const ExecuteParams& params) {
88   // Load the kernel.
89   se::StreamExecutor* executor = params.stream->parent();
90   LaunchDimensions launch_dimensions;
91   const se::KernelBase* kernel = nullptr;
92 
93   {
94     tensorflow::mutex_lock lock(mutex_);
95     auto it = kernel_cache_.find(executor);
96     CHECK(it != kernel_cache_.end())
97         << "Initialize() not called for StreamExecutor " << executor;
98     launch_dimensions = launch_dimensions_;
99     kernel = it->second.get();
100   }
101 
102   VLOG(3) << "Launching " << kernel->name();
103   absl::InlinedVector<se::DeviceMemoryBase, 4> buffer_args;
104   for (const BufferAllocation* arg : args_) {
105     se::DeviceMemoryBase buf =
106         params.buffer_allocations->GetDeviceAddress(arg->index());
107     VLOG(3) << "  Arg: alloc #" << arg->index() << ": " << buf.opaque() << "  ("
108             << buf.size() << "B)";
109     buffer_args.push_back(buf);
110   }
111 
112   if (VLOG_IS_ON(100)) {
113     PrintBufferContents(params.stream, buffer_args);
114   }
115 
116   auto op_profiler =
117       params.profiler->MakeScopedInstructionProfiler(profile_index());
118   return ExecuteKernelOnStream(*kernel, buffer_args, launch_dimensions,
119                                params.stream);
120 }
121 
122 }  // namespace gpu
123 }  // namespace xla
124