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/stream_executor_util.h"
23 #include "tensorflow/compiler/xla/status_macros.h"
24 #include "tensorflow/compiler/xla/types.h"
25 #include "tensorflow/compiler/xla/util.h"
26 #include "tensorflow/core/lib/core/errors.h"
27 #include "tensorflow/core/platform/logging.h"
28 #include "tensorflow/core/platform/stream_executor_no_cuda.h"
29 #include "tensorflow/stream_executor/device_memory.h"
30 #include "tensorflow/stream_executor/kernel.h"
31
32 namespace xla {
33 namespace gpu {
34
KernelThunk(ThunkInfo thunk_info,absl::Span<const BufferAllocation * const> args,const string & kernel_name,const LaunchDimensions & launch_dimensions)35 KernelThunk::KernelThunk(ThunkInfo thunk_info,
36 absl::Span<const BufferAllocation* const> args,
37 const string& kernel_name,
38 const LaunchDimensions& launch_dimensions)
39 : Thunk(Kind::kKernel, thunk_info),
40 args_(args.begin(), args.end()),
41 kernel_name_(kernel_name),
42 launch_dimensions_(launch_dimensions) {}
43
ToStringExtra(int indent) const44 std::string KernelThunk::ToStringExtra(int indent) const {
45 return " ,kernel = " + kernel_name_;
46 }
47
Initialize(const GpuExecutable & executable,se::StreamExecutor * executor)48 Status KernelThunk::Initialize(const GpuExecutable& executable,
49 se::StreamExecutor* executor) {
50 tensorflow::mutex_lock lock(mutex_);
51
52 // Load the kernel into the device if necessary.
53 //
54 // We could alternatively do this within ExecuteOnStream, but doing it here
55 // lets the time spent loading the kernel not count towards our execution
56 // profiles.
57 auto it = kernel_cache_.find(executor);
58 if (kernel_cache_.end() == it) {
59 TF_ASSIGN_OR_RETURN(
60 std::unique_ptr<se::KernelBase> kernel,
61 CreateKernel(kernel_name_, args_.size(), executable.text(),
62 executable.binary(), executor));
63
64 kernel_cache_.emplace(executor, std::move(kernel));
65 }
66
67 return Status::OK();
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 return ExecuteKernelOnStream(*kernel, buffer_args, launch_dimensions,
117 params.stream);
118 }
119
120 } // namespace gpu
121 } // namespace xla
122