1 /* Copyright 2022 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/cublas_lt_matmul_thunk.h"
17
18 #include <utility>
19
20 #include "tensorflow/compiler/xla/service/gpu/matmul_utils.h"
21 #include "tensorflow/compiler/xla/service/gpu/thunk.h"
22 #include "tensorflow/compiler/xla/status.h"
23 #include "tensorflow/core/platform/logging.h"
24 #include "tensorflow/stream_executor/cuda/cuda_blas_lt.h"
25 #include "tensorflow/stream_executor/device_memory.h"
26
27 namespace xla {
28 namespace gpu {
29
CublasLtMatmulThunk(ThunkInfo thunk_info,cublas_lt::MatmulPlan plan,int64_t algorithm_idx,BufferAllocation::Slice a_buffer,BufferAllocation::Slice b_buffer,BufferAllocation::Slice c_buffer,BufferAllocation::Slice d_buffer,BufferAllocation::Slice bias_buffer)30 CublasLtMatmulThunk::CublasLtMatmulThunk(
31 ThunkInfo thunk_info, cublas_lt::MatmulPlan plan, int64_t algorithm_idx,
32 BufferAllocation::Slice a_buffer, BufferAllocation::Slice b_buffer,
33 BufferAllocation::Slice c_buffer, BufferAllocation::Slice d_buffer,
34 BufferAllocation::Slice bias_buffer)
35 : Thunk(Kind::kCublasLtMatmul, thunk_info),
36 plan_(std::move(plan)),
37 algorithm_idx_(algorithm_idx),
38 a_buffer_(a_buffer),
39 b_buffer_(b_buffer),
40 c_buffer_(c_buffer),
41 d_buffer_(d_buffer),
42 bias_buffer_(bias_buffer) {}
43
ExecuteOnStream(const ExecuteParams & params)44 Status CublasLtMatmulThunk::ExecuteOnStream(const ExecuteParams& params) {
45 if (!algorithm_) {
46 TF_ASSIGN_OR_RETURN(
47 std::vector<se::cuda::BlasLt::MatmulAlgorithm> algorithms,
48 plan_.GetAlgorithms(params.stream));
49 TF_RET_CHECK(algorithm_idx_ >= 0 && algorithm_idx_ < algorithms.size());
50 algorithm_ = algorithms[algorithm_idx_];
51 }
52
53 VLOG(3) << "Running cublas_lt matmul thunk";
54 const BufferAllocations& allocs = *params.buffer_allocations;
55
56 se::DeviceMemoryBase bias;
57 if (bias_buffer_.allocation() != nullptr) {
58 bias = allocs.GetDeviceAddress(bias_buffer_);
59 }
60
61 se::OwningScratchAllocator<> scratch_allocator(allocs.device_ordinal(),
62 allocs.memory_allocator());
63 return plan_.ExecuteOnStream(
64 params.stream, allocs.GetDeviceAddress(a_buffer_),
65 allocs.GetDeviceAddress(b_buffer_), allocs.GetDeviceAddress(c_buffer_),
66 allocs.GetDeviceAddress(d_buffer_), bias, *algorithm_, scratch_allocator);
67 }
68
69 } // namespace gpu
70 } // namespace xla
71