cudnn.h 6.3 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 17 18
#define GLOG_NO_ABBREVIATED_SEVERITIES
#define GOOGLE_GLOG_DLL_DECL
#include <glog/logging.h>
Q
qijun 已提交
19

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

namespace paddle {
Q
qijun 已提交
26
namespace platform {
Q
qijun 已提交
27
namespace dynload {
Q
qijun 已提交
28

Y
Yu Yang 已提交
29 30
extern std::once_flag cudnn_dso_flag;
extern void* cudnn_dso_handle;
31
extern bool HasCUDNN();
Q
qijun 已提交
32 33 34

#ifdef PADDLE_USE_DSO

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

#else

Y
Yu Yang 已提交
53
#define DECLARE_DYNAMIC_LOAD_CUDNN_WRAP(__name)                  \
D
done  
dzhwinter 已提交
54 55 56
  struct DynLoad__##__name {                         \
    template <typename... Args>                      \
    inline cudnnStatus_t operator()(Args... args) { \
D
dzhwinter 已提交
57 58
      VLOG(3) << "cudnn call"; \
      return ::__name(args...);                        \
D
done  
dzhwinter 已提交
59 60
    }                                                \
  };                                                 \
Y
Yu Yang 已提交
61
  extern DynLoad__##__name __name
Q
qijun 已提交
62 63 64 65 66 67 68

#endif

/**
 * include all needed cudnn functions in HPPL
 * different cudnn version has different interfaces
 **/
Y
Yu Yang 已提交
69 70 71
#define CUDNN_DNN_ROUTINE_EACH(__macro)             \
  __macro(cudnnSetTensor4dDescriptor);              \
  __macro(cudnnSetTensor4dDescriptorEx);            \
72 73
  __macro(cudnnSetTensorNdDescriptor);              \
  __macro(cudnnGetTensorNdDescriptor);              \
Y
Yu Yang 已提交
74 75 76 77 78 79
  __macro(cudnnGetConvolutionNdForwardOutputDim);   \
  __macro(cudnnGetConvolutionForwardAlgorithm);     \
  __macro(cudnnCreateTensorDescriptor);             \
  __macro(cudnnDestroyTensorDescriptor);            \
  __macro(cudnnCreateFilterDescriptor);             \
  __macro(cudnnSetFilter4dDescriptor);              \
80 81
  __macro(cudnnSetFilterNdDescriptor);              \
  __macro(cudnnGetFilterNdDescriptor);              \
Y
Yu Yang 已提交
82
  __macro(cudnnSetPooling2dDescriptor);             \
83 84
  __macro(cudnnSetPoolingNdDescriptor);             \
  __macro(cudnnGetPoolingNdDescriptor);             \
Y
Yu Yang 已提交
85 86 87 88 89 90
  __macro(cudnnDestroyFilterDescriptor);            \
  __macro(cudnnCreateConvolutionDescriptor);        \
  __macro(cudnnCreatePoolingDescriptor);            \
  __macro(cudnnDestroyPoolingDescriptor);           \
  __macro(cudnnSetConvolution2dDescriptor);         \
  __macro(cudnnDestroyConvolutionDescriptor);       \
91 92
  __macro(cudnnSetConvolutionNdDescriptor);         \
  __macro(cudnnGetConvolutionNdDescriptor);         \
Q
Qiao Longfei 已提交
93
  __macro(cudnnDeriveBNTensorDescriptor);           \
Y
Yu Yang 已提交
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
  __macro(cudnnCreate);                             \
  __macro(cudnnDestroy);                            \
  __macro(cudnnSetStream);                          \
  __macro(cudnnActivationForward);                  \
  __macro(cudnnConvolutionForward);                 \
  __macro(cudnnConvolutionBackwardBias);            \
  __macro(cudnnGetConvolutionForwardWorkspaceSize); \
  __macro(cudnnTransformTensor);                    \
  __macro(cudnnPoolingForward);                     \
  __macro(cudnnPoolingBackward);                    \
  __macro(cudnnSoftmaxBackward);                    \
  __macro(cudnnSoftmaxForward);                     \
  __macro(cudnnGetVersion);                         \
  __macro(cudnnGetErrorString);
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 已提交
115 116 117

// APIs available after R3:
#if CUDNN_VERSION >= 3000
Y
Yu Yang 已提交
118 119 120 121 122 123
#define CUDNN_DNN_ROUTINE_EACH_AFTER_R3(__macro)           \
  __macro(cudnnGetConvolutionBackwardFilterWorkspaceSize); \
  __macro(cudnnGetConvolutionBackwardDataAlgorithm);       \
  __macro(cudnnGetConvolutionBackwardFilterAlgorithm);     \
  __macro(cudnnGetConvolutionBackwardDataWorkspaceSize);
CUDNN_DNN_ROUTINE_EACH_AFTER_R3(DECLARE_DYNAMIC_LOAD_CUDNN_WRAP)
Q
qijun 已提交
124 125 126 127
#endif

// APIs available after R4:
#if CUDNN_VERSION >= 4007
Y
Yu Yang 已提交
128 129 130 131 132
#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 已提交
133 134 135 136
#endif

// APIs in R5
#if CUDNN_VERSION >= 5000
Y
Yu Yang 已提交
137 138 139 140 141 142
#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 已提交
143 144
#endif

武毅 已提交
145 146
#if CUDNN_VERSION >= 7001
#define CUDNN_DNN_ROUTINE_EACH_R7(__macro) \
K
Kexin Zhao 已提交
147 148
  __macro(cudnnSetConvolutionGroupCount);  \
  __macro(cudnnSetConvolutionMathType);
武毅 已提交
149 150 151
CUDNN_DNN_ROUTINE_EACH_R7(DECLARE_DYNAMIC_LOAD_CUDNN_WRAP)
#endif

Q
qijun 已提交
152
}  // namespace dynload
Q
qijun 已提交
153
}  // namespace platform
Q
qijun 已提交
154
}  // namespace paddle