aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/compiler/xla/service/gpu/kernel_thunk.cc
blob: f56c1ce69f11ed79c8be76834269f29de93a9645 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/* Copyright 2017 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#include "tensorflow/compiler/xla/service/gpu/kernel_thunk.h"

#include "tensorflow/compiler/xla/ptr_util.h"
#include "tensorflow/compiler/xla/service/gpu/gpu_executable.h"
#include "tensorflow/compiler/xla/types.h"
#include "tensorflow/compiler/xla/util.h"
#include "tensorflow/core/lib/core/stringpiece.h"
#include "tensorflow/core/platform/logging.h"
#include "tensorflow/core/platform/stream_executor_no_cuda.h"

namespace xla {
namespace gpu {

KernelThunk::KernelThunk(
    tensorflow::gtl::ArraySlice<const BufferAllocation*> args,
    const string& kernel_name, const HloInstruction* hlo_instruction,
    int unroll_factor)
    : Thunk(Kind::kKernel, hlo_instruction),
      args_(args.begin(), args.end()),
      kernel_name_(kernel_name),
      unroll_factor_(unroll_factor) {}

Status KernelThunk::Initialize(const GpuExecutable& executable,
                               se::StreamExecutor* executor) {
  tensorflow::mutex_lock lock(mutex_);
  if (!loader_spec_) {
    loader_spec_.reset(new se::MultiKernelLoaderSpec(args_.size()));
    tensorflow::StringPiece ptx = executable.ptx();
    // Convert tensorflow::StringPiece to se::port::StringPiece because
    // StreamExecutor uses the latter.
    loader_spec_->AddCudaPtxInMemory(
        se::port::StringPiece(ptx.data(), ptx.size()), kernel_name_);

    if (!executable.cubin().empty()) {
      loader_spec_->AddCudaCubinInMemory(
          reinterpret_cast<const char*>(executable.cubin().data()),
          kernel_name_);
    }
  }

  // Load the kernel into the device if necessary.
  //
  // We could alternatively do this within ExecuteOnStream, but doing it here
  // lets the time spent loading the kernel not count towards our execution
  // profiles.
  auto it = kernel_cache_.find(executor);
  if (kernel_cache_.end() == it) {
    it = kernel_cache_.emplace(executor, se::KernelBase(executor)).first;
    if (!executor->GetKernel(*loader_spec_, &it->second)) {
      return InternalError("Unable to load kernel %s", kernel_name_.c_str());
    }
  }

  return Status::OK();
}

void KernelThunk::SetLaunchDimensions(const LaunchDimensions& launch_dims) {
  tensorflow::mutex_lock lock(mutex_);
  launch_dimensions_ = launch_dims;
}

Status KernelThunk::ExecuteOnStream(const BufferAllocations& buffer_allocations,
                                    se::Stream* stream) {
  // Load the kernel.
  se::StreamExecutor* executor = stream->parent();
  LaunchDimensions launch_dimensions;
  const se::KernelBase* kernel = nullptr;

  {
    tensorflow::mutex_lock lock(mutex_);
    auto it = kernel_cache_.find(executor);
    CHECK(it != kernel_cache_.end())
        << "Initialize() not called for StreamExecutor " << executor;
    launch_dimensions = launch_dimensions_;
    kernel = &it->second;
  }

  VLOG(3) << "Launching " << kernel->name();
  // Launch the kernel with potentially multiple blocks and threads.
  static constexpr int kKernelArgsLimit = 1024;
  auto kernel_args = MakeUnique<se::KernelArgsArray<kKernelArgsLimit>>();
  for (const BufferAllocation* arg : args_) {
    const auto& buf = buffer_allocations.GetDeviceAddress(arg->index());
    kernel_args->add_device_memory_argument(buf);
    VLOG(3) << "  Arg: alloc #" << arg->index() << ": " << buf.opaque() << " ("
            << buf.size() << "B)";
  }
  if (!stream->parent()->Launch(
          stream, se::ThreadDim(launch_dimensions.threads_per_block()),
          se::BlockDim(launch_dimensions.block_count()), *kernel,
          *kernel_args)) {
    return InternalError("Unable to launch kernel %s", kernel_name_.c_str());
  }
  return Status::OK();
}

}  // namespace gpu
}  // namespace xla