conv_miopen_helper.h 5.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* 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

17 18
#include "glog/logging.h"

19
#include "paddle/phi/kernels/gpudnn/conv_gpudnn_base.h"
20

21
namespace phi {
22

23
using ConvArgs = ConvArgsBase<miopenHandle_t, miopenDataType_t>;
24

Y
Yiqun Liu 已提交
25 26 27
template <typename PerfT>
struct SearchAlgorithm {};

28 29 30 31 32 33
template <>
struct SearchAlgorithm<miopenConvFwdAlgorithm_t> {
  using perf_t = miopenConvAlgoPerf_t;
  using algo_t = miopenConvFwdAlgorithm_t;

  template <typename T>
34 35 36 37
  static algo_t Find(const ConvArgs& args,
                     bool exhaustive_search,
                     bool deterministic,
                     size_t workspace_size,
H
hong 已提交
38
                     const phi::GPUContext& ctx) {
39 40
    algo_t algo;

H
hong 已提交
41
    auto workspace_handle = ctx.cudnn_workspace_handle();
42

43 44 45
    int find_count;
    miopenConvAlgoPerf_t find_result;
    auto cudnn_find_func = [&](void* cudnn_workspace_ptr) {
46
      PADDLE_ENFORCE_GPU_SUCCESS(
47
          phi::dynload::miopenFindConvolutionForwardAlgorithm(
48 49 50 51 52 53 54 55 56 57 58 59 60 61
              args.handle,
              args.idesc.desc(),
              args.x->data<T>(),
              args.wdesc.desc(),
              args.w->data<T>(),
              args.cdesc.desc(),
              args.odesc.desc(),
              const_cast<T*>(args.o->data<T>()),
              kNUM_CUDNN_FWD_ALGS,
              &find_count,
              &find_result,
              cudnn_workspace_ptr,
              workspace_size,
              false));
62 63
    };

R
ronnywang 已提交
64 65
    workspace_handle.RunFuncSync(cudnn_find_func, workspace_size);
    algo = find_result.fwd_algo;
66 67 68 69
    VLOG(3) << "choose algo " << algo;
    return algo;
  }

70
  static size_t GetWorkspaceSize(const ConvArgs& args) {
71
    size_t workspace_size = 0;
72
    PADDLE_ENFORCE_GPU_SUCCESS(
73
        phi::dynload::miopenConvolutionForwardGetWorkSpaceSize(
74 75 76 77 78 79
            args.handle,
            args.wdesc.desc(),
            args.idesc.desc(),
            args.cdesc.desc(),
            args.odesc.desc(),
            &workspace_size));
80 81 82 83 84 85 86 87 88 89
    return workspace_size;
  }
};

template <>
struct SearchAlgorithm<miopenConvBwdDataAlgorithm_t> {
  using perf_t = miopenConvAlgoPerf_t;
  using algo_t = miopenConvBwdDataAlgorithm_t;

  template <typename T>
90 91 92 93
  static algo_t Find(const ConvArgs& args,
                     bool exhaustive_search,
                     bool deterministic,
                     size_t workspace_size,
H
hong 已提交
94
                     const phi::GPUContext& ctx) {
95 96
    algo_t algo;

H
hong 已提交
97
    auto workspace_handle = ctx.cudnn_workspace_handle();
98

99 100 101
    int find_count;
    miopenConvAlgoPerf_t find_result;
    auto cudnn_find_func = [&](void* cudnn_workspace_ptr) {
102
      PADDLE_ENFORCE_GPU_SUCCESS(
103
          phi::dynload::miopenFindConvolutionBackwardDataAlgorithm(
104 105 106 107 108 109 110 111 112 113 114 115 116 117
              args.handle,
              args.odesc.desc(),
              args.o->data<T>(),
              args.wdesc.desc(),
              args.w->data<T>(),
              args.cdesc.desc(),
              args.idesc.desc(),
              const_cast<T*>(args.x->data<T>()),
              kNUM_CUDNN_BWD_DATA_ALGS,
              &find_count,
              &find_result,
              cudnn_workspace_ptr,
              workspace_size,
              false));
118 119
    };

R
ronnywang 已提交
120 121
    workspace_handle.RunFuncSync(cudnn_find_func, workspace_size);
    algo = find_result.bwd_data_algo;
122 123 124 125
    VLOG(3) << "choose algo " << algo;
    return algo;
  }

126
  static size_t GetWorkspaceSize(const ConvArgs& args) {
127
    size_t workspace_size = 0;
128
    PADDLE_ENFORCE_GPU_SUCCESS(
129
        phi::dynload::miopenConvolutionBackwardDataGetWorkSpaceSize(
130 131 132 133 134 135
            args.handle,
            args.odesc.desc(),
            args.wdesc.desc(),
            args.cdesc.desc(),
            args.idesc.desc(),
            &workspace_size));
136 137 138 139 140 141 142 143 144 145
    return workspace_size;
  }
};

template <>
struct SearchAlgorithm<miopenConvBwdWeightsAlgorithm_t> {
  using perf_t = miopenConvAlgoPerf_t;
  using algo_t = miopenConvBwdWeightsAlgorithm_t;

  template <typename T>
146 147 148 149
  static algo_t Find(const ConvArgs& args,
                     bool exhaustive_search,
                     bool deterministic,
                     size_t workspace_size,
H
hong 已提交
150
                     const phi::GPUContext& ctx) {
151 152
    algo_t algo;

H
hong 已提交
153
    auto workspace_handle = ctx.cudnn_workspace_handle();
154 155 156 157

    int find_count;
    miopenConvAlgoPerf_t find_result;
    auto cudnn_find_func = [&](void* cudnn_workspace_ptr) {
158
      PADDLE_ENFORCE_GPU_SUCCESS(
159
          phi::dynload::miopenFindConvolutionBackwardWeightsAlgorithm(
160 161 162 163 164 165 166 167 168 169 170 171 172 173
              args.handle,
              args.odesc.desc(),
              args.o->data<T>(),
              args.idesc.desc(),
              args.x->data<T>(),
              args.cdesc.desc(),
              args.wdesc.desc(),
              const_cast<T*>(args.w->data<T>()),
              kNUM_CUDNN_BWD_FILTER_ALGS,
              &find_count,
              &find_result,
              cudnn_workspace_ptr,
              workspace_size,
              false));
174 175
    };

R
ronnywang 已提交
176 177
    workspace_handle.RunFuncSync(cudnn_find_func, workspace_size);
    algo = find_result.bwd_weights_algo;
178 179 180 181
    VLOG(3) << "choose algo " << algo;
    return algo;
  }

182
  static size_t GetWorkspaceSize(const ConvArgs& args) {
183
    size_t workspace_size = 0;
184
    PADDLE_ENFORCE_GPU_SUCCESS(
185
        phi::dynload::miopenConvolutionBackwardWeightsGetWorkSpaceSize(
186 187 188 189 190 191
            args.handle,
            args.odesc.desc(),
            args.idesc.desc(),
            args.cdesc.desc(),
            args.wdesc.desc(),
            &workspace_size));
192 193 194 195
    return workspace_size;
  }
};

196
}  // namespace phi