conv_cudnn_op.cu.cc 15.7 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
武毅 已提交
2

L
Luo Tao 已提交
3 4 5
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
武毅 已提交
6

L
Luo Tao 已提交
7
    http://www.apache.org/licenses/LICENSE-2.0
武毅 已提交
8

L
Luo Tao 已提交
9 10 11 12 13
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. */
武毅 已提交
14

Y
Yi Wang 已提交
15 16 17 18 19 20
#include "paddle/fluid/framework/eigen.h"
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/memory/memory.h"
#include "paddle/fluid/operators/conv_op.h"
#include "paddle/fluid/platform/assert.h"
#include "paddle/fluid/platform/cudnn_helper.h"
K
Kexin Zhao 已提交
21
#include "paddle/fluid/platform/float16.h"
武毅 已提交
22

Y
Yu Yang 已提交
23
DEFINE_bool(cudnn_deterministic, false,
C
chengduoZH 已提交
24 25
            "Whether allow using an autotuning algorithm for convolution "
            "operator. The autotuning algorithm may be non-deterministic. If "
Y
Yu Yang 已提交
26
            "true, the algorithm is deterministic.");
C
chengduoZH 已提交
27

武毅 已提交
28 29 30 31 32 33 34 35
namespace paddle {
namespace operators {

using Tensor = framework::Tensor;
using ScopedTensorDescriptor = platform::ScopedTensorDescriptor;
using ScopedFilterDescriptor = platform::ScopedFilterDescriptor;
using ScopedConvolutionDescriptor = platform::ScopedConvolutionDescriptor;
using DataLayout = platform::DataLayout;
K
update  
Kexin Zhao 已提交
36 37
template <typename T>
using ScalingParamType = typename platform::CudnnDataType<T>::ScalingParamType;
武毅 已提交
38

Q
qiaolongfei 已提交
39 40
static constexpr size_t kCONV_CUDNN_WORKSPACE_LIMIT_BYTES =
    static_cast<size_t>(1024) * 1024 * 1024;
武毅 已提交
41 42

template <typename T>
43
class CUDNNConvOpKernel : public framework::OpKernel<T> {
武毅 已提交
44 45 46
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
    PADDLE_ENFORCE(platform::is_gpu_place(ctx.GetPlace()),
D
dzhwinter 已提交
47
                   "It must use CUDAPlace.");
武毅 已提交
48 49 50 51 52 53 54 55
    auto* input = ctx.Input<Tensor>("Input");
    auto* filter = ctx.Input<Tensor>("Filter");
    auto* output = ctx.Output<Tensor>("Output");

    std::vector<int> strides = ctx.Attr<std::vector<int>>("strides");
    std::vector<int> paddings = ctx.Attr<std::vector<int>>("paddings");
    std::vector<int> dilations = ctx.Attr<std::vector<int>>("dilations");
    int groups = ctx.Attr<int>("groups");
Q
qiaolongfei 已提交
56 57
    int64_t user_workspace_size =
        static_cast<size_t>(ctx.Attr<int>("workspace_size_MB"));
武毅 已提交
58 59 60 61 62 63 64 65 66 67 68

    const T* input_data = input->data<T>();
    const T* filter_data = filter->data<T>();
    T* output_data = output->mutable_data<T>(ctx.GetPlace());

    // ------------------- cudnn descriptors ---------------------
    ScopedTensorDescriptor input_desc;
    ScopedTensorDescriptor output_desc;
    ScopedFilterDescriptor filter_desc;
    ScopedConvolutionDescriptor conv_desc;
    DataLayout layout = DataLayout::kNCHW;
武毅 已提交
69 70 71 72 73 74 75
    if (input->dims().size() == 5) {
      layout = DataLayout::kNCDHW;
    }

    cudnnConvolutionDescriptor_t cudnn_conv_desc =
        conv_desc.descriptor<T>(paddings, strides, dilations);

武毅 已提交
76
#if CUDNN_VERSION_MIN(7, 0, 1)
武毅 已提交
77 78 79
    // cudnn 7 can support groups, no need to do it mannually
    // FIXME(typhoonzero): find a better way to disable groups
    // rather than setting it to 1.
W
Wu Yi 已提交
80
    CUDNN_ENFORCE(platform::dynload::cudnnSetConvolutionGroupCount(
武毅 已提交
81 82 83
        cudnn_conv_desc, groups));
    groups = 1;
#endif
武毅 已提交
84

C
chengduoZH 已提交
85 86 87 88 89 90
    cudnnTensorDescriptor_t cudnn_input_desc = input_desc.descriptor<T>(
        layout, framework::vectorize2int(input->dims()), groups);
    cudnnTensorDescriptor_t cudnn_output_desc = output_desc.descriptor<T>(
        layout, framework::vectorize2int(output->dims()), groups);
    cudnnFilterDescriptor_t cudnn_filter_desc = filter_desc.descriptor<T>(
        layout, framework::vectorize2int(filter->dims()), groups);
武毅 已提交
91 92

