miopen.h 8.9 KB
Newer Older
Y
Y_Xuan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/* Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.

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
#include <glog/logging.h>

#include <miopen/miopen.h>
19
#include <miopen/version.h>
Y
Y_Xuan 已提交
20 21 22 23
#include <mutex>  // NOLINT
#include "paddle/fluid/platform/dynload/dynamic_loader.h"
#include "paddle/fluid/platform/port.h"

24 25
#define MIOPEN_VERSION                                       \
  (MIOPEN_VERSION_MAJOR * 1000 + MIOPEN_VERSION_MINOR * 10 + \
26 27
   MIOPEN_VERSION_PATCH)  // NOLINT

28 29 30 31 32 33
// MIOPEN only support NCHW, just for compatibility with CUDNN API
typedef enum {
  MIOPEN_TENSOR_NCHW = 0,
  MIOPEN_TENSOR_NHWC = 1,
} miopenTensorFormat_t;

Y
Y_Xuan 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
namespace paddle {
namespace platform {
namespace dynload {

extern std::once_flag miopen_dso_flag;
extern void* miopen_dso_handle;
extern bool HasCUDNN();

inline const char* miopenGetErrorString(miopenStatus_t status) {
  switch (status) {
    case miopenStatusSuccess:
      return "MIOPEN_STATUS_SUCCESS";
    case miopenStatusNotInitialized:
      return "MIOPEN_STATUS_NOT_INITIALIZED";
    case miopenStatusInvalidValue:
      return "MIOPEN_STATUS_INVALID_VALUE";
    case miopenStatusBadParm:
      return "MIOPEN_STATUS_BAD_PARAM";
    case miopenStatusAllocFailed:
      return "MIOPEN_STATUS_ALLOC_FAILED";
    case miopenStatusInternalError:
      return "MIOPEN_STATUS_INTERNAL_ERROR";
    case miopenStatusNotImplemented:
      return "MIOPEN_STATUS_NOT_IMPLEMENTED";
58 59
    case miopenStatusUnsupportedOp:
      return "MIOPEN_STATUS_UNSUPPORTED_OP";
Y
Y_Xuan 已提交
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
    case miopenStatusUnknownError:
    default:
      return "MIOPEN_STATUS_UNKNOWN_ERROR";
  }
}

extern void EnforceCUDNNLoaded(const char* fn_name);
#define DECLARE_DYNAMIC_LOAD_MIOPEN_WRAP(__name)                            \
  struct DynLoad__##__name {                                                \
    template <typename... Args>                                             \
    auto operator()(Args... args) -> DECLARE_TYPE(__name, args...) {        \
      using miopen_func = decltype(&::__name);                              \
      std::call_once(miopen_dso_flag, []() {                                \
        miopen_dso_handle = paddle::platform::dynload::GetCUDNNDsoHandle(); \
      });                                                                   \
      EnforceCUDNNLoaded(#__name);                                          \
      static void* p_##__name = dlsym(miopen_dso_handle, #__name);          \
      return reinterpret_cast<miopen_func>(p_##__name)(args...);            \
    }                                                                       \
  };                                                                        \
  extern struct DynLoad__##__name __name

/**
 * include all needed miopen functions in HPPL
 **/
#define MIOPEN_DNN_ROUTINE_EACH(__macro)                  \
86
  __macro(miopenGetVersion);                              \
F
furnace 已提交
87
  __macro(miopenOpTensor);                                \
Y
Y_Xuan 已提交
88 89 90 91 92 93 94 95 96 97
  __macro(miopenSet4dTensorDescriptor);                   \
  __macro(miopenSetTensorDescriptor);                     \
  __macro(miopenInitConvolutionNdDescriptor);             \
  __macro(miopenFindConvolutionForwardAlgorithm);         \
  __macro(miopenGetConvolutionNdForwardOutputDim);        \
  __macro(miopenFindConvolutionBackwardDataAlgorithm);    \
  __macro(miopenFindConvolutionBackwardWeightsAlgorithm); \
  __macro(miopenGetTensorDescriptor);                     \
  __macro(miopenCreateTensorDescriptor);                  \
  __macro(miopenDestroyTensorDescriptor);                 \
98
  __macro(miopenGetTensorDescriptorSize);                 \
Y
Y_Xuan 已提交
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
  __macro(miopenSet2dPoolingDescriptor);                  \
  __macro(miopenGet2dPoolingDescriptor);                  \
  __macro(miopenGetPoolingNdForwardOutputDim);            \
  __macro(miopenCreateConvolutionDescriptor);             \
  __macro(miopenCreatePoolingDescriptor);                 \
  __macro(miopenDestroyPoolingDescriptor);                \
  __macro(miopenPoolingGetWorkSpaceSize);                 \
  __macro(miopenPoolingGetWorkSpaceSizeV2);               \
  __macro(miopenSetNdPoolingDescriptor);                  \
  __macro(miopenInitConvolutionDescriptor);               \
  __macro(miopenDestroyConvolutionDescriptor);            \
  __macro(miopenGetConvolutionNdDescriptor);              \
  __macro(miopenDeriveBNTensorDescriptor);                \
  __macro(miopenCreate);                                  \
  __macro(miopenDestroy);                                 \
  __macro(miopenSetStream);                               \
  __macro(miopenActivationForward);                       \
  __macro(miopenActivationBackward);                      \
  __macro(miopenConvolutionBackwardWeights);              \
  __macro(miopenConvolutionForward);                      \
