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