    int input_channels = input->dims()[1];
武毅 已提交
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
    int input_height, input_width, input_depth;
    if (input->dims().size() == 5) {
      input_depth = input->dims()[2];
      input_height = input->dims()[3];
      input_width = input->dims()[4];
    } else {  // dim size is enforced in InferShape
      input_depth = 1;
      input_height = input->dims()[2];
      input_width = input->dims()[3];
    }
    int output_channels = filter->dims()[0];
    int output_height, output_width, output_depth;
    if (output->dims().size() == 5) {
      output_depth = output->dims()[2];
      output_height = output->dims()[3];
      output_width = output->dims()[4];
    } else {
      output_depth = 1;
      output_height = output->dims()[2];
      output_width = output->dims()[3];
    }
武毅 已提交
114

武毅 已提交
115 116
    int group_offset_in =
        input_channels / groups * input_height * input_width * input_depth;
武毅 已提交
117
    int group_offset_out =
武毅 已提交
118
        output_channels / groups * output_height * output_width * output_depth;
武毅 已提交
119 120 121 122 123 124 125 126 127 128
    int group_offset_filter = filter->numel() / groups;
    // ------------------- cudnn conv workspace ---------------------
    void* cudnn_workspace = nullptr;
    size_t workspace_size_in_bytes;  // final workspace to allocate.
    size_t workspace_size_limit = kCONV_CUDNN_WORKSPACE_LIMIT_BYTES;
    if (user_workspace_size > 0) {
      workspace_size_limit = user_workspace_size * 1024 * 1024;
    }
    // ------------------- cudnn conv algorithm ---------------------
    cudnnConvolutionFwdAlgo_t algo;
Q
QI JUN 已提交
129 130
    auto& dev_ctx = ctx.template device_context<platform::CUDADeviceContext>();
    auto handle = dev_ctx.cudnn_handle();
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145

