• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2019 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/thunk_emitter.h"
17 
18 #include "tensorflow/compiler/xla/service/gpu/backend_configs.pb.h"
19 #include "tensorflow/compiler/xla/service/gpu/copy_thunk.h"
20 #include "tensorflow/compiler/xla/service/gpu/fft_thunk.h"
21 #include "tensorflow/compiler/xla/service/gpu/gemm_thunk.h"
22 #include "tensorflow/compiler/xla/service/gpu/ir_emission_utils.h"
23 #include "tensorflow/compiler/xla/service/gpu/sequential_thunk.h"
24 #include "tensorflow/compiler/xla/service/hlo_casting_utils.h"
25 
26 namespace xla {
27 namespace gpu {
28 
BuildGemmThunk(const HloInstruction * inst)29 std::unique_ptr<Thunk> ThunkEmitter::BuildGemmThunk(
30     const HloInstruction* inst) {
31   GpuGemmConfig config = GetGpuGemmConfig(inst);
32   const HloInstruction* lhs = inst->operand(0);
33   const HloInstruction* rhs = inst->operand(1);
34 
35   // The bias is passed inside the output buffer. If those buffers are shared
36   // we can just use it, otherwise copy the bias values into the output buffer
37   // first.
38   if (config.backend_config.beta() != 0.0) {
39     const HloInstruction* bias = inst->operand(2);
40     CHECK_EQ(bias->shape(), inst->shape());
41     if (GetAllocationSlice(*bias) != GetAllocationSlice(*inst)) {
42       std::vector<std::unique_ptr<Thunk>> thunks;
43       thunks.push_back(absl::make_unique<DeviceToDeviceCopyThunk>(
44           Thunk::ThunkInfo(),
45           /*source_buffer=*/GetAllocationSlice(*bias),
46           /*destination_buffer=*/GetAllocationSlice(*inst),
47           /*mem_size=*/ShapeUtil::ByteSizeOf(inst->shape())));
48       thunks.push_back(absl::make_unique<GemmThunk>(
49           context_->GetThunkInfo(inst), std::move(config),
50           GetAllocationSlice(*lhs),   // The buffer assigned to LHS.
51           GetAllocationSlice(*rhs),   // The buffer assigned to RHS.
52           GetAllocationSlice(*inst),  // The output buffer.
53           /*implements_whole_instruction=*/false));
54       return absl::make_unique<SequentialThunk>(context_->GetThunkInfo(inst),
55                                                 std::move(thunks));
56     }
57   }
58 
59   return absl::make_unique<GemmThunk>(
60       context_->GetThunkInfo(inst), std::move(config),
61       GetAllocationSlice(*lhs),   // The buffer assigned to LHS.
62       GetAllocationSlice(*rhs),   // The buffer assigned to RHS.
63       GetAllocationSlice(*inst),  // The output buffer.
64       /*implements_whole_instruction=*/true);
65 }
66 
GetThunkInfo(const HloInstruction * hlo) const67 Thunk::ThunkInfo ThunkEmitter::EmissionContext::GetThunkInfo(
68     const HloInstruction* hlo) const {
69   CHECK(hlo);
70   Thunk::ThunkInfo info;
71   info.profile_annotation = absl::StrFormat(
72       "Thunk:#hlo_op=%s,hlo_module=%s#", hlo->name(), hlo->GetModule()->name());
73   return info;
74 }
75 }  // namespace gpu
76 }  // namespace xla
77