• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_STREAM_EXECUTOR_UTIL_H_
17 #define TENSORFLOW_COMPILER_XLA_SERVICE_GPU_STREAM_EXECUTOR_UTIL_H_
18 
19 #include "absl/strings/string_view.h"
20 #include "absl/types/span.h"
21 #include "tensorflow/compiler/xla/layout.h"
22 #include "tensorflow/compiler/xla/service/gpu/ir_emission_utils.h"
23 #include "tensorflow/compiler/xla/service/gpu/launch_dimensions.h"
24 #include "tensorflow/compiler/xla/service/hlo_module_config.h"
25 #include "tensorflow/compiler/xla/statusor.h"
26 #include "tensorflow/compiler/xla/types.h"
27 #include "tensorflow/compiler/xla/xla_data.pb.h"
28 #include "tensorflow/core/platform/stream_executor_no_cuda.h"
29 #include "tensorflow/stream_executor/gpu/gpu_asm_opts.h"
30 #include "tensorflow/stream_executor/kernel_spec.h"
31 
32 // Helper functions for interacting with StreamExecutor.
33 
34 namespace xla {
35 namespace gpu {
36 
37 // Returns true if the given StreamExecutor is for a Volta or newer nvidia GPU.
38 bool IsVoltaOrLater(const se::StreamExecutor& stream_exec);
39 
40 // Returns (input, filter, output) XLA Layout protos given the StreamExecutor
41 // layouts.
42 StatusOr<std::tuple<Layout, Layout, Layout>>
43 StreamExecutorConvLayoutsToXlaLayouts(const ConvolutionDimensionNumbers& dnums,
44                                       se::dnn::DataLayout input,
45                                       se::dnn::FilterLayout filter,
46                                       se::dnn::DataLayout output);
47 
48 // Returns (input, filter, output) StreamExecutor layouts given the XLA layouts.
49 StatusOr<
50     std::tuple<se::dnn::DataLayout, se::dnn::FilterLayout, se::dnn::DataLayout>>
51 XlaConvLayoutsToStreamExecutorLayouts(const ConvolutionDimensionNumbers& dnums,
52                                       const Layout& input, const Layout& filter,
53                                       const Layout& output);
54 
55 // Generates and returns a unique lock per each provided executor.
56 // Guarantees that blocks of code both holding a lock for the same provided
57 // executor (as given by this function) will not be running concurrently.
58 //
59 // This is used to prevent other XLA instances from trying to autotune on a
60 // device while another thread is using it.
61 tensorflow::mutex_lock LockGpu(const se::StreamExecutor* stream_exec);
62 
63 // Creates a kernel with a provided name, based from provided PTX in ptx.
64 // The kernel should be executed using the provided executor.
65 // The argument cubin_data represents compiled PTX and may be left empty.
66 //
67 // The canonical storage for both ptx and cubin_data should outlive
68 // the lifetime of the kernel.
69 StatusOr<std::unique_ptr<se::KernelBase>> CreateKernel(
70     absl::string_view kernel_name, uint64 num_args, absl::string_view ptx,
71     absl::Span<const uint8> cubin_data, se::StreamExecutor* stream_exec);
72 
73 // Runs loaded kernel on the stream with the provided arguments.
74 Status ExecuteKernelOnStream(const se::KernelBase& kernel,
75                              absl::Span<const se::DeviceMemoryBase> args,
76                              const LaunchDimensions& dims, se::Stream* stream);
77 
78 // Create GpuAsmOpts out of HloModuleConfig.
79 se::GpuAsmOpts PtxOptsFromConfig(const HloModuleConfig& hlo_module_config);
80 
81 // Initializes `buffer` with random data on `stream`.
82 // `rng_state` is an inout parameter for the pseudorandom generator state.
83 // `buffer_type` determines what buffer would be filled out with.
84 //
85 // Precondition: `buffer_type` is a floating point type, `rng_state` needs to be
86 // initialized to zero on the first use.
87 void InitializeBuffer(se::Stream* stream, PrimitiveType buffer_type,
88                       int64* rng_state, se::DeviceMemoryBase buffer);
89 
90 StatusOr<se::dnn::ConvolutionKind> GetDNNConvKindFromCudnnConvKind(
91     CudnnConvKind kind);
92 StatusOr<se::dnn::DataType> GetDNNDataTypeFromPrimitiveType(PrimitiveType type);
93 
94 }  // namespace gpu
95 }  // namespace xla
96 
97 #endif  // TENSORFLOW_COMPILER_XLA_SERVICE_GPU_STREAM_EXECUTOR_UTIL_H_
98