1 /* Copyright 2018 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_GPU_CONV_ALGORITHM_PICKER_H_ 17 #define TENSORFLOW_COMPILER_XLA_SERVICE_GPU_GPU_CONV_ALGORITHM_PICKER_H_ 18 19 #include "absl/time/time.h" 20 #include "absl/types/optional.h" 21 #include "tensorflow/compiler/xla/service/compiler.h" 22 #include "tensorflow/compiler/xla/service/gpu/gpu_conv_runner.h" 23 #include "tensorflow/compiler/xla/service/hlo_instructions.h" 24 #include "tensorflow/compiler/xla/service/hlo_module.h" 25 #include "tensorflow/compiler/xla/service/hlo_pass_interface.h" 26 #include "tensorflow/core/platform/stream_executor_no_cuda.h" 27 #include "tensorflow/core/protobuf/autotuning.pb.h" 28 #include "tensorflow/stream_executor/device_memory_allocator.h" 29 30 namespace xla { 31 namespace gpu { 32 33 // Modifies CustomCalls to cudnn convolutions, choosing the best algorithm for 34 // each and adding explicit scratch space to the CustomCalls. 35 class GpuConvAlgorithmPicker : public HloModulePass { 36 public: 37 // If the `allocator` parameter is not null, we will use it to allocate temp 38 // memory while timing the various convolution algorithms. If it's null, 39 // we'll use the default allocator on the StreamExecutor. GpuConvAlgorithmPicker(se::StreamExecutor * stream_exec,se::DeviceMemoryAllocator * allocator)40 GpuConvAlgorithmPicker(se::StreamExecutor* stream_exec, 41 se::DeviceMemoryAllocator* allocator) 42 : stream_exec_(stream_exec), allocator_(allocator) {} 43 name()44 absl::string_view name() const override { 45 return "gpu-conv-algorithm-picker"; 46 } 47 48 StatusOr<bool> Run(HloModule* module) override; 49 50 private: 51 StatusOr<bool> RunOnComputation(HloComputation* computation); 52 StatusOr<bool> RunOnInstruction(HloInstruction* instr); 53 StatusOr<tensorflow::AutotuneResult> PickBestAlgorithm( 54 const HloCustomCallInstruction* instr); 55 56 #if (defined(GOOGLE_CUDA) && GOOGLE_CUDA) 57 StatusOr<tensorflow::AutotuneResult> PickBestAlgorithmNoCacheCuda( 58 const HloCustomCallInstruction* instr, 59 se::DeviceMemoryAllocator* allocator, se::Stream* stream); 60 #endif 61 62 StatusOr<tensorflow::AutotuneResult> PickBestAlgorithmNoCacheRocm( 63 const HloCustomCallInstruction* instr, 64 se::DeviceMemoryAllocator* allocator, se::Stream* stream); 65 66 se::StreamExecutor* stream_exec_; // never null 67 se::DeviceMemoryAllocator* allocator_; // may be null 68 }; 69 70 } // namespace gpu 71 } // namespace xla 72 #endif // TENSORFLOW_COMPILER_XLA_SERVICE_GPU_GPU_CONV_ALGORITHM_PICKER_H_ 73