    CUDNN_ENFORCE(platform::dynload::cudnnGetConvolutionForwardAlgorithm(
        handle, cudnn_input_desc, cudnn_filter_desc, cudnn_conv_desc,
        cudnn_output_desc, CUDNN_CONVOLUTION_FWD_SPECIFY_WORKSPACE_LIMIT,
        workspace_size_limit, &algo));

#if CUDA_VERSION >= 9000 && CUDNN_VERSION_MIN(7, 0, 1)
    // Tensor core is supported since the volta GPU and
    // is only enabled when input and filter data are float16
    if (dev_ctx.GetComputeCapability() >= 70 &&
        std::type_index(typeid(T)) ==
            std::type_index(typeid(platform::float16))) {
      CUDNN_ENFORCE(platform::dynload::cudnnSetConvolutionMathType(
          cudnn_conv_desc, CUDNN_TENSOR_OP_MATH));
      // Currently tensor core is only enabled using this algo
K
Kexin Zhao 已提交
146 147
      algo = CUDNN_CONVOLUTION_FWD_ALGO_IMPLICIT_PRECOMP_GEMM;
    } else {
148 149
      CUDNN_ENFORCE(platform::dynload::cudnnSetConvolutionMathType(
          cudnn_conv_desc, CUDNN_DEFAULT_MATH));
K
Kexin Zhao 已提交
150
    }
151
#endif
K
Kexin Zhao 已提交
152

武毅 已提交
153
    // get workspace size able to allocate
W
Wu Yi 已提交
154
    CUDNN_ENFORCE(platform::dynload::cudnnGetConvolutionForwardWorkspaceSize(
武毅 已提交
155 156
        handle, cudnn_input_desc, cudnn_filter_desc, cudnn_conv_desc,
        cudnn_output_desc, algo, &workspace_size_in_bytes));
K
Kexin Zhao 已提交
157 158 159 160 161
    // It is possible for float16 on Volta GPU to allocate more memory than
    // the limit because the algo is overrided to use tensor core.
    PADDLE_ENFORCE_LE(workspace_size_in_bytes, workspace_size_limit,
                      "workspace_size to be allocated exceeds the limit");

F
fengjiayi 已提交
162 163
    // Get cudnn workspace
    cudnn_workspace = dev_ctx.cudnn_workspace(workspace_size_in_bytes);
武毅 已提交
164
    // ------------------- cudnn conv forward ---------------------
K
update  
Kexin Zhao 已提交
165
    ScalingParamType<T> alpha = 1.0f, beta = 0.0f;
武毅 已提交
166
    for (int i = 0; i < groups; i++) {
W
Wu Yi 已提交
167
      CUDNN_ENFORCE(platform::dynload::cudnnConvolutionForward(
武毅 已提交
168 169 170 171 172 173 174 175 176
          handle, &alpha, cudnn_input_desc, input_data + i * group_offset_in,
          cudnn_filter_desc, filter_data + i * group_offset_filter,
          cudnn_conv_desc, algo, cudnn_workspace, workspace_size_in_bytes,
          &beta, cudnn_output_desc, output_data + i * group_offset_out));
    }
  }
};

template <typename T>
177
class CUDNNConvGradOpKernel : public framework::OpKernel<T> {
武毅 已提交
178 179 180
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
    PADDLE_ENFORCE(platform::is_gpu_place(ctx.GetPlace()),
D
dzhwinter 已提交
181
                   "It must use CUDAPlace.");
武毅 已提交
182 183 184 185 186 187 188 189 190 191 192 193 194 195
    auto input = ctx.Input<Tensor>("Input");
    auto filter = ctx.Input<Tensor>("Filter");
    auto output_grad = ctx.Input<Tensor>(framework::GradVarName("Output"));
    auto input_grad = ctx.Output<Tensor>(framework::GradVarName("Input"));
    auto filter_grad = ctx.Output<Tensor>(framework::GradVarName("Filter"));

    const T* input_data = input->data<T>();
    const T* output_grad_data = output_grad->data<T>();
    const T* filter_data = filter->data<T>();

    std::vector<int> strides = ctx.Attr<std::vector<int>>("strides");
    std::vector<int> paddings = ctx.Attr<std::vector<int>>("paddings");
    std::vector<int> dilations = ctx.Attr<std::vector<int>>("dilations");
    int groups = ctx.Attr<int>("groups");
Q
qiaolongfei 已提交
196 197
    int64_t user_workspace_size =
        static_cast<size_t>(ctx.Attr<int>("workspace_size_MB"));
武毅 已提交
198 199 200 201 202 203 204 205 206

    // ------------------- cudnn descriptors ---------------------
    ScopedTensorDescriptor input_desc;
    ScopedTensorDescriptor output_grad_desc;

    ScopedFilterDescriptor filter_desc;
    ScopedFilterDescriptor filter_grad_desc;
    ScopedConvolutionDescriptor conv_desc;
    DataLayout layout = DataLayout::kNCHW;
武毅 已提交
207 208 209 210 211 212 213
    if (input->dims().size() == 5) {
      layout = DataLayout::kNCDHW;
    }

    cudnnConvolutionDescriptor_t cudnn_conv_desc =
        conv_desc.descriptor<T>(paddings, strides, dilations);

武毅 已提交
214
#if CUDNN_VERSION_MIN(7, 0, 1)
武毅 已提交
215 216 217
    // cudnn 7 can support groups, no need to do it mannually
    // FIXME(typhoonzero): find a better way to disable groups
    // rather than setting it to 1.
W
Wu Yi 已提交
218
    CUDNN_ENFORCE(platform::dynload::cudnnSetConvolutionGroupCount(
武毅 已提交
219 220 221
        cudnn_conv_desc, groups));
    groups = 1;
#endif
武毅 已提交
222

C
chengduoZH 已提交
223 224
    cudnnTensorDescriptor_t cudnn_input_desc = input_desc.descriptor<T>(
        layout, framework::vectorize2int(input->dims()), groups);
武毅 已提交
225
    cudnnTensorDescriptor_t cudnn_output_grad_desc =
C
chengduoZH 已提交
226 227 228 229
        output_grad_desc.descriptor<T>(
            layout, framework::vectorize2int(output_grad->dims()), groups);
    cudnnFilterDescriptor_t cudnn_filter_desc = filter_desc.descriptor<T>(
        layout, framework::vectorize2int(filter->dims()), groups);
武毅 已提交
230 231

