cudnn_norm_conv.cu.h 16.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/* Copyright (c) 2021 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 "paddle/fluid/operators/fused/cudnn_fusion_helper.h"
18
#include "paddle/fluid/platform/device/gpu/gpu_dnn.h"
19 20 21 22 23 24

namespace paddle {
namespace operators {
using Tensor = framework::Tensor;
namespace dynload = platform::dynload;

25 26 27
template <typename T>
using ScalingParamType = typename platform::CudnnDataType<T>::ScalingParamType;

28
#if CUDNN_VERSION >= 8000
29 30 31

static size_t RoundUp(int64_t a, int64_t b) { return (a + b - 1) / b * b; }

32
template <typename T>
33 34 35 36 37 38 39
struct NormConvolutionArgs {
  NormConvolutionArgs() {
    dtype = platform::CudnnDataType<T>::type;
    format = CUDNN_TENSOR_NHWC;
    compute_type = platform::CudnnDataType<float>::type;
  }

L
Leo Chen 已提交
40
  void Set(const phi::GPUContext &ctx,
41
           const std::vector<int> &input_shape,
42
           const std::vector<int> &filter_shape,
43 44 45 46 47
           const std::vector<int> &output_shape,
           int padding,
           int stride,
           int dilation,
           int group) {
48
    PADDLE_ENFORCE_EQ(
49 50
        input_shape.size(),
        4U,
51
        platform::errors::InvalidArgument(
52
            "The size of input_shape is expected to 4. But received "
53
            "input_shape's size is %d, input_shape is [%s].",
54 55
            input_shape.size(),
            phi::make_ddim(input_shape)));
56
    PADDLE_ENFORCE_EQ(
57 58
        filter_shape.size(),
        4U,
59
        platform::errors::InvalidArgument(
60
            "The size of filter_shape is expected to 4. But received "
61
            "filter_shape's size is %d, filter_shape is [%s].",
62 63
            filter_shape.size(),
            phi::make_ddim(filter_shape)));
64 65 66 67 68
    PADDLE_ENFORCE_EQ(filter_shape[1] == filter_shape[2] &&
                          (filter_shape[1] == 1 || filter_shape[1] == 3),
                      true,
                      platform::errors::InvalidArgument(
                          "The filter_shape is expected to store as nhwc, and "
69
                          "h = w = 1 or 3. But received filter_shape is [%s].",
70
                          phi::make_ddim(filter_shape)));
71 72 73 74 75
    PADDLE_ENFORCE_EQ((filter_shape[0] % 32 == 0 && filter_shape[3] % 8 == 0),
                      true,
                      platform::errors::InvalidArgument(
                          "The input channel is expected to be multiple of 8, "
                          "and the output channel is expected to be multiple "
76
                          "of 32. But received input channel is %d, output "
77
                          "channel is %d.",
78 79
                          filter_shape[3],
                          filter_shape[0]));
80
    PADDLE_ENFORCE_EQ(
81 82
        output_shape.size(),
        4U,
83
        platform::errors::InvalidArgument(
84
            "The size of output_shape is expected to 4. But received "
85
            "filter_shape's size is %d, filter_shape is [%s].",
86 87
            output_shape.size(),
            phi::make_ddim(output_shape)));
88 89
    is_support = IsSupport(ctx, filter_shape, stride, dilation, group);
    PADDLE_ENFORCE_EQ(
90 91
        is_support,
        true,
92 93 94 95 96
        platform::errors::InvalidArgument(
            "Current test is only supported in the platforms with "
            "compatiblity greater than or equal to 70 and the kernel size "
            "must be equal to 1 or 3. When the kernel size is 1, "
            "the stride must be 1 if the compatiblity is equal to 70. "
97
            "Besides, the dilation and group must be equal to 1. But received "
98 99
            "compatiblity is %d, kernel size is %d, stride is %d, "
            "dilation is %d, group is %d",
100 101 102 103
            ctx.GetComputeCapability(),
            filter_shape[1],
            stride,
            dilation,
104
            group));
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126

    for (size_t i = 0; i < input_shape.size(); ++i) {
      in_dims.push_back(input_shape[i]);
    }
    for (size_t i = 0; i < filter_shape.size(); ++i) {
      filter_dims.push_back(filter_shape[i]);
    }
    paddings = {padding, padding};
    strides = {stride, stride};
    dilations = {dilation, dilation};

    in_desc.set(input_shape, format, dtype);
    filter_desc.set(filter_shape, format, dtype, group);
    out_desc.set(output_shape, format, dtype);

    int output_channel = filter_shape[0];
    std::vector<int> stats_shape = {1, 1, 1, output_channel};
    out_stats_desc.set(stats_shape, format, compute_type);

    conv_desc.set(dtype, paddings, strides, dilations, false, group);
  }

L
Leo Chen 已提交
127
  bool IsSupport(const phi::GPUContext &ctx,
128 129 130
                 const std::vector<int> &filter_shape,
                 int stride,
                 int dilation,
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
                 int group) {
    int kernel_size = filter_shape[1];
    if (dilation != 1 || group != 1) {
      return false;
    }
    if (ctx.GetComputeCapability() == 70) {
      if ((kernel_size == 3) || ((kernel_size == 1) && (stride == 1))) {
        return true;
      }
    } else if (ctx.GetComputeCapability() > 70) {
      if ((kernel_size == 3) || (kernel_size == 1)) {
        return true;
      }
    }
    return false;
  }

148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
  cudnnDataType_t dtype;
  cudnnTensorFormat_t format;
  cudnnDataType_t compute_type;

  std::vector<int64_t> in_dims;
  std::vector<int64_t> filter_dims;
  std::vector<int> strides;
  std::vector<int> paddings;
  std::vector<int> dilations;

  platform::TensorDescriptor in_desc;
  platform::FilterDescriptor filter_desc;
  platform::TensorDescriptor out_desc;
  platform::TensorDescriptor out_stats_desc;
  platform::ConvolutionDescriptor conv_desc;
163 164

  bool is_support;
165 166 167 168
};

template <typename T>
class CudnnNormConvolution {
169
 public:
L
Leo Chen 已提交
170
  CudnnNormConvolution(const phi::GPUContext &ctx,
171 172
                       const std::vector<int> &input_shape,
                       const std::vector<int> &filter_shape,
173 174 175 176
                       const std::vector<int> &output_shape,
                       const int &padding,
                       const int &stride,
                       const int &dilation,
177
                       const int &group) {
178 179 180 181 182 183 184 185
    args_.Set(ctx,
              input_shape,
              filter_shape,
              output_shape,
              padding,
              stride,
              dilation,
              group);
186
  }
187
  ~CudnnNormConvolution() {}
188

L
Leo Chen 已提交
189
  void Forward(const phi::GPUContext &ctx,
190 191 192 193
               const Tensor &input,
               const Tensor &filter,
               Tensor *output,
               Tensor *sum,
194
               Tensor *sum_of_squares) {
195
    auto cudnn_handle = ctx.cudnn_handle();
196
    auto place = ctx.GetPlace();
197 198 199 200 201 202

    CudnnFusionOp *fwd_op = GetForwardOp(ctx);
    size_t workspace_size = RoundUp(
        static_cast<int64_t>(fwd_op->GetWorkspaceSizeInBytes(cudnn_handle)),
        512);

203 204
    // Set variant_param
    // input ptr
205 206
    T *input_ptr = const_cast<T *>(input.data<T>());
    T *filter_ptr = const_cast<T *>(filter.data<T>());
207 208 209 210 211
    fwd_op->SetOpVariantParamAttrPtr(CUDNN_PTR_XDATA, input_ptr);
    fwd_op->SetOpVariantParamAttrPtr(CUDNN_PTR_WDATA, filter_ptr);
    fwd_op->SetOpVariantParamAttrPtr(
        CUDNN_SCALAR_SIZE_T_WORKSPACE_SIZE_IN_BYTES, &workspace_size);

212
    // output ptr
213 214 215
    T *output_ptr = output->mutable_data<T>(place);
    float *sum_ptr = sum->mutable_data<float>(place);
    float *sum_of_squares_ptr = sum_of_squares->mutable_data<float>(place);
216 217 218 219 220
    fwd_op->SetOpVariantParamAttrPtr(CUDNN_PTR_YDATA, output_ptr);
    fwd_op->SetOpVariantParamAttrPtr(CUDNN_PTR_YSUM, sum_ptr);
    fwd_op->SetOpVariantParamAttrPtr(CUDNN_PTR_YSQSUM, sum_of_squares_ptr);

    ctx.cudnn_workspace_handle().RunFunc(
221 222
        [&](void *workspace_ptr) {
          // workspace ptr
223
          fwd_op->SetOpVariantParamAttrPtr(CUDNN_PTR_WORKSPACE, workspace_ptr);
224
          // fused op execute
225
          fwd_op->Execute(cudnn_handle);
226
        },
227
        workspace_size);
228 229
  }

230
 private:
L
Leo Chen 已提交
231
  CudnnFusionOp *GetForwardOp(const phi::GPUContext &ctx) {
232 233 234 235
    framework::AlgorithmsCache<CudnnFusionOp *> &cache =
        *(CudnnFusionOpCache::Instance().GetForward());

    CudnnFusionOp *fwd_op = cache.GetAlgorithm(
236 237 238 239 240 241 242 243
        args_.in_dims,
        args_.filter_dims,
        args_.strides,
        args_.paddings,
        args_.dilations,
        0,
        static_cast<int64_t>(args_.dtype),
        [&]() {
244 245 246 247
          CudnnFusionOp *fwd_op =
              new CudnnFusionOp(CUDNN_FUSED_SCALE_BIAS_ACTIVATION_CONV_BNSTATS);

          // Set constant_param
248 249 250 251
          fwd_op->SetOpConstParamAttr({CUDNN_PARAM_XDATA_PLACEHOLDER,
                                       CUDNN_PARAM_WDATA_PLACEHOLDER,
                                       CUDNN_PARAM_YDATA_PLACEHOLDER},
                                      CUDNN_PTR_16B_ALIGNED);
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
          fwd_op->SetOpConstParamAttr(
              {CUDNN_PARAM_YSUM_PLACEHOLDER, CUDNN_PARAM_YSQSUM_PLACEHOLDER},
              CUDNN_PTR_16B_ALIGNED);

          // conv desc
          fwd_op->SetOpConstParamDesc(CUDNN_PARAM_CONV_DESC,
                                      args_.conv_desc.desc());
          // input desc
          fwd_op->SetOpConstParamDesc(CUDNN_PARAM_XDESC, args_.in_desc.desc());
          // filter desc
          fwd_op->SetOpConstParamDesc(CUDNN_PARAM_WDESC,
                                      args_.filter_desc.desc());
          // output desc
          fwd_op->SetOpConstParamDesc(CUDNN_PARAM_YDESC, args_.out_desc.desc());
          // output_stats desc
          fwd_op->SetOpConstParamDesc(CUDNN_PARAM_YSTATS_DESC,
                                      args_.out_stats_desc.desc());
          // batch_norm mode
          fwd_op->SetOpConstParamAttr(CUDNN_PARAM_BN_MODE,
                                      CUDNN_BATCHNORM_SPATIAL_PERSISTENT);

          // Make cudnn fused ops plan
          fwd_op->GetWorkspaceSizeInBytes(ctx.cudnn_handle());
          return fwd_op;
        });
    return fwd_op;
  }
279 280

 private:
281 282
  NormConvolutionArgs<T> args_;
};
283

284 285 286
template <typename T>
class CudnnNormConvolutionGrad {
 public:
L
Leo Chen 已提交
287
  CudnnNormConvolutionGrad(const phi::GPUContext &ctx,
288 289 290
                           const std::vector<int> &input_shape,
                           const std::vector<int> &filter_shape,
                           const std::vector<int> &output_shape,
291 292 293 294 295 296 297 298 299 300 301 302
                           const int &padding,
                           const int &stride,
                           const int &dilation,
                           const int &group) {
    args_.Set(ctx,
              input_shape,
              filter_shape,
              output_shape,
              padding,
              stride,
              dilation,
              group);
303 304 305
    dgrad_algo_ = CUDNN_CONVOLUTION_BWD_DATA_ALGO_1;
  }
  ~CudnnNormConvolutionGrad() {}
306

L
Leo Chen 已提交
307
  void Backward(const phi::GPUContext &ctx,
308 309 310 311 312
                const Tensor &input,
                const Tensor &filter,
                const Tensor &output_grad,
                Tensor *input_grad,
                Tensor *filter_grad,
313 314 315 316 317 318 319 320 321
                bool use_addto = false) {
    auto place = ctx.GetPlace();
    T *input_ptr = const_cast<T *>(input.data<T>());
    T *filter_ptr = const_cast<T *>(filter.data<T>());
    T *output_grad_ptr = const_cast<T *>(output_grad.data<T>());

    if (filter_grad) {
      T *filter_grad_ptr = filter_grad->mutable_data<T>(place);
      BackwardFilter(ctx, output_grad_ptr, input_ptr, filter_grad_ptr);
322
    }
323 324 325
    if (input_grad) {
      T *input_grad_ptr = input_grad->mutable_data<T>(place);
      BackwardData(ctx, output_grad_ptr, filter_ptr, input_grad_ptr, use_addto);
326 327
    }
  }
328

329
 private:
L
Leo Chen 已提交
330
  void BackwardFilter(const phi::GPUContext &ctx,
331 332 333
                      T *output_grad_ptr,
                      T *input_ptr,
                      T *filter_grad_ptr) {
334
    auto cudnn_handle = ctx.cudnn_handle();
335

336 337 338 339
    CudnnFusionOp *wgrad_op = GetBackwardFilterOp(ctx);
    size_t workspace_size = RoundUp(
        static_cast<int64_t>(wgrad_op->GetWorkspaceSizeInBytes(cudnn_handle)),
        512);
340

341 342 343 344 345
    wgrad_op->SetOpVariantParamAttrPtr(CUDNN_PTR_XDATA, input_ptr);
    wgrad_op->SetOpVariantParamAttrPtr(CUDNN_PTR_DYDATA, output_grad_ptr);
    wgrad_op->SetOpVariantParamAttrPtr(CUDNN_PTR_DWDATA, filter_grad_ptr);
    wgrad_op->SetOpVariantParamAttrPtr(
        CUDNN_SCALAR_SIZE_T_WORKSPACE_SIZE_IN_BYTES, &workspace_size);
346

347 348 349 350 351 352 353 354 355
    ctx.cudnn_workspace_handle().RunFunc(
        [&](void *workspace_ptr) {
          // workspace ptr
          wgrad_op->SetOpVariantParamAttrPtr(CUDNN_PTR_WORKSPACE,
                                             workspace_ptr);
          // fused op execute
          wgrad_op->Execute(cudnn_handle);
        },
        workspace_size);
356 357
  }

L
Leo Chen 已提交
358
  void BackwardData(const phi::GPUContext &ctx,
359 360 361 362
                    T *output_grad_ptr,
                    T *filter_ptr,
                    T *input_grad_ptr,
                    bool use_addto = false) {
363 364 365 366 367 368 369 370
    auto cudnn_handle = ctx.cudnn_handle();
    size_t workspace_size = GetWorkspaceSizeBwdData(ctx);

    // Convolution dgrad followed optionally by batchnorm dgrad
    ScalingParamType<T> alpha = 1.0f;
    ScalingParamType<T> beta = use_addto ? 1.0f : 0.0f;
    ctx.cudnn_workspace_handle().RunFunc(
        [&](void *cudnn_workspace_ptr) {
371
          PADDLE_ENFORCE_GPU_SUCCESS(
372
              platform::dynload::cudnnConvolutionBackwardData(
373 374 375 376 377 378 379 380 381 382 383 384 385
                  cudnn_handle,
                  &alpha,
                  args_.filter_desc.desc(),
                  filter_ptr,
                  args_.out_desc.desc(),
                  output_grad_ptr,
                  args_.conv_desc.desc(),
                  dgrad_algo_,
                  cudnn_workspace_ptr,
                  workspace_size,
                  &beta,
                  args_.in_desc.desc(),
                  input_grad_ptr));
386 387
        },
        workspace_size);
388 389
  }

L
Leo Chen 已提交
390
  CudnnFusionOp *GetBackwardFilterOp(const phi::GPUContext &ctx) {
391 392 393 394
    framework::AlgorithmsCache<CudnnFusionOp *> &cache =
        *(CudnnFusionOpCache::Instance().GetBackward());

    CudnnFusionOp *wgrad_op = cache.GetAlgorithm(
395 396 397 398 399 400 401 402
        args_.in_dims,
        args_.filter_dims,
        args_.strides,
        args_.paddings,
        args_.dilations,
        0,
        static_cast<int64_t>(args_.dtype),
        [&]() {
403 404 405
          CudnnFusionOp *wgrad_op =
              new CudnnFusionOp(CUDNN_FUSED_SCALE_BIAS_ACTIVATION_WGRAD);

406 407 408 409
          wgrad_op->SetOpConstParamAttr({CUDNN_PARAM_DYDATA_PLACEHOLDER,
                                         CUDNN_PARAM_XDATA_PLACEHOLDER,
                                         CUDNN_PARAM_DWDATA_PLACEHOLDER},
                                        CUDNN_PTR_16B_ALIGNED);
410

411 412 413 414 415 416 417 418 419 420 421 422 423 424
          // conv desc
          wgrad_op->SetOpConstParamDesc(CUDNN_PARAM_CONV_DESC,
                                        args_.conv_desc.desc());
          // input desc
          wgrad_op->SetOpConstParamDesc(CUDNN_PARAM_XDESC,
                                        args_.in_desc.desc());
          // filter desc
          wgrad_op->SetOpConstParamDesc(CUDNN_PARAM_DWDESC,
                                        args_.filter_desc.desc());
          // output desc
          wgrad_op->SetOpConstParamDesc(CUDNN_PARAM_DYDESC,
                                        args_.out_desc.desc());
          wgrad_op->SetOpConstParamAttr(CUDNN_PARAM_BN_MODE,
                                        CUDNN_BATCHNORM_SPATIAL_PERSISTENT);
425

426 427 428 429 430 431 432
          // Make cudnn fused ops plan
          wgrad_op->GetWorkspaceSizeInBytes(ctx.cudnn_handle());
          return wgrad_op;
        });
    return wgrad_op;
  }

L
Leo Chen 已提交
433
  size_t GetWorkspaceSizeBwdData(const phi::GPUContext &ctx) {
434 435
    size_t workspace_size = 0U;
    auto handle = ctx.cudnn_handle();
436
    PADDLE_ENFORCE_GPU_SUCCESS(
437
        platform::dynload::cudnnGetConvolutionBackwardDataWorkspaceSize(
438 439 440 441 442 443
            handle,
            args_.filter_desc.desc(),
            args_.out_desc.desc(),
            args_.conv_desc.desc(),
            args_.in_desc.desc(),
            dgrad_algo_,
444 445 446 447 448 449 450
            &workspace_size));
    return RoundUp(workspace_size, 512);
  }

 private:
  NormConvolutionArgs<T> args_;
  cudnnConvolutionBwdDataAlgo_t dgrad_algo_;
451
};
452

453 454 455
#endif
}  // namespace operators
}  // namespace paddle