|
| 1 | +/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve. |
| 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 | +#pragma once |
| 16 | + |
| 17 | +#include "paddle/platform/dynload/cudnn.h" |
| 18 | +#include "paddle/platform/enforce.h" |
| 19 | +#include "paddle/platform/macros.h" |
| 20 | + |
| 21 | +namespace paddle { |
| 22 | +namespace platform { |
| 23 | + |
| 24 | +enum class DataLayout { |
| 25 | + kNHWC, |
| 26 | + kNCHW, |
| 27 | + kNCHW_VECT_C, |
| 28 | +}; |
| 29 | + |
| 30 | +enum class PoolingMode { |
| 31 | + kMaximum, |
| 32 | + kAverage, |
| 33 | +}; |
| 34 | + |
| 35 | +template <typename T> |
| 36 | +class CudnnDataType; |
| 37 | + |
| 38 | +template <> |
| 39 | +class CudnnDataType<float> { |
| 40 | + public: |
| 41 | + static const cudnnDataType_t type = CUDNN_DATA_FLOAT; |
| 42 | +}; |
| 43 | + |
| 44 | +template <> |
| 45 | +class CudnnDataType<double> { |
| 46 | + public: |
| 47 | + static const cudnnDataType_t type = CUDNN_DATA_DOUBLE; |
| 48 | +}; |
| 49 | + |
| 50 | +inline cudnnTensorFormat_t GetCudnnTensorFormat(const DataLayout& order) { |
| 51 | + switch (order) { |
| 52 | + case DataLayout::kNHWC: |
| 53 | + return CUDNN_TENSOR_NHWC; |
| 54 | + case DataLayout::kNCHW: |
| 55 | + return CUDNN_TENSOR_NCHW; |
| 56 | + default: |
| 57 | + PADDLE_THROW("Unknown cudnn equivalent for order"); |
| 58 | + } |
| 59 | + return CUDNN_TENSOR_NCHW; |
| 60 | +} |
| 61 | + |
| 62 | +class ScopedTensorDescriptor { |
| 63 | + public: |
| 64 | + ScopedTensorDescriptor() { |
| 65 | + PADDLE_ENFORCE(dynload::cudnnCreateTensorDescriptor(&desc_)); |
| 66 | + } |
| 67 | + ~ScopedTensorDescriptor() { |
| 68 | + PADDLE_ENFORCE(dynload::cudnnDestroyTensorDescriptor(desc_)); |
| 69 | + } |
| 70 | + |
| 71 | + inline cudnnTensorDescriptor_t descriptor(const cudnnTensorFormat_t format, |
| 72 | + const cudnnDataType_t type, |
| 73 | + const std::vector<int>& dims) { |
| 74 | + // the format is not used now, but it maybe useful feature |
| 75 | + std::vector<int> strides(dims.size()); |
| 76 | + strides[dims.size() - 1] = 1; |
| 77 | + for (int i = dims.size() - 2; i >= 0; i--) { |
| 78 | + strides[i] = dims[i + 1] * strides[i + 1]; |
| 79 | + } |
| 80 | + PADDLE_ENFORCE(dynload::cudnnSetTensorNdDescriptor( |
| 81 | + desc_, type, dims.size(), dims.data(), strides.data())); |
| 82 | + return desc_; |
| 83 | + } |
| 84 | + |
| 85 | + template <typename T> |
| 86 | + inline cudnnTensorDescriptor_t descriptor(const DataLayout& order, |
| 87 | + const std::vector<int>& dims) { |
| 88 | + return descriptor(GetCudnnTensorFormat(order), CudnnDataType<T>::type, |
| 89 | + dims); |
| 90 | + } |
| 91 | + |
| 92 | + private: |
| 93 | + cudnnTensorDescriptor_t desc_; |
| 94 | + DISABLE_COPY_AND_ASSIGN(ScopedTensorDescriptor); |
| 95 | +}; |
| 96 | + |
| 97 | +class ScopedFilterDescriptor { |
| 98 | + public: |
| 99 | + ScopedFilterDescriptor() { |
| 100 | + PADDLE_ENFORCE(dynload::cudnnCreateFilterDescriptor(&desc_)); |
| 101 | + } |
| 102 | + ~ScopedFilterDescriptor() { |
| 103 | + PADDLE_ENFORCE(dynload::cudnnDestroyFilterDescriptor(desc_)); |
| 104 | + } |
| 105 | + |
| 106 | + inline cudnnFilterDescriptor_t descriptor(const cudnnTensorFormat_t format, |
| 107 | + const cudnnDataType_t type, |
| 108 | + const std::vector<int>& kernel) { |
| 109 | + // filter layout: output input spatial_dim_y spatial_dim_x |
| 110 | + PADDLE_ENFORCE(dynload::cudnnSetFilterNdDescriptor( |
| 111 | + desc_, type, format, kernel.size(), kernel.data())); |
| 112 | + return desc_; |
| 113 | + } |
| 114 | + |
| 115 | + template <typename T> |
| 116 | + inline cudnnFilterDescriptor_t descriptor(const DataLayout& order, |
| 117 | + const std::vector<int>& kernel) { |
| 118 | + return descriptor(GetCudnnTensorFormat(order), CudnnDataType<T>::type, |
| 119 | + kernel); |
| 120 | + } |
| 121 | + |
| 122 | + private: |
| 123 | + cudnnFilterDescriptor_t desc_; |
| 124 | + DISABLE_COPY_AND_ASSIGN(ScopedFilterDescriptor); |
| 125 | +}; |
| 126 | + |
| 127 | +class ScopedConvolutionDescriptor { |
| 128 | + public: |
| 129 | + ScopedConvolutionDescriptor() { |
| 130 | + PADDLE_ENFORCE(dynload::cudnnCreateConvolutionDescriptor(&desc_)); |
| 131 | + } |
| 132 | + ~ScopedConvolutionDescriptor() { |
| 133 | + PADDLE_ENFORCE(dynload::cudnnDestroyConvolutionDescriptor(desc_)); |
| 134 | + } |
| 135 | + |
| 136 | + inline cudnnConvolutionDescriptor_t descriptor( |
| 137 | + cudnnDataType_t type, const std::vector<int>& pads, |
| 138 | + const std::vector<int>& strides, const std::vector<int>& dilations) { |
| 139 | + PADDLE_ENFORCE_EQ(pads.size(), strides.size()); |
| 140 | + PADDLE_ENFORCE_EQ(pads.size(), dilations.size()); |
| 141 | + |
| 142 | +#if CUDNN_VERSION < 6000 |
| 143 | + // cudnn v5 does not support dilation conv, the argument is called upscale |
| 144 | + // instead of dilations and it is must be one. |
| 145 | + for (size_t i = 0; i < dilations.size(); ++i) { |
| 146 | + PADDLE_ENFORCE_EQ( |
| 147 | + dilations[i], 1, |
| 148 | + "Dilations conv is not supported in this cuDNN version"); |
| 149 | + } |
| 150 | +#endif |
| 151 | + |
| 152 | + PADDLE_ENFORCE(dynload::cudnnSetConvolutionNdDescriptor( |
| 153 | + desc_, pads.size(), pads.data(), strides.data(), dilations.data(), |
| 154 | + CUDNN_CROSS_CORRELATION, type)); |
| 155 | + return desc_; |
| 156 | + } |
| 157 | + |
| 158 | + template <typename T> |
| 159 | + inline cudnnConvolutionDescriptor_t descriptor( |
| 160 | + const std::vector<int>& pads, const std::vector<int>& strides, |
| 161 | + const std::vector<int>& dilations) { |
| 162 | + return descriptor(CudnnDataType<T>::type, pads, strides, dilations); |
| 163 | + } |
| 164 | + |
| 165 | + private: |
| 166 | + cudnnConvolutionDescriptor_t desc_; |
| 167 | + DISABLE_COPY_AND_ASSIGN(ScopedConvolutionDescriptor); |
| 168 | +}; |
| 169 | + |
| 170 | +class ScopedPoolingDescriptor { |
| 171 | + public: |
| 172 | + ScopedPoolingDescriptor() { |
| 173 | + PADDLE_ENFORCE(dynload::cudnnCreatePoolingDescriptor(&desc_)); |
| 174 | + } |
| 175 | + ~ScopedPoolingDescriptor() { |
| 176 | + PADDLE_ENFORCE(dynload::cudnnDestroyPoolingDescriptor(desc_)); |
| 177 | + } |
| 178 | + |
| 179 | + inline cudnnPoolingDescriptor_t descriptor(const PoolingMode& mode, |
| 180 | + const std::vector<int>& kernel, |
| 181 | + const std::vector<int>& pads, |
| 182 | + const std::vector<int>& strides) { |
| 183 | + PADDLE_ENFORCE_EQ(kernel.size(), pads.size()); |
| 184 | + PADDLE_ENFORCE_EQ(kernel.size(), strides.size()); |
| 185 | + PADDLE_ENFORCE(dynload::cudnnSetPoolingNdDescriptor( |
| 186 | + desc_, (mode == PoolingMode::kMaximum |
| 187 | + ? CUDNN_POOLING_MAX |
| 188 | + : CUDNN_POOLING_AVERAGE_COUNT_EXCLUDE_PADDING), |
| 189 | + CUDNN_PROPAGATE_NAN, // Always propagate nans. |
| 190 | + kernel.size(), kernel.data(), pads.data(), strides.data())); |
| 191 | + return desc_; |
| 192 | + } |
| 193 | + |
| 194 | + private: |
| 195 | + cudnnPoolingDescriptor_t desc_; |
| 196 | + DISABLE_COPY_AND_ASSIGN(ScopedPoolingDescriptor); |
| 197 | +}; |
| 198 | + |
| 199 | +} // namespace platform |
| 200 | +} // namespace paddle |
0 commit comments