    int input_channels = input->dims()[1];
武毅 已提交
232 233 234 235 236 237 238 239 240 241 242
    int input_height, input_width, input_depth;
    if (input->dims().size() == 5) {
      input_depth = input->dims()[2];
      input_height = input->dims()[3];
      input_width = input->dims()[4];
    } else {  // dim size is enforced in InferShape
      input_depth = 1;
      input_height = input->dims()[2];
      input_width = input->dims()[3];
    }

武毅 已提交
243
    int output_grad_channels = filter->dims()[0];
武毅 已提交
244 245 246 247 248 249 250 251 252 253
    int output_grad_height, output_grad_width, output_grad_depth;
    if (input->dims().size() == 5) {
      output_grad_depth = output_grad->dims()[2];
      output_grad_height = output_grad->dims()[3];
      output_grad_width = output_grad->dims()[4];
    } else {
      output_grad_depth = 1;
      output_grad_height = output_grad->dims()[2];
      output_grad_width = output_grad->dims()[3];
    }
武毅 已提交
254

武毅 已提交
255 256 257 258
    int group_offset_in =
        input_channels / groups * input_height * input_width * input_depth;
    int group_offset_out = output_grad_channels / groups * output_grad_height *
                           output_grad_width * output_grad_depth;
武毅 已提交
259 260 261 262 263 264 265 266 267 268
    int group_offset_filter = filter->numel() / groups;
    // ------------------- cudnn backward algorithm ---------------------
    cudnnConvolutionBwdDataAlgo_t data_algo;
    cudnnConvolutionBwdFilterAlgo_t filter_algo;
    size_t workspace_size_in_bytes = 0, tmp_size = 0;
    size_t workspace_size_limit = kCONV_CUDNN_WORKSPACE_LIMIT_BYTES;
    if (user_workspace_size > 0) {
      workspace_size_limit = user_workspace_size * 1024 * 1024;
    }

Q
QI JUN 已提交
269 270
    auto& dev_ctx = ctx.template device_context<platform::CUDADeviceContext>();
    auto handle = dev_ctx.cudnn_handle();
武毅 已提交
271
    if (input_grad) {
Y
Yu Yang 已提交
272
      if (!FLAGS_cudnn_deterministic) {
W
Wu Yi 已提交
273
        CUDNN_ENFORCE(
C
chengduoZH 已提交
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
            platform::dynload::cudnnGetConvolutionBackwardDataAlgorithm(
                handle, cudnn_filter_desc,
                // dyDesc: Handle to the previously initialized input
                // differential
                // tensor descriptor.
                cudnn_output_grad_desc, cudnn_conv_desc,
                // dxDesc: Handle to the previously initialized output tensor
                // descriptor.
                cudnn_input_desc,
                CUDNN_CONVOLUTION_BWD_DATA_SPECIFY_WORKSPACE_LIMIT,
                workspace_size_limit, &data_algo));
      } else {
        data_algo = CUDNN_CONVOLUTION_BWD_DATA_ALGO_1;
      }

W
Wu Yi 已提交
289
      CUDNN_ENFORCE(
武毅 已提交
290 291
          platform::dynload::cudnnGetConvolutionBackwardDataWorkspaceSize(
              handle, cudnn_filter_desc, cudnn_output_grad_desc,
武毅 已提交
292
              cudnn_conv_desc, cudnn_input_desc, data_algo, &tmp_size));
武毅 已提交
293 294 295 296
      workspace_size_in_bytes = std::max(workspace_size_in_bytes, tmp_size);
    }