R
ronnywang 已提交
119
  __macro(miopenConvolutionForwardBias);                  \
Y
Y_Xuan 已提交
120 121 122 123 124 125 126
  __macro(miopenConvolutionBackwardBias);                 \
  __macro(miopenConvolutionForwardGetWorkSpaceSize);      \
  __macro(miopenConvolutionBackwardDataGetWorkSpaceSize); \
  __macro(miopenTransformTensor);                         \
  __macro(miopenPoolingForward);                          \
  __macro(miopenPoolingBackward);                         \
  __macro(miopenSoftmaxBackward);                         \
127
  __macro(miopenSoftmaxBackward_V2);                      \
Y
Y_Xuan 已提交
128
  __macro(miopenSoftmaxForward);                          \
129
  __macro(miopenSoftmaxForward_V2);                       \
Y
Y_Xuan 已提交
130
  __macro(miopenCreateDropoutDescriptor);                 \
131 132
  __macro(miopenDestroyDropoutDescriptor);                \
  __macro(miopenRestoreDropoutDescriptor);                \
Y
Y_Xuan 已提交
133 134 135
  __macro(miopenDropoutGetStatesSize);                    \
  __macro(miopenSetDropoutDescriptor);                    \
  __macro(miopenCreateRNNDescriptor);                     \
136
  __macro(miopenDestroyRNNDescriptor);                    \
Y
Y_Xuan 已提交
137
  __macro(miopenSetRNNDescriptor);                        \
R
ronnywang 已提交
138
  __macro(miopenSetRNNDescriptor_V2);                     \
Y
Y_Xuan 已提交
139 140 141 142 143 144 145
  __macro(miopenGetRNNParamsSize);                        \
  __macro(miopenGetRNNWorkspaceSize);                     \
  __macro(miopenGetRNNTrainingReserveSize);               \
  __macro(miopenRNNForwardTraining);                      \
  __macro(miopenRNNBackwardData);                         \
  __macro(miopenRNNBackwardWeights);                      \
  __macro(miopenRNNForwardInference);                     \
146
  __macro(miopenGetTensorNumBytes);
Y
Y_Xuan 已提交
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198

MIOPEN_DNN_ROUTINE_EACH(DECLARE_DYNAMIC_LOAD_MIOPEN_WRAP)

#define MIOPEN_DNN_ROUTINE_EACH_R2(__macro) \
  __macro(miopenConvolutionBackwardData);
MIOPEN_DNN_ROUTINE_EACH_R2(DECLARE_DYNAMIC_LOAD_MIOPEN_WRAP)

// APIs available after R3:
#define MIOPEN_DNN_ROUTINE_EACH_AFTER_R3(__macro) \
  __macro(miopenConvolutionBackwardWeightsGetWorkSpaceSize);
MIOPEN_DNN_ROUTINE_EACH_AFTER_R3(DECLARE_DYNAMIC_LOAD_MIOPEN_WRAP)

// APIs available after R4:
#define MIOPEN_DNN_ROUTINE_EACH_AFTER_R4(__macro)    \
  __macro(miopenBatchNormalizationForwardTraining);  \
  __macro(miopenBatchNormalizationForwardInference); \
  __macro(miopenBatchNormalizationBackward);
MIOPEN_DNN_ROUTINE_EACH_AFTER_R4(DECLARE_DYNAMIC_LOAD_MIOPEN_WRAP)

// APIs in R5
#define MIOPEN_DNN_ROUTINE_EACH_R5(__macro)  \
  __macro(miopenCreateActivationDescriptor); \
  __macro(miopenSetActivationDescriptor);    \
  __macro(miopenGetActivationDescriptor);    \
  __macro(miopenDestroyActivationDescriptor);
MIOPEN_DNN_ROUTINE_EACH_R5(DECLARE_DYNAMIC_LOAD_MIOPEN_WRAP)

// APIs in R6
#define MIOPEN_DNN_ROUTINE_EACH_R6(__macro) \
/*__macro(miopenSetRNNDescriptor_v6);*/
MIOPEN_DNN_ROUTINE_EACH_R6(DECLARE_DYNAMIC_LOAD_MIOPEN_WRAP)

#define MIOPEN_DNN_ROUTINE_EACH_R7(__macro) \
  __macro(miopenSetConvolutionGroupCount);  \
  __macro(miopenCreateCTCLossDescriptor);   \
  __macro(miopenDestroyCTCLossDescriptor);  \
  __macro(miopenGetCTCLossDescriptor);      \
  __macro(miopenSetCTCLossDescriptor);      \
  __macro(miopenGetCTCLossWorkspaceSize);   \
  __macro(miopenCTCLoss);
MIOPEN_DNN_ROUTINE_EACH_R7(DECLARE_DYNAMIC_LOAD_MIOPEN_WRAP)

#define MIOPEN_DNN_ROUTINE_EACH_AFTER_R7(__macro)                    \
/*__macro(cudnnGetBatchNormalizationForwardTrainingExWorkspaceSize); \
__macro(cudnnBatchNormalizationForwardTrainingEx);                   \
__macro(cudnnGetBatchNormalizationBackwardExWorkspaceSize);          \
__macro(cudnnBatchNormalizationBackwardEx);                          \
__macro(cudnnGetBatchNormalizationTrainingExReserveSpaceSize);*/
MIOPEN_DNN_ROUTINE_EACH_AFTER_R7(DECLARE_DYNAMIC_LOAD_MIOPEN_WRAP)
}  // namespace dynload
}  // namespace platform
}  // namespace paddle