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 #ifndef TENSORFLOW_COMPILER_XLA_SERVICE_GPU_THUNK_EMITTER_H_ 17 #define TENSORFLOW_COMPILER_XLA_SERVICE_GPU_THUNK_EMITTER_H_ 18 19 #include "tensorflow/compiler/xla/service/buffer_assignment.h" 20 #include "tensorflow/compiler/xla/service/gpu/thunk.h" 21 #include "tensorflow/compiler/xla/statusor.h" 22 #include "tensorflow/compiler/xla/types.h" 23 24 namespace xla { 25 namespace gpu { 26 27 // Implements handling of GPU execution for HLO operations that are handed off 28 // to specialized thunks that do not require code generation. Intended to be 29 // mixed into GPU emitters. 30 class ThunkEmitter { 31 public: 32 class EmissionContext { 33 public: 34 virtual void AddThunkToThunkSequence(std::unique_ptr<Thunk> thunk) = 0; 35 virtual StatusOr<BufferAllocation::Slice> MaybeGetAllocationSlice( 36 const HloInstruction& hlo, const ShapeIndex& index) const = 0; 37 virtual int64 ByteSizeOf(const Shape& shape) const = 0; 38 virtual absl::string_view platform_name() const = 0; 39 virtual Thunk::ThunkInfo GetThunkInfo(const HloInstruction* hlo) const; 40 41 virtual ~EmissionContext() = default; 42 }; 43 ThunkEmitter(EmissionContext * context)44 explicit ThunkEmitter(EmissionContext* context) : context_(context) {} 45 46 Status HandleTriangularSolve(HloInstruction* hlo); 47 48 private: 49 EmissionContext* context_; 50 AddThunkToThunkSequence(std::unique_ptr<Thunk> thunk)51 void AddThunkToThunkSequence(std::unique_ptr<Thunk> thunk) { 52 return context_->AddThunkToThunkSequence(std::move(thunk)); 53 } 54 MaybeGetAllocationSlice(const HloInstruction & hlo,const ShapeIndex & index)55 StatusOr<BufferAllocation::Slice> MaybeGetAllocationSlice( 56 const HloInstruction& hlo, const ShapeIndex& index) const { 57 return context_->MaybeGetAllocationSlice(hlo, index); 58 } 59 ByteSizeOf(const Shape & shape)60 int64 ByteSizeOf(const Shape& shape) { return context_->ByteSizeOf(shape); } 61 platform_name()62 absl::string_view platform_name() const { return context_->platform_name(); } 63 64 BufferAllocation::Slice GetAllocationSlice( 65 const HloInstruction& hlo, const ShapeIndex& index = {}) const { 66 return MaybeGetAllocationSlice(hlo, index).ValueOrDie(); 67 } 68 69 // Returns a CholeskyThunk that calls cuSolver to implement `inst`. 70 std::unique_ptr<Thunk> BuildCholeskyThunk(const HloInstruction* inst); 71 72 // Returns a GemmThunk that calls gemm to implement `inst`. The caller needs 73 // to make sure `inst` outlives the lifetime of the returned Thunk object. 74 std::unique_ptr<Thunk> BuildGemmThunk(const HloInstruction* inst); 75 }; 76 77 } // namespace gpu 78 } // namespace xla 79 80 #endif // TENSORFLOW_COMPILER_XLA_SERVICE_GPU_THUNK_EMITTER_H_ 81