    if (filter_grad) {
Y
Yu Yang 已提交
297
      if (!FLAGS_cudnn_deterministic) {
W
Wu Yi 已提交
298
        CUDNN_ENFORCE(
C
chengduoZH 已提交
299 300 301 302 303 304 305 306
            platform::dynload::cudnnGetConvolutionBackwardFilterAlgorithm(
                handle, cudnn_input_desc, cudnn_output_grad_desc,
                cudnn_conv_desc, cudnn_filter_desc,
                CUDNN_CONVOLUTION_BWD_FILTER_SPECIFY_WORKSPACE_LIMIT,
                workspace_size_limit, &filter_algo));
      } else {
        filter_algo = CUDNN_CONVOLUTION_BWD_FILTER_ALGO_1;
      }
武毅 已提交
307

W
Wu Yi 已提交
308
      CUDNN_ENFORCE(
武毅 已提交
309 310 311 312 313 314
          platform::dynload::cudnnGetConvolutionBackwardFilterWorkspaceSize(
              handle, cudnn_input_desc, cudnn_output_grad_desc, cudnn_conv_desc,
              cudnn_filter_desc, filter_algo, &tmp_size));
      workspace_size_in_bytes = std::max(workspace_size_in_bytes, tmp_size);
    }
    // ------------------- cudnn conv workspace ---------------------
F
fengjiayi 已提交
315
    void* cudnn_workspace = dev_ctx.cudnn_workspace(workspace_size_in_bytes);
武毅 已提交
316
    // ------------------- cudnn conv backward data ---------------------
K
update  
Kexin Zhao 已提交
317
    ScalingParamType<T> alpha = 1.0f, beta = 0.0f;
武毅 已提交
318 319
    if (input_grad) {
      T* input_grad_data = input_grad->mutable_data<T>(ctx.GetPlace());
C
chengduoZH 已提交
320 321
      // Because beta is zero, it is unnecessary to reset input_grad.

武毅 已提交
322
      for (int i = 0; i < groups; i++) {
W
Wu Yi 已提交
323
        CUDNN_ENFORCE(platform::dynload::cudnnConvolutionBackwardData(
武毅 已提交
324 325 326
            handle, &alpha, cudnn_filter_desc,
            filter_data + i * group_offset_filter, cudnn_output_grad_desc,
            output_grad_data + i * group_offset_out, cudnn_conv_desc, data_algo,
武毅 已提交
327 328
            cudnn_workspace, workspace_size_in_bytes, &beta, cudnn_input_desc,
            input_grad_data + i * group_offset_in));
武毅 已提交
329 330 331 332 333
      }
    }
    // ------------------- cudnn conv backward filter ---------------------
    if (filter_grad) {
      T* filter_grad_data = filter_grad->mutable_data<T>(ctx.GetPlace());
C
chengduoZH 已提交
334
      // Because beta is zero, it is unnecessary to reset filter_grad.
武毅 已提交
335
      for (int i = 0; i < groups; i++) {
W
Wu Yi 已提交
336
        CUDNN_ENFORCE(platform::dynload::cudnnConvolutionBackwardFilter(
武毅 已提交
337 338 339
            handle, &alpha, cudnn_input_desc, input_data + i * group_offset_in,
            cudnn_output_grad_desc, output_grad_data + i * group_offset_out,
            cudnn_conv_desc, filter_algo, cudnn_workspace,
武毅 已提交
340
            workspace_size_in_bytes, &beta, cudnn_filter_desc,
武毅 已提交
341 342 343 344 345 346 347 348 349
            filter_grad_data + i * group_offset_filter));
      }
    }
  }
};

}  // namespace operators
}  // namespace paddle

K
Kexin Zhao 已提交
350 351
namespace plat = paddle::platform;
REGISTER_OP_KERNEL(conv2d, CUDNN, plat::CUDAPlace,
352
                   paddle::operators::CUDNNConvOpKernel<float>,
K
Kexin Zhao 已提交
353
                   paddle::operators::CUDNNConvOpKernel<double>,
K
Kexin Zhao 已提交
354
                   paddle::operators::CUDNNConvOpKernel<plat::float16>);
K
Kexin Zhao 已提交
355
REGISTER_OP_KERNEL(conv2d_grad, CUDNN, plat::CUDAPlace,
356
                   paddle::operators::CUDNNConvGradOpKernel<float>,
357
                   paddle::operators::CUDNNConvGradOpKernel<double>);
358

K
Kexin Zhao 已提交
359
REGISTER_OP_KERNEL(conv3d, CUDNN, plat::CUDAPlace,
360
                   paddle::operators::CUDNNConvOpKernel<float>,
K
Kexin Zhao 已提交
361 362
                   paddle::operators::CUDNNConvOpKernel<double>,
                   paddle::operators::CUDNNConvOpKernel<plat::float16>);
K
Kexin Zhao 已提交
363
REGISTER_OP_KERNEL(conv3d_grad, CUDNN, plat::CUDAPlace,
364
                   paddle::operators::CUDNNConvGradOpKernel<float>,
365
                   paddle::operators::CUDNNConvGradOpKernel<double>);