cudnn.h 9.8 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
Q
qijun 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */

#pragma once
D
dzhwinter 已提交
16
#include <glog/logging.h>
Q
qijun 已提交
17

Q
qijun 已提交
18
#include <cudnn.h>
K
Kexin Zhao 已提交
19
#include <mutex>  // NOLINT
Y
Yi Wang 已提交
20
#include "paddle/fluid/platform/dynload/dynamic_loader.h"
D
dzhwinter 已提交
21
#include "paddle/fluid/platform/port.h"
Q
qijun 已提交
22 23

namespace paddle {
Q
qijun 已提交
24
namespace platform {
Q
qijun 已提交
25
namespace dynload {
Q
qijun 已提交
26

Y
Yu Yang 已提交
27 28
extern std::once_flag cudnn_dso_flag;
extern void* cudnn_dso_handle;
29
extern bool HasCUDNN();
Q
qijun 已提交
30

31
extern void EnforceCUDNNLoaded(const char* fn_name);
32 33 34
#define DECLARE_DYNAMIC_LOAD_CUDNN_WRAP(__name)                            \
  struct DynLoad__##__name {                                               \
    template <typename... Args>                                            \
P
peizhilin 已提交
35
    auto operator()(Args... args) -> DECLARE_TYPE(__name, args...) {       \
36
      using cudnn_func = decltype(&::__name);                              \
37 38 39 40
      std::call_once(cudnn_dso_flag, []() {                                \
        cudnn_dso_handle = paddle::platform::dynload::GetCUDNNDsoHandle(); \
      });                                                                  \
      EnforceCUDNNLoaded(#__name);                                         \
Y
yuyang18 已提交
41
      static void* p_##__name = dlsym(cudnn_dso_handle, #__name);          \
42 43 44
      return reinterpret_cast<cudnn_func>(p_##__name)(args...);            \
    }                                                                      \
  };                                                                       \
Y
Yu Yang 已提交
45
  extern struct DynLoad__##__name __name
Q
qijun 已提交
46 47 48 49 50

/**
 * include all needed cudnn functions in HPPL
 * different cudnn version has different interfaces
 **/
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
#define CUDNN_DNN_ROUTINE_EACH(__macro)                   \
  __macro(cudnnSetTensor4dDescriptor);                    \
  __macro(cudnnSetTensor4dDescriptorEx);                  \
  __macro(cudnnSetTensorNdDescriptor);                    \
  __macro(cudnnGetTensorNdDescriptor);                    \
  __macro(cudnnGetConvolutionNdForwardOutputDim);         \
  __macro(cudnnCreateTensorDescriptor);                   \
  __macro(cudnnDestroyTensorDescriptor);                  \
  __macro(cudnnCreateFilterDescriptor);                   \
  __macro(cudnnSetFilter4dDescriptor);                    \
  __macro(cudnnSetFilterNdDescriptor);                    \
  __macro(cudnnGetFilterNdDescriptor);                    \
  __macro(cudnnSetPooling2dDescriptor);                   \
  __macro(cudnnSetPoolingNdDescriptor);                   \
  __macro(cudnnGetPoolingNdDescriptor);                   \
  __macro(cudnnDestroyFilterDescriptor);                  \
  __macro(cudnnCreateConvolutionDescriptor);              \
  __macro(cudnnCreatePoolingDescriptor);                  \
  __macro(cudnnDestroyPoolingDescriptor);                 \
  __macro(cudnnSetConvolution2dDescriptor);               \
  __macro(cudnnDestroyConvolutionDescriptor);             \
  __macro(cudnnSetConvolutionNdDescriptor);               \
  __macro(cudnnGetConvolutionNdDescriptor);               \
  __macro(cudnnDeriveBNTensorDescriptor);                 \
  __macro(cudnnCreateSpatialTransformerDescriptor);       \
  __macro(cudnnSetSpatialTransformerNdDescriptor);        \
  __macro(cudnnDestroySpatialTransformerDescriptor);      \
  __macro(cudnnSpatialTfGridGeneratorForward);            \
  __macro(cudnnSpatialTfGridGeneratorBackward);           \
  __macro(cudnnSpatialTfSamplerForward);                  \
  __macro(cudnnSpatialTfSamplerBackward);                 \
  __macro(cudnnCreate);                                   \
  __macro(cudnnDestroy);                                  \
  __macro(cudnnSetStream);                                \
  __macro(cudnnActivationForward);                        \
86
  __macro(cudnnActivationBackward);                       \
87 88 89 90 91 92 93 94 95 96 97 98
  __macro(cudnnConvolutionForward);                       \
  __macro(cudnnConvolutionBackwardBias);                  \
  __macro(cudnnGetConvolutionForwardWorkspaceSize);       \
  __macro(cudnnTransformTensor);                          \
  __macro(cudnnPoolingForward);                           \
  __macro(cudnnPoolingBackward);                          \
  __macro(cudnnSoftmaxBackward);                          \
  __macro(cudnnSoftmaxForward);                           \
  __macro(cudnnGetVersion);                               \
  __macro(cudnnFindConvolutionForwardAlgorithmEx);        \
  __macro(cudnnFindConvolutionBackwardFilterAlgorithmEx); \
  __macro(cudnnFindConvolutionBackwardDataAlgorithmEx);   \
L
liuhongyu 已提交
99 100 101 102
  __macro(cudnnGetErrorString);                           \
  __macro(cudnnCreateDropoutDescriptor);                  \
  __macro(cudnnDropoutGetStatesSize);                     \
  __macro(cudnnSetDropoutDescriptor);                     \
G
GaoWei8 已提交
103
  __macro(cudnnRestoreDropoutDescriptor);                 \
L
liuhongyu 已提交
104 105 106 107 108 109 110 111 112
  __macro(cudnnCreateRNNDescriptor);                      \
  __macro(cudnnGetRNNParamsSize);                         \
  __macro(cudnnGetRNNWorkspaceSize);                      \
  __macro(cudnnGetRNNTrainingReserveSize);                \
  __macro(cudnnRNNForwardTraining);                       \
  __macro(cudnnRNNBackwardData);                          \
  __macro(cudnnRNNBackwardWeights);                       \
  __macro(cudnnRNNForwardInference);                      \
  __macro(cudnnDestroyDropoutDescriptor);                 \
113 114
  __macro(cudnnDestroyRNNDescriptor);                     \
  __macro(cudnnSetTensorNdDescriptorEx);
L
liuhongyu 已提交
115

Y
Yu Yang 已提交
116 117 118 119 120 121 122
CUDNN_DNN_ROUTINE_EACH(DECLARE_DYNAMIC_LOAD_CUDNN_WRAP)

#define CUDNN_DNN_ROUTINE_EACH_R2(__macro) \
  __macro(cudnnAddTensor);                 \
  __macro(cudnnConvolutionBackwardData);   \
  __macro(cudnnConvolutionBackwardFilter);
CUDNN_DNN_ROUTINE_EACH_R2(DECLARE_DYNAMIC_LOAD_CUDNN_WRAP)
Q
qijun 已提交
123 124 125

// APIs available after R3:
#if CUDNN_VERSION >= 3000
Y
Yu Yang 已提交
126 127 128 129
#define CUDNN_DNN_ROUTINE_EACH_AFTER_R3(__macro)           \
  __macro(cudnnGetConvolutionBackwardFilterWorkspaceSize); \
  __macro(cudnnGetConvolutionBackwardDataWorkspaceSize);
CUDNN_DNN_ROUTINE_EACH_AFTER_R3(DECLARE_DYNAMIC_LOAD_CUDNN_WRAP)
Q
qijun 已提交
130 131
#endif

132 133 134 135 136 137 138 139 140
// APIs available after R3:
#if CUDNN_VERSION >= 3000 && CUDNN_VERSION < 8000
#define CUDNN_DNN_ROUTINE_EACH_AFTER_R3_LESS_R8(__macro) \
  __macro(cudnnGetConvolutionBackwardFilterAlgorithm);   \
  __macro(cudnnGetConvolutionForwardAlgorithm);          \
  __macro(cudnnSetRNNDescriptor);
CUDNN_DNN_ROUTINE_EACH_AFTER_R3_LESS_R8(DECLARE_DYNAMIC_LOAD_CUDNN_WRAP)
#endif

Q
qijun 已提交
141 142
// APIs available after R4:
#if CUDNN_VERSION >= 4007
Y
Yu Yang 已提交
143 144 145 146 147
#define CUDNN_DNN_ROUTINE_EACH_AFTER_R4(__macro)    \
  __macro(cudnnBatchNormalizationForwardTraining);  \
  __macro(cudnnBatchNormalizationForwardInference); \
  __macro(cudnnBatchNormalizationBackward);
CUDNN_DNN_ROUTINE_EACH_AFTER_R4(DECLARE_DYNAMIC_LOAD_CUDNN_WRAP)
Q
qijun 已提交
148 149 150 151
#endif

// APIs in R5
#if CUDNN_VERSION >= 5000
Y
Yu Yang 已提交
152 153 154 155 156 157
#define CUDNN_DNN_ROUTINE_EACH_R5(__macro)  \
  __macro(cudnnCreateActivationDescriptor); \
  __macro(cudnnSetActivationDescriptor);    \
  __macro(cudnnGetActivationDescriptor);    \
  __macro(cudnnDestroyActivationDescriptor);
CUDNN_DNN_ROUTINE_EACH_R5(DECLARE_DYNAMIC_LOAD_CUDNN_WRAP)
Q
qijun 已提交
158 159
#endif

L
liuhongyu 已提交
160 161
// APIs in R6
#if CUDNN_VERSION >= 6000
L
liuhongyu 已提交
162
#define CUDNN_DNN_ROUTINE_EACH_R6(__macro) __macro(cudnnSetRNNDescriptor_v6);
L
liuhongyu 已提交
163 164 165
CUDNN_DNN_ROUTINE_EACH_R6(DECLARE_DYNAMIC_LOAD_CUDNN_WRAP)
#endif

武毅 已提交
166
#if CUDNN_VERSION >= 7001
167 168 169 170 171 172 173 174 175 176 177 178 179
#define CUDNN_DNN_ROUTINE_EACH_R7(__macro)                \
  __macro(cudnnSetConvolutionGroupCount);                 \
  __macro(cudnnSetConvolutionMathType);                   \
  __macro(cudnnConvolutionBiasActivationForward);         \
  __macro(cudnnCreateCTCLossDescriptor);                  \
  __macro(cudnnDestroyCTCLossDescriptor);                 \
  __macro(cudnnGetCTCLossDescriptor);                     \
  __macro(cudnnSetCTCLossDescriptor);                     \
  __macro(cudnnGetCTCLossWorkspaceSize);                  \
  __macro(cudnnCTCLoss);                                  \
  __macro(cudnnGetConvolutionBackwardDataAlgorithm_v7);   \
  __macro(cudnnGetConvolutionBackwardFilterAlgorithm_v7); \
  __macro(cudnnGetConvolutionForwardAlgorithm_v7);
武毅 已提交
180 181 182
CUDNN_DNN_ROUTINE_EACH_R7(DECLARE_DYNAMIC_LOAD_CUDNN_WRAP)
#endif

G
GaoWei8 已提交
183 184 185 186 187 188 189 190 191 192 193 194 195
#if CUDNN_VERSION >= 7201
#define CUDNN_DNN_ROUTINE_EACH_AFTER_TWO_R7(__macro) \
  __macro(cudnnCreateRNNDataDescriptor);             \
  __macro(cudnnDestroyRNNDataDescriptor);            \
  __macro(cudnnSetRNNDataDescriptor);                \
  __macro(cudnnSetRNNPaddingMode);                   \
  __macro(cudnnRNNForwardTrainingEx);                \
  __macro(cudnnRNNBackwardDataEx);                   \
  __macro(cudnnRNNBackwardWeightsEx);                \
  __macro(cudnnRNNForwardInferenceEx);
CUDNN_DNN_ROUTINE_EACH_AFTER_TWO_R7(DECLARE_DYNAMIC_LOAD_CUDNN_WRAP)
#endif

196 197 198 199 200 201 202 203 204
#if CUDNN_VERSION >= 7401
#define CUDNN_DNN_ROUTINE_EACH_AFTER_R7(__macro)                     \
  __macro(cudnnGetBatchNormalizationForwardTrainingExWorkspaceSize); \
  __macro(cudnnBatchNormalizationForwardTrainingEx);                 \
  __macro(cudnnGetBatchNormalizationBackwardExWorkspaceSize);        \
  __macro(cudnnBatchNormalizationBackwardEx);                        \
  __macro(cudnnGetBatchNormalizationTrainingExReserveSpaceSize);
CUDNN_DNN_ROUTINE_EACH_AFTER_R7(DECLARE_DYNAMIC_LOAD_CUDNN_WRAP)
#endif
205 206 207 208 209 210

#if CUDNN_VERSION >= 8000
#define CUDNN_DNN_ROUTINE_EACH_R8(__macro) __macro(cudnnSetRNNDescriptor_v8);
CUDNN_DNN_ROUTINE_EACH_R8(DECLARE_DYNAMIC_LOAD_CUDNN_WRAP)
#endif

Q
qijun 已提交
211
}  // namespace dynload
Q
qijun 已提交
212
}  // namespace platform
Q
qijun 已提交
213
}  // namespace paddle