• 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_GEMM_THUNK_H_
17 #define TENSORFLOW_COMPILER_XLA_SERVICE_GPU_GEMM_THUNK_H_
18 
19 #include "tensorflow/compiler/xla/service/buffer_assignment.h"
20 #include "tensorflow/compiler/xla/service/gpu/backend_configs.pb.h"
21 #include "tensorflow/compiler/xla/service/gpu/buffer_allocations.h"
22 #include "tensorflow/compiler/xla/service/gpu/gpu_executable.h"
23 #include "tensorflow/compiler/xla/service/gpu/hlo_execution_profiler.h"
24 #include "tensorflow/compiler/xla/service/gpu/thunk.h"
25 #include "tensorflow/compiler/xla/service/hlo_instruction.h"
26 #include "tensorflow/compiler/xla/xla_data.pb.h"
27 #include "tensorflow/core/lib/core/status.h"
28 #include "tensorflow/core/platform/stream_executor_no_cuda.h"
29 #include "tensorflow/stream_executor/blas.h"
30 
31 namespace xla {
32 namespace gpu {
33 
34 // This class stores everything that StreamExecutor needs to launch a BLAS gemm.
35 // It is generated by IrEmitter.
36 
37 struct GpuGemmConfig {
38   Shape lhs_shape;
39   Shape rhs_shape;
40   Shape output_shape;
41   GemmBackendConfig backend_config;
42 };
43 
44 GpuGemmConfig GetGpuGemmConfig(const HloInstruction* gemm);
45 
46 // This is thread-compatible.
47 class GemmThunk : public Thunk {
48  public:
49   // Constructs a thunk that computes "output = (lhs <dot> rhs) * alpha" using
50   // BLAS gemm (alpha is stored in the instruction GemmBackendConfig).
51   GemmThunk(ThunkInfo thunk_info, GpuGemmConfig config,
52             const BufferAllocation::Slice& lhs_buffer,
53             const BufferAllocation::Slice& rhs_buffer,
54             const BufferAllocation::Slice& output_buffer,
55             bool implements_whole_instruction);
56 
57   GemmThunk(const GemmThunk&) = delete;
58   GemmThunk& operator=(const GemmThunk&) = delete;
59 
60   Status ExecuteOnStream(const ExecuteParams& params) override;
61 
62  private:
63   const GpuGemmConfig config_;
64   const BufferAllocation::Slice lhs_buffer_;
65   const BufferAllocation::Slice rhs_buffer_;
66   const BufferAllocation::Slice output_buffer_;
67   const bool implements_whole_instruction_;
68 };
69 
70 // Run the given GEMM instruction `gemm` subject to the configuration
71 // in `gemm_config` and the passed buffers.
72 //
73 // `implements_whole_instruction` is used for the default profiler creation
74 // if the `profiler` is not supplied. False value indicates that the created
75 // profiler will not specifically profile the `gemm` instruction.
76 //
77 // If `algorithm` is provided, it overrides the one specified in
78 // `gemm_config.backend_config`.
79 Status RunGemm(
80     const GpuGemmConfig& gemm_config, se::DeviceMemoryBase lhs_buffer,
81     se::DeviceMemoryBase rhs_buffer, se::DeviceMemoryBase output_buffer,
82     se::Stream* stream, bool implements_whole_instruction,
83     absl::optional<int64> profile_index,
84     HloExecutionProfiler* profiler = nullptr,
85     se::blas::ProfileResult* profile_result = nullptr,
86     absl::optional<se::blas::AlgorithmType> algorithm = absl::nullopt);
87 
88 }  // namespace gpu
89 }  // namespace xla
90 
91 #endif  // TENSORFLOW_COMPILER_XLA_SERVICE_GPU_GEMM_THUNK_H_
92