• 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 #ifndef TENSORFLOW_COMPILER_XLA_SERVICE_GPU_KERNEL_THUNK_H_
17 #define TENSORFLOW_COMPILER_XLA_SERVICE_GPU_KERNEL_THUNK_H_
18 
19 #include <memory>
20 #include <string>
21 #include <vector>
22 
23 #include "absl/types/span.h"
24 #include "tensorflow/compiler/xla/service/buffer_assignment.h"
25 #include "tensorflow/compiler/xla/service/gpu/buffer_allocations.h"
26 #include "tensorflow/compiler/xla/service/gpu/hlo_execution_profiler.h"
27 #include "tensorflow/compiler/xla/service/gpu/partition_assignment.h"
28 #include "tensorflow/compiler/xla/service/gpu/thunk.h"
29 #include "tensorflow/compiler/xla/service/hlo_instruction.h"
30 #include "tensorflow/compiler/xla/types.h"
31 #include "tensorflow/core/platform/mutex.h"
32 #include "tensorflow/core/platform/stream_executor_no_cuda.h"
33 #include "tensorflow/core/platform/thread_annotations.h"
34 
35 namespace xla {
36 namespace gpu {
37 
38 class GpuExecutable;
39 
40 // This class stores everything that StreamExecutor needs for launching a
41 // kernel. It implements the ExecuteOnStream interface for GpuExecutable to
42 // invoke the corresponding kernel.
43 //
44 // This is thread-compatible.
45 class KernelThunk : public Thunk {
46  public:
47   // Constructs a thunk for the given kernel.
48   //
49   // `hlo_instruction` is as in Thunk. Other arguments are as the class members.
50   KernelThunk(absl::Span<const BufferAllocation* const> args,
51               const string& kernel_name, const HloInstruction* hlo_instruction,
52               int unroll_factor);
53   KernelThunk(const KernelThunk&) = delete;
54   KernelThunk& operator=(const KernelThunk&) = delete;
55   ~KernelThunk() override = default;
56 
kernel_name()57   const string& kernel_name() const { return kernel_name_; }
unroll_factor()58   int unroll_factor() const { return unroll_factor_; }
59   void SetLaunchDimensions(const LaunchDimensions& launch_dims);
60 
61   Status Initialize(const GpuExecutable& executable,
62                     se::StreamExecutor* executor) override;
63 
64   // Executes the kernel for the thunk on "stream", which must be non-null.
65   Status ExecuteOnStream(const BufferAllocations& buffer_allocations,
66                          se::Stream* stream,
67                          HloExecutionProfiler* profiler) override;
68 
69  private:
70   // Buffers passed to the kernel as arguments.
71   const std::vector<const BufferAllocation*> args_;
72 
73   // Entry kernel name for the computation.
74   const string kernel_name_;
75 
76   // The number of times this kernel should be unrolled. This works as a
77   // multiplier on the number of elements produced by a GPU thread.
78   const int unroll_factor_;
79 
80   // The thread and block dimension used to launch the kernel.
81   // Will be set by IrEmitterUnnested.
82   LaunchDimensions launch_dimensions_;
83 
84   // Describes how to load this kernel. ExecuteOnStream reuses this loader
85   // specification for all executions.
86   mutable tensorflow::mutex mutex_;
87   std::unique_ptr<se::MultiKernelLoaderSpec> loader_spec_ GUARDED_BY(mutex_);
88 
89   // Loaded kernels for each `StreamExecutor`.  Requires pointer stability of
90   // values.
91   std::unordered_map<se::StreamExecutor*, se::KernelBase> kernel_cache_
92       GUARDED_BY(mutex_);
93 };
94 
95 }  // namespace gpu
96 }  // namespace xla
97 
98 #endif  // TENSORFLOW_COMPILER_XLA_SERVICE_GPU_KERNEL_THUNK_H_
99